单片机源程序如下:
/*
*******************************************************************************
* 《手把手教你学51单片机(C语言版)》
* 配套 KST-51 单片机开发板 示例源代码
*
* 文件名:main.c
* 描 述:第16章 作业题4 带温度显示的电子钟
* 版本号:v1.0.0
* 备 注:在lesson15_3基础上添加温度,温度显示在第二行右端,原时间位置相应左移
*******************************************************************************
*/
#include struct sTime { //日期时间结构体定义 unsigned int year; unsigned char mon; unsigned char day; unsigned char hour; unsigned char min; unsigned char sec; unsigned char week; }; bit flag1s = 0; //1s定时标志 bit flag200ms = 1; //200ms定时标志 struct sTime bufTime; //日期时间缓冲区 unsigned char setIndex = 0; //时间设置索引 unsigned char T0RH = 0; //T0重载值的高字节 unsigned char T0RL = 0; //T0重载值的低字节 void ConfigTimer0(unsigned int ms); void RefreshTimeShow(); void GetShowTemp(); extern void InitDS1302(); extern void GetRealTime(struct sTime *time); extern void SetRealTime(struct sTime *time); extern void KeyScan(); extern void KeyDriver(); 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(); extern void LcdCloseCursor(); extern bit Start18B20(); extern bit Get18B20Temp(int *temp); void main() { unsigned char psec=0xAA; //秒备份,初值AA确保首次读取时间后会刷新显示 EA = 1; //开总中断 ConfigTimer0(1); //T0定时1ms Start18B20(); //启动DS18B20 InitDS1302(); //初始化实时时钟 InitLcd1602(); //初始化液晶 //初始化屏幕上固定不变的内容 LcdShowStr(3, 0, "20 - - "); LcdShowStr(1, 1, " : : --`C"); while (1) { KeyDriver(); //调用按键驱动 if (flag200ms && (setIndex == 0)) { //每隔200ms且未处于设置状态时, flag200ms = 0; GetRealTime(&bufTime); //获取当前时间 if (psec != bufTime.sec) //检测到时间有变化时刷新显示 { RefreshTimeShow(); psec = bufTime.sec; //用当前值更新上次秒数 } } if (flag1s && (setIndex == 0)) //每隔1s读取并显示温度 { flag1s = 0; GetShowTemp(); } } } /* 将一个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] = '