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);
}
Definitely wrong. No idea what you want to achieve.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)
Also wrong. LM35 gives °C output without substracting a value.temp = temp - 273; // Converts Farenheit to Celcius.
Thank you very much !Definitely wrong. No idea what you want to achieve.
View attachment 184288
Also wrong. LM35 gives °C output without substracting a value.
I use the datasheet in first place, then application notes provided by the manufacturer.When in doubt, it seemed to me to check in the reference document, the data sheet
Finally it is very confusing, because datasheet (most popular is Texas Instruments or National Conductor) never indicate pinout numbers.I use the datasheet in first place, then application notes provided by the manufacturer.
Then maybe other documents.
Klaus
Hi,Finally it is very confusing, because datasheet (most popular is Texas Instruments or National Conductor) never indicate pinout numbers.
Thanks Paul !hello,
LM35DZ don't measure negative temp ... give direct °C
LM135 can do it ...
Yes it is true. A recent TI datasheet shows, at chapter 5 the pinout with pin number & name.Hi,
I don´t kow what you are talking about.
TI datasheet, page #5
National semiconductor page #2
I would have been very surprised if they were not providing such basic informations.
I mean, they want to sell these devices to earn money, so it would be counter productive...
Klaus
yes, there are several examples on the datasheet.Concerning negative values, I guess there's a special wiring with LM35 when using only a VCC of 5 V ?
Thanks for schematics.yes, there are several examples on the datasheet.
the easiest IMO, is the two diodes and pull down resistor
View attachment 184431
just note that for a correct measurement, you need to read both the VOUT and the GND of the LM35 (that goes to the diodes). but if needed you can omit the GND measurement and calculate with a fixed value.
Thanks for schematics. I added also a 150 nF capacitor between V+ (pin 1) and GND (pin 3) of LM35DZ. But with the following part of my code, with ADC channels 3 and channels 4, even I have a wrong positive temperature (see photos).yes, there are several examples on the datasheet.
the easiest IMO, is the two diodes and pull down resistor
View attachment 184431
just note that for a correct measurement, you need to read both the VOUT and the GND of the LM35 (that goes to the diodes). but if needed you can omit the GND measurement and calculate with a fixed value.
....
voltage_at_Vout_pin = ADC_Read(3); // Reads analog voltage at Vout pin of LM35DZ.
voltage_at_GND_pin = ADC_Read(4); // Reads analog voltage at GND pin of LM35DZ.
temp_Celsius = (voltage_at_Vout_pin - voltage_at_GND_pin) * 4.888; // Reads analog voltage and converts it to Celsius degrees (4.888 = 5000 mV / 1023). 5000 mV = 5V = VCC of MCU.
temp_Celsius = temp_Celsius / 10; // With LM35, each 10 mV output correspond to 1°C.
temp_Kelvin = (temp_Celsius * 9/5) + 32; // Converts Celsius degrees to Kelvin degrees.
...
Dear Brian,Please tell us the types of variables you are using (char, int, float etc.).
This could simply be a problem of storing in the wrong kind of variable or missing an essential typecast.
All the "Voltage_at_" variables should be integers and all the "temp_" ones should be floats.
Brian.
void main()
{
PORTB = 0; // Initialisation du PORT B à 0.
PORTC = 0; // Initialisation du PORT C à 0.
PORTD = 0; // Initialisation du PORT D à 0.
ANSEL = 0b00011000; // b3 = 1 (ANS3) : sets pin 5 (AN3) as analog input.
// b4 = 1 (ANS4) : sets pin 7 (AN4) as analog input.
//Or
//ANSEL = 24;
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
{
int voltage_at_Vout_pin, voltage_at_GND_pin;
float temp;
char txt[13];
ADC_initialization();
TRISD = 0x00;
while(1)
{
voltage_at_Vout_pin = ADC_read(3); // Reads analog voltage at Vout pin of LM35DZ.
voltage_at_GND_pin = ADC_read(4); // Reads analog voltage at GND pin of LM35DZ.
temp = (voltage_at_Vout_pin - voltage_at_GND_pin) * 4.888; // Reads analog voltage and converts it to Celsius degrees (4.888 = 5000 mV / 1023). 5000 mV = 5V = VCC of MCU.
temp = temp / 10; // With LM35, each 10 mV output correspond to 1°C.
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_v1(temp, &txt[0], 2);
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);
}
Merci Susan.A couple of things straight off (even without knowing the data types - although that really is important!).
You are using a very small MCU so floating point maths is all done via library code that is slow and takes up a lot of space. Try to use scaled integers where ever possible.
What are the values you are getting from the ADC?
Also the "9/5" is probably going to cause a problem. It could well be that it is being calculated by the compiler (or the run-time - it really doesn't matter) to be 1 as it will be seeing this an integer division.
Susan
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?