#include <reg51.h>
struct sTime { //日期时间结构体定义
unsigned int year;
unsigned char mon;
unsigned char day;
unsigned char hour;
unsigned char min;
unsigned char sec;
unsigned char week;
};
sbit K1 = P1^0;
sbit K2 = P1^1;
sbit K3 = P1^2;
sbit K4 = P1^3;
sbit K5 = P1^4;
sbit BEEP=P2^6;
bit flag200ms = 1; //200ms定时标志
bit flag1s = 1; //1s定时标志
unsigned char setIndex = 0; //时间设置索引
unsigned char state=0;
struct sTime bufTime; //日期时间缓冲区
void timer0_init(); //定时器初始化
void RefreshTimeShow();
void delay(unsigned int i);
void KeyAction();
void been(unsigned int time);
void delay1(unsigned int i);
//void LcdWriteCmd(unsigned char cmd);
unsigned char IntToString(unsigned char *str, int dat);
extern bit Start18B20();
extern void LcdWriteDat(unsigned char dat);
extern bit Get18B20Temp(int *temp);
extern void InitDS1302();
extern void GetRealTime(struct sTime *time);
extern void SetRealTime(struct sTime *time);
extern void InitLcd1602();
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
extern void LcdSetCursor(unsigned char x, unsigned char y);
extern void LcdOpenCursor(void);
extern void LcdCloseCursor(void);
unsigned char *week[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
void main()
{
bit res;
int temp; //读取到的当前温度值
int intT, decT; //温度值的整数和小数部分
unsigned char len;
unsigned char str[12];
unsigned char psec=0xAA; //秒备份,初值AA确保首次读取时间后会刷新显示
EA = 1; //开总中断
timer0_init();
InitDS1302(); //初始化实时时钟
InitLcd1602(); //初始化液晶
Start18B20(); //温度//启动一次 18B20 温度转换,返回值-表示是否启动成功
//初始化屏幕上固定不变的内容
LcdShowStr(1, 0, "20 - - ");
LcdShowStr(1, 1, " : : ");
while (1)
{
KeyAction();
if(state)
{
LcdOpenCursor();
//LcdWriteCmd(0x0F); //打开光标闪烁效果
}
else
{
LcdCloseCursor(); //关闭光标闪烁效果
}
if (flag200ms&&(setIndex == 0))
{
flag200ms = 0;
GetRealTime(&bufTime); //获取当前时间
if (psec != bufTime.sec) //检测到时间有变化时刷新显示
{
RefreshTimeShow();
psec = bufTime.sec; //用当前值更新上次秒数
}
}
if(flag1s)
{
flag1s=0;
res = Get18B20Temp(&temp); //读取当前温度
if (res) //读取成功时,刷新当前温度显示
{
intT = temp >> 4; //分离出温度值整数部分
decT = temp & 0xF; //分离出温度值小数部分
len = IntToString(str, intT); //整数部分转换为字符串
str[len++] = '.'; //添加小数点
decT = (decT*10) / 16; //二进制的小数部分转换为1位十进制位
str[len++] = decT + '0'; //十进制小数位再转换为ASCII字符
while (len < 6) //用空格补齐到6个字符长度
{
str[len++] = ' ';
}
str[len] = '\0'; //添加字符串结束符
LcdShowStr(10, 1, str); //显示到液晶屏上
LcdSetCursor(14,1);
LcdWriteDat(0xdf);
LcdSetCursor(15,1);
LcdWriteDat('C');
}
else //读取失败时,提示错误信息
{
LcdShowStr(0, 0, "error!");
}
Start18B20(); //重新启动下一次转换
}
}
}
/* 将一个BCD码字节显示到屏幕上,(x,y)-屏幕起始坐标,bcd-待显示BCD码 */
void ShowBcdByte(unsigned char x, unsigned char y, unsigned char bcd)
{
unsigned char str[4];
str[0] = (bcd >> 4) + '0';
str[1] = (bcd&0x0F) + '0';
str[2] = '\0';
LcdShowStr(x, y, str);
}
/* 刷新日期时间的显示 */
void RefreshTimeShow()
{
ShowBcdByte(3, 0, bufTime.year);
ShowBcdByte(6, 0, bufTime.mon);
ShowBcdByte(9, 0, bufTime.day);
ShowBcdByte(1, 1, bufTime.hour);
ShowBcdByte(4, 1, bufTime.min);
ShowBcdByte(7, 1, bufTime.sec);
switch(bufTime.week){
case 1:LcdShowStr(12, 0, week[0]); break;
case 2:LcdShowStr(12, 0, week[1]); break;
case 3:LcdShowStr(12, 0, week[2]); break;
case 4:LcdShowStr(12, 0, week[3]); break;
case 5:LcdShowStr(12, 0, week[4]); break;
case 6:LcdShowStr(12, 0, week[5]); break;
case 7:LcdShowStr(12, 0, week[6]); break;
default: LcdShowStr(12,0,"error"); break;
}
}
/* 刷新当前设置位的光标指示 */
void RefreshSetShow()
{
switch (setIndex)
{
case 1: LcdSetCursor(3, 0); break;
case 2: LcdSetCursor(4, 0); break;
case 3: LcdSetCursor(6, 0); break;
case 4: LcdSetCursor(7, 0); break;
case 5: LcdSetCursor(9, 0); break;
case 6: LcdSetCursor(10, 0); break;
case 7: LcdSetCursor(1, 1); break;
case 8: LcdSetCursor(2, 1); break;
case 9: LcdSetCursor(4, 1); break;
case 10: LcdSetCursor(5, 1); break;
case 11: LcdSetCursor(7, 1); break;
case 12: LcdSetCursor(8, 1); break;
default: break;
}
}
/* 递增一个BCD码的高位 */
unsigned char IncBcdHigh(unsigned char bcd)
{
if ((bcd&0xF0) < 0x90)
bcd += 0x10;
else
bcd &= 0x0F;
return bcd;
}
/* 递增一个BCD码的低位 */
unsigned char IncBcdLow(unsigned char bcd)
{
if ((bcd&0x0F) < 0x09)
bcd += 0x01;
else
bcd &= 0xF0;
return bcd;
}
/* 递减一个BCD码的高位 */
unsigned char DecBcdHigh(unsigned char bcd)
{
if ((bcd&0xF0) > 0x00)
bcd -= 0x10;
else
bcd |= 0x90;
return bcd;
}
/* 递减一个BCD码的低位 */
unsigned char DecBcdLow(unsigned char bcd)
{
if ((bcd&0x0F) > 0x00)
bcd -= 0x01;
else
bcd |= 0x09;
return bcd;
}
/* 递增时间当前设置位的值 */
void IncSetTime()
{
switch (setIndex)
{
case 1: bufTime.year = IncBcdHigh(bufTime.year); break;
case 2: bufTime.year = IncBcdLow(bufTime.year); break;
case 3: bufTime.mon = IncBcdHigh(bufTime.mon); break;
case 4: bufTime.mon = IncBcdLow(bufTime.mon); break;
case 5: bufTime.day = IncBcdHigh(bufTime.day); break;
case 6: bufTime.day = IncBcdLow(bufTime.day); break;
case 7: bufTime.hour = IncBcdHigh(bufTime.hour); break;
case 8: bufTime.hour = IncBcdLow(bufTime.hour); break;
case 9: bufTime.min = IncBcdHigh(bufTime.min); break;
case 10: bufTime.min = IncBcdLow(bufTime.min); break;
case 11: bufTime.sec = IncBcdHigh(bufTime.sec); break;
case 12: bufTime.sec = IncBcdLow(bufTime.sec); break;
default: break;
}
RefreshTimeShow();
RefreshSetShow();
}
/* 递减时间当前设置位的值 */
void DecSetTime()
{
switch (setIndex)
{
case 1: bufTime.year = DecBcdHigh(bufTime.year); break;
case 2: bufTime.year = DecBcdLow(bufTime.year); break;
case 3: bufTime.mon = DecBcdHigh(bufTime.mon); break;
case 4: bufTime.mon = DecBcdLow(bufTime.mon); break;
case 5: bufTime.day = DecBcdHigh(bufTime.day); break;
case 6: bufTime.day = DecBcdLow(bufTime.day); break;
case 7: bufTime.hour = DecBcdHigh(bufTime.hour); break;
case 8: bufTime.hour = DecBcdLow(bufTime.hour); break;
case 9: bufTime.min = DecBcdHigh(bufTime.min); break;
case 10: bufTime.min = DecBcdLow(bufTime.min); break;
case 11: bufTime.sec = DecBcdHigh(bufTime.sec); break;
case 12: bufTime.sec = DecBcdLow(bufTime.sec); break;
default: break;
}
RefreshTimeShow();
Refresh
- 1
- 2
- 3
- 4
- 5
- 6
前往页