基于51单片机的热敏电阻温度计的代码
时间: 2023-07-29 19:07:10 浏览: 66
以下是基于51单片机的热敏电阻温度计的代码示例:
```c
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P1^0; //定义DS18B20的信号线
uchar code table[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //定义显示数码管0-9及A-F的值
void delay(uint z) //毫秒级延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
uchar ReadTemperature() //读取温度函数
{
uchar c,s;
DQ=0;
delay(1);
DQ=1;
delay(1);
s=DQ;
delay(1);
if(s==0)
{
DQ=0;
delay(1);
DQ=1;
delay(1);
c=DQ;
delay(50);
return c;
}
else
{
return 0xff;
}
}
void Display(uchar num) //数码管显示函数
{
P0=table[num];
delay(10);
}
void main()
{
uchar temperature,speed,i,j;
while(1)
{
temperature=ReadTemperature(); //读取温度
if(temperature!=0xff) //如果读取温度成功
{
if(temperature==0) //如果温度为零
{
Display(0); //数码管显示0
}
else
{
speed=temperature/10; //计算十位数
if(speed>0) //如果十位数大于0
{
Display(speed); //数码管显示十位数
}
else
{
Display(16); //数码管显示A(空位)
}
speed=temperature%10; //计算个位数
Display(speed); //数码管显示个位数
}
}
}
}
```
该代码中,使用了DS18B20温度传感器进行温度检测,将检测结果通过数码管显示出来。其中,ReadTemperature()函数用于读取温度,Display()函数用于数码管显示,delay()函数为毫秒级延时函数。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)