Eric_O
Advanced Member level 4
When measuring room temperature (approximatively between 21°C to 25°C) with PIC 16F887 and LM35DZ, LCD displays a room temperature of 100°C when room temperature is 21°C.
LM35 GND (pin 3) : connected to GND of MCU.
LM35 output (pin 2) : connected to ADC 3 of MCU and also to VCC thru a 2.2 K resistor.
LM35 output (pin 1) : not connected (instead of connected to VCC in some cases)
Regarding the datasheet of LM35 I wrote the code as follow and respected the math formulas.
Could somebody explain me why the displayed temperature is so inconsistent ?
Thanks !
LM35 GND (pin 3) : connected to GND of MCU.
LM35 output (pin 2) : connected to ADC 3 of MCU and also to VCC thru a 2.2 K resistor.
LM35 output (pin 1) : not connected (instead of connected to VCC in some cases)
Regarding the datasheet of LM35 I wrote the code as follow and respected the math formulas.
Code:
void main()
{
PORTB = 0; // Initialisation du PORT B à 0.
PORTC = 0; // Initialisation du PORT C à 0.
PORTD = 0; // Initialisation du PORT D à 0.
ANSEL = 0b00001000; // b3 = 1 (ANS3) : sets pin 5 (AN3) as analog input.
ANSELH = 0b00000000;
TRISB = 0b00000000; // PORT B : b0 à b7 configurés en sortie.
TRISC = 0b00000000; // PORT C : b0 à b7 configurés en sortie.
TRISD = 0b00000000; // PORT D : b0 à b7 configurés en sortie.
C1ON_bit = 0; // CMC1CON register > b7 > C1ON bit = 0 > Disable comparator 1.
C2ON_bit = 0; // CMC2CON register > b7 > C1ON bit = 0 > Disable comparator 2.
SCS_bit = 0; // OSCCON register > b0 > SCS bit = 0 > External oscillator (quartz) is used as a clock source.
LCD_Init_v4 (void);
pointeur_de_char = &texte[0]; // pointeur_de_char pointe sur le premier élément du tableau "texte", soit texte[0].
// Autrement dit, pointeur_de_char contient l'adresse (&) de texte[0].
do
{
float adc;
float volt, temp;
char txt[13];
ADC_initialization();
TRISD = 0x00;
while(1)
{
adc = (ADC_read(3)); // Reads analog values.
volt = adc * 4.88281; // Converts it into the voltage.
temp = volt / 10.0; // Gets the temperature values.
temp = temp - 273; // Converts Farenheit to Celcius.
StrConstRamCpy(pointeur_de_char, "Temperature ... "); // OK.
LCD_Write_String_At(0, 0, pointeur_de_char); // OK. Voir dans void LCD_Write_String(char *msg), le while(*(msg + k) > 0).
float2ascii_v2(temp, &txt[0], 2);
LCD_Write_String_At(1, 0, &txt[0]); // Write txt in 2nd row, starting at 1st digit.
LCD_Write_String_At(1, 13, " °C"); // Write " °C" in 2nd row, starting at 14th digit.
delay_ms(3000);
}
}
while(1);
}
Could somebody explain me why the displayed temperature is so inconsistent ?
Thanks !