1、ds1302+lcd1602程序#include#include#define uint unsigned int#define uchar unsigned char/*1602液晶显示位定义*/#define lcd_data P0sbit rs=P17;sbit rw=P16;sbit en=P24;void write_1602_cmd(uint);void write_1602_dat(uint);void delay1ms(uint);void lcd1602_init();uchar nyx16= 2011-00-00 MON ;uchar sfm13 = 00:00:00 ;/
2、*ds1302的位定义*/sbit rst=P22;sbit sclk=P14;sbit io=P15;void write_ds1302_byte(uint);void lcd_busy_wait();void write_ds1302(uint,uint);uint read_ds1302(uint);void init_time();void read_time();void xieshijian2(uint add,uint shu);void xieshijian1(uint add,uint shu);void xiexingqi(uint add,uint shu);void t
3、ime_bcd(uint x);uint time_wadd=0x8c,0x88,0x86,0x8a,0x84,0x82,0x80;uint time_radd=0x8d,0x89,0x87,0x8b,0x85,0x83,0x81;uint time_init7=11,0,0,1,0,0,0;uint time_shuzhi7=0,0,0,0,0,0,0;uchar xingqi73=MON,TUS,WEN,THU,FRI,STA,SUN; /按键定义sbit key1=P10;sbit key2=P11;sbit key3=P12;/标志位定义void keyscan(); uint gn_
4、flag;/程序开始void main() uint num; lcd1602_init(); init_time(); write_1602_cmd(0x80); for(num=0;num16;num+) write_1602_dat(nyxnum); write_1602_cmd(0x80+0x40); for(num=0;num0;i-) for(j=110;j0;j-);/*ds1302的时序操作*/void write_ds1302_byte(uint date) uint i; for(i=0;i=1; sclk=1; void write_ds1302(uint cmd,uin
5、t date) rst=0; _nop_(); sclk=0; _nop_(); rst=1; _nop_(); write_ds1302_byte(cmd); write_ds1302_byte(date); rst=0; _nop_(); io=1; sclk=1; uint read_ds1302(uint add) uint shu,i; rst=0; _nop_(); sclk=0; _nop_(); rst=1; _nop_(); write_ds1302_byte(add); for(i=0;i=1; sclk=0; if(io) shu|=0x80; sclk=1; rst=0
6、; _nop_(); io=1; sclk=1; return shu;void init_time() uint i,a,b; for(i=0;i7;i+) a=time_initi/10; b=time_initi%10; time_initi=a*16+b; write_ds1302(0x8e,0x00); for(i=0;i7;i+) write_ds1302(time_waddi,time_initi); write_ds1302(0x8e,0x80); void read_time() uint i; for(i=0;i7;i+) time_shuzhii=read_ds1302(
7、time_raddi); void xieshijian2(uint add,uint shu) uint a,b; a=shu/16; b=shu%16; write_1602_cmd(0x80+0x40+add); write_1602_dat(0x30+a); write_1602_dat(0x30+b); void xieshijian1(uint add,uint shu) uint a,b; a=shu/16; b=shu%16; write_1602_cmd(0x80+add); write_1602_dat(0x30+a); write_1602_dat(0x30+b);voi
8、d xiexingqi(uint add,uint shu) uint i; write_1602_cmd(0x80+add); for(i=0;i3;i+) write_1602_dat(xingqishu-1i); void time_bcd(uint x) uint a,b; a=time_shuzhix/16; b=time_shuzhix%16; if(b=0x0a) a+; b=0; time_shuzhix=a*16+b; /*按键设置时间*/void keyscan() uint i; if(key1=0) delay1ms(5); if(key1=0) while(!key1
9、); gn_flag+; if(gn_flag=8) gn_flag=0; write_1602_cmd(0x0c); write_ds1302(0x8e,0x00); for(i=0;i7;i+) write_ds1302(time_waddi,time_shuzhii); write_ds1302(0x8e,0x10); switch(gn_flag) case 1 : write_1602_cmd(0x80+0x40+11); write_1602_cmd(0x0f);break; case 2 : write_1602_cmd(0x80+0x40+8);break; case 3 :
10、write_1602_cmd(0x80+0x40+5);break; case 4 : write_1602_cmd(0x80+14);break; case 5 : write_1602_cmd(0x80+10);break; case 6 : write_1602_cmd(0x80+7);break; case 7 : write_1602_cmd(0x80+4);break; default: break; if(key2=0) delay1ms(5); if(key2=0) while(!key2); switch(gn_flag) case 1 : time_shuzhi6+; ti
11、me_bcd(6); if(time_shuzhi6=0x59) time_shuzhi6=0 ; xieshijian2(10,time_shuzhi6); write_1602_cmd(0x80+0x40+11); break; case 2 : time_shuzhi5+; time_bcd(5); if(time_shuzhi5=0x59) time_shuzhi5=0 ; xieshijian2(7,time_shuzhi5); write_1602_cmd(0x80+0x40+8); break; case 3 : time_shuzhi4+; time_bcd(4); if(ti
12、me_shuzhi4=0x12) time_shuzhi4=0 ; xieshijian2(4,time_shuzhi4); write_1602_cmd(0x80+0x40+5); break; case 4 : time_shuzhi3+; time_bcd(3); if(time_shuzhi3=0x8) time_shuzhi3=1 ; xiexingqi(12,time_shuzhi3); write_1602_cmd(0x80+14); break; case 5 : time_shuzhi2+; time_bcd(2); if(time_shuzhi2=0x30) time_sh
13、uzhi2=0 ; xieshijian1(9,time_shuzhi2); write_1602_cmd(0x80+10); break; case 6 : time_shuzhi1+; time_bcd(1); if(time_shuzhi1=0x13) time_shuzhi1=1 ; xieshijian1(6,time_shuzhi1); write_1602_cmd(0x80+7); break; case 7 : time_shuzhi0+; time_bcd(0); if(time_shuzhi6=0x50) time_shuzhi6=0 ; xieshijian1(3,time_shuzhi0); write_1602_cmd(0x80+4); break; default : break;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1