第四次优化代码,因蜂鸣器问题,删除闹钟部分代码。

This commit is contained in:
Andy0613
2026-07-07 09:17:14 +08:00
parent eb0ae8ee8a
commit fd4305cfbe
14 changed files with 5015 additions and 0 deletions
+198
View File
@@ -0,0 +1,198 @@
$NOMOD51
;------------------------------------------------------------------------------
; This file is part of the C51 Compiler package
; Copyright (c) 1988-2005 Keil Elektronik GmbH and Keil Software, Inc.
; Version 8.01
;
; *** <<< Use Configuration Wizard in Context Menu >>> ***
;------------------------------------------------------------------------------
; STARTUP.A51: This code is executed after processor reset.
;
; To translate this file use A51 with the following invocation:
;
; A51 STARTUP.A51
;
; To link the modified STARTUP.OBJ file to your application use the following
; Lx51 invocation:
;
; Lx51 your object file list, STARTUP.OBJ controls
;
;------------------------------------------------------------------------------
;
; User-defined <h> Power-On Initialization of Memory
;
; With the following EQU statements the initialization of memory
; at processor reset can be defined:
;
; <o> IDATALEN: IDATA memory size <0x0-0x100>
; <i> Note: The absolute start-address of IDATA memory is always 0
; <i> The IDATA space overlaps physically the DATA and BIT areas.
IDATALEN EQU 80H
;
; <o> XDATASTART: XDATA memory start address <0x0-0xFFFF>
; <i> The absolute start address of XDATA memory
XDATASTART EQU 0
;
; <o> XDATALEN: XDATA memory size <0x0-0xFFFF>
; <i> The length of XDATA memory in bytes.
XDATALEN EQU 0
;
; <o> PDATASTART: PDATA memory start address <0x0-0xFFFF>
; <i> The absolute start address of PDATA memory
PDATASTART EQU 0H
;
; <o> PDATALEN: PDATA memory size <0x0-0xFF>
; <i> The length of PDATA memory in bytes.
PDATALEN EQU 0H
;
;</h>
;------------------------------------------------------------------------------
;
;<h> Reentrant Stack Initialization
;
; The following EQU statements define the stack pointer for reentrant
; functions and initialized it:
;
; <h> Stack Space for reentrant functions in the SMALL model.
; <q> IBPSTACK: Enable SMALL model reentrant stack
; <i> Stack space for reentrant functions in the SMALL model.
IBPSTACK EQU 0 ; set to 1 if small reentrant is used.
; <o> IBPSTACKTOP: End address of SMALL model stack <0x0-0xFF>
; <i> Set the top of the stack to the highest location.
IBPSTACKTOP EQU 0xFF +1 ; default 0FFH+1
; </h>
;
; <h> Stack Space for reentrant functions in the LARGE model.
; <q> XBPSTACK: Enable LARGE model reentrant stack
; <i> Stack space for reentrant functions in the LARGE model.
XBPSTACK EQU 0 ; set to 1 if large reentrant is used.
; <o> XBPSTACKTOP: End address of LARGE model stack <0x0-0xFFFF>
; <i> Set the top of the stack to the highest location.
XBPSTACKTOP EQU 0xFFFF +1 ; default 0FFFFH+1
; </h>
;
; <h> Stack Space for reentrant functions in the COMPACT model.
; <q> PBPSTACK: Enable COMPACT model reentrant stack
; <i> Stack space for reentrant functions in the COMPACT model.
PBPSTACK EQU 0 ; set to 1 if compact reentrant is used.
;
; <o> PBPSTACKTOP: End address of COMPACT model stack <0x0-0xFFFF>
; <i> Set the top of the stack to the highest location.
PBPSTACKTOP EQU 0xFF +1 ; default 0FFH+1
; </h>
;</h>
;------------------------------------------------------------------------------
;
; Memory Page for Using the Compact Model with 64 KByte xdata RAM
; <e>Compact Model Page Definition
;
; <i>Define the XDATA page used for PDATA variables.
; <i>PPAGE must conform with the PPAGE set in the linker invocation.
;
; Enable pdata memory page initalization
PPAGEENABLE EQU 0 ; set to 1 if pdata object are used.
;
; <o> PPAGE number <0x0-0xFF>
; <i> uppermost 256-byte address of the page used for PDATA variables.
PPAGE EQU 0
;
; <o> SFR address which supplies uppermost address byte <0x0-0xFF>
; <i> most 8051 variants use P2 as uppermost address byte
PPAGE_SFR DATA 0A0H
;
; </e>
;------------------------------------------------------------------------------
; Standard SFR Symbols
ACC DATA 0E0H
B DATA 0F0H
SP DATA 81H
DPL DATA 82H
DPH DATA 83H
NAME ?C_STARTUP
?C_C51STARTUP SEGMENT CODE
?STACK SEGMENT IDATA
RSEG ?STACK
DS 1
EXTRN CODE (?C_START)
PUBLIC ?C_STARTUP
CSEG AT 0
?C_STARTUP: LJMP STARTUP1
RSEG ?C_C51STARTUP
STARTUP1:
IF IDATALEN <> 0
MOV R0,#IDATALEN - 1
CLR A
IDATALOOP: MOV @R0,A
DJNZ R0,IDATALOOP
ENDIF
IF XDATALEN <> 0
MOV DPTR,#XDATASTART
MOV R7,#LOW (XDATALEN)
IF (LOW (XDATALEN)) <> 0
MOV R6,#(HIGH (XDATALEN)) +1
ELSE
MOV R6,#HIGH (XDATALEN)
ENDIF
CLR A
XDATALOOP: MOVX @DPTR,A
INC DPTR
DJNZ R7,XDATALOOP
DJNZ R6,XDATALOOP
ENDIF
IF PPAGEENABLE <> 0
MOV PPAGE_SFR,#PPAGE
ENDIF
IF PDATALEN <> 0
MOV R0,#LOW (PDATASTART)
MOV R7,#LOW (PDATALEN)
CLR A
PDATALOOP: MOVX @R0,A
INC R0
DJNZ R7,PDATALOOP
ENDIF
IF IBPSTACK <> 0
EXTRN DATA (?C_IBP)
MOV ?C_IBP,#LOW IBPSTACKTOP
ENDIF
IF XBPSTACK <> 0
EXTRN DATA (?C_XBP)
MOV ?C_XBP,#HIGH XBPSTACKTOP
MOV ?C_XBP+1,#LOW XBPSTACKTOP
ENDIF
IF PBPSTACK <> 0
EXTRN DATA (?C_PBP)
MOV ?C_PBP,#LOW PBPSTACKTOP
ENDIF
MOV SP,#?STACK-1
; This code is required if you use L51_BANK.A51 with Banking Mode 4
;<h> Code Banking
; <q> Select Bank 0 for L51_BANK.A51 Mode 4
#if 0
; <i> Initialize bank mechanism to code bank 0 when using L51_BANK.A51 with Banking Mode 4.
EXTRN CODE (?B_SWITCH0)
CALL ?B_SWITCH0 ; init bank mechanism to code bank 0
#endif
;</h>
LJMP ?C_START
END
+136
View File
@@ -0,0 +1,136 @@
#include"ds1302.h"
/* DS1302读写地址 */
/* 存储顺序为:秒分时日月周年 */
uchar code READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};
uchar code WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};
/* DS1302默认时间:2013年1月1日周二 12:00:00 */
/* 存储顺序:秒 分 时 日 月 周 年 */
/* 存储格式:BCD码 */
uchar TIME[7] = {0, 0, 0x12, 0x01, 0x01, 0x02, 0x13};
/*
* 函数名:Ds1302Write
* 功能 :向DS1302写入数据,地址+数据
* 输入 :addr=寄存器地址,dat=要写入的数据
* 输出 :无
*/
void Ds1302Write(uchar addr, uchar dat)
{
uchar n;
RST = 0;
_nop_();
SCLK = 0; /* 先将时钟线拉低 */
_nop_();
RST = 1; /* 然后RST(CE)拉高,开始通信 */
_nop_();
for (n=0; n<8; n++) /* 发送8位地址指令 */
{
DSIO = addr & 0x01; /* 数据从低位开始 */
addr >>= 1;
SCLK = 1; /* 上升沿DS1302读取数据 */
_nop_();
SCLK = 0;
_nop_();
}
for (n=0; n<8; n++) /* 写入8位数据 */
{
DSIO = dat & 0x01;
dat >>= 1;
SCLK = 1; /* 上升沿DS1302读取数据 */
_nop_();
SCLK = 0;
_nop_();
}
RST = 0; /* 结束数据传输 */
_nop_();
}
/*
* 函数名:Ds1302Read
* 功能 :从DS1302读取一个字节数据
* 输入 :addr=寄存器地址
* 返回 :读取到的数据
*/
uchar Ds1302Read(uchar addr)
{
uchar n,dat,dat1;
RST = 0;
_nop_();
SCLK = 0; /* 先将时钟线拉低 */
_nop_();
RST = 1; /* 然后RST(CE)拉高,开始通信 */
_nop_();
for(n=0; n<8; n++) /* 发送8位地址指令 */
{
DSIO = addr & 0x01; /* 数据从低位开始 */
addr >>= 1;
SCLK = 1; /* 上升沿DS1302读取数据 */
_nop_();
SCLK = 0; /* 下降沿DS1302输出数据 */
_nop_();
}
_nop_();
for(n=0; n<8; n++) /* 读取8位数据 */
{
dat1 = DSIO; /* 从高位开始 */
dat = (dat>>1) | (dat1<<7);
SCLK = 1;
_nop_();
SCLK = 0; /* 下降沿DS1302输出数据 */
_nop_();
}
RST = 0;
_nop_(); /* 确保DS1302复位时间保证稳定 */
SCLK = 1;
_nop_();
DSIO = 0;
_nop_();
DSIO = 1;
_nop_();
return dat;
}
/*
* 函数名:Ds1302Init
* 功能 :初始化DS1302时钟芯片
* 输入 :无
* 输出 :无
* 说明 :判断CH位,如果时钟未运行则写入默认时间
*/
void Ds1302Init()
{
uchar n;
uchar sec;
sec = Ds1302Read(0x81); /* 读取秒寄存器检查CH位 */
if(!(sec & 0x80)) /* CH=0表示时钟正在运行,无需初始化 */
return; /* 首次上电,写入默认时间 */
Ds1302Write(0x8E,0X00); /* 关闭写保护 */
for (n=0; n<7; n++)
{
Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);
}
Ds1302Write(0x8E,0x80); /* 开启写保护 */
}
/*
* 函数名:Ds1302ReadTime
* 功能 :读取DS1302当前时间到TIME数组
* 输入 :无
* 输出 :无
*/
void Ds1302ReadTime()
{
uchar n;
for (n=0; n<7; n++) /* 读取7个字节的时间数据 */
{
TIME[n] = Ds1302Read(READ_RTC_ADDR[n]);
}
}
+31
View File
@@ -0,0 +1,31 @@
#ifndef __DS1302_H_
#define __DS1302_H_
/* 包含头文件 */
#include<reg51.h>
#include<intrins.h>
/* 数据类型重定义 */
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
/* DS1302使用的IO口定义 */
sbit DSIO=P3^4; /* 数据线 */
sbit RST=P3^5; /* 复位/使能脚 */
sbit SCLK=P3^6; /* 时钟线 */
/* 外部函数声明 */
void Ds1302Write(uchar addr, uchar dat); /* 写DS1302 */
uchar Ds1302Read(uchar addr); /* 读DS1302 */
void Ds1302Init(); /* 初始化DS1302 */
void Ds1302ReadTime(); /* 读取DS1302时间 */
/* 外部全局变量声明 */
extern uchar TIME[7]; /* 时间缓冲区 */
#endif
+93
View File
@@ -0,0 +1,93 @@
#include "ds18b20.h"
/* DS18B20复位 */
void DS18B20_Reset(void)
{
uchar i;
DS18B20_DQ = 0; /* 拉低总线 */
for(i=0; i<200; i++); /* 延时约480us */
DS18B20_DQ = 1; /* 释放总线 */
for(i=0; i<100; i++); /* 等待从机应答 */
}
/* DS18B20写入一个字节 */
void DS18B20_WriteByte(uchar dat)
{
uchar i, j;
for(i=0; i<8; i++)
{
DS18B20_DQ = 0; /* 拉低总线开始写时隙 */
_nop_();
if(dat & 0x01) /* 写1 */
DS18B20_DQ = 1;
else /* 写0 */
DS18B20_DQ = 0;
for(j=0; j<30; j++); /* 保持时间约60us */
DS18B20_DQ = 1; /* 释放总线 */
_nop_();
_nop_();
dat >>= 1; /* 下一位 */
}
}
/* DS18B20读取一个字节 */
uchar DS18B20_ReadByte(void)
{
uchar i, j, dat = 0;
for(i=0; i<8; i++)
{
dat >>= 1;
DS18B20_DQ = 0; /* 拉低总线开始读时隙 */
_nop_();
DS18B20_DQ = 1; /* 释放总线 */
_nop_();
_nop_();
if(DS18B20_DQ) /* 读取数据 */
dat |= 0x80;
for(j=0; j<20; j++); /* 延时等待 */
}
return dat;
}
/* 启动温度转换 */
void DS18B20_StartConvert(void)
{
DS18B20_Reset(); /* 复位 */
DS18B20_WriteByte(0xCC); /* 跳过ROM */
DS18B20_WriteByte(0x44); /* 启动温度转换 */
}
/* 设置DS18B20分辨率 */
/* res: 0x1F=9bit, 0x3F=10bit, 0x5F=11bit, 0x7F=12bit */
void DS18B20_SetResolution(uchar res)
{
DS18B20_Reset();
DS18B20_WriteByte(0xCC); /* 跳过ROM */
DS18B20_WriteByte(0x4E); /* 写暂存器 */
DS18B20_WriteByte(0x00); /* TH报警值 */
DS18B20_WriteByte(0x00); /* TL报警值 */
DS18B20_WriteByte(res); /* 设置寄存器 */
DS18B20_Reset();
DS18B20_WriteByte(0xCC); /* 跳过ROM */
DS18B20_WriteByte(0x48); /* 将暂存器写到EEPROM */
}
/* 获取温度值 */
/* 返回1表示成功,temp存放原始温度值 */
bit DS18B20_GetTemp(int *temp)
{
uchar tempL, tempH;
int raw;
DS18B20_Reset();
DS18B20_WriteByte(0xCC); /* 跳过ROM */
DS18B20_WriteByte(0xBE); /* 读取暂存器 */
tempL = DS18B20_ReadByte(); /* 读取温度低字节 */
tempH = DS18B20_ReadByte(); /* 读取温度高字节 */
raw = (tempH << 8) | tempL; /* 合成16位温度值 */
*temp = raw;
return 1;
}
+27
View File
@@ -0,0 +1,27 @@
#ifndef __DS18B20_H_
#define __DS18B20_H_
#include<reg51.h>
#include<intrins.h>
/* 数据类型重定义 */
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
/* DS18B20单总线引脚定义 */
sbit DS18B20_DQ = P3^7;
/* 函数声明 */
void DS18B20_Reset(void); /* 复位DS18B20 */
void DS18B20_WriteByte(uchar dat); /* 写入一个字节 */
uchar DS18B20_ReadByte(void); /* 读取一个字节 */
void DS18B20_StartConvert(void); /* 开始温度转换 */
bit DS18B20_GetTemp(int *temp); /* 获取温度值 */
void DS18B20_SetResolution(uchar res); /* 设置分辨率 */
#endif
+135
View File
@@ -0,0 +1,135 @@
#include"lcd.h"
/*
* 函数名:Lcd1602_Delay1ms
* 功能 :软件延时1ms
* 输入 :c=延时ms数
* 输出 :无
* 说明 :在12MHz晶振12分频下的延时函数
*/
void Lcd1602_Delay1ms(uint c)
{
uchar a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
/*
* 函数名:LcdWriteCom
* 功能 :向LCD写入8位指令
* 输入 :com=指令字节
* 输出 :无
*/
#ifndef LCD1602_4PINS /* 8位总线读写模式 */
void LcdWriteCom(uchar com)
{
LCD1602_E = 0; /* 使能信号拉低 */
LCD1602_RS = 0; /* 选择指令寄存器 */
LCD1602_RW = 0; /* 选择写操作 */
LCD1602_DATAPINS = com; /* 写入指令 */
Lcd1602_Delay1ms(1); /* 等待数据稳定 */
LCD1602_E = 1; /* 写入时序 */
Lcd1602_Delay1ms(5); /* 保持时间 */
LCD1602_E = 0;
}
#else /* 4位总线读写模式 */
void LcdWriteCom(uchar com)
{
LCD1602_E = 0;
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DATAPINS = com; /* 写高4位 */
Lcd1602_Delay1ms(1);
LCD1602_E = 1;
Lcd1602_Delay1ms(1);
LCD1602_E = 0;
Lcd1602_Delay1ms(1);
LCD1602_DATAPINS = com << 4; /* 写低4位 */
Lcd1602_Delay1ms(1);
LCD1602_E = 1;
Lcd1602_Delay1ms(1);
LCD1602_E = 0;
}
#endif
/*
* 函数名:LcdWriteData
* 功能 :向LCD写入8位数据
* 输入 :dat=数据字节
* 输出 :无
*/
#ifndef LCD1602_4PINS /* 8位总线读写模式 */
void LcdWriteData(uchar dat)
{
LCD1602_E = 0; /* 使能信号拉低 */
LCD1602_RS = 1; /* 选择数据寄存器 */
LCD1602_RW = 0; /* 选择写操作 */
LCD1602_DATAPINS = dat; /* 写入数据 */
Lcd1602_Delay1ms(1);
LCD1602_E = 1; /* 写入时序 */
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
#else /* 4位总线读写模式 */
void LcdWriteData(uchar dat)
{
LCD1602_E = 0;
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DATAPINS = dat; /* 写高4位 */
Lcd1602_Delay1ms(1);
LCD1602_E = 1;
Lcd1602_Delay1ms(1);
LCD1602_E = 0;
Lcd1602_Delay1ms(1);
LCD1602_DATAPINS = dat << 4; /* 写低4位 */
Lcd1602_Delay1ms(1);
LCD1602_E = 1;
Lcd1602_Delay1ms(1);
LCD1602_E = 0;
}
#endif
/*
* 函数名:LcdInit
* 功能 :初始化LCD1602液晶
* 输入 :无
* 输出 :无
*/
#ifndef LCD1602_4PINS /* 8位总线读写模式 */
void LcdInit()
{
LcdWriteCom(0x38); /* 设置显示模式:16x2, 5x7点阵, 8位 */
LcdWriteCom(0x0c); /* 开显示,不显示光标 */
LcdWriteCom(0x06); /* 写入数据后地址自动+1 */
LcdWriteCom(0x01); /* 清屏 */
LcdWriteCom(0x80); /* 设置DDRAM地址为0x00,第一行开始 */
}
#else /* 4位总线读写模式 */
void LcdInit()
{
LcdWriteCom(0x32); /* 从8位模式转为4位模式 */
LcdWriteCom(0x28); /* 4位模式下的初始化 */
LcdWriteCom(0x0c); /* 开显示,不显示光标 */
LcdWriteCom(0x06); /* 写入数据后地址自动+1 */
LcdWriteCom(0x01); /* 清屏 */
LcdWriteCom(0x80); /* 设置DDRAM地址为0x00,第一行开始 */
}
#endif
+34
View File
@@ -0,0 +1,34 @@
#ifndef __LCD_H_
#define __LCD_H_
/*
* 使用4位总线时取消注释 LCD1602_4PINS
* 使用8位总线时注释掉此行
*/
#define LCD1602_4PINS
/* 包含头文件 */
#include<reg51.h>
/* 数据类型重定义 */
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
/* 引脚定义 */
#define LCD1602_DATAPINS P0 /* LCD数据口 */
sbit LCD1602_E=P2^7; /* 使能信号 */
sbit LCD1602_RW=P2^5; /* 读写选择 */
sbit LCD1602_RS=P2^6; /* 指令/数据选择 */
/* 函数声明 */
void Lcd1602_Delay1ms(uint c); /* 延时函数(12MHz下) */
void LcdWriteCom(uchar com); /* 写指令 */
void LcdWriteData(uchar dat); /* 写数据 */
void LcdInit(); /* 初始化LCD */
#endif
+476
View File
@@ -0,0 +1,476 @@
#include<reg51.h>
#include<intrins.h>
#include"lcd.h"
#include"ds1302.h"
#include"ds18b20.h"
/* 端口定义 */
sbit K1=P3^1; /* 模式/确认键 */
sbit K2=P3^0; /* 切换/返回键 */
sbit K3=P3^2; /* 加/开始暂停 */
sbit K4=P3^3; /* 减/复位清0 */
/* 全局变量 */
unsigned char mode = 0; /* 模式: 0=时钟, 2=秒表 */
unsigned char last_mode = 0xFF; /* 上一模式,用于检测模式切换 */
int temperature = 0; /* 温度值(10倍) */
unsigned char temp_cnt = 0; /* 温度采集计数 */
unsigned char alarm_hour=6, alarm_min=0, alarm_on=0; /* 闹钟参数 */
/* 秒表变量 */
unsigned int sw_sec = 0; /* 秒表秒数 */
unsigned char sw_csec = 0; /* 秒表百分秒 */
bit sw_running = 0; /* 秒表运行状态 */
/* 宏定义 */
#define BCD2DEC(bcd) (((bcd)>>4)*10 + ((bcd)&0x0f))
#define DEC2BCD(dec) (((dec)/10<<4) | ((dec)%10))
/* 函数声明 */
void Delay10ms(void);
void DelayMs(unsigned int ms);
void Timer1Init(void);
void TempPros(void);
void AlarmJudge(void);
void AlarmBuzPro(void);
void SetAlarm(void);
void SetTime(void);
void LcdFramework(void);
void LcdUpdate(void);
void KeyScan(void);
/* K1按键检测:去抖 + 确认释放后返回1 */
bit K1_Pressed(void)
{
if(K1) return 0;
Delay10ms();
if(K1) return 0;
while(!K1);
return 1;
}
/* K2按键检测:去抖 + 确认释放后返回1 */
bit K2_Pressed(void)
{
if(K2) return 0;
Delay10ms();
if(K2) return 0;
while(!K2);
return 1;
}
/* K3按键检测:去抖 + 确认释放后返回1 */
bit K3_Pressed(void)
{
if(K3) return 0;
Delay10ms();
if(K3) return 0;
while(!K3);
return 1;
}
/* K4按键检测:去抖 + 确认释放后返回1 */
bit K4_Pressed(void)
{
if(K4) return 0;
Delay10ms();
if(K4) return 0;
while(!K4);
return 1;
}
/* LCD写字符串 */
void LcdWriteStr(unsigned char code *str)
{
while(*str) LcdWriteData(*str++);
}
/* LCD定位 + 2位十进制数显示 */
void LcdShowNumAt(unsigned char addr, unsigned char num)
{
LcdWriteCom(addr);
LcdWriteData('0' + num / 10);
LcdWriteData('0' + num % 10);
}
/* 通用参数调整函数:循环检测K3/K4加减,K1确认退出 */
void AdjustValue(unsigned char *val, unsigned char maxVal,
unsigned char dispAddr, unsigned char cursorAddr)
{
LcdWriteCom(cursorAddr); LcdWriteCom(0x0f);
while(1)
{
if(K3_Pressed())
{
if(*val < maxVal) (*val)++; else *val = 0;
LcdShowNumAt(dispAddr, *val);
LcdWriteCom(cursorAddr); LcdWriteCom(0x0f);
}
if(K4_Pressed())
{
if(*val > 0) (*val)--; else *val = maxVal;
LcdShowNumAt(dispAddr, *val);
LcdWriteCom(cursorAddr); LcdWriteCom(0x0f);
}
if(K1_Pressed()) { LcdWriteCom(0x0c); return; }
}
}
/* 主函数 */
void main()
{
Timer1Init(); /* 初始化定时器1 */
LcdInit(); /* 初始化LCD1602 */
Ds1302Init(); /* 初始化DS1302 */
DS18B20_SetResolution(0x3F); /* 设置DS18B20分辨率10位 */
DS18B20_StartConvert(); /* 开始温度转换 */
/* 从DS1302 RAM读取闹钟参数 */
alarm_hour = Ds1302Read(0xC1);
if(alarm_hour > 23) alarm_hour = 6;
alarm_min = Ds1302Read(0xC3);
if(alarm_min > 59) alarm_min = 0;
alarm_on = Ds1302Read(0xC5);
if(alarm_on > 1) alarm_on = 0;
/* 主循环 */
while(1)
{
Ds1302ReadTime(); /* 读取DS1302时间 */
/* 每5次循环采集一次温度 */
temp_cnt++;
if(temp_cnt >= 5)
{
temp_cnt = 0;
TempPros();
}
/* 模式切换时刷新LCD框架 */
if(mode != last_mode)
{
LcdFramework();
last_mode = mode;
}
AlarmJudge(); /* 闹钟判断 */
AlarmBuzPro(); /* 闹钟蜂鸣器处理 */
KeyScan(); /* 按键扫描 */
LcdUpdate(); /* LCD显示更新 */
}
}
/* LCD写2位十进制数(当前位置) */
void ShowLcdNum(unsigned char num)
{
LcdWriteData('0' + num / 10);
LcdWriteData('0' + num % 10);
}
/* LCD显示星期写入 */
void ShowLcdWeek(unsigned char week)
{
switch(week)
{
case 1: LcdWriteData('S'); LcdWriteData('u'); LcdWriteData('n'); break; /* Sunday */
case 2: LcdWriteData('M'); LcdWriteData('o'); LcdWriteData('n'); break; /* Monday */
case 3: LcdWriteData('T'); LcdWriteData('u'); LcdWriteData('e'); break; /* Tuesday */
case 4: LcdWriteData('W'); LcdWriteData('e'); LcdWriteData('d'); break; /* Wednesday */
case 5: LcdWriteData('T'); LcdWriteData('h'); LcdWriteData('u'); break; /* Thursday */
case 6: LcdWriteData('F'); LcdWriteData('r'); LcdWriteData('i'); break; /* Friday */
case 7: LcdWriteData('S'); LcdWriteData('a'); LcdWriteData('t'); break; /* Saturday */
}
}
/* LCD框架:刷新静态结构(按模式) */
void LcdFramework()
{
LcdWriteCom(0x0c); /* 关闭显示光标 */
if(mode == 0) /* 时钟模式框架 */
{
LcdWriteCom(0x80);
LcdWriteStr("20 - - ");
LcdWriteCom(0xC0);
LcdWriteStr(" : : ");
}
else if(mode == 2) /* 秒表模式框架 */
{
LcdWriteCom(0x80);
LcdWriteStr(" Stop Watch ");
LcdWriteCom(0xC0);
LcdWriteStr(" : : . ");
}
}
/* LCD:刷新动态数据 */
void LcdUpdate()
{
int temp_disp;
if(mode == 0) /* 时钟模式 */
{
LcdShowNumAt(0x82, BCD2DEC(TIME[6])); /* 年 */
LcdShowNumAt(0x85, BCD2DEC(TIME[4])); /* 月 */
LcdShowNumAt(0x88, BCD2DEC(TIME[3])); /* 日 */
LcdWriteCom(0x8C);
ShowLcdWeek(TIME[5] & 0x07); /* 星期 */
LcdShowNumAt(0xC0, BCD2DEC(TIME[2])); /* 时 */
LcdShowNumAt(0xC3, BCD2DEC(TIME[1])); /* 分 */
LcdShowNumAt(0xC6, BCD2DEC(TIME[0])); /* 秒 */
/* 显示温度:-12.3C */
LcdWriteCom(0xC9);
temp_disp = temperature;
if(temp_disp < 0) { LcdWriteData('-'); temp_disp = -temp_disp; }
else { LcdWriteData(' '); }
LcdWriteData('0' + temp_disp / 100);
LcdWriteData('0' + (temp_disp / 10) % 10);
LcdWriteData('.');
LcdWriteData('0' + temp_disp % 10);
LcdWriteData('C');
}
else if(mode == 2) /* 秒表模式 */
{
LcdShowNumAt(0xC0, sw_sec / 3600); /* 时 */
LcdShowNumAt(0xC3, (sw_sec % 3600) / 60); /* 分 */
LcdShowNumAt(0xC6, sw_sec % 60); /* 秒 */
LcdShowNumAt(0xC9, sw_csec); /* 百分秒 */
}
}
/* 按键扫描 */
void KeyScan()
{
if(mode == 0) /* 时钟模式按键 */
{
if(K1_Pressed())
{
SetTime();
last_mode = 0xFF; /* 强制刷新显示 */
}
if(K2_Pressed())
{
mode = 2;
sw_running = 0;
sw_sec = 0;
sw_csec = 0;
LcdFramework();
last_mode = mode;
LcdUpdate();
}
}
else if(mode == 2) /* 秒表模式按键 */
{
if(K3_Pressed())
{
sw_running = !sw_running; /* 切换运行状态 */
TR1 = sw_running; /* 控制定时器启停 */
}
if(K4_Pressed())
{
if(!sw_running) { sw_sec = 0; sw_csec = 0; } /* 停止时清零 */
}
if(K2_Pressed())
{
mode = 0;
TR1 = 0;
sw_running = 0;
sw_sec = 0;
sw_csec = 0;
LcdFramework();
last_mode = mode;
LcdUpdate();
}
}
}
/* 时间设置:K1切换字段,K3/K4加/减,最后一个字段后自动写入DS1302 */
void SetTime()
{
unsigned char y,mo,d,w,h,mi,s;
/* BCD转十进制 */
y = BCD2DEC(TIME[6]);
mo = BCD2DEC(TIME[4]);
d = BCD2DEC(TIME[3]);
w = TIME[5] & 0x07;
h = BCD2DEC(TIME[2]);
mi = BCD2DEC(TIME[1]);
s = BCD2DEC(TIME[0]);
/* 清除显示 */
LcdWriteCom(0x80);
LcdWriteStr("20 - - ");
LcdWriteCom(0xC0);
LcdWriteStr(" : : ");
/* 显示当前时间 */
LcdShowNumAt(0x82, y);
LcdShowNumAt(0x85, mo);
LcdShowNumAt(0x88, d);
LcdWriteCom(0x8C); ShowLcdWeek(w);
LcdShowNumAt(0xC0, h);
LcdShowNumAt(0xC3, mi);
LcdShowNumAt(0xC6, s);
/* 日期调节 */
AdjustValue(&y, 99, 0x82, 0x82);
AdjustValue(&mo, 12, 0x85, 0x85);
AdjustValue(&d, 31, 0x88, 0x88);
AdjustValue(&h, 23, 0xC0, 0xC0);
AdjustValue(&mi, 59, 0xC3, 0xC3);
AdjustValue(&s, 59, 0xC6, 0xC6);
/* 星期调节(手动) */
LcdWriteCom(0x8C); LcdWriteCom(0x0f);
while(1)
{
if(K3_Pressed()) { if(w<7) w++; else w=1; LcdWriteCom(0x8C); ShowLcdWeek(w); LcdWriteCom(0x8C); LcdWriteCom(0x0f); }
if(K4_Pressed()) { if(w>1) w--; else w=7; LcdWriteCom(0x8C); ShowLcdWeek(w); LcdWriteCom(0x8C); LcdWriteCom(0x0f); }
if(K1_Pressed()) { break; }
}
/* 转换为BCD存入TIME数组 */
TIME[6] = DEC2BCD(y);
TIME[4] = DEC2BCD(mo);
TIME[3] = DEC2BCD(d);
TIME[5] = w;
TIME[2] = DEC2BCD(h);
TIME[1] = DEC2BCD(mi);
TIME[0] = DEC2BCD(s);
/* 写入DS1302 */
Ds1302Write(0x8E, 0x00);
Ds1302Write(0x80, TIME[0]);
Ds1302Write(0x82, TIME[1]);
Ds1302Write(0x84, TIME[2]);
Ds1302Write(0x86, TIME[3]);
Ds1302Write(0x88, TIME[4]);
Ds1302Write(0x8a, TIME[5]);
Ds1302Write(0x8c, TIME[6]);
Ds1302Write(0x8E, 0x80);
/* 恢复显示 */
LcdWriteCom(0x0c);
last_mode = 0xFF;
}
/* 闹钟设置:使用AdjustValue重复利用代码 */
void SetAlarm()
{
LcdWriteCom(0x80);
LcdWriteStr("Alarm Clock Set ");
LcdWriteCom(0xC0);
LcdWriteStr(" : ");
LcdShowNumAt(0xC3, alarm_hour);
LcdShowNumAt(0xC6, alarm_min);
LcdWriteCom(0xCA);
LcdWriteStr(alarm_on ? " ON" : "OFF");
AdjustValue(&alarm_hour, 23, 0xC3, 0xC4); /* 调整小时 */
AdjustValue(&alarm_min, 59, 0xC6, 0xC7); /* 调整分钟 */
LcdWriteCom(0xCC); LcdWriteCom(0x0f);
while(1) /* 闹钟开关 */
{
if(K3_Pressed()) { alarm_on = 1; LcdWriteCom(0xCA); LcdWriteStr(" ON"); LcdWriteCom(0xCC); LcdWriteCom(0x0f); }
if(K4_Pressed()) { alarm_on = 0; LcdWriteCom(0xCA); LcdWriteStr("OFF"); LcdWriteCom(0xCC); LcdWriteCom(0x0f); }
if(K1_Pressed()) { break; }
}
/* 保存闹钟参数到DS1302 RAM */
LcdWriteCom(0x0c); /* 关闭光标 */
Ds1302Write(0x8E, 0x00); /* 允许写DS1302 */
Ds1302Write(0xC0, alarm_hour); /* 存小时到RAM */
Ds1302Write(0xC2, alarm_min); /* 存分钟到RAM */
Ds1302Write(0xC4, alarm_on); /* 存开关状态到RAM */
Ds1302Write(0x8E, 0x80); /* 禁止写DS1302 */
}
/* 定时器1初始化(供秒表计时) */
void Timer1Init()
{
TMOD &= 0x0F; /* 清T1位 */
TMOD |= 0x10; /* 设置T1为模式1(16位定时器) */
ET1 = 1; /* 使能T1中断 */
EA = 1; /* 总中断使能 */
}
/* 定时器1中断服务(约10ms中断一次) */
void Timer1() interrupt 3
{
TH1 = 0xD8; /* 装载初值 */
TL1 = 0xF0;
if(sw_running) /* 秒表运行时 */
{
sw_csec++;
if(sw_csec >= 100) /* 百分秒满100进1秒 */
{
sw_csec = 0;
sw_sec++;
}
}
}
/* 温度处理:读取DS18B20并转换为10倍温度值 */
void TempPros()
{
int raw;
unsigned char sign = 0;
if(!DS18B20_GetTemp(&raw)) /* 获取原始温度 */
return;
if(raw & 0x8000) /* 负温度处理 */
{
raw = ~raw + 1;
sign = 1;
}
temperature = raw * 10 / 16; /* 转换为10倍温度值 */
if(sign)
temperature = -temperature;
DS18B20_StartConvert(); /* 启动下一次温度转换 */
}
/* 闹钟判断(空函数) */
void AlarmJudge(void)
{
}
/* 闹钟蜂鸣器处理(蜂鸣器已坏) */
void AlarmBuzPro(void)
{
}
/* 毫秒延时 @12MHz */
void DelayMs(unsigned int ms)
{
unsigned int i, j;
for(i=0; i<ms; i++)
for(j=0; j<112; j++);
}
/* 10ms延时 @12MHz */
void Delay10ms(void)
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
Binary file not shown.
+232
View File
@@ -0,0 +1,232 @@
:0300000002058E68
:0C058E00787FE4F6D8FD75812B0205D5BE
:10078A00323020202D20202D202020202020202023
:10079A000020203A20203A2020202020202020203B
:1007AA00200020202053746F7020576174636820E2
:1007BA00200020203A20203A20202E20202020200D
:1007CA002000416C61726D20436C6F636B2053652E
:1007DA00742000202020203A2020202020202020C1
:0B07EA00202000204F4E004F4646002C
:1009F8000123000128FF02260000012A0001240625
:0F0A080001250001290002210000012B00C1007F
:100B4B0030B102C322120BB230B102C32230B1FD5D
:020B5B00D322A3
:100B5D0030B002C322120BB230B002C32230B0FD4E
:020B6D00D32291
:100B6F0030B202C322120BB230B202C32230B2FD36
:020B7F00D3227F
:100B810030B302C322120BB230B302C32230B3FD21
:020B9100D3226D
:100A48008E0F8F10851082850F83E4936017051031
:100A5800E510AE0F7002050F14F5828E83E493FF44
:060A6800120BCB80DF221F
:0C09CD00AB0FAA10A9111203B4FDAF1368
:1009D9008D15120BBFE51575F00A842430FF120B33
:0F09E900CBE51575F00A84E5F02430FF020BCB47
:1006A6008B0F8A1089118D12AF14120BBF7F0F1298
:1006B6000BBF120B6F5026AB0FAA10A9111203B471
:1006C600C39512500774011203CD8004E41203EFA0
:1006D6001209CDAF14120BBF7F0F120BBF120B8185
:1006E6005027AB0FAA10A9111203B4D394004007E8
:1006F60074FF1203CD8005E5121203EF1209CDAF88
:1007060014120BBF7F0F120BBF120B4B50A47F0CA2
:04071600120BBF22E1
:10071A00120BD7120AF41209A07F3F120A94120B85
:10072A00E27FC112089A8F24E524D39417400375F7
:10073A0024067FC312089A8F25E525D3943B4003EC
:10074A00E4F5257FC512089A8F29E529D39401403B
:10075A0003E4F529120003052AE52AC39405400695
:10076A00E4F52A12084DE5236528600612092D854D
:10077A00232812001912001A1207F512026180D4F6
:100B2F008F1DE51D75F00A842430FF120BCBE51DD8
:0C0B3F0075F00A84E5F02430FF020BCBB7
:10061A00EF14B4070040030206A590063275F003F2
:10062A00A4C58325F0C58373020647020650020655
:10063A005E02066C02067A0206880206967F53124A
:10064A000BCB7F7580077F4D120BCB7F6F120BCBC5
:10065A007F6E80447F54120BCB7F75120BCB7F6564
:10066A0080367F57120BCB7F65120BCB7F648028B5
:10067A007F54120BCB7F68120BCB7F75801A7F4693
:10068A00120BCB7F72120BCB7F69800C7F53120B3C
:0C069A00CB7F61120BCB7F74120BCB22C4
:10092D007F0C120BBFE52370177F80120BBF7E0764
:10093D007F8A120A487FC0120BBF7E077F9B801AE9
:10094D00E523B402187F80120BBF7E077FAC120A1D
:0E095D00487FC0120BBF7E077FBC120A4822E3
:10026100E5236003020365E51CC4540F75F00AA47D
:10027100FFE51C540F2FFD7F821209D9E51AC454E2
:100281000F75F00AA4FFE51A540F2FFD7F8512099F
:10029100D9E519C4540F75F00AA4FFE519540F2FBD
:1002A100FD7F881209D97F8C120BBFE51B5407FF14
:1002B10012061AE518C4540F75F00AA4FFE5185484
:1002C1000F2FFD7FC01209D9E517C4540F75F00A2D
:1002D100A4FFE517540F2FFD7FC31209D9E516C4FA
:1002E100540F75F00AA4FFE516540F2FFD7FC612B7
:1002F10009D97FC9120BBF852608852709C3E508DF
:100301006480948050127F2D120BCBC3E49509F5C4
:1003110009E49508F50880057F20120BCBAE08AFE4
:10032100097C007D64120468EF2430FF120BCBAE10
:1003310008AF097C007D0A1204687C007D0A120462
:1003410068ED2430FF120BCB7F2E120BCBAE08AF22
:10035100097C007D0A120468ED2430FF120BCB7F6B
:1003610043020BCBE52364027048AE21AF227C0E21
:100371007D10120413AD077FC01209D9AE21AF223F
:100381007C0E7D101204137E007F3CCFCDCFCECCEE
:10039100CE120413AD077FC31209D9AE21AF227C5F
:1003A100007D3C1204137FC61209D9AD2B7FC912FF
:0203B10009D968
:0103B3002227
:1007F500E5237018120B4B500612001E7528FF12C8
:100805000B5D5043752302C200E4802CE52364028E
:100815007035120B6F5006B200A200928E120B813A
:10082500500A200007E4F521F522F52B120B5D5047
:1008350016E4F523C28EC200F521F522F52B120927
:080845002D8523281202612217
:10001E00E51CC4540F75F00AA4FFE51C540F2FF510
:10002E0008E51AC4540F75F00AA4FFE51A540F2FF1
:10003E00F509E519C4540F75F00AA4FFE519540F1C
:10004E002FF50AE51B5407F50BE518C4540F75F090
:10005E000AA4FFE518540F2FF50CE517C4540F75BD
:10006E00F00AA4FFE517540F2FF50DE516C4540F33
:10007E0075F00AA4FFE516540F2FF50E7F80120BB4
:10008E00BF7E077F8A120A487FC0120BBF7E077F92
:10009E009B120A48AD087F821209D9AD097F8512DD
:1000AE0009D9AD0A7F881209D97F8C120BBFAF0B0D
:1000BE0012061AAD0C7FC01209D9AD0D7FC31209FD
:1000CE00D9AD0E7FC61209D97B007A007908751357
:1000DE00827514827D631206A67B007A00790975FB
:1000EE0013857514857D0C1206A67B007A00790A9D
:1000FE007513887514887D1F1206A67B007A007909
:10010E000C7513C07514C07D171206A67B007A00FD
:10011E00790D7513C37514C37D3B1206A67B007A49
:10012E0000790E7513C67514C67D3B1206A67F8C1C
:10013E00120BBF7F0F120BBF120B6F5022E50BC3BA
:10014E0094075004050B8003750B017F8C120BBFB7
:10015E00AF0B12061A7F8C120BBF7F0F120BBF1242
:10016E000B815021E50B94014004150B8003750B98
:10017E00077F8C120BBFAF0B12061A7F8C120BBFB0
:10018E007F0F120BBF120B4B50AEE50875F00A84B1
:10019E00C454F0FFE50875F00A84E5F04FF51CE550
:1001AE000975F00A84C454F0FFE50975F00A84E578
:1001BE00F04FF51AE50A75F00A84C454F0FFE50A0B
:1001CE0075F00A84E5F04FF519850B1BE50C75F0FB
:1001DE000A84C454F0FFE50C75F00A84E5F04FF57F
:1001EE0018E50D75F00A84C454F0FFE50D75F00A9C
:1001FE0084E5F04FF517E50E75F00A84C454F0FF50
:10020E00E50E75F00A84E5F04FF516E4FD7F8E12CB
:10021E00096BAD167F8012096BAD177F8212096BC9
:10022E00AD187F8412096BAD197F8612096BAD1A5A
:10023E007F8812096BAD1B7F8A12096BAD1C7F8CF8
:10024E0012096B7D807F8E12096B7F0C120BBF75AE
:02025E0028FF77
:01026000227B
:1004BD007F80120BBF7E077FCC120A487FC0120BC4
:1004CD00BF7E077FDD120A48AD247FC31209D9AD67
:1004DD00257FC61209D97FCA120BBFE52960067E9A
:1004ED00077FED80047E077FF1120A487B007A00BA
:1004FD0079247513C37514C47D171206A67B007A73
:10050D000079257513C67514C77D3B1206A67FCCE1
:10051D00120BBF7F0F120BBF120B6F5019752901F4
:10052D007FCA120BBF7E077FED120A487FCC120BDC
:10053D00BF7F0F120BBF120B815019E4F5297FCA33
:10054D00120BBF7E077FF1120A487FCC120BBF7FC3
:10055D000F120BBF120B4B50BF7F0C120BBFE4FDE4
:10056D007F8E12096BAD247FC012096BAD257FC242
:10057D0012096BAD297FC412096B7D807F8E020934
:01058D006B02
:0B0BD70053890F438910D2ABD2AF222C
:03001B00020A6E68
:100A6E00C0E0C0D0758DD8758BF0300014052BE525
:100A7E002BC39464400B752B000522E522700205F2
:060A8E0021D0D0D0E032BF
:10084D00E4F50AFB7A007908120B12503FE50830E7
:10085D00E714E509F4FFE508F4FEEF2401F509E4DA
:10086D003EF508750A01AE08AF097C007D0A120439
:10087D00017D101204688E268F27E50A600BC3E4F4
:0C088D009527F527E49526F526120BE2CE
:01089900223C
:0100190022C4
:01001A0022C3
:100AB500E4FDFCC3ED9FEC9E5015E4FBFA0BBB0077
:0F0AC500010AEB64704A70F50DBD00010C80E46E
:010AD40022FF
:0C0BB2007F017E267D82DDFEDEFADFF68C
:010BBE002214
:1008E400C2A58B807F017E00120914D2A77F017EEE
:1008F40000120914C2A77F017E00120914EBC4542C
:10090400F0F5807F017E00120914D2A77F017E00DA
:10091400D3EF9400EE9400400F7DC77C01DCFEDD34
:08092400FAEF1F70EB1E80E8E2
:01092C0022A8
:0C0BBF00AB07C2A7C2A61208E4C2A7221E
:0C0BCB00AB07C2A7D2A61208E4C2A72202
:100AF4007F32120BBF7F28120BBF7F0C120BBF7FFC
:0E0B040006120BBF7F01120BBF7F80020BBFDA
:0E0BA40081838587898B8D80828486888A8CE8
:090A170007160000120101021390
:10096B00C2B500C2B600D2B500E4FEEF1392B4EFED
:10097B00C313FFD2B600C2B6000EBE08EEE4FEED06
:10098B001392B4EDC313FDD2B600C2B6000EBE086F
:04099B00EEC2B500F3
:01099F002235
:10089A00C2B500C2B600D2B500E4FDEF1392B4EFC0
:1008AA00C313FFD2B600C2B6000DBD08EE00E4FDC8
:1008BA00A2B4E433FFC43333335480FCEEC3134C85
:1008CA00FED2B600C2B6000DBD08E5C2B500D2B66A
:0908DA0000C2B400D2B400AF0664
:0108E30022F2
:1009A0007F8112089AEF30E723E4FD7F8E12096BF6
:1009B000E4FCEC900BAB93FF74162CF8E6FD1209E7
:0C09C0006B0CBC07ED7D807F8E12096B74
:0109CC002208
:10000300E4FBEB900BA493FF12089A74162BF8A64B
:05001300070BBB07ED27
:0100180022C5
:100B9300C2B7E4FF0FBFC8FCD2B7E4FF0FBF64FCCA
:010BA300222F
:050A2100120B937FCCD5
:100A2600E4FEC2B700EF30E004D2B78002C2B7E4FA
:100A3600FD0DBD1EFCD2B70000EFC313FF0EBE08AE
:010A4600E1CE
:010A4700228C
:100AD500E4FFFEEFC313FFC2B700D2B7000030B783
:0E0AE50003430780E4FD0DBD14FC0EBE08E5C2
:010AF30022E0
:080BE200120A217F44020A26D9
:100A9400AC07120A217F4E120A26E4FF120A26E44A
:100AA400FF120A26AF04120A26120A217F48020AFC
:010AB400261B
:100B1200120A217FBE120A26120AD5AC07120AD582
:0C0B2200EFFFAD04EDCF8FF012049ED366
:010B2E0022A4
:10059A0002071AE493A3F8E493A34003F68001F256
:1005AA0008DFF48029E493A3F85407240CC8C33362
:1005BA00C4540F4420C8834004F456800146F6DF31
:1005CA00E4800B01020408102040809009F8E47EC0
:1005DA00019360BCA3FF543F30E509541FFEE49326
:1005EA00A360010ECF54C025E060A840B8E493A3ED
:1005FA00FAE493A3F8E493A3C8C582C8CAC583CA18
:10060A00F0A3C8C582C8CAC583CADFE9DEE780BECF
:010A200000D5
:1003B400BB010689828A83E0225002E722BBFE0247
:0903C400E32289828A83E493227A
:1003CD00BB010989828A83F8E028F022500327F7C0
:1003DD0022BBFE05F8E328F3228A838982F8E49391
:0203ED002822C4
:1003EF00BB010689828A83F0225002F722BBFE01ED
:0203FF00F322E7
:10040100EF8DF0A4A8F0CF8CF0A428CE8DF0A42E0F
:02041100FE22C9
:10041300BC000BBE0029EF8DF084FFADF022E4CCCD
:10042300F875F008EF2FFFEE33FEEC33FCEE9DEC96
:10043300984005FCEE9DFE0FD5F0E9E4CEFD22EDDC
:10044300F8F5F0EE8420D21CFEADF075F008EF2F26
:10045300FFED33FD4007985006D5F0F222C398FD17
:050463000FD5F0EA22B4
:10046800C2D5EC30E709B2D5E4C39DFDE49CFCEEAF
:1004780030E715B2D5E4C39FFFE49EFE120413C310
:10048800E49DFDE49CFC800312041330D507C3E40B
:060498009FFFE49EFE221E
:10049E00BB010A89828A83F0E5F0A3F0225006F7A9
:0F04AE0009A7F01922BBFE06F3E5F009F31922A6
:00000001FF
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+380
View File
@@ -0,0 +1,380 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="project_proj.xsd">
<SchemaVersion>1.0</SchemaVersion>
<Header>### uVision Project, (C) Keil Software</Header>
<Targets>
<Target>
<TargetName>Target 1</TargetName>
<ToolsetNumber>0x0</ToolsetNumber>
<ToolsetName>MCS-51</ToolsetName>
<TargetOption>
<TargetCommonOption>
<Device>AT89C51</Device>
<Vendor>Atmel</Vendor>
<Cpu>IRAM(0-0x7F) IROM(0-0xFFF) CLOCK(24000000)</Cpu>
<FlashUtilSpec></FlashUtilSpec>
<StartupFile>"LIB\STARTUP.A51" ("Standard 8051 Startup Code")</StartupFile>
<FlashDriverDll></FlashDriverDll>
<DeviceId>2976</DeviceId>
<RegisterFile>REGX51.H</RegisterFile>
<MemoryEnv></MemoryEnv>
<Cmp></Cmp>
<Asm></Asm>
<Linker></Linker>
<OHString></OHString>
<InfinionOptionDll></InfinionOptionDll>
<SLE66CMisc></SLE66CMisc>
<SLE66AMisc></SLE66AMisc>
<SLE66LinkerMisc></SLE66LinkerMisc>
<UseEnv>0</UseEnv>
<BinPath></BinPath>
<IncludePath></IncludePath>
<LibPath></LibPath>
<RegisterFilePath>Atmel\</RegisterFilePath>
<DBRegisterFilePath>Atmel\</DBRegisterFilePath>
<TargetStatus>
<Error>0</Error>
<ExitCodeStop>0</ExitCodeStop>
<ButtonStop>0</ButtonStop>
<NotGenerated>0</NotGenerated>
<InvalidFlash>1</InvalidFlash>
</TargetStatus>
<OutputDirectory>.\</OutputDirectory>
<OutputName>pro</OutputName>
<CreateExecutable>1</CreateExecutable>
<CreateLib>0</CreateLib>
<CreateHexFile>1</CreateHexFile>
<DebugInformation>1</DebugInformation>
<BrowseInformation>1</BrowseInformation>
<ListingPath>.\</ListingPath>
<HexFormatSelection>0</HexFormatSelection>
<Merge32K>0</Merge32K>
<CreateBatchFile>0</CreateBatchFile>
<BeforeCompile>
<RunUserProg1>0</RunUserProg1>
<RunUserProg2>0</RunUserProg2>
<UserProg1Name></UserProg1Name>
<UserProg2Name></UserProg2Name>
<UserProg1Dos16Mode>0</UserProg1Dos16Mode>
<UserProg2Dos16Mode>0</UserProg2Dos16Mode>
</BeforeCompile>
<BeforeMake>
<RunUserProg1>0</RunUserProg1>
<RunUserProg2>0</RunUserProg2>
<UserProg1Name></UserProg1Name>
<UserProg2Name></UserProg2Name>
<UserProg1Dos16Mode>0</UserProg1Dos16Mode>
<UserProg2Dos16Mode>0</UserProg2Dos16Mode>
</BeforeMake>
<AfterMake>
<RunUserProg1>0</RunUserProg1>
<RunUserProg2>0</RunUserProg2>
<UserProg1Name></UserProg1Name>
<UserProg2Name></UserProg2Name>
<UserProg1Dos16Mode>0</UserProg1Dos16Mode>
<UserProg2Dos16Mode>0</UserProg2Dos16Mode>
</AfterMake>
<SelectedForBatchBuild>0</SelectedForBatchBuild>
<SVCSIdString></SVCSIdString>
</TargetCommonOption>
<CommonProperty>
<UseCPPCompiler>0</UseCPPCompiler>
<RVCTCodeConst>0</RVCTCodeConst>
<RVCTZI>0</RVCTZI>
<RVCTOtherData>0</RVCTOtherData>
<ModuleSelection>0</ModuleSelection>
<IncludeInBuild>1</IncludeInBuild>
<AlwaysBuild>0</AlwaysBuild>
<GenerateAssemblyFile>0</GenerateAssemblyFile>
<AssembleAssemblyFile>0</AssembleAssemblyFile>
<PublicsOnly>0</PublicsOnly>
<StopOnExitCode>3</StopOnExitCode>
<CustomArgument></CustomArgument>
<IncludeLibraryModules></IncludeLibraryModules>
<BankNo>65535</BankNo>
</CommonProperty>
<DllOption>
<SimDllName>S8051.DLL</SimDllName>
<SimDllArguments></SimDllArguments>
<SimDlgDll>DP51.DLL</SimDlgDll>
<SimDlgDllArguments>-p51</SimDlgDllArguments>
<TargetDllName>S8051.DLL</TargetDllName>
<TargetDllArguments></TargetDllArguments>
<TargetDlgDll>TP51.DLL</TargetDlgDll>
<TargetDlgDllArguments>-p51</TargetDlgDllArguments>
</DllOption>
<DebugOption>
<OPTHX>
<HexSelection>0</HexSelection>
<HexRangeLowAddress>0</HexRangeLowAddress>
<HexRangeHighAddress>0</HexRangeHighAddress>
<HexOffset>0</HexOffset>
<Oh166RecLen>16</Oh166RecLen>
</OPTHX>
<Simulator>
<UseSimulator>1</UseSimulator>
<LoadApplicationAtStartup>1</LoadApplicationAtStartup>
<RunToMain>1</RunToMain>
<RestoreBreakpoints>1</RestoreBreakpoints>
<RestoreWatchpoints>1</RestoreWatchpoints>
<RestoreMemoryDisplay>1</RestoreMemoryDisplay>
<RestoreFunctions>1</RestoreFunctions>
<RestoreToolbox>1</RestoreToolbox>
<LimitSpeedToRealTime>0</LimitSpeedToRealTime>
</Simulator>
<Target>
<UseTarget>0</UseTarget>
<LoadApplicationAtStartup>1</LoadApplicationAtStartup>
<RunToMain>0</RunToMain>
<RestoreBreakpoints>1</RestoreBreakpoints>
<RestoreWatchpoints>1</RestoreWatchpoints>
<RestoreMemoryDisplay>1</RestoreMemoryDisplay>
<RestoreFunctions>0</RestoreFunctions>
<RestoreToolbox>1</RestoreToolbox>
</Target>
<RunDebugAfterBuild>0</RunDebugAfterBuild>
<TargetSelection>-1</TargetSelection>
<SimDlls>
<CpuDll></CpuDll>
<CpuDllArguments></CpuDllArguments>
<PeripheralDll></PeripheralDll>
<PeripheralDllArguments></PeripheralDllArguments>
<InitializationFile></InitializationFile>
</SimDlls>
<TargetDlls>
<CpuDll></CpuDll>
<CpuDllArguments></CpuDllArguments>
<PeripheralDll></PeripheralDll>
<PeripheralDllArguments></PeripheralDllArguments>
<InitializationFile></InitializationFile>
<Driver></Driver>
</TargetDlls>
</DebugOption>
<Utilities>
<Flash1>
<UseTargetDll>0</UseTargetDll>
<UseExternalTool>0</UseExternalTool>
<RunIndependent>0</RunIndependent>
<UpdateFlashBeforeDebugging>0</UpdateFlashBeforeDebugging>
<Capability>0</Capability>
<DriverSelection>-1</DriverSelection>
</Flash1>
<Flash2></Flash2>
<Flash3></Flash3>
<Flash4></Flash4>
</Utilities>
<Target51>
<Target51Misc>
<MemoryModel>0</MemoryModel>
<RTOS>0</RTOS>
<RomSize>2</RomSize>
<DataHold>0</DataHold>
<XDataHold>0</XDataHold>
<UseOnchipRom>0</UseOnchipRom>
<UseOnchipArithmetic>0</UseOnchipArithmetic>
<UseMultipleDPTR>0</UseMultipleDPTR>
<UseOnchipXram>0</UseOnchipXram>
<HadIRAM>1</HadIRAM>
<HadXRAM>0</HadXRAM>
<HadIROM>1</HadIROM>
<Moda2>0</Moda2>
<Moddp2>0</Moddp2>
<Modp2>0</Modp2>
<Mod517dp>0</Mod517dp>
<Mod517au>0</Mod517au>
<Mode2>0</Mode2>
<useCB>0</useCB>
<useXB>0</useXB>
<useL251>0</useL251>
<useA251>0</useA251>
<Mx51>0</Mx51>
<ModC812>0</ModC812>
<ModCont>0</ModCont>
<Lp51>0</Lp51>
<useXBS>0</useXBS>
<ModDA>0</ModDA>
<ModAB2>0</ModAB2>
<Mx51P>0</Mx51P>
<hadXRAM2>0</hadXRAM2>
<uocXram2>0</uocXram2>
<ModC2>0</ModC2>
<ModH2>0</ModH2>
<Mdu_R515>0</Mdu_R515>
<Mdu_F120>0</Mdu_F120>
<Psoc>0</Psoc>
<hadIROM2>0</hadIROM2>
<ModSmx2>0</ModSmx2>
<cBanks>0</cBanks>
<xBanks>0</xBanks>
<OnChipMemories>
<RCB>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0xffff</Size>
</RCB>
<RXB>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</RXB>
<Ocm1>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</Ocm1>
<Ocm2>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</Ocm2>
<Ocm3>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</Ocm3>
<Ocr1>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</Ocr1>
<Ocr2>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</Ocr2>
<Ocr3>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</Ocr3>
<IRO>
<Type>1</Type>
<StartAddress>0x0</StartAddress>
<Size>0x1000</Size>
</IRO>
<IRA>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x80</Size>
</IRA>
<XRA>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</XRA>
<XRA512>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</XRA512>
<IROM512>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</IROM512>
</OnChipMemories>
</Target51Misc>
<C51>
<RegisterColoring>0</RegisterColoring>
<VariablesInOrder>0</VariablesInOrder>
<IntegerPromotion>1</IntegerPromotion>
<uAregs>0</uAregs>
<UseInterruptVector>1</UseInterruptVector>
<Fuzzy>3</Fuzzy>
<Optimize>8</Optimize>
<WarningLevel>2</WarningLevel>
<SizeSpeed>1</SizeSpeed>
<ObjectExtend>1</ObjectExtend>
<ACallAJmp>0</ACallAJmp>
<InterruptVectorAddress>0</InterruptVectorAddress>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
<Undefine></Undefine>
<IncludePath></IncludePath>
</VariousControls>
</C51>
<Ax51>
<UseMpl>0</UseMpl>
<UseStandard>1</UseStandard>
<UseCase>0</UseCase>
<UseMod51>0</UseMod51>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
<Undefine></Undefine>
<IncludePath></IncludePath>
</VariousControls>
</Ax51>
<Lx51>
<useFile>0</useFile>
<linkonly>0</linkonly>
<UseMemoryFromTarget>1</UseMemoryFromTarget>
<CaseSensitiveSymbols>0</CaseSensitiveSymbols>
<WarningLevel>2</WarningLevel>
<DataOverlaying>1</DataOverlaying>
<OverlayString></OverlayString>
<MiscControls></MiscControls>
<DisableWarningNumbers></DisableWarningNumbers>
<LinkerCmdFile></LinkerCmdFile>
<Assign></Assign>
<ReserveString></ReserveString>
<CClasses></CClasses>
<UserClasses></UserClasses>
<CSection></CSection>
<UserSection></UserSection>
<CodeBaseAddress></CodeBaseAddress>
<XDataBaseAddress></XDataBaseAddress>
<PDataBaseAddress></PDataBaseAddress>
<BitBaseAddress></BitBaseAddress>
<DataBaseAddress></DataBaseAddress>
<IDataBaseAddress></IDataBaseAddress>
<Precede></Precede>
<Stack></Stack>
<CodeSegmentName></CodeSegmentName>
<XDataSegmentName></XDataSegmentName>
<BitSegmentName></BitSegmentName>
<DataSegmentName></DataSegmentName>
<IDataSegmentName></IDataSegmentName>
</Lx51>
</Target51>
</TargetOption>
<Groups>
<Group>
<GroupName>Source Group 1</GroupName>
<Files>
<File>
<FileName>STARTUP.A51</FileName>
<FileType>2</FileType>
<FilePath>.\STARTUP.A51</FilePath>
</File>
<File>
<FileName>main.c</FileName>
<FileType>1</FileType>
<FilePath>.\main.c</FilePath>
</File>
<File>
<FileName>lcd.c</FileName>
<FileType>1</FileType>
<FilePath>.\lcd.c</FilePath>
</File>
<File>
<FileName>ds1302.c</FileName>
<FileType>1</FileType>
<FilePath>.\ds1302.c</FilePath>
</File>
<File>
<FileName>ds18b20.c</FileName>
<FileType>1</FileType>
<FilePath>.\ds18b20.c</FilePath>
</File>
</Files>
</Group>
</Groups>
</Target>
</Targets>
</Project>
+181
View File
@@ -0,0 +1,181 @@
# 系统模块结构图(函数调用关系)
## main.c 模块
```
main() ──────────────────────────────────────────────────────────────────────
│ [初始化]
├── Timer1Init() // 定时器1初始化
├── LcdInit() ──→ lcd.c // LCD1602初始化
├── Ds1302Init() ──→ ds1302.c // DS1302初始化
│ └── Ds1302Read(), Ds1302Write()
├── DS18B20_SetResolution() ──→ ds18b20.c
├── DS18B20_StartConvert() ──→ ds18b20.c
└── Ds1302Read() ──→ ds1302.c // 读取闹钟参数
│
│ [主循环 while(1)]
├── Ds1302ReadTime() ──→ ds1302.c // 每次循环读取时钟
├── TempPros() // 温度处理(每5次循环)
│ ├── DS18B20_GetTemp() ──→ ds18b20.c
│ └── DS18B20_StartConvert()
├── LcdFramework() // 模式切换时刷新框架
│ └── LcdWriteStr() → LcdWriteData() ──→ lcd.c
├── AlarmJudge() // 闹钟时间匹配判断
├── AlarmBuzPro() // 蜂鸣器驱动
│ └── DelayMs()
├── KeyScan() // 按键扫描
│ └── KeyPressed() (×5) // 消抖检测
│ ├── SetAlarm() // K1: 闹钟设置
│ │ ├── LcdWriteStr() → lcd.c
│ │ ├── LcdShowNumAt() → lcd.c
│ │ ├── AdjustValue() // 小时/分钟调节
│ │ │ ├── KeyPressed()
│ │ │ ├── KeySound()
│ │ │ └── LcdShowNumAt()
│ │ ├── KeyPressed()
│ │ │ └── KeySound()
│ │ └── Ds1302Write() → ds1302.c
│ ├── KeySound() / ReturnSound()
│ │ └── DelayMs()
│ └── 修改秒表全局变量
│
└── LcdUpdate() // LCD数据刷新
├── LcdShowNumAt() → lcd.c
├── ShowLcdWeek()
│ └── LcdWriteData() → lcd.c
├── LcdWriteCom() → lcd.c
└── LcdWriteData() → lcd.c
Timer1() interrupt 3 // 定时器中断(10ms)
└── sw_running == 1 → sw_csec++ → sw_sec++ // 秒表计时
```
## ds1302.c 模块
```
Ds1302Init()
├── Ds1302Read(0x81) // 读秒寄存器,检测CH位
└── Ds1302Write() × 9 // 写入默认时间 + 关闭写保护
Ds1302ReadTime()
└── Ds1302Read() × 7 // 批量读取7个时间寄存器
Ds1302Write(addr, dat) // SPI协议写
├── RST=1, SCLK=0
├── 循环8次: 逐位发送地址(LSB→MSB)
└── 循环8次: 逐位发送数据
Ds1302Read(addr) // SPI协议读
├── RST=1, SCLK=0
├── 循环8次: 逐位发送地址
└── 循环8次: 逐位读取数据(MSB→LSB)
```
## ds18b20.c 模块
```
DS18B20_Reset()
├── DQ=0 → 延时480μs
└── DQ=1 → 等待应答
DS18B20_WriteByte(dat) // 单总线写
└── 循环8次: DQ=0 → 写bit → 延时60μs → DQ=1
DS18B20_ReadByte() // 单总线读
└── 循环8次: DQ=0 → DQ=1 → 读bit → 延时
DS18B20_StartConvert() // 启动温度转换
├── DS18B20_Reset()
├── DS18B20_WriteByte(0xCC) // 跳过ROM
└── DS18B20_WriteByte(0x44) // 启动转换
DS18B20_SetResolution(res) // 设置分辨率
├── DS18B20_Reset()
├── DS18B20_WriteByte(0xCC)
├── DS18B20_WriteByte(0x4E) // 写暂存器
├── DS18B20_WriteByte(0x00) // TH
├── DS18B20_WriteByte(0x00) // TL
├── DS18B20_WriteByte(res) // 配置寄存器
├── DS18B20_Reset()
├── DS18B20_WriteByte(0xCC)
└── DS18B20_WriteByte(0x48) // 复制到EEPROM
DS18B20_GetTemp(int *temp) // 读取温度值
├── DS18B20_Reset()
├── DS18B20_WriteByte(0xCC)
├── DS18B20_WriteByte(0xBE) // 读暂存器
├── DS18B20_ReadByte() // 低字节
└── DS18B20_ReadByte() // 高字节
```
## lcd.c 模块
```
LcdInit()
└── LcdWriteCom() × 5 // 初始化指令序列
LcdWriteCom(com) // 写指令(4位模式)
├── RS=0, RW=0
├── P0=com → 写高4位
├── E=1 → 延时 → E=0
└── P0=com<<4 → 写低4位
└── E=1 → 延时 → E=0
└── 内部调用 Lcd1602_Delay1ms()
LcdWriteData(dat) // 写数据(4位模式)
├── RS=1, RW=0
├── P0=dat → 写高4位
├── E=1 → 延时 → E=0
└── P0=dat<<4 → 写低4位
└── E=1 → 延时 → E=0
└── 内部调用 Lcd1602_Delay1ms()
```
## 函数调用总图
```
┌─────────────────────────────────────────────────────────────────────┐
│ main() 主循环 │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ DS1302 │ │ DS18B20 │ │ LCD1602 │ │ 按键扫描 │ │ 闹钟处理 │ │
│ │ 时钟模块 │ │ 温度模块 │ │ 显示模块 │ │ KeyScan │ │ Judge │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
│ │ │ │ │ │ │
│ ┌────▼────┐ ┌────▼────┐ ┌────▼────┐ ┌────▼────┐ ┌────▼────┐ │
│ │ReadTime │ │TempPros │ │LcdUpdate│ │KeyPressed│ │AlarmBuz │ │
│ │Init │ │GetTemp │ │Framework│ │SetAlarm │ │Pro │ │
│ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ Timer1 中断 (10ms) │ │
│ │ → 秒表 sw_csec / sw_sec 累加 │ │
│ └─────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────┘
│
┌──────────────────┼──────────────────┐
▼ ▼ ▼
┌───────────┐ ┌───────────┐ ┌───────────┐
│ ds1302.c │ │ ds18b20.c │ │ lcd.c │
│ 底层驱动 │ │ 底层驱动 │ │ 底层驱动 │
│ SPI协议 │ │ 单总线协议 │ │ 并行接口 │
└───────────┘ └───────────┘ └───────────┘
│ │ │
▼ ▼ ▼
┌───────────────────────────────────────────────┐
│ STC89C52 硬件层 │
│ P1^0-2 DS1302 │ P3^7 DS18B20 │ P0/P2 LCD │
│ P3^0-3 按键 │ P2^0 蜂鸣器 │ Timer1 │
└───────────────────────────────────────────────┘
```
## 文件间依赖
```
main.c ──→ lcd.h (LcdInit, LcdWriteCom, LcdWriteData)
main.c ──→ ds1302.h (Ds1302Init, Ds1302ReadTime, Ds1302Read, Ds1302Write)
main.c ──→ ds18b20.h (DS18B20_GetTemp, DS18B20_StartConvert, DS18B20_SetResolution)
ds1302.c ──→ reg51.h, intrins.h
ds18b20.c ──→ reg51.h, intrins.h
lcd.c ──→ reg51.h
```