完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
关于用51控制ds1302和lcd1602显示时间的程序问题,主要问题是在于写入的数据小于8就能正常写入,但是数据大于8的时候就lcd1602就显示是一堆问号!本人入门菜鸟,求高手帮助,一下是程序:#include
#include #define uchar unsigned char #define uint unsigned int unsigned char code read_addr[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d}; unsigned char code write_addr[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c}; unsigned char time[7] = {0x00,0x00, 0x12, 0x01, 0x01, 0x02, 0x13};//设置时间秒分时日月星期年 uint nianshi,niange,yueshi,yuege,rvshi,rvge,shishi,shige,fenshi,fenge,miaoshi,miaoge; uint miao,fen,shi,rv,yue,nian,timenian,timeyue,timerv,timeshi,timefen,timemiao; ***it SCLK=P3^6; ***it RST=P3^5; ***it DSIO=P3^4; ***it LCDE=P2^7; ***it RS=P2^6; ***it RW=P2^5; ***it KEY1=P1^1; ***it KEY2=P1^2; ***it KEY0=P1^0; uchar n,flag,num; void delay(unsigned int z) //延时子函数1 { unsigned char x,y; for(x=z;x>0;x--) { for(y=20;y>0;y--); } } void delay1(uint z) //延时子函数2 { uchar x; for(x=z;x>0;x--); } void write_com(unsigned char com)//1602液晶写入命令 { RS=0; RW=0; LCDE=0; P0=com; delay(5); LCDE=1; delay(5); LCDE=0; } void write_date(unsigned char date)//1602液晶写入数据 { RS=1; RW=0; LCDE=0; P0=date; delay(5); LCDE=1; delay(5); LCDE=0; } void write(unsigned char addr,unsigned char date) //ds1302写入数据 { unsigned char i; RST=0; _nop_(); SCLK=0; _nop_(); RST=1; _nop_(); for(i=0;i<8;i++) { DSIO=addr&0x01; addr=addr>>1; SCLK=1; _nop_(); SCLK=0; _nop_(); } for(i=0;i<8;i++) { DSIO=date; date>>=1; SCLK=1; _nop_(); SCLK=0; _nop_(); } RST=0; _nop_(); } unsigned char read(unsigned char addr) //ds1302读出数据 { unsigned char i,dat,dat1; RST=0; delay1(2); SCLK=0; delay1(2); RST=1; delay1(2); for(i=0;i<8;i++) { DSIO=addr&0x01; addr>>=1; SCLK=1; delay1(2); SCLK=0; delay1(2); } delay1(2); for(i=0;i<8;i++) { dat1=DSIO; dat=(dat>>1)|(dat1<<7); SCLK=1; delay1(2); SCLK=0; delay1(2); } RST=0; delay1(2); SCLK=1; delay1(2); SCLK=0; delay1(2); DSIO=0; delay1(2); DSIO=1; delay1(2); return dat; } void readtime() // ds1302连续读出时间 { unsigned char i; for(i=0;i<7;i++) { time[i]= read(read_addr[i]); } } void zhuanhuan() //十六进制转十进制 { nianshi=time[6]/16; niange=(time[6]&0x0f); yueshi=time[4]/16; yuege=(time[4]&0x0f); // write_com(0x88); rvshi=time[3]/16; rvge=(time[3]&0x0f); shishi=time[2]/16; shige=(time[2]&0x0f); //write_com(0x80+0x40+3); fenshi=time[1]/16; fenge=(time[1]&0x0f); // write_com(0x80+0x40+6); miaoshi=time[0]/16; miaoge=(time[0]&0x0f); } void zhuanhuan1() //十进制转十六进制 { time[6]=nianshi*16+niange; time[4]=yueshi*16+yuege; time[3]=rvshi*16+rvge; time[2]=shishi*16+shige; time[1]=fenshi*16+fenge; time[0]=miaoshi*16+miaoge; } void xianshi() //1602显示时间 { write_com(0x80+3); write_date(0x32); write_date(0x30); write_date('0'+nianshi); write_date('0'+niange); write_date('-'); // write_com(0x85); write_date('0'+yueshi); write_date('0'+yuege); write_date('-'); // write_com(0x88); write_date('0'+rvshi); write_date('0'+rvge); write_com(0x80+0x40+4); write_date('0'+shishi); write_date('0'+shige); write_date('-'); //write_com(0x80+0x40+3); write_date('0'+fenshi); write_date('0'+fenge); write_date('-'); // write_com(0x80+0x40+6); write_date('0'+miaoshi); write_date('0'+miaoge); } void ds1302init() //ds1302初始化 { unsigned char i; write(0x8e,0x00); for(i=0;i<7;i++) { write(write_addr[i],time[i]); } write(0x8e,0x80); } void init() //1602初始化 { RW=0; LCDE=0; write_com(0x38); write_com(0x0e); write_com(0x06); write_com(0x01); write_com(0x80); } void tiaozhuan() //按键跳转函数 { if(KEY0==0) { delay(5); if(KEY0==0) { n++; while(!KEY0); } } } void chuandi1() //时间传递函数1 { miao=miaoshi*10+miaoge; fen=fenshi*10+fenge; shi=shishi*10+shige; rv=rvshi*10+rvge; yue=yueshi*10+yuege; nian=nianshi*10+niange; } void chuandi2() //时间传递函数2 { nianshi=nian/10; niange=nian%10; yueshi=yue/10; yuege=yue%10; rvshi=rv/10; rvge=rv%10; shishi=shi/10; shige=shi%10; fenshi=fen/10; fenge=fen%10; miaoshi=miao/10; miaoge=miao%10; } void keyscan() //按键修改时间函数 { if(KEY0==0) { delay(5); if(KEY0==0) { n++; while(!KEY0) ; if(n==1); chuandi1(); while(n==1) { flag=1; write_com(0x80+0x40+10);//光标回到秒处 write_com(0x0f); //光标闪烁 tiaozhuan(); if(KEY1==0) { delay(5); if(KEY1==0) { while(!KEY1); miao++; if(miao==60) miao=0; chuandi2(); xianshi(); } } if(KEY2==0) { delay(5); if(KEY2==0) { while(!KEY2); miao--; if(miao==-1) miao=59; chuandi2(); xianshi(); } } // zhuanhuan1(); // write(0x8e,0x00); // write(write_addr[0],timemiao); // write(0x8e,0x80) ; } while(n==2) { //flag=1; write_com(0x80+0x40+7); tiaozhuan(); //chuandi1(); if(KEY1==0) { delay(5); if(KEY1==0) { while(!KEY1); fen++; if(fen==60) fen=0; chuandi2(); xianshi(); } } if(KEY2==0) { delay(5); if(KEY2==0) { while(!KEY2); fen--; if(fen==-1) fen=59; chuandi2(); xianshi(); } } // zhuanhuan1(); // // write(0x8e,0x00); // write(write_addr[1],timefen); // write(0x8e,0x80) ; } while(n==3) { //flag=1; write_com(0x80+0x40+4); tiaozhuan(); //chuandi1(); if(KEY1==0) { delay(5); if(KEY1==0) { while(!KEY1); shi++; if(shi==24) shi=0; chuandi2(); xianshi(); } } if(KEY2==0) { delay(5); if(KEY2==0) { while(!KEY2); shi--; if(shi==-1) shi=23; chuandi2(); xianshi(); // zhuanhuan1(); // write(0x8e,0x00); //写入ds1302数据 // write(write_addr[2],timeshi); // write(0x8e,0x80) ; } } } while(n==4) { write_com(0x80+11) ; tiaozhuan(); // chuandi1(); if(KEY1==0) { delay(5); if(KEY1==0) { while(!KEY1); rv++; if(rv==32) rv=0; chuandi2(); xianshi(); } } if(KEY2==0) { delay(5); if(KEY2==0) { while(!KEY2); rv--; if(rv==0) rv=31; chuandi2(); xianshi(); } } // zhuanhuan1(); // write(0x8e,0x00); // write(write_addr[3],timerv); // write(0x8e,0x80) ; } while(n==5) { write_com(0x80+8); tiaozhuan(); //chuandi1(); if(KEY1==0) { delay(5); if(KEY1==0) { while(!KEY1); yue++; if(yue==13) yue=0; chuandi2(); xianshi(); } } if(KEY2==0) { delay(5); if(KEY2==0) { while(!KEY2); yue--; if(yue==0) yue=12; chuandi2(); xianshi(); } } // zhuanhuan1(); // write(0x8e,0x00); // write(write_addr[4],timeyue); // write(0x8e,0x80) ; } while(n==6) { write_com(0x80+5); //chuandi1(); if(KEY1==0) { delay(5); if(KEY1==0) { while(!KEY1); nian++; if(nian==101) nian=0; chuandi2(); xianshi(); } } if(KEY2==0) { delay(5); if(KEY2==0) { while(!KEY2); nian--; if(nian==-1) nian=100; chuandi2(); xianshi(); } } // zhuanhuan1(); // write(0x8e,0x00); // write(write_addr[6],timenian); // write(0x8e,0x80) ; if(KEY0==0) { delay(5); if(KEY0==0) { n=0; flag=0; write_com(0x0c) ; while(!KEY0); } } } zhuanhuan1(); ds1302init(); } } } void main() //主函数 { init(); ds1302init(); while(1) { keyscan(); readtime(); if(flag==0) //有按键按下时液晶显示停止在最后显示的时间 { zhuanhuan(); } xianshi(); } } |
|
相关推荐
10个回答
|
|
程序精简一点 这个太长了 不好看出问题 也挺费时间的
|
|
|
|
主要问题是在于写入的数据小于8就能正常写入,但是数据大于8的时候就lcd1602就显示是一堆问号
------------------- 因为你定义的 read_addr[7]和 write_addr[7] 只有7个字长 |
|
|
|
经过我的调试仿真终于发现问题的所在,你DS1302写入数据的函数中这条语句DSIO=date;有问题。改为DSIO=date&0x01;就能正常显示了。
|
|
|
|
是数据大于8还是数据长度大于8才出问题?
|
|
|
|
额滴神啊,这也太长了
|
|
|
|
真是这个问题啊!!!谢谢你啊!!! |
|
|
|
依然落叶飘零 发表于 2014-5-1 14:08 大神能帮我分析一下这个吗。。。万分感谢啊https://bbs.elecfans.com/jishu_490308_1_1.html |
|
|
|
|
|
|
|
大哥能把proteus电路图发给我吗,万分感谢
|
|
|
|
大哥能把proteus电路图发给我吗,万分感谢
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
飞凌嵌入式-ELFBOARD 解决PCB布线时无法捕捉到焊盘中心的问题
600 浏览 0 评论
Openharmony软件评估指南-米尔瑞芯微RK3568开发板
876 浏览 0 评论
STM32CUBEMX(7)--移植雅特力AT32F403AVGT(兼容STM32F103VGT6),DAC输出电压
1126 浏览 0 评论
【RA-Eco-RA0E1-32PIN-V1.0开发板试用】开发板简介与上电测试
1095 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-shell脚本编写之传参
620 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11056 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-9-12 06:06 , Processed in 0.843452 second(s), Total 89, Slave 73 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191