我要设计一个基于单片机的非接触式人体红外测温系统,系统包括STC89C52单片机,MLX90614红外测温模块,LCD1602显示模块等装置,请写一段代码,实现测量人体温度并将温度值显示在LCD1602上的功能
时间: 2024-02-28 07:57:36 浏览: 64
红外测温程序_MLX90614ESF_pourwdl_传感器_stc89c52与mlx90614_红外测温程序
好的,以下是代码实现:
```c
#include <reg52.h>
#define SDA P0_1
#define SCL P0_0
#define ADDR 0x5A // MLX90614的I2C地址
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;
void delay(unsigned int count)
{
unsigned int i,j;
for(i=0;i<count;i++)
for(j=0;j<1275;j++);
}
void I2C_start()
{
SDA = 1;
SCL = 1;
delay(5);
SDA = 0;
delay(5);
SCL = 0;
}
void I2C_stop()
{
SDA = 0;
SCL = 1;
delay(5);
SDA = 1;
delay(5);
}
void I2C_write(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
SDA = dat&0x80;
SCL = 1;
delay(5);
SCL = 0;
dat <<= 1;
}
}
unsigned char I2C_read_ack()
{
unsigned char i, dat = 0;
SDA = 1;
for(i=0;i<8;i++)
{
SCL = 1;
delay(5);
dat <<= 1;
dat |= SDA;
SCL = 0;
}
SDA = 0;
return dat;
}
unsigned char I2C_read_nack()
{
unsigned char i, dat = 0;
SDA = 1;
for(i=0;i<8;i++)
{
SCL = 1;
delay(5);
dat <<= 1;
dat |= SDA;
SCL = 0;
}
SDA = 0;
return dat;
}
void LCD_send_cmd(unsigned char cmd)
{
RS = 0;
RW = 0;
EN = 1;
P0 = cmd;
delay(5);
EN = 0;
}
void LCD_send_data(unsigned char dat)
{
RS = 1;
RW = 0;
EN = 1;
P0 = dat;
delay(5);
EN = 0;
}
void LCD_init()
{
LCD_send_cmd(0x38);
LCD_send_cmd(0x0c);
LCD_send_cmd(0x06);
LCD_send_cmd(0x01);
}
void LCD_display_temp(float temp)
{
unsigned char buf[16];
sprintf(buf, "Temp: %.1fC ", temp);
LCD_send_cmd(0x80);
for(unsigned char i=0; i<16; i++)
{
if(buf[i] == '\0')
break;
LCD_send_data(buf[i]);
}
}
float MLX90614_read_temp()
{
float temp;
unsigned char i;
I2C_start();
I2C_write(ADDR<<1);
I2C_write(0x07);
I2C_start();
I2C_write((ADDR<<1) | 1);
i = I2C_read_ack();
temp = I2C_read_ack();
temp += (float)I2C_read_nack()/16.0;
I2C_stop();
return temp;
}
void main()
{
float temp;
LCD_init();
while(1)
{
temp = MLX90614_read_temp();
LCD_display_temp(temp);
delay(1000);
}
}
```
这段代码实现了通过I2C协议从MLX90614红外测温模块中读取温度值,并将温度值通过LCD1602显示出来。在主函数中,通过一个循环不断地读取温度值,并显示在LCD上。
阅读全文