From 6094aff6c1c144395a4dbbd87cb576e982b46301 Mon Sep 17 00:00:00 2001 From: fengbh <1953356163@qq.com> Date: Mon, 15 Jan 2024 20:30:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E4=B8=AD=E6=96=AD=EF=BC=8C=E6=8F=90=E9=AB=98=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E7=B2=BE=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/time0.h | 12 +++++++++++ include/uart.h | 2 +- src/main.c | 54 ++++++++++++++++++++++++++----------------------- src/time0.c | 26 ++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 26 deletions(-) create mode 100644 include/time0.h create mode 100644 src/time0.c diff --git a/include/time0.h b/include/time0.h new file mode 100644 index 0000000..cf02ed9 --- /dev/null +++ b/include/time0.h @@ -0,0 +1,12 @@ +#ifndef _time0_H +#define _time0_H + +#include "public.h" + +//定义LED1管脚 +#define LED1 P2_0 + +//函数声明 +void time0_init(void); + +#endif \ No newline at end of file diff --git a/include/uart.h b/include/uart.h index 85729cc..8aa7952 100644 --- a/include/uart.h +++ b/include/uart.h @@ -17,6 +17,6 @@ extern __bit busy; void uart_init(u8 baud); void send_string(char *s); void send_data(u8 dat); -int putchar(char c); +int putchar(char c); #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 14d449c..06091c0 100644 --- a/src/main.c +++ b/src/main.c @@ -13,8 +13,10 @@ #include "ds18b20.h" #include "uart.h" #include "stdio.h" +#include "time0.h" - +int HH=0, MM=0, SS=0; +int temp_value=0; /******************************************************************************* * 函 数 名 : main * 函数功能 : 主函数 @@ -22,16 +24,13 @@ * 输 出 : 无 *******************************************************************************/ void main(void) -{ - +{ u8 i=0; - int pre_temp_value=0; - int temp_value=0; u8 temp_buf[5]; - int HH=0, MM=0, SS=0; uart_init(0XFA);//波特率为9600 ds18b20_init();//初始化DS18B20 + time0_init();//定时器0中断配置 while(1) { @@ -54,26 +53,7 @@ void main(void) smg_display(temp_buf,4); // 串口打印 - // if(pre_temp_value != temp_value){ - // pre_temp_value = temp_value; // printf("temp = %.1f\n", temp_value/10.0); - // } - - // delay - if(temp_value != 0) { - SS ++; - if (SS == 60){ - SS = 0; - MM ++; - } - if(MM == 60){ - MM = 0; - HH ++; - } - delay_ms(1000); - printf("%02d:%02d:%02d temp = %.1f\r\n", HH, MM, SS, temp_value/10.0); - } - } } @@ -94,4 +74,28 @@ void uart(void) __interrupt (4) //串口通信中断函数 TI = 0; busy = 0; } +} + +void time0(void) __interrupt (1) //定时器0中断函数 +{ + static u16 i;//定义静态变量i + TH0=0XFC; //给定时器赋初值,定时1ms + TL0=0X18; + i++; + if(i==1000) + { + i=0; + LED1=!LED1; + + SS ++; + if (SS == 60){ + SS = 0; + MM ++; + } + if(MM == 60){ + MM = 0; + HH ++; + } + printf("%02d:%02d:%02d temp = %.1f\r\n", HH, MM, SS, temp_value/10.0); + } } \ No newline at end of file diff --git a/src/time0.c b/src/time0.c new file mode 100644 index 0000000..6368b59 --- /dev/null +++ b/src/time0.c @@ -0,0 +1,26 @@ +/************************************************************************************** +深圳市普中科技有限公司(PRECHIN 普中) +技术支持:www.prechin.net + +实验名称:定时器0实验 +接线说明: +实验现象:下载程序后,D1指示灯间隔1s闪烁 +注意事项: +***************************************************************************************/ +#include "time0.h" + +/******************************************************************************* +* 函 数 名 : time0_init +* 函数功能 : 定时器0中断配置函数,通过设置TH和TL即可确定定时时间 +* 输 入 : 无 +* 输 出 : 无 +*******************************************************************************/ +void time0_init(void) +{ + TMOD|=0X01;//选择为定时器0模式,工作方式1 + TH0=0XFC; //给定时器赋初值,定时1ms + TL0=0X18; + ET0=1;//打开定时器0中断允许 + EA=1;//打开总中断 + TR0=1;//打开定时器 +}