增加定时器中断,提高定时精度

This commit is contained in:
fengbh 2024-01-15 20:30:02 +08:00
parent a46186cd18
commit 6094aff6c1
4 changed files with 68 additions and 26 deletions

12
include/time0.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef _time0_H
#define _time0_H
#include "public.h"
//定义LED1管脚
#define LED1 P2_0
//函数声明
void time0_init(void);
#endif

View File

@ -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

View File

@ -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);
}
}

26
src/time0.c Normal file
View File

@ -0,0 +1,26 @@
/**************************************************************************************
PRECHIN
www.prechin.net
0
线
D1指示灯间隔1s闪烁
***************************************************************************************/
#include "time0.h"
/*******************************************************************************
* : time0_init
* : 0TH和TL即可确定定时时间
* :
* :
*******************************************************************************/
void time0_init(void)
{
TMOD|=0X01;//选择为定时器0模式工作方式1
TH0=0XFC; //给定时器赋初值定时1ms
TL0=0X18;
ET0=1;//打开定时器0中断允许
EA=1;//打开总中断
TR0=1;//打开定时器
}