-1
我已将LM35温度传感器连接到AN1的模拟通道,然后通过PIC16F887中的串行RS232传输温度。我已经读过温度值,但这个值经常变化为31和32,同样当触摸LM35温度变化非常快时,如32,33等。如何控制恒温值。这是我的代码,请任何人帮助我。PIC16F887读取模数转换器中的问题LM35温度值经常变化
#include <htc.h>
void main(void)
{
TRISA1 = 1;
ANS1 = 1;
OSCCON = 0x78; // OSCILLATOR CONTROL REGISTER
TXSTA = 0x26;
RCSTA = 0x90;
SPBRG = 10;
ADCON1 = 0x80;
unsigned int current_temp, initial_temp = temperature();
transmit_char(initial_temp);
flag = 0;
while (1)
{
current_temp = temperature();
if((current_temp == (initial_temp + 1)) || (current_temp == (initial_temp - 1)))
{
flag = 1;
}
if(flag == 1)
{
flag = 0;
transmit_char(current_temp);
initial_temp = current_temp;
}
}
}
int temperature(void)
{
ADCON0 = 0xC5;
GODONE = 1;
while(GODONE);
int temp;
temp = (ADRESH << 8) + ADRESL;
temp = temp/2;
return temp;
}
一些变化是正常的。阅读数据表并确保您的硬件是正确的(噪音等)。从描述中,我没有看到这是一个软件问题。 – Olaf