51单片机温度报警器用热敏电阻的程序
时间: 2023-08-13 21:09:10 浏览: 70
以下是51单片机温度报警器使用热敏电阻的程序示例,仅供参考:
```c
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit beep=P2^3; //蜂鸣器
void Delay10ms(uint c) //延时函数
{
uchar a,b;
for(;c>0;c--)
for(b=199;b>0;b--)
for(a=1;a>0;a--);
}
uchar GetADCResult() //获取ADC结果
{
uchar i,result=0;
for(i=0;i<8;i++)
{
result=result>>1;
P1_7=1;
Delay10ms(20); //等待电压稳定
P1_7=0;
if(P1_6==1)
result=result|0x80;
Delay10ms(20); //等待ADC转换
}
return result;
}
void main()
{
uchar temp;
while(1)
{
temp=GetADCResult(); //获取ADC结果
if(temp>0x90) //判断温度是否超过阈值
beep=0; //超过阈值时蜂鸣器响铃
else
beep=1;
}
}
```
在这个程序中,我们首先使用了一个延时函数`Delay10ms()`来等待电压稳定和ADC转换完成。然后,我们使用了`GetADCResult()`函数来获取ADC结果,该函数通过判断P1.6引脚的电平来获取电阻值。最后,我们可以根据温度阈值来控制蜂鸣器的响铃。当温度超过阈值时,蜂鸣器响铃,否则蜂鸣器不响铃。请注意,这只是一个示例程序,您可以根据您的具体应用需求进行修改。