?? ds-1302.c
字號:
sbit B0=B^0;
sbit B1=B^1;
sbit B2=B^2;
sbit B3=B^3;
sbit B4=B^4;
sbit B5=B^5;
sbit B6=B^6;
sbit B7=B^7;
sbit SCLK=P1^0;
sbit RST=P1^5;
sbit DIO=P1^1;
void Write_DS1302(unsigned char WR_Addr, unsigned char WR_Data) //寫入的地址,寫入的位置
{
SCLK=0; RST=1; //寫完了地址,8位的
B=WR_Addr;
DIO=B0; SCLK=1; SCLK=0;
DIO=B1; SCLK=1; SCLK=0;
DIO=B2; SCLK=1; SCLK=0;
DIO=B3; SCLK=1; SCLK=0;
DIO=B4; SCLK=1; SCLK=0;
DIO=B5; SCLK=1; SCLK=0;
DIO=B6; SCLK=1; SCLK=0;
DIO=B7; SCLK=1; SCLK=0;
//寫入數據
B=WR_Data;
DIO=B0; SCLK=1; SCLK=0;
DIO=B1; SCLK=1; SCLK=0;
DIO=B2; SCLK=1; SCLK=0;
DIO=B3; SCLK=1; SCLK=0;
DIO=B4; SCLK=1; SCLK=0;
DIO=B5; SCLK=1; SCLK=0;
DIO=B6; SCLK=1; SCLK=0;
DIO=B7; SCLK=1;
RST=0;
}
unsigned char Read_DS1302(unsigned char WR_Addr)
{
SCLK=0; RST=1;
B=WR_Addr;
//寫入地址
DIO=B0; SCLK=1; SCLK=0;
DIO=B1; SCLK=1; SCLK=0;
DIO=B2; SCLK=1; SCLK=0;
DIO=B3; SCLK=1; SCLK=0;
DIO=B4; SCLK=1; SCLK=0;
DIO=B5; SCLK=1; SCLK=0;
DIO=B6; SCLK=1; SCLK=0;
DIO=B7; SCLK=1;
//讀出數據
SCLK=0; B0=DIO; SCLK=1;
SCLK=0; B1=DIO; SCLK=1;
SCLK=0; B2=DIO; SCLK=1;
SCLK=0; B3=DIO; SCLK=1;
SCLK=0; B4=DIO; SCLK=1;
SCLK=0; B5=DIO; SCLK=1;
SCLK=0; B6=DIO; SCLK=1;
SCLK=0; B7=DIO; SCLK=1;
RST=0;
return B;
}
unsigned char temp,time_sec,time_min,time_hour,time_date,time_day,time_mon,time_year;
void Init_DS1302(void)
{
Write_DS1302(0x8e,0x00); //寫入允許
temp=Read_DS1302(0x81); //回讀秒寄存器
temp=temp&0x7f; //最高位置0
Write_DS1302(0x80,temp); //寫入秒寄存器
temp=Read_DS1302(0x85); //回讀小時
temp=temp&0x7f; //最高位置0,24小時模式
Write_DS1302(0x84,temp); //寫入小時
Write_DS1302(0x90,0xa5); //充電控制:允許充電,一個二極管,2K電阻
Write_DS1302(0x8e,0x80); //寫入保護
}
void Read_DS1302_Clock(void)
{
unsigned char temp;
//讀秒
temp=Read_DS1302(0x81)&0x7f;
time_sec=temp%16+temp/16*10;
//讀分鐘
temp=Read_DS1302(0x83);
time_min=temp%16+temp/16*10;
//讀小時
temp=Read_DS1302(0x85);
time_hour=temp%16+temp/16*10;
//讀星期
time_day=Read_DS1302(0x8b);
//讀日期
temp=Read_DS1302(0x87);
time_date=temp%16+temp/16*10;
//讀月
temp=Read_DS1302(0x89);
time_mon=temp%16+temp/16*10;
//讀年
temp=Read_DS1302(0x8d);
time_year=temp%16+temp/16*10;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -