程序源码:#include "delay.h"
#include "LCD1602.h"
#include "ds18b20.h"
#include "key.h"
#include "led.h"
#include "timer.h"
#include "stdio.h"
#include "stdlib.h"
short Tpup = 37,Tpdw = 10; //温度上限 默认37 下限默认10
int main(void)
{
short temprature; //定义温度变量
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
delay_init(); //延时函数初始化
Lcd1602_Init(); //液晶1602初始化
LED_Init();
DS18B20_Init();
while(DS18B20_Init()!=0) //ds18b20初始化检测
{
Lcd1602_String(0,0,(u8 *)" DS18B20 Error! ");
delay_ms(800);
Lcd1602_String(0,0,(u8 *)" ");
delay_ms(800);
}
Lcd1602_String(0,0,(u8 *)"Temp: ---.-"); //从第一行第一列开始显示
Lcd1602_Write_Data(0XDF);
Lcd1602_Write_Data('C');
TIM2_Int_Init(199,7199); //10Khz的计数频率,计数到200为20ms
KEY_Init(); //初始化端口
Lcd1602_String(0,0,(u8 *)"Temp:"); //从第一行第一列开始显示
while(1)
{
if(flag_timer==1) //600毫秒赋一次值 flag_timer是标志位
{
flag_timer = 0; //清除标志 等待下一次600ms
TIM_Cmd(TIM2, DISABLE); //使能TIMx
temprature=DS18B20_Get_Temp(); //读取温度
TIM_Cmd(TIM2, ENABLE); //使能TIMx
Lcd1602_String(0,0,(u8 *)"Temp:"); //从第一行第一列开始显示
if(temprature>=0) //如果温度大于等于0
{
Lcd1602_Write_Data(' ');
}
else //如果温度是负的
{
temprature = abs(temprature); //取绝对值
Lcd1602_Write_Data('-'); //显示负号
}
Lcd1602_Write_Data(temprature/1000+0X30);
Lcd1602_Write_Data(temprature%1000/100+0X30);
Lcd1602_Write_Data(temprature%1000%100/10+0X30);
Lcd1602_Write_Data('.');
Lcd1602_Write_Data(temprature%1000%100%10+0X30);
Lcd1602_Write_Data(0XDF);
Lcd1602_Write_Data('C');
if(temprature>(Tpup*10)
||temprature<(Tpdw*10)) //当前温度超过上下限 上下限值没有扩大十倍 所以要*10
{
flag_beep = 1; //开报警标志 执行报警程序在定时器中断
BEEP = 0;
}
else
{
BEEP = 1;
flag_beep = 0; //关报警标志位
}
}
KEY_Set();
Lcd1602_String(0,1,"H:"); //从第二行第一列开始显示
Lcd1602_Write_Data(Tpup/100+0X30);
Lcd1602_Write_Data(Tpup%100/10+0X30);
Lcd1602_Write_Data(Tpup%100%10+0X30);
Lcd1602_Write_Data(0XDF);
Lcd1602_Write_Data('C');
Lcd1602_String(8,1,"L:"); //从第二行第九列开始显示
Lcd1602_Write_Data(Tpdw/100+0X30);
Lcd1602_Write_Data(Tpdw%100/10+0X30);
Lcd1602_Write_Data(Tpdw%100%10+0X30);
Lcd1602_Write_Data(0XDF);
Lcd1602_Write_Data('C');
}
}
|