Hi susuying,
LM35 is a temperature proportional voltage out sensor, (get the datasheet).
You have to read Voltage of Vout of LM35 and convert it to temperature. (+ 10.0 mV/°C scale factor). First you have implement that in your code. Then assign temperature to t, after that above code will work.
google the topic, link to same kind projects are;
Interfacing LM35 Temperature Sensor with PIC Microcontroller. | eXtreme Electronics
Welcome to Cytron Technologies - Robot . Head to Toe : PIC Training Malaysia, PIC Microcontroller
C:\Users\Odin\Desktop\babe\coding\temperature_sens or2.c:76:Error [1105] symbol '[B]ADON[/B]' has not been defined
C:\Users\Odin\Desktop\babe\coding\temperature_sens or2.c:76:Error [1101] [B]lvalue required[/B]
C:\Users\Odin\Desktop\babe\coding\temperature_sens or2.c:78:Error [1105] symbol [B]'GO_DONE[/B]' has not been defined
C:\Users\Odin\Desktop\babe\coding\temperature_sens or2.c:78:Error [1101] [B]lvalue[/B] required
C:\Users\Odin\Desktop\babe\coding\temperature_sens or2.c:80:Error [1105] symbol '[B]GO_DONE[/B]' has not been defined
C:\Users\Odin\Desktop\babe\coding\temperature_sens or2.c:82:Error [1105] symbol '[B]ADON[/B]' has not been defined
C:\Users\Odin\Desktop\babe\coding\temperature_sens or2.c:82:Error [1101] [B]lvalue[/B] required
C:\Users\Odin\Desktop\babe\coding\temperature_sens or2.c:91:Warning [2058] [B]call of function without prototype[/B]
C:\Users\Odin\Desktop\babe\coding\temperature_sens or2.c:104:Warning [2058] [B]call of function without prototype[/B]
C:\Users\Odin\Desktop\babe\coding\temperature_sens or2.c:105:Warning [2052] unexpected return value
Halting build on first failure as requested.
----------------------------------------------------------------------
Debug build of project `C:\Users\Odin\Desktop\babe\coding\temperature sensor2.mcp' failed.
Language tool versions: mpasmwin.exe v5.35, mplink.exe v4.35, mcc18.exe v3.35
Preprocessor symbol `__DEBUG' is defined.
Tue Mar 15 09:56:29 2011
#include "p18f4550.h"
#include "adc.h"
#include "delays.h"
#pragma config WDT = OFF
#define LEDPin1 LATAbits.LATA3 //Define LEDPin as PORT A Pin 1
#define LEDTris1 TRISAbits.TRISA3 //Define LEDTris as TRISA Pin 1
#define LEDPin2 LATAbits.LATA5 //Define LEDPin as PORT A Pin 2
#define LEDTris2 TRISAbits.TRISA5 //Define LEDTris as TRISA Pin 2
#define LEDPin3 LATEbits.LATE0 //Define LEDPin as PORT E Pin 3
#define LEDTris3 TRISEbits.TRISE0 //Define LEDTris as TRISE Pin 3
#define LEDPin4 LATEbits.LATE1 //Define LEDPin as PORT E Pin 4
#define LEDTris4 TRISEbits.TRISE1 //Define LEDTris as TRISE Pin 4
#define LEDPin5 LATEbits.LATE2 //Define LEDPin as PORT E Pin 5
#define LEDTris5 TRISEbits.TRISE2 //Define LEDTris as TRISE Pin 5
void main(void)
{
double temperature=0.25;
/**************** CONFIGURATION THE OSCILLATOR FOR PIC *********************/
//OSCCONbits.SCS1=1; // Use of the internal oscillator at 8Mhz frequency
//OSCCONbits.SCS0=1; // Utilisation for the internal osciallator in PIC
//OSCCONbits.IRCF0=1; //
//OSCCONbits.IRCF1=1; // Frequency oscillator at 8Mhz
//OSCCONbits.IRCF2=1; //
/****************************************************************************/
//TRISBbits.TRISB0=1; // Configure RB0 as input for the Temperature sensor
//TRISBbits.TRISB1=0; // Configure RB1 as output for the LED.
//TRISBbits.TRISB2=0; // Configure RB2 as output for the LED.
//TRISBbits.TRISB3=0; // Configure RB3 as output for the LED.
//TRISBbits.TRISB4=0; // Configure RB4 as output for the LED.
//TRISBbits.TRISB5=0; // Configure RB5 as output for the LED.
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_4_TAD,
ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS,
ADC_2ANA); // Open the ADC in the Channel 0 (RA0; AN0,RA1;RN1 as input pin) with +5V reference level (=Vdd) and 0V as Vss. AN0-AN1 as analog while AN3-AN15 as digital pin
while(1)
{
// Delay10TCYx( 5 );
ConvertADC();
while(BusyADC());
temperature=ReadADC();
if (temperature=0.24)
{
LEDTris1 = 0;//Set LED Pin data direction to OUTPUT
LEDPin1 = 1;//Set LED Pin
while(1)
{
LEDPin1 = ~LEDPin1;//Toggle LED Pin
Delay10KTCYx(25);//Delay 250K cycles (1 second at 1MHz since each instruction takes 4 cycles)
}
}
else if (temperature=0.25)
{
LEDTris2 = 0;//Set LED Pin data direction to OUTPUT
LEDPin2 = 1;//Set LED Pin
while(1)
{
LEDPin2 = ~LEDPin2;//Toggle LED Pin
Delay10KTCYx(25);//Delay 250K cycles (1 second at 1MHz since each instruction takes 4 cycles)
}
}
else if (temperature=0.26)
{
LEDTris3 = 0;//Set LED Pin data direction to OUTPUT
LEDPin3 = 1;//Set LED Pin
while(1)
{
LEDPin3 = ~LEDPin3;//Toggle LED Pin
Delay10KTCYx(25);//Delay 250K cycles (1 second at 1MHz since each instruction takes 4 cycles)
}
}
else if (temperature=0.27)
{
LEDTris4 = 0;//Set LED Pin data direction to OUTPUT
LEDPin4 = 1;//Set LED Pin
while(1)
{
LEDPin4 = ~LEDPin4;//Toggle LED Pin
Delay10KTCYx(25);//Delay 250K cycles (1 second at 1MHz since each instruction takes 4 cycles)
}
}
else if (temperature=0.28)
{
LEDTris5 = 0;//Set LED Pin data direction to OUTPUT
LEDPin5 = 1;//Set LED Pin
while(1)
{
LEDPin5 = ~LEDPin5;//Toggle LED Pin
Delay10KTCYx(25);//Delay 250K cycles (1 second at 1MHz since each instruction takes 4 cycles)
}
}
}
CloseADC(); // In reality, the ADC is never closed due to the while(1)...
}
I think you have a range/resolution problem for your IF statements (too accurate, what if temperature is 0.250001?
Instead of using a finite value for each level comparison, use a range.
Something like...
if (temp <= 0.24)
Blink LED1
else if ((temp > 0.24) && (temp <= 0.25))
Blink LED2
else if ((temp > 0.25) && (temp <= 0.26))
Blink LED3
else if ((temp > 0.26) && (temp <= 0.27))
Blink LED4
else if (temp > 0.27)
Blink LED5
Also, notice the continuity between the ranges... example, LED2 include 0.25, but LED3 takes everything just above 0.25, up to, and including 0.26.
while(1)
{
// Delay10TCYx( 5 );
void ConvertADC(void);
char BusyADC(void);
temperature=ReadADC();}
closeADC();
}
susuying, I haven't done PIC programming in over 6 years, so I'm definitely not the one to ask about those finer details. Personally, I'd start by finding some example code, running it, and digging into it to understand how the code functions. Some simple examples on ADC sampling should come with the compiler package, I'd imagine. Failing that, I'd start searching the 'net to find code examples on your same target device (PIC MCU).
There are a lot of PIC folks on these forums, so it might be worth your time to hunt around the threads for other people with PIC code.
Sorry that I can't be more help at the detail level.
#include <p18f4550.h>
#include <adc.h>
#include <delays.h>
#pragma config WDT = OFF
#define LEDPin1 LATAbits.LATA3 //Define LEDPin as PORT A Pin 1
#define LEDTris1 TRISAbits.TRISA3 //Define LEDTris as TRISA Pin 1
#define LEDPin2 LATAbits.LATA5 //Define LEDPin as PORT A Pin 2
#define LEDTris2 TRISAbits.TRISA5 //Define LEDTris as TRISA Pin 2
#define LEDPin3 LATEbits.LATE0 //Define LEDPin as PORT E Pin 3
#define LEDTris3 TRISEbits.TRISE0 //Define LEDTris as TRISE Pin 3
#define LEDPin4 LATEbits.LATE1 //Define LEDPin as PORT E Pin 4
#define LEDTris4 TRISEbits.TRISE1 //Define LEDTris as TRISE Pin 4
#define LEDPin5 LATEbits.LATE2 //Define LEDPin as PORT E Pin 5
#define LEDTris5 TRISEbits.TRISE2 //Define LEDTris as TRISE Pin 5
//prototype
void openADC(void);
void ConvertADC( void );
char BusyADC( void );
int ReadADC( void );
void CloseADC( void );
void initializeA(void);
void initializeE(void);
// openADC definition
void openADC()
{
ADCON1=0b00001101; //AN2=1 (voltage reference configuration bit), AN0.AN1 is the analog I/O while AN2-AN15 is the digital I/o
ADCON2=0b10010010; //right justified, 4 TAD, FOSC/32
}
void initializeA()
{
PORTA = 0;
LATA = 0;
TRISA=0b01010111; //assign port A3 and A5 as the output pin
}
void initializeE()
{
PORTE = 0;
LATE = 0;
TRISE=0b00000000; //assign port E0,E1 and E2 as the output pin
}
//start main code
void main()
{
double temperature;
initializeA(); //call port A initialization
initializeE(); //call port E initialization
openADC();//call adc condiguration
LATA = 0b11111111; //turn off all lights on LATA
LATE = 0b11111111; //turn off all lights on LATE
// OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_4_TAD,ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS,ADC_2ANA); // Open the ADC in the Channel 0 (RA0; AN0,RA1;RN1 as input pin) with +5V reference level (=Vdd) and 0V as Vss. AN0-AN1 as analog while AN3-AN15 as digital pin
//ADConversion start
ADCON0=0b00000001; //ADCON0 in channel 1,ADOC=1 (A/D converter module is enabled)
Delay10TCYx(5); // NOP
ConvertADC();
while( BusyADC() );
temperature = ReadADC();
//ADConversion end
// Delay10TCYx( 5 );
// ConvertADC();
// while(BusyADC());
// temperature=ReadADC();
CloseADC(); // In reality, the ADC is never closed due to the while(1)...
if (temperature<=0.24)
{
//LEDTris1 = 0;//Set LED Pin data direction to OUTPUT
LEDPin1 = 1;//Set LED Pin
while(1)
{
LEDPin1 = ~LEDPin1;//Toggle LED Pin
Delay10KTCYx(25);//Delay 250K cycles (1 second at 1MHz since each instruction takes 4 cycles)
}
}
else if ((temperature>0.24)&&(temperature<=0.25))
{
//LEDTris2 = 0;//Set LED Pin data direction to OUTPUT
LEDPin2 = 1;//Set LED Pin
while(1)
{
LEDPin2 = ~LEDPin2;//Toggle LED Pin
Delay10KTCYx(25);//Delay 250K cycles (1 second at 1MHz since each instruction takes 4 cycles)
}
}
else if ((temperature>0.25)&&(temperature<=0.26))
{
//LEDTris3 = 0;//Set LED Pin data direction to OUTPUT
LEDPin3 = 1;//Set LED Pin
while(1)
{
LEDPin3 = ~LEDPin3;//Toggle LED Pin
Delay10KTCYx(25);//Delay 250K cycles (1 second at 1MHz since each instruction takes 4 cycles)
}
}
else if ((temperature>0.26)&&(temperature<=0.27))
{
//LEDTris4 = 0;//Set LED Pin data direction to OUTPUT
LEDPin4 = 1;//Set LED Pin
while(1)
{
LEDPin4 = ~LEDPin4;//Toggle LED Pin
Delay10KTCYx(25);//Delay 250K cycles (1 second at 1MHz since each instruction takes 4 cycles)
}
}
else if ((temperature>0.27)&&(temperature<=0.28))
{
//LEDTris5 = 0;//Set LED Pin data direction to OUTPUT
LEDPin5 = 1;//Set LED Pin
while(1)
{
LEDPin5 = ~LEDPin5;//Toggle LED Pin
Delay10KTCYx(25);//Delay 250K cycles (1 second at 1MHz since each instruction takes 4 cycles)
}
}
}
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?