#define DectoBCD(time) time/10*16+time%10
#define BcdtoDec(time) time/16*10+time%16
sbit SCK=P1^7;
sbit IO=P2^3;
sbit CE = P1^3; // DS1302复位
void set_ds1302_time(unsigned char sec,unsigned char min,unsigned char hour)
{
Write_Ds1302_Byte(0x8e,0x00); //关闭写保护
Write_Ds1302_Byte(0x80,DectoBCD(sec)); //读0x81
Write_Ds1302_Byte(0x82,DectoBCD(min)); //读0x83
Write_Ds1302_Byte(0x84,DectoBCD(hour)); //读0x85
Write_Ds1302_Byte(0x8e,0x80); //开启写保护
}
void read_ds1302(void)
{
if(TH2<0xf0)
{
hour=Read_Ds1302_Byte ( 0x85 );
min=Read_Ds1302_Byte ( 0x83 );
sec=Read_Ds1302_Byte ( 0x81 );
sec=BcdtoDec(sec);
min=BcdtoDec(min);
hour=BcdtoDec(hour);
}
}
void ds1302_stop(void);
{
unsigned char tmep;
temp = Read_Ds1302_Byte ( 0x81);
Write_Ds1302_Byte(0x8e,0x00); //关闭写保护
Write_Ds1302_Byte(0x81,temp|0x80); //关闭写保护
Write_Ds1302_Byte(0x8e,0x80); //关闭写保护
}
void ds1302_start(void)
{
unsigned char tmep;
temp = Read_Ds1302_Byte ( 0x81);
Write_Ds1302_Byte(0x8e,0x00); //关闭写保护
Write_Ds1302_Byte(0x81,temp& 0x7f); //关闭写保护
Write_Ds1302_Byte(0x8e,0x80); //关闭写保护
}
蓝桥杯ds1302
于 2024-04-11 21:20:51 首次发布