增加定时器中断,提高定时精度
This commit is contained in:
parent
a46186cd18
commit
6094aff6c1
12
include/time0.h
Normal file
12
include/time0.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#ifndef _time0_H
|
||||||
|
#define _time0_H
|
||||||
|
|
||||||
|
#include "public.h"
|
||||||
|
|
||||||
|
//定义LED1管脚
|
||||||
|
#define LED1 P2_0
|
||||||
|
|
||||||
|
//函数声明
|
||||||
|
void time0_init(void);
|
||||||
|
|
||||||
|
#endif
|
52
src/main.c
52
src/main.c
@ -13,8 +13,10 @@
|
|||||||
#include "ds18b20.h"
|
#include "ds18b20.h"
|
||||||
#include "uart.h"
|
#include "uart.h"
|
||||||
#include "stdio.h"
|
#include "stdio.h"
|
||||||
|
#include "time0.h"
|
||||||
|
|
||||||
|
int HH=0, MM=0, SS=0;
|
||||||
|
int temp_value=0;
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* 函 数 名 : main
|
* 函 数 名 : main
|
||||||
* 函数功能 : 主函数
|
* 函数功能 : 主函数
|
||||||
@ -23,15 +25,12 @@
|
|||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
void main(void)
|
void main(void)
|
||||||
{
|
{
|
||||||
|
|
||||||
u8 i=0;
|
u8 i=0;
|
||||||
int pre_temp_value=0;
|
|
||||||
int temp_value=0;
|
|
||||||
u8 temp_buf[5];
|
u8 temp_buf[5];
|
||||||
int HH=0, MM=0, SS=0;
|
|
||||||
|
|
||||||
uart_init(0XFA);//波特率为9600
|
uart_init(0XFA);//波特率为9600
|
||||||
ds18b20_init();//初始化DS18B20
|
ds18b20_init();//初始化DS18B20
|
||||||
|
time0_init();//定时器0中断配置
|
||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
@ -54,26 +53,7 @@ void main(void)
|
|||||||
smg_display(temp_buf,4);
|
smg_display(temp_buf,4);
|
||||||
|
|
||||||
// 串口打印
|
// 串口打印
|
||||||
// if(pre_temp_value != temp_value){
|
|
||||||
// pre_temp_value = temp_value;
|
|
||||||
// printf("temp = %.1f\n", temp_value/10.0);
|
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,3 +75,27 @@ void uart(void) __interrupt (4) //串口通信中断函数
|
|||||||
busy = 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
26
src/time0.c
Normal file
@ -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;//打开定时器
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user