"); //-->
***************************************************************************/
#include <REGX51.H>
#include "16x2LCDDriver.h" //LCD头文件
#include "DS18B20Driver.h" //DS18B20头文件
#include "DS1302Driver.h" //DS1302头文件
struct TimerStruct //定义时钟的数据结构
{
unsigned char Year;
unsigned char Month;
unsigned char Day;
unsigned char Week;
unsigned char Hour;
unsigned char Minute;
unsigned char Second;
};
unsigned char DateBuffer[]={"2005/01/01"};
unsigned char TimeBuffer[]= {"00:00:00"};
unsigned char LCDDisplayBuffer[]={" "};
unsigned int SecondCounter;//秒计数器(无符号整型)
void timer0(void) interrupt 1//定时T0溢出中断,每25mS产生一次中断
{
unsigned int Temp;
unsigned char TemperatureH;
unsigned char TemperatureL;
struct TimerStruct RealClock;
TH0=(65536-46080)/256;//重新装入定时25mS的初值到TH0,TL0
TL0=(65536-46080)%256;
SecondCounter++;//秒计数器加1
if(SecondCounter==40)//到40,表示1S时间到
{
SecondCounter=0;
while(DS18B20_Reset());//向DS18B20发复位命令
DS18B20_Write(0xCC); //向DS18B20发读数据命令
DS18B20_Write(0xBE);
TemperatureL=DS18B20_Read();//读DS18B20的第1个BYTE
TemperatureH=DS18B20_Read();//读DS18B20的第2个Byte
while(DS18B20_Reset());//向DS18B20发复位命令
DS18B20_Write(0xCC);//向DS18B20发启动温度转换开始命令
DS18B20_Write(0x44);
//----以下是关于读取DS18B20的数据时行处理,并将结果显示出来
Temp=TemperatureH*256+TemperatureL;
if(Temp & 0xf800)
{
LCDDisplayBuffer[0]=-;
Temp=(~Temp)+1;
TemperatureL=Temp%256;
TemperatureH=Temp/256;
}
Temp>>=4;
LCDDisplayBuffer[1]=Temp/10+0x30;
LCDDisplayBuffer[2]=Temp%10+0x30;
Temp=TemperatureL;
Temp&=0x000F;
Temp*=625;
Temp/=100;
LCDDisplayBuffer[3]=.;
LCDDisplayBuffer[4]=Temp/10+0x30;
LCDDisplayBuffer[5]=Temp%10+0x30;
LCDDisplayBuffer[6]=C;
//---以下是读取DS1302的时钟信息,并将结果存储到显示缓冲区中
Temp=Read_DS1302_RTC(0x81);//读秒信息
RealClock.Second=Temp;
TimeBuffer[7]=Temp%16+0x30;
TimeBuffer[6]=Temp/16+0x30;
Temp=Read_DS1302_RTC(0x83);//读分信息
RealClock.Minute=Temp;
TimeBuffer[4]=Temp%16+0x30;
TimeBuffer[3]=Temp/16+0x30;
Temp=Read_DS1302_RTC(0x85);//读时信息
RealClock.Hour=Temp;
TimeBuffer[1]=Temp%16+0x30;
TimeBuffer[0]=Temp/16+0x30;
Temp=Read_DS1302_RTC(0x87);//读日信息
RealClock.Day=Temp;
DateBuffer[9]=Temp%16+0x30;
DateBuffer[8]=Temp/16+0x30;
Temp=Read_DS1302_RTC(0x89);//读月信息
RealClock.Month=Temp;
DateBuffer[6]=Temp%16+0x30;
DateBuffer[5]=Temp/16+0x30;
Temp=Read_DS1302_RTC(0x8d);//读年信息
RealClock.Year=Temp;
DateBuffer[3]=Temp%16+0x30;
DateBuffer[2]=Temp/16+0x30;
//---送到LCD上显示出来
lcd_displaystr_ram(0,3,DateBuffer);//刷新LCD上的日期信息
lcd_displaystr_ram(1,1,TimeBuffer);//刷新LCD上的时间显示信息
lcd_displaystr_ram(1,sizeof(TimeBuffer),LCDDisplayBuffer);
}
}
void main(void)
{
TMOD=0x01;//T0工作于16位定时功能
TH0=(65536-46080)/256;//T0定时25mS的初值装入TH0,TL0
TL0=(65536-46080)%256;
TR0=1;//启动T0开始工作
ET0=1;//允许T0溢出中断
EA=1;//CPU开中断
initize_lcd();//初始化LCD
Init_DS1302_RTC();//初始化DS1302
//clock initize
Write_DS1302_RTC(0x8e,0x00);
Write_DS1302_RTC(0x80,0x00);
Write_DS1302_RTC(0x82,0x55);
Write_DS1302_RTC(0x84,0x11);
Write_DS1302_RTC(0x86,0x16);
Write_DS1302_RTC(0x88,0x11);
Write_DS1302_RTC(0x8a,0x05);
Write_DS1302_RTC(0x8c,0x05);
Write_DS1302_RTC(0x8e,0x80);
while(1)
{
;
}
}
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。