#include<reg52.h> //包含頭文件,一般情況不需要改動,頭文件包含特殊功能寄存器的定義
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit SCK=P3^6; //時鐘
sbit SDA=P3^4; //數據
sbit RST = P3^5;// DS1302復位
sbit key1=P1^0; // 調時
sbit key2=P1^1; // 調分
sbit key3=P1^2; // 調秒
sbit key4=P1^3; // 退出
sbit beep=P1^5; //定義蜂鳴器
sbit LS138A=P2^2;
sbit LS138B=P2^3;
sbit LS138C=P2^4;
bit ReadRTC_Flag;//定義讀DS1302標志
unsigned char l_tmpdate[7]={0,0,12,15,5,3,8};//秒分時日月周年08-05-15 12:00:00
unsigned char l_tmpdisplay[8];
unsigned char set; //按鍵退出標志
unsigned char NOW[6]={1,7,3,0,0,0,}; // 鬧鈴設置數組 17:30:00 開鬧鐘
//code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分時日月周年 最低位讀寫位
//code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
code unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
//共陰數碼管 0-9 '-' '熄滅‘表
/******************************************************************/
/* 函數聲明 */
/******************************************************************/
void Write_Ds1302_byte(unsigned char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );
void Read_RTC(void);//read RTC
void Set_RTC(void); //set RTC
void InitTIMER0(void);//inital timer0
/******************************************************************/
/* 延時函數 */
/******************************************************************/
void dlms(unsigned int n)
{ int i,j;
for(i=0;i<n;i++)
for(j=0;j<85;j++);
}
/******************************************************************/
/* 定時器0初始化 */
/******************************************************************/
void InitTIMER0(void)
{
TMOD|=0x01;//定時器設置 16位
TH0=0xef;//初始化值
TL0=0xf0;
ET0=1;
TR0=1;
EA=1;
}
/******************************************************************/
/* 寫一個字節 */
/******************************************************************/
void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++) //循環8次 寫入數據
{
SCK=0;
SDA=temp&0x01; //每次傳輸低字
節
temp>>=1; //右移一位
SCK=1;
}
}
/******************************************************************/
/* 寫入DS1302 */
/******************************************************************/
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_(); //啟動
Write_Ds1302_Byte(address); //發送地址
Write_Ds1302_Byte(dat); //發送數據
RST=0; //恢復
}
/******************************************************************/
/* 讀出DS1302數據 */
/******************************************************************/
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0;
_nop_();
_nop_();
SCK=0;
_nop_();
_nop_();
RST=1;
_nop_();
_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++) //循環8次 讀取數據
{
if(SDA)
temp|=0x80; //每次傳輸低字節
SCK=0;
temp>>=1; //右移一位
_nop_();
_nop_();
_nop_();
SCK=1;
}
RST=0;
_nop_(); //以下為DS1302復位的穩定時間
_nop_();
RST=0;
SCK=0;
_nop_();
_nop_();
_nop_();
_nop_();
SCK=1;
_nop_();
_nop_();
SDA=0;
_nop_();
_nop_();
SDA=1;
_nop_();
_nop_();
return (temp); //返回
}
/******************************************************************/
/* 讀時鐘數據 */
/******************************************************************/
void Read_RTC(void) //讀取 日歷
{
// unsigned char i,*p;
l_tmpdate[0]=Read_Ds1302(0x81); //讀秒寄存器
l_tmpdate[1]=Read_Ds1302(0x83); //讀分寄存器
l_tmpdate[2]=Read_Ds1302(0x85); //讀時寄存器
// l_tmpdate[3]=Read_Ds1302(0x87); //讀日寄存器
// l_tmpdate[4]=Read_Ds1302(0x89); //讀月寄存器
// l_tmpdate[5]=Read_Ds1302(0x8b); //讀周寄存器
// l_tmpdate[6]=Read_Ds1302(0x8d); //讀年寄存器
/* p=read_rtc_address; //地址傳遞
for(i=0;i<7;i++) //分7次讀取 秒分時日月周年
{
l_tmpdate[i]=Read_Ds1302(*p);//將l_tmpdate[i]的數寫入地址Read_Ds1302
p++;
}*/
}
/******************************************************************/
/* 設定時鐘數據 */
/******************************************************************/
void Set_RTC(void) //設定 日歷
{
// unsigned char i,p,tmp;
l_tmpdate[0]=l_tmpdate[0]%10+l_tmpdate[0]/10*16; //BCD處理
l_tmpdate[1]=l_tmpdate[1]%10+l_tmpdate[1]/10*16;
l_tmpdate[2]=l_tmpdate[2]%10+l_tmpdate[2]/10*16;
// l_tmpdate[3]=l_tmpdate[3]%10+l_tmpdate[3]/10*16;
// l_tmpdate[4]=l_tmpdate[4]%10+l_tmpdate[4]/10*16;
// l_tmpdate[5]=l_tmpdate[5]%10+l_tmpdate[5]/10*16;
// l_tmpdat
ate[6]=l_tmpdate[6]%10+l_tmpdate[6]/10*16;
/* for(i=0;i<7;i++){ //BCD處理
tmp=l_tmpdate[i]/10;
l_tmpdate[i]=l_tmpdate[i]%10;
l_tmpdate[i]=l_tmpdate[i]+tmp*16;
}*/
Write_Ds1302(0x8E,0X00);
// p=write_rtc_address; //傳地址
Write_Ds1302(0x80,l_tmpdate[0]); //寫秒寄存器
Write_Ds1302(0x82,l_tmpdate[1]); //寫分寄存器
Write_Ds1302(0x84,l_tmpdate[2]); //寫時寄存器
// Write_Ds1302(0x86,l_tmpdate[3]); //寫日寄存器
// Write_Ds1302(0x88,l_tmpdate[4]); //寫月寄存器
// Write_Ds1302(0x8a,l_tmpdate[5]); //寫周寄存器
// Write_Ds1302(0x8c,l_tmpdate[6]); //寫年寄存器
/* for(i=0;i<7;i++) //7次寫入 秒分時日月周年
{
Write_Ds1302(*p,l_tmpdate[i]);
p++;
} */
Write_Ds1302(0x8E,0x80);
}
/******************************************************************/
/* 定時器中斷函數 */
/******************************************************************/
void tim(void) interrupt 1 using 1
//中斷,用于數碼管掃描
{
static unsigned char i,num;
TH0=0xf5;
TL0=0xe0;
P0=table[l_tmpdisplay[i]]; //查表法得到要顯示數字的數碼段
switch(i)
{
case 0:LS138A=0; LS138B=0; LS138C=0; break;
case 1:LS138A=1; LS138B=0; LS138C=0; break;
case 2:LS138A=0; LS138B=1; LS138C=0; break;
case 3:LS138A=1; LS138B=1; LS138C=0; break;
case 4:LS138A=0; LS138B=0; LS138C=1; break;
case 5:LS138A=1; LS138B=0; LS138C=1; break;
case 6:LS138A=0; LS138B=1; LS138C=1; break;
case 7:LS138A=1; LS138B=1; LS138C=1; break;
}
i++;
if(i==8)
{
i=0;
num++;
if(10==num) //隔段時間讀取1302的數據。時間間隔可以調整
{
ReadRTC_Flag=1; //使用標志位判斷
num=0;
}
}
}
/************************************************************************/
/* 鬧鐘函數 */
/************************************************************************/
void naozhong()
{
if(l_tmpdisplay[0]==NOW[0]&&l_tmpdisplay[1]==NOW[1]
&&l_tmpdisplay[3]==NOW[2]&&l_tmpdisplay[4]==NOW[3]
&&l_tmpdisplay[6]==NOW[4]&&l_tmpdisplay[7]==NOW[5]) // 設置NOW的時間與時鐘時間顯示比較
{
beep=0; //開蜂鳴器
dlms(2000);
beep=1;
}
}
/************************************************************************/
/* 鍵盤掃描函數 */
/************************************************************************/
void keyscan()
{
if((key1==0)||(key2==0)||(key3==0)) // 檢測有按鍵按下執行下面語句
{
set=1;
while(set)
{
if(key1==0) //調時按鍵
{
dlms(200);
while(!key1); //松手檢測
l_tmpdate[2]=((l_tmpdate[2]+1)>>4)*10 + ((l_tmpdate[2]+1)&0x0f); //
時加1
if(l_tmpdate[2]>23) l_tmpdate[2]=0; //判斷
l_tmpdate[2]=l_tmpdate[2]%10+l_tmpdate[2]/10*16; //轉十進制
Write_Ds1302(0x8E,0X00); //開寫保護
Write_Ds1302(0x84,l_tmpdate[2]); //寫時寄存器 此處寫調時寄存器就行,以免把所以寄存器全寫上按鍵加時出亂
Write_Ds1302(0x8E,0x80); //關寫保護
set=0; //退出
}
if(key2==0) //調分按鍵
{
dlms(200);
while(!key2); //松手檢測
l_tmpdate[1]=((l_tmpdate[1]+1)>>4)*10 + ((l_tmpdate[1]+1)&0x0f); //分加1
if(l_tmpdate[1]>59) l_tmpdate[1]=0; //判斷
l_tmpdate[1]=l_tmpdate[1]%10+l_tmpdate[1]/10*16; //轉十進制
Write_Ds1302(0x8E,0X00); //開寫保護
Write_Ds1302(0x82,l_tmpdate[1]); //寫分寄存器
Write_Ds1302(0x8E,0x80); //關寫保護
set=0;
}
if(key3==0) //調秒按鍵
{
dlms(200);
while(!key3); //松手檢測
l_tmpdate[0]=((l_tmpdate[0]+1)>>4)*10 + ((l_tmpdate[0]+1)&0x0f); //秒加1
if(l_tmpdate[0]>59) l_tmpdate[0]=0; //判斷
l_tmpdate[0]=l_tmpdate[0]%10+l_tmpdate[0]/10*16; //轉十進制
Write_Ds1302(0x8E,0X00); //開寫保護
Write_Ds1302(0x80,l_tmpdate[0]); //寫秒寄存器
Write_Ds1302(0x8E,0x80); //關寫保護
set=0;
}
/*if(key4==0) //退出按鍵
{
dlms(50);
while(!key4); //松手檢測
set=0;
} */
}
}
}
/******************************************************************/
/* 主函數 */
/******************************************************************/
void main(void)
{
InitTIMER0(); //初始化定時器0
Set_RTC(); //寫入時鐘值,如果使用備用電池時候,不需要沒每次上電寫入,此程序應該屏蔽
while(1)
{
keyscan(); //按鍵調時、分、秒
if(ReadRTC_Flag) // ReadRTC_Flag==1執行下面語句
{
ReadRTC_Flag=0;
Read_RTC();
l_tmpdisplay[0]=l_tmpdate[2]/16; //數據的轉換,因我們采用數碼管0~9的顯示,將數據分開
l_tmpdisplay[1]=l_tmpdate[2]&0x0f;
l_tmpdisplay[2]=10; //加入"-"
l_tmpdisplay[3]=l_tmpdate[1]/16;
l_tmpdisplay[4]=l_tmpdate[1]&0x0f;
l_tmpdisplay[5]=10;
l_tmpdisplay[6]=l_tmpdate[0]/16;
l_tmpdisplay[7]=l_tmpdate[0]&0x0f;
}
naozhong(); //定時鬧鐘
}} |