📄 ds1302.c
字号:
#include "regx52.h"
#include "lcd1602.h"
#define DS_RST P2_5
#define DS_IO P2_7
#define DS_CLK P2_6
unsigned char Time[7]={0x00,0x00,0x00,0x01,0x01,0x01,0x08};
unsigned char Temp_Buffer[2];
void DS_Open(void)
{
DS_CLK=0;
DS_RST=1;
}
void DS_Close(void)
{
DS_RST=0;
DS_CLK=0;
}
void DS_Write_Byte(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(dat&0x01)
{
DS_IO=1;
}
else
{
DS_IO=0;
}
DS_CLK=1;
_nop_();
_nop_();
DS_CLK=0;
dat>>=1;
}
DS_IO=1;
}
unsigned char DS_Read_Byte(void)
{
unsigned char i,r_dat;
for(i=0;i<8;i++)
{
r_dat>>=1;
if(DS_IO)
{
r_dat+=0x80;
}
DS_CLK=1;
_nop_();
_nop_();
DS_CLK=0;
}
return r_dat;
}
void DS_Get_Time(unsigned char *p)
{
unsigned char i;
DS_Open();
DS_Write_Byte(0xbf);
DS_Close();
DS_Open();
DS_Write_Byte(0xbf);
for(i=0;i<7;i++)
{
p[i]=DS_Read_Byte();
}
DS_Close();
}
void DS_Set_Time(unsigned char *p)
{
unsigned char i;
p[0]&=0x7f;
DS_Open();
DS_Write_Byte(0xbe);
for(i=0;i<7;i++)
{
DS_Write_Byte(p[i]);
}
DS_Write_Byte(0x00);
DS_Close();
}
void DS_Initial(void)
{
unsigned char dat;
DS_RST=0;
DS_Open();
DS_Write_Byte(0x8e);
DS_Write_Byte(0x00);
DS_Close();
DS_Open();
DS_Write_Byte(0x81);
dat=DS_Read_Byte();
DS_Close();
dat&=0x7f;
DS_Open();
DS_Write_Byte(0x80);
DS_Write_Byte(dat);
DS_Close();
DS_Open();
DS_Write_Byte(0x90);
DS_Write_Byte(0xa5);
DS_Close();
DS_Open();
DS_Write_Byte(0x8e);
DS_Write_Byte(0x80);
DS_Close();
}
void Change_To_Char(unsigned int t,unsigned char *str)
{
unsigned char a[2];
char i, j;
a[0]=(t/10)%10;
a[1]=t%10;
for(i=0; i<2; i++)
a[i]=a[i]+'0';
for(i=0; (a[i]=='0')&&(i<2); i++)
{
;
}
for(j=0; j<i; j++)
{
*str='0';
str++;
}
for(; i<2; i++)
{
*str=a[i];
str++;
}
*str='\0';
}
void Delay(void)
{
unsigned char i,j;
for(i=0;i<224;i++)
for(j=0;j<224;j++)
;
}
void main(void)
{
unsigned char ii;
unsigned char year;
Lcd_Initial();
DS_Initial();
Gotoxy(0,0);
Print(" / / ");
Gotoxy(0,1);
Print(" : ");
DS_Set_Time(&Time[0]);
while(1)
{
year=20;
DS_Get_Time(&Time[0]);
ii=Time[1]&0x0f;
Time[1]>>=4;
Time[1]&=0x07;
Time[1]=Time[1]*10+ii;
Change_To_Char(Time[1],&Temp_Buffer[0]);
Gotoxy(3,1);
Print(&Temp_Buffer[0]);
if(Time[2]&&0x80)
{
if(Time[2]&&0x20)
{
Gotoxy(5,1);
Print("(AM)");
}
else
{
Gotoxy(5,1);
Print("(PM)");
}
}
else
{
Gotoxy(5,1);
Print("(24th)");
ii=Time[2]&0x0f;
Time[2]>>=4;
Time[2]&=0x03;
Time[2]=Time[2]*10+ii;
Change_To_Char(Time[2],&Temp_Buffer[0]);
Gotoxy(0,1);
Print(&Temp_Buffer[0]);
}
ii=Time[3]&0x0f;
Time[3]>>=4;
Time[3]&=0x03;
Time[3]=Time[3]*10+ii;
Change_To_Char(Time[3],&Temp_Buffer[0]);
Gotoxy(8,0);
Print(&Temp_Buffer[0]);
ii=Time[4]&0x0f;
Time[4]>>=4;
Time[4]&=0x01;
Time[4]=Time[4]*10+ii;
Change_To_Char(Time[4],&Temp_Buffer[0]);
Gotoxy(5,0);
Print(&Temp_Buffer[0]);
ii=Time[6]&0x0f;
Time[6]>>=4;
Time[6]&=0x0f;
Time[6]=Time[6]*10+ii;
Change_To_Char(Time[6],&Temp_Buffer[0]);
Gotoxy(2,0);
Print(&Temp_Buffer[0]);
Change_To_Char(year,&Temp_Buffer[0]);
Gotoxy(0,0);
Print(&Temp_Buffer[0]);
ii=Time[5]&0x07;
Gotoxy(12,1);
switch(ii)
{
case 0x01: Print("Mon"); break;
case 0x02: Print("Tue"); break;
case 0x03: Print("Wen"); break;
case 0x04: Print("Tue"); break;
case 0x05: Print("Fir"); break;
case 0x06: Print("Sat"); break;
case 0x07: Print("Sun"); break;
default: break;
}
Delay();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -