#include "C8051F340.h" #define uint unsigned int #define uchar unsigned char sbit SCLK = P2^3 ; //DS1302時鐘 sbit DIO = P2^4 ; //DS1302數(shù)據(jù) sbit CE = P2^5 ; //DS1302片選 void Ds1302_Write_Byte (uchar addr, uchar dat); uchar Ds1302_Read_Byte ( uchar addr ); void Ds1302_Write_Time(void); void Ds1302_Read_Time(void); void Ds1302_Init(void) #define WRITE_SECOND 0x80 #define WRITE_MINUTE 0x82 #define WRITE_HOUR 0x84 #define WRITE_DAY 0x86 #define WRITE_MONTH 0x88 #define WRITE_WEEK 0x8A #define WRITE_YEAR 0x8C #define WRITE_TIMER_FLAG 0xC0 #define READ_SECOND 0x81 #define READ_MINUTE 0x83 #define READ_HOUR 0x85 #define READ_DAY 0x87 #define READ_MONTH 0x89 #define READ_WEEK 0x8B #define READ_YEAR 0x8D #define READ_TIMER_FLAG 0xC1 #define WRITE_PROTECT 0x8E uchar time_buf1[8] = {20,11,8,31,13,35,10,3}; //空年月日時分秒周 uchar time_buf[8]; //空年月日時分秒周 //============================================ //函數(shù)名稱:void Ds1302_Write_Byte (byte addr, byte dat) //功能: 串行發(fā)送地址、數(shù)據(jù),先發(fā)低位,且在上升沿發(fā)送 //參數(shù)傳遞:有,地址和數(shù)據(jù) //返回值: 無 //=========================================== void Ds1302_Write_Byte (uchar addr, uchar dat) { uchar i; //DIOOUT; //數(shù)據(jù)端口定義為輸出 CE = 1; //寫入目標地址:addr addr = addr & 0xFE; //最低位置零 for (i = 0; i < 8; i ++) { if (addr & 0x01) { DIO = 1; } else { DIO = 0; } SCLK = 1; SCLK = 0; addr = addr >> 1; } //寫入數(shù)據(jù):d for (i = 0; i < 8; i ++) { if (dat & 0x01) { DIO = 1; } else { DIO = 0; } SCLK = 1; SCLK = 0; dat = dat >> 1; } CE = 0;; //停止DS1302總線 } //=============================================== //函數(shù)名稱:byte Ds1302_Read_Byte ( byte addr ) //功能: 串行讀取數(shù)據(jù),先發(fā)低位,且在下降沿發(fā)送 //參數(shù)傳遞:有,地址 //返回值: 有,讀取的數(shù)據(jù) //=============================================== uchar Ds1302_Read_Byte ( uchar addr ) { uchar i; uchar temp; CE = 1; //寫入目標地址:addr addr = addr | 0x01; //最低位置高 for (i = 0; i < 8; i ++) { if (addr & 0x01) { DIO = 1; } else { DIO = 0; } SCLK = 1; SCLK = 0; addr = addr >> 1; } //輸出數(shù)據(jù):temp //DIOIN; //數(shù)據(jù)端口定義為輸入 for (i = 0; i < 8; i ++) { temp = temp >> 1; if (DIO) { temp |= 0x80; } else { temp &= 0x7F; } SCLK = 1; SCLK = 0; } CE = 0; //停止DS1302總線 //DIOOUT; //數(shù)據(jù)端口定義為輸出 return temp; } //=============================================== // 向DS1302寫入時鐘數(shù)據(jù) //=============================================== void Ds1302_Write_Time(void) { uchar i,tmp; for(i=0;i<8;i++) { //BCD處理 tmp=time_buf1[i]/10; time_buf[i]=time_buf1[i]%10; time_buf[i]=time_buf[i]+tmp*16; } Ds1302_Write_Byte(WRITE_PROTECT,0x00); //關(guān)閉寫保護 Ds1302_Write_Byte(WRITE_SECOND,0x80); //暫停 //Ds1302_Write_Byte(ds1302_charger_add,0xa9); //涓流充電 Ds1302_Write_Byte(WRITE_YEAR,time_buf[1]); //年 Ds1302_Write_Byte(WRITE_MONTH,time_buf[2]); //月 Ds1302_Write_Byte(WRITE_DAY,time_buf[3]); //日 Ds1302_Write_Byte(WRITE_HOUR,time_buf[4]); //時 Ds1302_Write_Byte(WRITE_MINUTE,time_buf[5]); //分 Ds1302_Write_Byte(WRITE_SECOND,time_buf[6]); //秒 Ds1302_Write_Byte(WRITE_WEEK,time_buf[7]); //周 Ds1302_Write_Byte(WRITE_PROTECT,0x80); //打開寫保護 } //======================================== // 從DS1302讀出時鐘數(shù)據(jù) //======================================== void Ds1302_Read_Time(void) { uchar i,tmp; time_buf[1]=Ds1302_Read_Byte(READ_YEAR); //年 time_buf[2]=Ds1302_Read_Byte(READ_MONTH); //月 time_buf[3]=Ds1302_Read_Byte(READ_DAY); //日 time_buf[4]=Ds1302_Read_Byte(READ_HOUR); //時 time_buf[5]=Ds1302_Read_Byte(READ_MINUTE); //分 time_buf[6]=(Ds1302_Read_Byte(READ_SECOND))&0x7F; //秒 time_buf[7]=Ds1302_Read_Byte(READ_WEEK); //周 for(i=0;i<8;i++) { //BCD處理 tmp=time_buf[i]/16; time_buf1[i]=time_buf[i]%16; time_buf1[i]=time_buf1[i]+tmp*10; } } //========================================== // DS1302初始化 //========================================== void Ds1302_Init(void) { CE = 0; //RST腳置低 SCLK = 0; //SCK腳置低 Ds1302_Write_Byte(WRITE_SECOND,0x00); //開始 }
|