// 名称:用 DS1302 与 1602LCD 设计的可调式电子日历与时钟 // 说明:本例会自动调节合法日期时间,对于星期的调节会在 // 调整年月日时自动完成,闰年问题也会自动判断。 //------------------------------------------------------ #include #include #define uchar unsigned char #define uint unsigned int sbit SDA=P1^0; //DS1302 数据线 sbit CLK=P1^1; //DSB1302 时钟线 sbit RST=P1^2; //DS1302 复位线 sbit RS=P2^0; //LCD 寄存器选择 sbit RW=P2^1; //LCD 读/写控制 sbit EN=P2^2; //LCD 启用 sbit K1=P3^4; //选择 sbit K2=P3^5; //加 sbit K3=P3^6; //减 sbit K4=P3^7; //确定 uchar tCount=0; uchar dat; //定义参数 //一年中每个月的天数,二月的天数由年份决定 uchar MonthsDays[]={31,0,31,30,31,30,31,31,30,31,30,31}; //周日,周一到周六{0,1-6} [读取 DS1302 时分别是 1-7] uchar *WEEK[]={"SUN","MON","TUS","WEN","THU","FRI","SAT"}; //LCD 显示缓冲 uchar LCD_DSY_BUFFER1[]={"DATE 00-00-00 "} ; uchar LCD_DSY_BUFFER2[]={"TIME 00:00:00 "} ; uchar DateTime[7]; //所读取的日期时间 char Adjust_Index=-1;//当前调节的时间对象:秒,分,时,日,月,年(0,1,2,3,4,6) uchar Change_Flag[]="-MHDM-Y"; //(分,时,日,月,年) (不调节秒周) //延时,向 DS1302 写、读一字节以及从 DS1302 指定位置度、写数据的程序 //延时--------------------------------------------------- void DelayMS(uint x) { uchar i; while(x--) for(i=0;i<120;i++); } //向 DS1302 写入一个字节 void Write_A_Byte_TO_DS1302(uchar x) { uchar i; for(i=0;i<8;i++) { SDA=x&1;CLK=1; CLK=0; x>>=1; } } //从 DA1302 读取一字节--------------------------------------- uchar Get_A_Byte_FROM_DS1302() { uchar i,b,t; for(i=0;i<8;i++) { b>>=1; t=SDA;b|=t<<7;CLK=1;CLK=0; } //BCD 码转换 return b/16*10+b%16; } //从 DS1302 指定的位置读数据-------------------------------------- uchar Read_Data(uchar addr) { uchar dat; RST=0;CLK=0;RST=1; Write_A_Byte_TO_DS1302(addr); dat=Get_A_Byte_FROM_DS1302(); CLK=1;RST=0; return da...