功能介绍:
0.本系统采用STC89C52作为单片机
1.LCD1602液晶实时显示当前检测的温度值
2.所检测温度值为3个热电偶温度值的平均值
3.温度传感器采用MAX6675+热电偶的方式,测量温度范围广
4.电源接口采用DC002直接输入5V供电
原理图:
PCB:
主程序:
#include "main.h"
bit refreshFlag = 1;
unsigned int tempBuf = 0; //读取温度值
float temperature[3] = 0.0; //实际温度值
char dis0[16]; //定义显示区域临时存储数组
void main(void)
{
Timer0_Init();
LCD_Init(); //初始化液晶
DelayMs(200); //延时有助于稳定
LCD_Clear(); //清屏
LCD_DispStr(0, 0, " Temperature "); //显示第一行
while (1) //主循环
{
if (refreshFlag == 1) //2s获取数据并发送
{
refreshFlag = 0;
TR0 = 0;
DispNormal();
TR0 = 1;
}
}
}
void DispNormal(void)
{
//获取温度
tempBuf = ReadMAX6675_1();
GetCurrentTemp(tempBuf);
tempBuf &= 0x7ff8;
tempBuf >>= 3; //取D14~D3有效数据
temperature[0] = (float)tempBuf * 1023.75 / 4096; //减去合适的环境温度
//获取温度
tempBuf = ReadMAX6675_2();
GetCurrentTemp(tempBuf);
tempBuf &= 0x7ff8;
tempBuf >>= 3; //取D14~D3有效数据
temperature[1] = (float)tempBuf * 1023.75 / 4096; //减去合适的环境温度
//获取温度
tempBuf = ReadMAX6675_3();
GetCurrentTemp(tempBuf);
tempBuf &= 0x7ff8;
tempBuf >>= 3; //取D14~D3有效数据
temperature[2] = (float)tempBuf * 1023.75 / 4096; //减去合适的环境温度
//液晶显示
LCD_DispStr(0, 0, " Temperature "); //显示第一行
sprintf(dis0, " %6.1f", (temperature[0] + temperature[1] + temperature[2])/3); //打印求取平均值并打印温度值
LCD_DispStr(0, 1, dis0); //显示第一行
LCD_DispOneChar(9, 1, 0xdf); //写入温度右上角点
LCD_DispOneChar(10, 1, 'C'); //写入温度C
}
/*------------------------------------------------
定时器初始化子程序
------------------------------------------------*/
void Timer0_Init(void)
{
TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TH0 = (65536 - 10000) / 256; //重新赋值 10ms
TL0 = (65536 - 10000) % 256;
EA = 1; //总中断打开
ET0 = 1; //定时器中断打开
TR0 = 1; //定时器开关打开
}
/*------------------------------------------------
定时器中断子程序
------------------------------------------------*/
void Timer0_Interrupt(void) interrupt 1
{
static unsigned char time10ms = 0;
TH0 = (65536 - 10000) / 256; //重新赋值 10ms
TL0 = (65536 - 10000) % 256;
time10ms++;
if (time10ms >= 200) // 2s
{
time10ms = 0;
refreshFlag = 1;
}
}