#include "LCD1602.h"
uchar TimeData[7]={30, 55,15, 4, 8, 6, 18};
//DS1302дһ¸ö×Ö½Ú
void DS1302_Write_Byte(uchar dat)
{
uchar i;
for(i = 0; i < 8 ; i++) //д°Ë´Î
{
SCLK = 0; //ÀµÍʱÖÓÏß ÔÊÐíÊý¾ÝÏ߱仯
IO = dat & 0x01; // °Ñ×îµÍλµÄÊý¾ÝË͵½IO¿Ú
SCLK = 1; //À¸ßʱÖÓÏß ²úÉúÉÏÉýÑØ Ð´ÈëÊý¾Ý
dat >>= 1;//ÓÒÒÆ °Ñ¸ßλÒÀ´ÎÍùµÍλÒƶ¯
}
}
//DS1302¶ÁÒ»¸ö×Ö½Ú
uchar DS302_Read_Byte()
{
uchar i,dat;
for(i = 0; i < 8; i++)//
{
SCLK = 0;// ÔÊÐíÏÖÔÚµÄÊý¾Ý±ä»¯ ²¢²úÉúÒ»¸öϽµÑØ´ú±í¶ÁÊý¾Ý DS1302°ÑÊý¾ÝË͸øµ¥Æ¬»ú
dat >>=1;// ´Ó×îµÍλ½ÓÊÕ ·ÅÈë×î¸ßλ ÒªÓÒÒÆ7´Î µÚÒ»´ÎÓÒµÄÊÇ0
if(IO)
dat |= 0x80;// °ÑÊý¾Ý·ÅÈë×î¸ßλ
SCLK = 1;// À¸ßʱÖÓÏß ±ãÓÚ²úÉúÏÂÒ»¸ö¶ÁÐźÅ
}
return dat;
}
//ds1302дÈë¹ý³Ì дÈëÒ»¸ö×Ö½Ú
void DS1302_Write(uchar cmd,uchar dat)
{
RST = 0;// ³õʼ»¯Ê¹ÄܶË
SCLK = 0;//³õʼ»¯Ê±ÖÓÏß
RST = 1; // À¸ßʹÄÜ¶Ë ÔÊÐí¶Áд
DS1302_Write_Byte(cmd);// дÈëÊý¾ÝµÄµØÖ·
DS1302_Write_Byte(dat);// дÈëµØÖ·µÄÊý¾Ý
}
//DS1302¶Á³ö¹ý³Ì ¶Á³öÒ»¸ö×Ö½Ú
uchar DS1302_Read(uchar cmd)
{
uchar dat;
RST = 0;
SCLK = 0;
RST = 1;
DS1302_Write_Byte(cmd);//дÈë¶Á³öÊý¾ÝµÄµØÖ·
dat = DS1302_Read_Byte();// ´ÓµØÖ·ÖжÁ³öÊý¾Ý
return dat;
}
//Çå³ýд±£»¤
void DS1302_Clear_WP()
{
DS1302_Write(0x8e,0x00);//ÄÜдÈëÊý¾Ý
}
//¿ªÆôд±£»¤
void DS1302_Set_WP()
{
DS1302_Write(0x8e,0x80);//²»ÔÊÐíдÈëÊý¾Ý
RST = 0;
SCLK = 0;
}
//ÉèÖÃDS1£³£°£²
// Á¬ÐøдÈë7¸ö×Ö½ÚµÄÊý¾Ý
void Set_DS1302_Time(uchar addr)
{
uchar i,j;
DS1302_Clear_WP();
for(i = 0;i < 7;i++)
{
j = TimeData[i]/10;
TimeData[i] %= 10;
TimeData[i] += j*16;
DS1302_Write(addr,TimeData[i]);
addr += 2;//дµØÖ·Ö¸Õë¼Ó¶þ
}
DS1302_Set_WP();
}
//¶ÁDS1302
uchar Read_DS1302_Time(uchar addr)
{
uchar i,dat;
DS1302_Clear_WP();
for(i = 0; i < 7; i++)
{
TimeData[i] = DS1302_Read_Byte();
addr +=2;
}
DS1302_Set_WP();
return dat;
}
void main()
{
Lcd1602_Init();
Set_DS1302_Time(DS1302_Write_addr);//дÈëµØÖ·
LcdShowStr(0,1,"Date:"); //1602ÏÔʾ×Ö·û´®
LcdShowStr(0,0,"Time:"); //1602ÏÔʾ×Ö·
while(1)
{
Read_DS1302_Time(DS1302_Read_addr);
DisplayOneStr(6, 0,TimeData[6]);//Äê
DisplayOneChar(8,0,'-');
DisplayOneStr(9,0,TimeData[4]);//ÔÂ
DisplayOneChar(11,0,'-');
DisplayOneStr(12,0,TimeData[3]);//ÈÕ
DisplayOneChar(13,0,'-');
DisplayOneStr(6,1,TimeData[2]);//ʱ
DisplayOneChar(8,1,':');
DisplayOneStr(9,1,TimeData[1]);//·Ö
DisplayOneChar(10,1,':');
DisplayOneStr(11,1,TimeData[0]);//Ãë
DisplayOneChar(13,1,':');
Delay_Ms(1000);
}
}