uart_temp/src/main.c
2024-07-15 01:22:00 +08:00

110 lines
2.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**************************************************************************************
深圳市普中科技有限公司PRECHIN 普中)
技术支持www.prechin.net
实验名称DS18B20温度传感器实验
接线说明:
实验现象下载程序后插上DS18B20温度传感器数码管显示检测的温度值
注意事项:注意温度传感器的方向,在接口处我们已经用丝印画了一个凸起,
所以只需要将温度传感器对应插入即可
***************************************************************************************/
#include "public.h"
#include "smg.h"
#include "ds18b20.h"
#include "uart.h"
#include "stdio.h"
#include "time0.h"
int HH=0, MM=0, SS=0;
int send_tmp = 0;
/*******************************************************************************
* 函 数 名 : main
* 函数功能 : 主函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void main(void)
{
u8 i=0;
u8 temp_buf[5];
int temp_value = 0;
uart_init(0XFA);//波特率为9600
ds18b20_init();//初始化DS18B20
time0_init();//定时器0中断配置
while(1)
{
i++;
if(i%50==0)//间隔一段时间读取温度值,间隔时间要大于温度传感器转换温度时间
temp_value=ds18b20_read_temperture()*10;//保留温度值小数后一位
// 数码管显示
if(temp_value<0)//负温度
{
temp_value=-temp_value;
temp_buf[0]=0x40;//显示负号
}
else
temp_buf[0]=0x00;//不显示
temp_buf[1]=gsmg_code[temp_value/1000];//百位
temp_buf[2]=gsmg_code[temp_value%1000/100];//十位
temp_buf[3]=gsmg_code[temp_value%1000%100/10]|0x80;//个位+小数点
temp_buf[4]=gsmg_code[temp_value%1000%100%10];//小数点后一位
smg_display(temp_buf,4);
// 串口打印
if(send_tmp == 1) {
send_tmp = 0;
printf("%02d:%02d:%02d temp = %.1f\r\n", HH, MM, SS, temp_value/10.0);
// for(u8 i=0;i<RESET_PASSWORD_LENGTH;i++) {
// printf("i=%0d, %C[%x] = %C[%x] is %0d\r\n", i, reset_password[i], reset_password[i], receice_password[i], receice_password[i],receice_password[i]!= reset_password[i]);
// }
}
}
}
void uart(void) __interrupt (4) //串口通信中断函数
{
u8 rec_data;
if(RI)
{
RI = 0; //清除接收中断标志位
rec_data=SBUF; //存储接收到的数据
SBUF=rec_data; //将接收到的数据放入到发送寄存器
while(!TI); //等待发送数据完成
TI=0; //清除发送完成标志?
auto_reset(rec_data);
}
if(TI)
{
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;
send_tmp = 1;
SS ++;
if (SS == 60){
SS = 0;
MM ++;
}
if(MM == 60){
MM = 0;
HH ++;
}
}
}