完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
#include
#include ***it lcdrs=P2^5; ***it lcden=P2^7; ***it rst=P1^5; ***it io=P1^6; ***it sclk=P1^7; ***it led=P3^0; ***it k1=P2^0; ***it k2=P2^1; ***it k3=P2^2; ***it k4=P2^3; unsigned char code chuzhi[7]={14,6,12,31,23,59,54}; unsigned char code xieadd[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80}; unsigned char code duadd[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81}; unsigned char xie[7]={0}; unsigned char du[7]={0}; unsigned char flag; void delay(unsigned char ims) { int i,j; for(i=ims;i>0;i--) for(j=110;j>0;j--); } void write_com(unsigned char com) { lcdrs=0; P0=com; lcden=1; delay(1); lcden=0; } void write_data(unsigned char date) { lcdrs=1; P0=date; lcden=1; delay(1); lcden=0; } void desplay_main() { write_com(0x80); write_data('2'); write_data('0'); write_com(0x80+4); write_data('/'); write_com(0x80+7); write_data('/'); write_com(0x80+11); write_data('w'); write_data('e'); write_data('e'); write_data('k'); write_com(0x80+0x40+2); write_data(':'); write_com(0x80+0x40+5); write_data(':'); } void init() { lcden=0; write_com(0x38);//设置点阵,8位数据接口 write_com(0x0c);//开显示,光标显示,光标闪 write_com(0x06);//写一个字符指针加一,整屏不移动 write_com(0x01);//清屏 write_com(0x80);//初始化指针 desplay_main(); k1=1; k2=1; k3=1; k4=1; } void desplay_year(unsigned char shu) { int shi,ge; shi=shu/10; ge=shu%10; write_com(0x80+2); write_data(0x30+shi); write_data(0x30+ge); } void desplay_week(unsigned char shu) { write_com(0x80+15); write_data(0x30+shu); } void desplay_month(unsigned char shu) { int shi,ge; shi=shu/10; ge=shu%10; write_com(0x80+5); write_data(0x30+shi); write_data(0x30+ge); } void desplay_day(unsigned char shu) { int shi,ge; shi=shu/10; ge=shu%10; write_com(0x80+8); write_data(0x30+shi); write_data(0x30+ge); } void desplay_hour(unsigned char shu) { int shi,ge; shi=shu/10; ge=shu%10; write_com(0x80+0x40); write_data(0x30+shi); write_data(0x30+ge); } void desplay_minute(unsigned char shu) { int shi,ge; shi=shu/10; ge=shu%10; write_com(0x80+0x40+3); write_data(0x30+shi); write_data(0x30+ge); } void desplay_second(unsigned char shu) { int shi,ge; shi=shu/10; ge=shu%10; write_com(0x80+0x40+6); write_data(0x30+shi); write_data(0x30+ge); } void write_ds1302_byte(unsigned char dat) { unsigned char i; for(i=0;i<8;i++) { sclk=0; io=dat&0x01; dat=dat>>1; sclk=1; } } void write_ds1302(unsigned char add,unsigned char dat) { rst=0; _nop_(); sclk=0; _nop_(); rst=1; _nop_(); write_ds1302_byte(add); write_ds1302_byte(dat); rst=0; _nop_(); io=1; sclk=1; } unsigned char read_ds1302(unsigned char add) { unsigned char i,shu; rst=0; _nop_(); sclk=0; _nop_(); rst=1; _nop_(); write_ds1302_byte(add); for(i=0;i<8;i++) { shu=shu>>1; sclk=0; if(io==1) shu=shu|0x80; sclk=1; } rst=0; _nop_(); sclk=0; _nop_(); sclk=1; _nop_(); io=1; return shu; } void set() { unsigned char i,j; for(i=0;i<7;i++) { j=chuzhi[i]/10; xie[i]=chuzhi[i]%10; xie[i]=j*16+xie[i]; } write_ds1302(0x8e,0x00); for(i=0;i<7;i++) { write_ds1302(xieadd[i],xie[i]); } write_ds1302(0x8e,0x80); } void read_shuju() { unsigned char i; for(i=0;i<7;i++) { du[i]=read_ds1302(duadd[i]); } } void chuli() { unsigned char i,j; for(i=0;i<7;i++) { j=du[i]/16; du[i]=du[i]%16; du[i]=j*10+du[i]; } } void desplay_ds1302() { desplay_year(du[0]); desplay_month(du[2]); desplay_day(du[3]); desplay_week(du[1]); desplay_hour(du[4]); desplay_minute(du[5]); desplay_second(du[6]); } void guangbiao(unsigned char add) { write_com(add); write_com(0x0f); } void set_hour() { char time; unsigned char j,value; guangbiao(0x80+0x40+1); time=read_ds1302(duadd[4]); j=time/16; time=time%16; time=j*10+time; if(k2==0) { delay(5); if(k2==0) { time++; if(time==24) time=0;while(!k2); } j=time/10; value=time%10; value=j*16+value; write_ds1302(0x8e,0x00); write_ds1302(xieadd[4],value); write_ds1302(0x8e,0x80); desplay_hour(time); } if(k3==0) { delay(5); if(k3==0) { time--; if(time==-1) time=23;while(!k3); } j=time/10; value=time%10; value=j*16+value; write_ds1302(0x8e,0x00); write_ds1302(xieadd[4],value); write_ds1302(0x8e,0x80); desplay_hour(time); } } void set_minute() { char time; unsigned char j,value; guangbiao(0x80+0x40+4); time=read_ds1302(duadd[5]); j=time/16; time=time%16; time=j*10+time; if(k2==0) { delay(5); if(k2==0) { time++; if(time==60) time=0; } while(!k2); j=time/10; value=time%10; value=j*16+value; write_ds1302(0x8e,0x00); write_ds1302(xieadd[5],value); write_ds1302(0x8e,0x80); desplay_minute(time); } if(k3==0) { delay(5); if(k3==0) { time--; if(time==-1) time=59; } while(!k3); j=time/10; value=time%10; value=j*16+value; write_ds1302(0x8e,0x00); write_ds1302(xieadd[5],value); write_ds1302(0x8e,0x80); desplay_minute(time); } } void set_second_zero() { write_ds1302(0x8e,0x00); write_ds1302(xieadd[6],0); write_ds1302(0x8e,0x80); } void set_year() { char time; unsigned char j,value; guangbiao(0x80+3); time=read_ds1302(duadd[0]); j=time/16; time=time%16; time=j*10+time; if(k2==0) { delay(5); if(k2==0) { time++; if(time==100) time=0; } while(!k2); j=time/10; value=time%10; value=j*16+value; write_ds1302(0x8e,0x00); write_ds1302(xieadd[0],value); write_ds1302(0x8e,0x80); desplay_year(time); } if(k3==0) { delay(5); if(k3==0) { time--; if(time==-1) time=99; } while(!k3); j=time/10; value=time%10; value=j*16+value; write_ds1302(0x8e,0x00); write_ds1302(xieadd[0],value); write_ds1302(0x8e,0x80); desplay_year(time); } } void set_month() { char time; unsigned char j,value; guangbiao(0x80+6); time=read_ds1302(duadd[2]); j=time/16; time=time%16; time=j*10+time; if(k2==0) { delay(5); if(k2==0) { time++; if(time==13) time=0; } while(!k2); j=time/10; value=time%10; value=j*16+value; write_ds1302(0x8e,0x00); write_ds1302(xieadd[2],value); write_ds1302(0x8e,0x80); desplay_month(time); } if(k3==0) { delay(5); if(k3==0) { time--; if(time==-1) time=12; } while(!k3); j=time/10; value=time%10; value=j*16+value; write_ds1302(0x8e,0x00); write_ds1302(xieadd[2],value); write_ds1302(0x8e,0x80); desplay_month(time); } } void set_day() { char time; unsigned char j,value; guangbiao(0x80+9); time=read_ds1302(duadd[3]); j=time/16; time=time%16; time=j*10+time; if(k2==0) { delay(5); if(k2==0) { time++; if(time==31) time=0; } while(!k2); j=time/10; value=time%10; value=j*16+value; write_ds1302(0x8e,0x00); write_ds1302(xieadd[3],value); write_ds1302(0x8e,0x80); desplay_day(time); } if(k3==0) { delay(5); if(k3==0) { time--; if(time==-1) time=31; } while(!k3); j=time/10; value=time%10; value=j*16+value; write_ds1302(0x8e,0x00); write_ds1302(xieadd[3],value); write_ds1302(0x8e,0x80); desplay_day(time); } } void set_week() { char time; unsigned char j,value; guangbiao(0x80+15); time=read_ds1302(duadd[1]); j=time/16; time=time%16; time=j*10+time; if(k2==0) { delay(5); if(k2==0) { time++; if(time==8) time=1; } while(!k2); j=time/10; value=time%10; value=j*16+value; write_ds1302(0x8e,0x00); write_ds1302(xieadd[1],value); write_ds1302(0x8e,0x80); desplay_week(time); } if(k3==0) { delay(5); if(k3==0) { time--; if(time==-1) time=7; } while(!k3); j=time/10; value=time%10; value=j*16+value; write_ds1302(0x8e,0x00); write_ds1302(xieadd[1],value); write_ds1302(0x8e,0x80); desplay_week(time); } } void set_time() { if(k1==0) { delay(5); if(k1==0) { flag++; while(!k1); desplay_ds1302(); guangbiao(0x80+0x40+1); } while(flag!=7) { if(k1==0) { delay(5); if(k1==0) { flag=flag+1;while(!k1); } } if(flag==1) set_hour(); if(flag==2) set_minute(); if(flag==3) set_year(); if(flag==4) set_month(); if(flag==5) set_day(); if(flag==6) set_week(); } flag=0; set_second_zero(); } } void main() { init(); set(); while(1) { read_shuju(); chuli(); set_time(); write_com(0x0c); desplay_ds1302(); } }
|
|
相关推荐
2个回答
|
|
{:16:}{:16:}
|
|
|
|
没人理,新手求解
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
飞凌嵌入式ElfBoard EL 1板卡-i2c与从设备通讯编程示例之i2c-tools工具使用
976 浏览 0 评论
stc15f2k60s2利用串口传输字模存储到eeprom并进行点阵显示
1093 浏览 1 评论
1356 浏览 0 评论
607 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-串口通讯编程示例之串口编写程序
1236 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11600 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-4 04:06 , Processed in 0.526042 second(s), Total 76, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号