基于51單片機(jī)設(shè)計的DS1302時鐘程序
/******************************************************#include reg52.h>DS1302時鐘程序 *******************************************************/sbit ACC_0 = ACC^0;sbit ACC_1 = ACC^1;sbit ACC_2 = ACC^2;sbit ACC_3 = ACC^3;sbit ACC_4 = ACC^4;sbit ACC_5 = ACC^5;sbit ACC_6 = ACC^6;sbit ACC_7 = ACC^7; //**************************sbit T_SCLK = P3^5; // DS1302時鐘信號 7腳sbit T_DIO= P3^6; // DS1302數(shù)據(jù)信號 6腳sbit T_CE = P3^7;//**************************uchar sec,min,hour,day,month,week,year; //時間變量//**************************uchar DS1302_addr[]={0x80, //0,寫入秒(Second)寄存器0x81, //1,讀出秒(Second)寄存器0x82, //2,寫入分(Minute)寄存器0x83, //3,讀出分(Minute)寄存器0x84, //4,寫入小時(Hour)寄存器0x85, //5,讀出小時(Hour)寄存器0x86, //6,寫入日(Day)寄存器0x87, //7,讀出日(Day)寄存器0x88, //8,寫入月份(Month)寄存器0x89, //9,讀出月份(Month)寄存器0x8a, //10,寫入周(Week)寄存器0x8b, //11,讀出周(Week)寄存器0x8c, //12,寫入年份(Year)寄存器0x8d, //13,讀出年份(Year)寄存器0x8e, //14,寫保護(hù)_寄存器0x8f //15,涓流充電} ;//***************寫入一字節(jié)***********************void DS1302_Input_Byte(uchar Input) //向時鐘IC寫入一字節(jié){ uchar i;ACC =Input;for(i=8; i>0; i--){ T_DIO = ACC_0; //相當(dāng)于匯編中的 RRCT_SCLK = 1;T_SCLK = 0;ACC = ACC >> 1; } }//**************讀取一字節(jié)()*************************uchar DS1302_Output_Byte(void) //從時鐘IC讀取一字節(jié)(){ uchar i; for(i=8; i>0; i--){ ACC>>=1; T_DIO= 1;ACC_7 = T_DIO;T_SCLK = 1; //相當(dāng)于匯編中的 RRC T_SCLK = 0; } return(ACC); }//**************向時鐘IC寫數(shù)據(jù)*************************void DS1302_Write_one( uchar addr,dat ) // 寫入地址、數(shù)據(jù)子程序 {T_CE=0; //T_CE引腳為低,數(shù)據(jù)傳送中止T_SCLK=0; //清零時鐘總線T_CE = 1; //T_CE引腳為高,邏輯控制有效DS1302_Input_Byte(addr); // 地址,命令 DS1302_Input_Byte(dat); // 寫1Byte數(shù)據(jù)T_SCLK = 1;T_CE = 0; }//************從數(shù)據(jù)讀取數(shù)據(jù)*****************************uchar DS1302_Read ( uchar addr ) //數(shù)據(jù)讀取子程序{uchar date;T_CE=0; T_SCLK=0; T_CE = 1; DS1302_Input_Byte(addr); // 地址,命令 date = DS1302_Output_Byte(); // 讀1Byte數(shù)據(jù)T_SCLK = 1;T_CE = 0;return(date);}//************從數(shù)據(jù)讀取數(shù)據(jù)****************************void DS1302_Write( uchar sec_w,min_w,hour_w,day_w,month_w,week_w,year_w ){DS1302_Write_one(0x8e,0x00);DS1302_Write_one(0x80,sec_w);DS1302_Write_one(0x82,min_w);DS1302_Write_one(0x84,hour_w);DS1302_Write_one(0x86,day_w);DS1302_Write_one(0x88,month_w);DS1302_Write_one(0x8a,week_w);DS1302_Write_one(0x8c,year_w);DS1302_Write_one(0x8e,0x80);} //************從數(shù)據(jù)讀取數(shù)據(jù)*****************************/*void DS1302_readtime() {sec=DS1302_Read(0x81); //讀秒min=DS1302_Read(0x83); //讀分hour=DS1302_Read(0x85); //讀時day=DS1302_Read(0x87); //讀日month=DS1302_Read(0x89); //讀月year=DS1302_Read(0x8d); //讀年week=DS1302_Read(0x8b); //讀星期}*///************數(shù)碼管DS1302顯示*****************************void DS1302_SMG_display(){ /* DS1302_readtime();SMG_display(6,sec%0x10);delay(1);SMG_display(5,sec/0x10);delay(1);SMG_display(4,min%0x10);delay(1);SMG_display(3,min/0x10);delay(1);SMG_display(2,hour%0x10);delay(1);SMG_display(1,hour/0x10);delay(1);*/////////////////////////////////////////////uchar i=0,a=6,*p;p=DS1302_addr;*p++;for(i=0;i3;i++){SMG_display(a,DS1302_Read(*p)%0x10);a--;delay(0);SMG_display(a,DS1302_Read(*p)/0x10); delay(0);a--;*p++;*p++;}delay(0); }//注意:有些單片機(jī),需將T_DIO信號腳接個上拉電阻,否會出現(xiàn)數(shù)據(jù)讀出錯誤.
51單片機(jī)相關(guān)文章:51單片機(jī)教程
評論