基于51单片机的热敏电阻并且当温度超过 37°时,蜂鸣器响的模拟电子温度计的代码
时间: 2024-01-24 20:05:24 浏览: 157
以下是基于51单片机的热敏电阻并且当温度超过37℃时,蜂鸣器响的模拟电子温度计的代码:
```
#include<reg52.h> // 头文件
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
sbit D1=P2^0;
sbit D2=P2^1;
sbit D3=P2^2;
sbit D4=P2^3;
sbit beep=P2^7;
uchar temp;
void delay(uint z) // 延时函数
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void display(uchar num) // 数码管显示函数
{
uchar i;
D4=1;
P0=table[num/1000];
delay(1);
D4=0;
D3=1;
P0=table[num%1000/100];
delay(1);
D3=0;
D2=1;
P0=table[num%100/10];
delay(1);
D2=0;
D1=1;
P0=table[num%10];
delay(1);
D1=0;
}
void main() // 主函数
{
while(1)
{
AD0=1;
temp=AD0; // 读取AD转换值
if(temp>148) // 假设148对应37℃
beep=1; // 蜂鸣器响
else
beep=0; // 蜂鸣器不响
display(temp); // 显示温度值
}
}
```
注:需根据具体硬件电路及热敏电阻特性进行调整。
阅读全文
相关推荐

















