1、万年历初始源程序12864、DS1302制作的电子万年历代码如下:/*名称:电子万年历功能:自动计时100年,节日、时间提醒作者:甘春生实验板:KX-1N/*#include#define uint unsigned int#define uchar unsigned charsbit RS=P20;sbit RW=P21;sbit EN=P22;sbit FUN=P10;sbit OK=P11;sbit INC=P12;sbit DEC=P13;sbit RST=P25;sbit IO=P26;sbit SCLK=P27;uchar hour=10;uchar min=18;uchar se
2、c=30;uchar year=10;uchar mon=1;uchar date=20;uchar day=3;uchar count=0;uchar flag=0;uchar num=0;uchar i=6;void delay(uint i) uint j; for(;i0;i-) for(j=0;j125;j+);void Write_Ds1302_Byte(uchar temp) uchar i; for(i=0;i=1; SCL=1; void Write_Ds1302(uchar address,uchar dat) RST=0; SCLK=0; RST=1; Write_Ds1
3、302_Byte(address); Write_Ds1302_Byte(dat); SCLK=1; RST=0;uchar Read_Ds1302(uchar address) uchar i,temp=0x00; RST=0; SCLK=0; RST=1; Write_Ds1302_Byte(address); for(i=0;i=1; SCLK=0; if(IO) temp=temp|0x80; SCLK=1; RST=0; IO=0; return temp;void Check_Busy(void) RW=1;/RW=1为读 RS=0;/RS=0为指令 EN=1; while(P2&
4、0X80); void Write_com(uchar com) Check_Busy(); RW=0; RS=0; P2=com; EN=1; delay(1); EN=0;void Write_dat(uchar dat) Check_Busy(); RW=0; RS=1; P2=dat; EN=1; delay(1); EN=0; void Lcd_Pos(uchar Xpos,uchar Ypos) uchar tmp; Xpos&=0x0f; Ypos&=0x03; tmp=Xpos; if(Ypos=0) tmp|=0x80; if(Ypos=1) tmp|=0x90; if(Yp
5、os=2) tmp|=0x88; if(Ypos=3) tmp|=0x98; Write_com(tmp);void Write_LCD_String(uchar x,uchar y,uchar *s) Lcd_Pos(x,y); Lcd_Pos(x,y); while (*s!=0) Write_dat(*s); s+; delay(1); void LCD_Rst(void) Write_com(0x30); Check_Busy(); Write_com(0x01); Write_com(0x06); Write_com(0x0c); delay(5);void Write_day(uc
6、har Sel) switch (Sel) case 1:Write_LCD_String(6,1,一);break; case 2:Write_LCD_String(6,1,二);break; case 3:Lcd_Pos(6,1);Write_dat(0xc8);Write_dat(0xfd);break; case 4:Write_LCD_String(6,1,四);break; case 5:Write_LCD_String(6,1,五);break; case 6:Write_LCD_String(6,1,六);break; case 7:Write_LCD_String(6,1,日
7、);break; default :break; void Updat_Time(void) Lcd_Pos(3,0);Write_dat(0x30+(year/10);Write_dat(0x30+(year%10); Lcd_Pos(0,1);Write_dat(0x30+(mon/10);Write_dat(0x30+(mon%10); Lcd_Pos(2,1);Write_dat(0x30+(date/10);Write_dat(0x30+(date%10); Lcd_Pos(0,2);Write_dat(0x30+(hour/10);Write_dat(0x30+(hour%10);
8、 Lcd_Pos(2,2);Write_dat(0x30+(min/10);Write_dat(0x30+(min%10); Lcd_Pos(4,2);Write_dat(0x30+(sec/10);Write_dat(0x30+(sec%10); Write_day(day);void Write_Time(void) Write_Ds1302(0x8e,0x00); Write_Ds1302(0x8c,(year/10)*16+year%10); Write_Ds1302(0x88,(mon/10)*16+mon%10); Write_Ds1302(0x86,(date/10)*16+da
9、te%10); Write_Ds1302(0x84,(hour/10)*16+hour%10); Write_Ds1302(0x82,(min/10)*16+min%10); Write_Ds1302(0x80,(sec/10)*16+sec%10); Write_Ds1302(0x8a,(day/10)*16+day%10); Write_Ds1302(0x8e,0x80);void Read_Time(void) year=(Read_Ds1302(0x8d)4)*10+Read_Ds1302(0x8d)%16; mon=(Read_Ds1302(0x89)4)*10+Read_Ds130
10、2(0x89)%16; date=(Read_Ds1302(0x87)4)*10+Read_Ds1302(0x87)%16; hour=(Read_Ds1302(0x85)4)*10+Read_Ds1302(0x85)%16; min=(Read_Ds1302(0x83)4)*10+Read_Ds1302(0x83)%16; sec=(Read_Ds1302(0x81)4)*10+Read_Ds1302(0x81)%16; day=(Read_Ds1302(0x8b)4)*10+Read_Ds1302(0x8b)%16; Updat_Time(); void Set_Time(void) if
11、(FUN=0) delay(10); if(FUN=0) while(!FUN); flag=1; num+; if(num=8) num=1; if(flag) if(num=1) Lcd_Pos(0,2); Write_com(0x0d); if(INC=0) delay(10); if(INC=0) while(!INC); hour+; if(hour=24) hour=0; Updat_Time(); if(DEC=0) delay(10); if(DEC=0) while(!DEC); hour-; if(hour=-1) hour=23; Updat_Time(); if(num
12、=2) Lcd_Pos(2,2); Write_com(0x0d); if(INC=0) delay(10); if(INC=0) while(!INC); min+; if(min=60) min=0; Updat_Time(); if(DEC=0) delay(10); if(DEC=0) while(!DEC); min-; if(min=-1) min=59; Updat_Time(); if(num=3) Lcd_Pos(4,2); Write_com(0x0d); if(INC=0) delay(10); if(INC=0) while(!INC); sec+; if(sec=60
13、) sec=0; Updat_Time(); if(DEC=0) delay(10); if(DEC=0) while(!DEC); sec-; if(sec=-1) sec=59; Updat_Time(); if(num=4) Lcd_Pos(3,0); Write_com(0x0d); if(INC=0) delay(10); if(INC=0) while(!INC); year+; if(year=100) year=0; Updat_Time(); if(DEC=0) delay(10); if(DEC=0) while(!DEC); year-; if(year=-1) sec=
14、99; Updat_Time(); if(num=5) Lcd_Pos(0,1); Write_com(0x0d); if(INC=0) delay(10); if(INC=0) while(!INC); mon+; if(mon=13) mon=1; Updat_Time(); if(DEC=0) delay(10); if(DEC=0) while(!DEC); mon-; if(mon=0) mon=12; Updat_Time(); if(num=6) Lcd_Pos(2,1); Write_com(0x0d); if(INC=0) delay(10); if(INC=0) while
15、(!INC); date+; if(date=32) date=1; Updat_Time(); if(DEC=0) delay(10); if(DEC=0) while(!DEC); date-; if(date=0) date=31; Updat_Time(); if(num=7) Lcd_Pos(6,1); Write_com(0x0d); if(INC=0) delay(10); if(INC=0) while(!INC); day+; if(day=8) day=1; Updat_Time(); if(DEC=0) delay(10); if(DEC=0) while(!DEC); day-; if(day=0) day=7; Updat_Time(); i
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1