完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
DS1302数字钟程序哪里有错?刚开始显示正常,一按按钮值就回不来了,求解
#include #define uint unsigned int #define uchar unsigned char ***it rs=P2^5; //LCD1602 ***it rw=P2^6; ***it en=P2^7; ***it sclk=P1^6; //DS1302 ***it ds1302_io=P3^5; ***it rst=P1^7; ***it s1=P3^2; //KEY ***it s2=P3^3; ***it s3=P3^4; uchar code table1[]=" 20 - - "; uchar code table2[]=" : : "; uchar a,miao,shi,fen,day,month,year,week,flag,s1num,value; void delay(uint z); void write_ds1302(uchar command,uchar value); //***it ACC0=ACC^0; //***it ACC7=ACC^7; void write_com(uchar com) { rs=0; rw=0; P0=com; delay(5); en=1; delay(5); en=0; } void write_data(uchar date) { rs=1; rw=0; P0=date; delay(5); en=1; delay(5); en=0; } void init() { uchar num; /*TMOD=0x11; TH0=0; TL0=0; EA=1; ET0=1; TR0=1;*/ en=0; write_com(0x38); write_com(0x0c); write_com(0x06); write_com(0x01); write_com(0x80); for(num=0;num<15;num++) { write_data(table1[num]); delay(5); } write_com(0x80+0x40); for(num=0;num<10;num++) { write_data(table2[num]); delay(5); } } void write_nyr(uchar add,uchar date) { uchar shi,ge; shi=date/10; ge=date%10; write_com(0x80+add); write_data(0x30+shi); write_data(0x30+ge); } void write_sfm(uchar add,uchar date) { uchar shi,ge; shi=date/10; ge=date%10; write_com(0x80+0x40+add); write_data(0x30+shi); write_data(0x30+ge); } /*void write_temp(uchar add,uchar date) { uchar shi,ge; shi=date/10; ge=date%10; write_com(0x80+0x40+add); write_data(0x30+shi); write_data(0x30+ge); write_data(0xdf); write_data(0x43); } */ void write_week(uchar week) { write_com(0x80+12); switch(week) { case 1:write_data('M');delay(5); write_data('O');delay(5); write_data('N'); break; case 2:write_data('T');delay(5); write_data('U');delay(5); write_data('E'); break; case 3:write_data('W');delay(5); write_data('E');delay(5); write_data('D'); break; case 4:write_data('T');delay(5); write_data('H');delay(5); write_data('U'); break; case 5:write_data('F');delay(5); write_data('R');delay(5); write_data('I'); break; case 6:write_data('S');delay(5); write_data('A');delay(5); write_data('T'); break; case 7:write_data('S');delay(5); write_data('U');delay(5); write_data('N'); break; } } void ds1302_init() { rst=0; sclk=0; write_ds1302(0x8e,0x00); write_ds1302(0x8e,0x80); } void write_ds1302(uchar command,uchar value) { uchar i=0; rst=0; sclk=0; rst=1; for(i=0;i<8;i++) { ds1302_io=command&0x01; sclk=0; sclk=1; command>>=1; } for(i=0;i<8;i++) { ds1302_io=value&0x01; sclk=0; sclk=1; value>>=1; } } uchar read_ds1302(uchar command) { uchar i=0; uchar value=0; rst=0; sclk=0; rst=1; for(i=0;i<8;i++) { ds1302_io=command&0x01; sclk=0; sclk=1; command>>=1; } for(i=0;i<8;i++) { sclk=1; sclk=0; if(ds1302_io) { value|=0x01< } } rst=0; //value = (value /16 * 10) + value %16; return value; } void keyscan() { if(s1==0) //功能键按下 { delay(5); if(s1==0) { s1num++; flag=1; while(!s1); if(s1num==1) { // TR0=0; write_com(0x80+0x40+8); //miao write_com(0x0f); value=(miao)/10*16+(miao)%10; write_ds1302(0x8e,0x00); write_ds1302(0x80,0x80|value); write_ds1302(0x8e,0x80); } if(s1num==2) //fen { write_com(0x80+0x40+5); } if(s1num==3) //shi { write_com(0x80+0x40+2); } if(s1num==4) //week { write_com(0x80+12); //write_com(0x0f); } if(s1num==5) //day { write_com(0x80+9); } if(s1num==6) //month { write_com(0x80+6); } if(s1num==7) //year { write_com(0x80+3); } if(s1num==8) { // TR0=1; s1num=0; write_com(0x0c); flag=0; value=(miao)/10*16+(miao)%10; write_ds1302(0x8e,0x00); write_ds1302(0x80,0x80|value); write_ds1302(0x8e,0x80); } } } if(s1num!=0) { if(s2==0) //数字增加键按下 { delay(5); if(s2==0) { while(!s2); if(s1num==1) { miao++; if(miao==60) miao=0; write_sfm(8,miao); value=(miao)/10*16+(miao)%10; write_ds1302(0x8e,0x00); write_ds1302(0x80,value); write_ds1302(0x8e,0x80); write_com(0x80+0x40+8); } if(s1num==2) { fen++; if(fen==60) fen=0; write_sfm(5,fen); value=(fen)/10*16+(fen)%10; write_ds1302(0x8e,0x00); write_ds1302(0x82,value); write_ds1302(0x8e,0x80); write_com(0x80+0x40+5); } if(s1num==3) { shi++; if(shi==24) shi=0; write_sfm(2,shi); value=(shi)/10*16+(shi)%10; write_ds1302(0x8e,0x00); write_ds1302(0x84,value); write_ds1302(0x8e,0x80); write_com(0x80+0x40+2); } if(s1num==4) { week++; if(week==8) week=1; write_com(0x80+12); write_week(week); value=(week)/10*16+(week)%10; write_ds1302(0x8e,0x00); write_ds1302(0x8a,value); write_ds1302(0x8e,0x80); } if(s1num==5) { day++; if(day==32) day=1; write_nyr(9,day); value=(day)/10*16+(day)%10; write_ds1302(0x8e,0x00); write_ds1302(0x86,value); write_ds1302(0x8e,0x80); write_com(0x80+9); } if(s1num==6) { month++; if(month==13) month=1; write_nyr(6,month); value=(month)/10*16+(month)%10; write_ds1302(0x8e,0x00); write_ds1302(0x88,value); write_ds1302(0x8e,0x80); write_com(0x80+6); } if(s1num==7) { year++; if(year==99) year=0; write_nyr(3,year); value=(year)/10*16+(year)%10; write_ds1302(0x8e,0x00); write_ds1302(0x8c,value); write_ds1302(0x8e,0x80); write_com(0x80+3); } } } } } uchar BCD_Decimal(uchar bcd) { uchar Decimal; Decimal=bcd>>4; return(Decimal=Decimal*10+(bcd&=0x0F)); } void main() { init(); ds1302_init(); while(1) { keyscan(); if(flag==0) { miao=BCD_Decimal(read_ds1302(0x81)); fen=BCD_Decimal(read_ds1302(0x83)); shi=BCD_Decimal(read_ds1302(0x85)); week=BCD_Decimal(read_ds1302(0x8b)); day=BCD_Decimal(read_ds1302(0x87)); month=BCD_Decimal(read_ds1302(0x89)); year=BCD_Decimal(read_ds1302(0x8d)); write_sfm(8,miao); write_sfm(5,fen); write_sfm(2,shi); write_nyr(9,day); write_nyr(6,month); write_nyr(3,year); write_week(week); } } } void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); }
|
|
相关推荐
5个回答
|
|
|
|
我看你的程序,确实太 为难了点,没有备注,好费力啊.......看了下ds1302的驱动,觉得 读数据那个函数两个for循环之间,需要拉高ds1302_IO,驱动部分,没有发现其他问题.....
顺便问一下, 开始你不按键的情况下, ds1302正常走时么? 在你的ds1302init部分,么有看到你 启用外部晶振啊......应该不会走时, 会定在那儿的。。 当你按键之后,你写 ds1302秒寄存器的时候, 这儿加上了 0x80, 即置了秒的自高位,激活了晶振....... 所以我觉得,你的程序,读出来ds1302后,直接显示秒的值,导致出错, 应该换算一下 ,要忽略 读出来 秒的值 的最高位。。。。miao &=~0x80; 然后在送到显示屏 ,试试。。。。。。另外,程序要是你自己写的,你应该可以发现ds1302秒寄存器的定义哟。。。。。 评分
|
||
|
||
你的按键接的是单片机的P32和P33还有P34口,可以利用单片机的INT0(P32)和INT1(P33)两个外部中断来写按键处理程序。
还有就是你的DS1302驱动函数和我写的不大一样,能不能运行不好说,直接看看不出来。 |
|
|
|
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
867 浏览 0 评论
562 浏览 0 评论
嵌入式学习-搭建自己的ubuntu系统之ubuntu网络设置
691 浏览 0 评论
龙芯中科胡伟武:3B6600 八核桌面 CPU 性能将达到英特尔中高端酷睿 12~13 代水平
753 浏览 0 评论
树莓派Pico 2发布,搭载RP2350双核RISC-V和Arm Cortex-M33微控制器!
714 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
10737 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-8-18 16:11 , Processed in 0.584936 second(s), Total 84, Slave 64 queries .
Powered by 电子发烧友网
© 2015 www.ws-dc.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号