@annotation :本开发板上DS1302的原理图
@annotation : The command byte is always input starting with the LSB(bit 0);
@annotation :Close Write Protext example,Write_Ds1302_Byte(0X8E,0X00);
@annotation : Address table
@annotation : 根据时序图,接收数据和发送数据低位在前
DS1302.C——时钟
//=====================================================
#include "MAIN.H"
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3;
u8 data SetNum[] = {0,0,0,0,0,0,0}; //时间数组
//=====================================================
//@role :存放写地址和读地址的合集
typedef data enum
{
Seconds_W = 0X80,Seconds_R ,Minutes_W , Minutes_R ,Hour_W , Hour_R ,Data_W ,Data_R ,Month_W , Month_R ,Day_W , Day_R,Year_W , Year_R ,WP_W , WP_R ,
} Office;
//=====================================================
//@role :设定时间
//十进制转BCD存入
void DS1302_SetTime()
{
Write_Ds1302_Byte( WP_W ,0X00); // close write protect
Write_Ds1302_Byte( Year_W,SetNum[0]/10*16+SetNum[0]%10); // years
Write_Ds1302_Byte( Month_W,SetNum[1]/10*16+SetNum[1]%10); // months
Write_Ds1302_Byte( Data_W,SetNum[2]/10*16+SetNum[2]%10); // data
Write_Ds1302_Byte( Hour_W,SetNum[3]/10*16+SetNum[3]%10); // hours
Write_Ds1302_Byte( Minutes_W,SetNum[4]/10*16+SetNum[4]%10); // Minutes
Write_Ds1302_Byte( Seconds_W,SetNum[5]/10*16+SetNum[5]%10); // Senconds
Write_Ds1302_Byte( Day_W ,SetNum[6]/10*16+SetNum[5]%10); // week (1-7)
Write_Ds1302_Byte( WP_W,0X80); // up write protect
}
//=====================================================
//@role : 读取时间数组中的时间
//BCD转10进制读出
void DS1302_ReadTime()
{
SetNum[0] = Read_Ds1302_Byte(Year_R)/16*10+Read_Ds1302_Byte(Year_R)%16;
SetNum[1] = Read_Ds1302_Byte(Month_R)/16*10+Read_Ds1302_Byte(Month_R)%16;
SetNum[2] = Read_Ds1302_Byte(Data_R)/16*10+Read_Ds1302_Byte(Data_R)%16;
SetNum[3] = Read_Ds1302_Byte(Hour_R)/16*10+Read_Ds1302_Byte(Hour_R)%16;
SetNum[4] = Read_Ds1302_Byte(Minutes_R)/16*10+Read_Ds1302_Byte(Minutes_R)%16;
SetNum[5] = Read_Ds1302_Byte(Seconds_R)/16*10+Read_Ds1302_Byte(Seconds_R)%16;
SetNum[6] = Read_Ds1302_Byte(Day_R)/16*10+Read_Ds1302_Byte(Day_R)%16;
}
//=====================================================