51单片机温度读取显示
时间: 2023-08-24 11:03:58 浏览: 64
可以使用51单片机连接温度传感器,读取传感器的温度值,并将温度值显示在数码管或LCD屏幕上。以下是简单的步骤:
1. 连接温度传感器到51单片机的ADC引脚。
2. 配置ADC转换器,选择正确的参考电压和采样时钟频率。
3. 读取ADC转换器的值,将其转换为温度值。
4. 将温度值转换为数码管或LCD屏幕上的数字,显示出来。
具体实现可以参考51单片机相关的开发文档和示例代码。
相关问题
51单片机读取温度程序
以下是51单片机读取温度的基本程序:
```c
#include <reg51.h>
#include <stdio.h>
#define LCD_DATA P2
#define LCD_RS P1_0
#define LCD_RW P1_1
#define LCD_EN P1_2
unsigned int temp;
void delay(unsigned int n) //延时函数
{
while(n--);
}
void init_lcd() //LCD初始化函数
{
delay(15000);
LCD_RS = 0;
LCD_RW = 0;
LCD_DATA = 0x38;
LCD_EN = 1;
delay(5000);
LCD_EN = 0;
delay(5000);
LCD_DATA = 0x0c;
LCD_EN = 1;
delay(5000);
LCD_EN = 0;
delay(5000);
LCD_DATA = 0x01;
LCD_EN = 1;
delay(5000);
LCD_EN = 0;
delay(5000);
LCD_DATA = 0x06;
LCD_EN = 1;
delay(5000);
LCD_EN = 0;
delay(5000);
}
void write_lcd(unsigned char dat) //向LCD写入数据
{
LCD_RS = 1;
LCD_RW = 0;
LCD_DATA = dat;
LCD_EN = 1;
delay(5000);
LCD_EN = 0;
delay(5000);
}
void display_temp() //显示温度
{
unsigned char a,b,c,d;
a = temp / 100;
b = (temp % 100) / 10;
c = temp % 10;
d = 0xdf; //温度符号
write_lcd(d);
write_lcd(a + '0');
write_lcd('.');
write_lcd(b + '0');
write_lcd(c + '0');
write_lcd('C');
}
void main()
{
unsigned char i;
init_lcd();
while(1)
{
ADC_CONTR = 0x90; //使能ADC转换
delay(100); //延时等待转换完成
if(ADC_CONTR & 0x10) //判断转换是否完成
{
temp = ADC_RES;
temp = (temp * 330) / 1024; //将ADC转换的值转换为温度值
display_temp();
}
delay(1000); //每隔1s读取一次温度
}
}
```
这个程序使用了51单片机的内置ADC模块来读取温度传感器的输出,然后将转换的结果通过LCD显示出来。需要注意的是,该程序为简单示例程序,具体实现方式可能会因具体的硬件环境而有所不同。
51单片机温度显示代码
以下是51单片机温度显示的代码,使用的是LM35温度传感器:
```
#include <reg52.h> //头文件
#define uchar unsigned char
#define uint unsigned int
sbit DQ = P1^4; //定义DS18B20的数据引脚
uchar code table[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90}; //数码管显示0~9的编码
void delay(uint z) //延时函数
{
uint x, y;
for(x = z; x > 0; x--)
for(y = 110; y > 0; y--);
}
uchar Init_DS18B20() //DS18B20初始化函数
{
uchar i;
DQ = 1;
delay(2);
DQ = 0;
delay(80);
DQ = 1;
delay(10);
i = DQ;
delay(10);
return i;
}
void Write_DS18B20(uchar dat) //写一个字节
{
uchar j;
for(j = 0; j < 8; j++)
{
DQ = 0;
DQ = dat & 0x01;
delay(5);
DQ = 1;
dat >>= 1;
}
}
uchar Read_DS18B20() //读一个字节
{
uchar i, j, dat = 0;
for(j = 8; j > 0; j--)
{
DQ = 0;
DQ = 1;
i = DQ;
delay(5);
dat <<= 1;
if(i == 1) dat |= 0x01;
delay(10);
}
return dat;
}
void Convert_DS18B20() //温度转换函数
{
Init_DS18B20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
}
int Read_Temperature() //读取温度
{
uchar temp_low, temp_high;
int temperature;
Init_DS18B20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
temp_low = Read_DS18B20();
temp_high = Read_DS18B20();
temperature = temp_high;
temperature <<= 8;
temperature |= temp_low;
temperature = temperature * 0.625;
return temperature;
}
void Display_Temperature(int temperature) //数码管显示函数
{
uchar shiwei, gewei, temp;
temp = temperature / 10;
shiwei = temp % 10;
gewei = temperature % 10;
P0 = 0x7f;
P2 = 0xfe;
P0 = table[shiwei];
delay(1000);
P0 = 0x7f;
P2 = 0xfd;
P0 = table[gewei];
delay(1000);
}
void main()
{
int temperature;
while(1)
{
Convert_DS18B20();
delay(200);
temperature = Read_Temperature();
Display_Temperature(temperature);
}
}
```
该代码需要使用LM35温度传感器,并且连接方式如下:
- LM35的VCC引脚连接到单片机的5V电源引脚。
- LM35的GND引脚连接到单片机的GND引脚。
- LM35的OUT引脚连接到单片机的P2.0引脚。