仿真图
/*主函数*/
- #include<reg52.h>
- extern void Init_1602();
- extern void ShowStr_1602(unsigned char x,unsigned char y,unsigned char *str);
- extern void Init_ds1302();
- extern unsigned char Read_ds1302(unsigned char reg);
- bit flag200ms=0;
- unsigned char cnt;
- unsigned char time[8];
- unsigned char str[12];
- unsigned char stmp=0xAA;
- void Init_time0();
- void main()
- {
- unsigned char i;
- Init_time0();
- Init_ds1302();
- Init_1602();
-
- while(1)
- {
- if(flag200ms)
- {
- flag200ms=0;
- for(i=0;i<7;i++)
- {
- time[i]=Read_ds1302(i);
- }
- if(stmp!=time[0])
- {
- str[0]='2';
- str[1]='0';
- str[2]=(time[6]>>4)+'0';
- str[3]=(time[6]&0x0F)+'0';
- str[4]='-';
- str[5]=(time[4]>>4)+'0';
- str[6]=(time[4]&0x0f)+'0';
- str[7]='-';
- str[8]=(time[3]>>4)+'0';
- str[9]=(time[3]&0x0f)+'0';
- str[10]='\0';
- ShowStr_1602(0,0,str);
- ShowStr_1602(0,1,"Week:");
- str[0]=(time[5]&0x0f)+'0';
- str[1]='\0';
- ShowStr_1602(5,1,str);
- str[0]=(time[2]>>4)+'0';
- str[1]=(time[2]&0x0f)+'0';
- str[2]=':';
- str[3]=(time[1]>>4)+'0';
- str[4]=(time[1]&0x0f)+'0';
- str[5]=':';
- str[6]=(time[0]>>4)+'0';
- str[7]=(time[0]&0x0f)+'0';
- str[8]='\0';
- ShowStr_1602(8,1,str);
- stmp=time[0];
- }
- }
- }
- }
- void Init_time0()
- {
- TMOD = 0x01;
- TH0 = 0x0DC;
- TL0 = 0x00;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- void interrupttime0()interrupt 1
- {
- TH0 = 0x0DC;
- TL0 = 0x00;
- cnt++;
- if(cnt>=2)
- {
- cnt=0;
- flag200ms=1;
- }
- }
- /*DS1302*/[/i][i]#include<reg52.h>
- sbit DS1302_RST=P2^1;
- sbit DS1302_IO=P2^2;
- sbit DS1302_SCLK=P2^3;
- void WriteByte_ds1302(unsigned char dat)
- {
- unsigned char mask;
- for(mask=0x01;mask!=0;mask<<=1)
- {
- if(mask&dat!=0)
- DS1302_IO=1;
- else
- DS1302_IO=0;
- DS1302_SCLK=1;
- DS1302_SCLK=0;
- }
- }
- void Write_ds1302(unsigned char reg,unsigned char dat)
- {
- DS1302_RST=1;
- WriteByte_ds1302((reg<<1)|0x80);
- WriteByte_ds1302(dat);
- DS1302_RST=0;
- }
- unsigned char ReadByte_ds1302()
- {
- unsigned char date;
- unsigned char mask;
- for(mask=0x01;mask!=0;mask<<=1)
- {
- if(DS1302_IO!=0)
- {
- date|=mask;
- }
- DS1302_SCLK=1;
- DS1302_SCLK=0;
- }
- return date;
- }
- unsigned char Read_ds1302(unsigned char reg)
- {
- unsigned char date;
- DS1302_RST=1;
- WriteByte_ds1302(reg<<1|0x81);
- date=ReadByte_ds1302();
- DS1302_RST=0;
-
- return date;
-
- }
- void Init_ds1302()
- {
- unsigned char time[]={0x10,0x3,0x11,0x27,0x15,0x25,0x30};
- unsigned char i;
-
-
- DS1302_RST=0;
- DS1302_SCLK=0;
- i=Read_ds1302(0);
- if((i&0x80)!=0)
- {
- Write_ds1302(7,0x00);
- for(i=0;i<7;i++)
- {
- Write_ds1302(i,time[i]);
- }
- }
- }
复制代码 |