Problem with PIC18F4550 with the LM35

Status
Not open for further replies.

susuying

Newbie level 4
Joined
Mar 14, 2011
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,433
hey,
i currently use the LM35 sensor to detect the temperature changes. the microcontroller that i use is PIC18F4550, and the output detector is just the LED.
i using 5 LED as the output.
Below is my coding, is there any problem?


void main(void)
{
TRISB=0B00000000
#define LED1 PortBbits.RB0
if (t=25)
{
LEC1=1; //will make port B0 high
}
else
{
LED1=0; //will make port B0 low
}

#define LED2 PortBbits.RB1

if(t=26)
{
LEC2=1; //will make port B1 high
}
else
{
LED2=0; //will make port B1 low
}

#define LED3 PortBbits.RB2
if(t=27)
{
LED3=1; //will make port B2 high
}
else
{
LED3=0; //will make port B2 low
}

#define LED4 PortBbits.RB3
if(t=28)
{
LED4=1; //will make port B3 high
}
else
{
LED4=0; //will make port B3 low
}
 

Off the cuff, here are my primary concerns:

1. Where do you read in the ADC value into your program? (I don't see anything being assigned to "t").

2. Where is your code to process the raw ADC value (0 to 65535 --> -10C to 50C, or whatever the scaling is...)

3. Your program will run exactly once, so the LED will blip once, then the program ends. You should wrap the entire program in an endless while loop, like so:

while(1)
{
<your code goes here>
} //loop repeats until you reset the PIC, or disconnect power
 

Agreement with "enjunear", with one exception ,I have my doubt that even LEDs will blink once, just have a look how you have defined LEDs blinking:
{
LEC1=1; //will make port B0 high
}
else
{
LED1=0; //will make port B0 low
}

I dont see PORTB0 going high/low unless both are LED1.
 
this is the complete source code. But it still got error, i do not understand where is the error. Can is use if...else if in c compliler? can it read it?
/********************************************************************

LM35 Temperature Sensor INTERFACING TEST PROGRAM

---------------------------------------------------------
Simple Program to connect with LM temperature sensor using the
internal ADC of PIC MCU.

The program displays the current environment temperature on
LCD Module.

MCU: PIC18FXXXX Series from Microchip.
Compiler: HI-TECH C Compiler for PIC18 MCUs (Embedded C Compilers and Tools for Software Development: HI-TECH Software)


********************************************************************/
#include <p18f4550.h>
#include <math.h>

#define LED1 PortBbits.RB0
#define LED2 PortBbits.RB1
#define LED3 PortBbits.RB2
#define LED4 PortBbits.RB3
#define LED5 PortBbits.RB4

//Simple Delay Routine
void Wait(unsigned int delay)
{
for(;delay;delay--)
__delay_us(100);
}

//Function to Initialise the ADC Module
void ADCInit()
{
//We use default value for +/- Vref

//VCFG0=0,VCFG1=0
//That means +Vref = Vdd (5v) and -Vref=GEN

//Port Configuration
//We also use default value here too
//All ANx channels are Analog

/*
ADCON2

*ADC Result Right Justified.
*Acquisition Time = 2TAD
*Conversion Clock = 32 Tosc
*/

ADCON2=0b10001010;
}

//Function to Read given ADC channel (0-13)
unsigned int ADCRead(unsigned char ch)
{
if(ch>13) return 0; //Invalid Channel

ADCON0=0x00;

ADCON0=(ch<<2); //Select ADC Channel

ADON=1; //switch on the adc module

GODONE=1;//Start conversion

while(GODONE); //wait for the conversion to finish

ADON=0; //switch off adc

return ADRES;
}
void main()
{

//Initialize the ADC Module

ADCInit();


while(1)
{
unsigned int val; //ADC Value

unsigned int t; //Temperature


val=ADCRead(0); //Read Channel 0


Wait(1000);
}





TRISB=0B00000000 [ this code show an error]

else if (t=25)
{
LEC1=1; //will make port B0 high
}
else if
{
LED1=0; //will make port B0 low
}



else if(t=26)
{
LEC2=1; //will make port B1 high
}
else if
{
LED2=0; //will make port B1 low
}


else if(t=27)
{
LED3=1; //will make port B2 high
}
else if
{
LED3=0; //will make port B2 low
}


else if(t=28)
{
LED4=1; //will make port B3 high
}
else if
{
LED4=0; //will make port B3 low
}


else if(t=29)
{
LED5=1; //will make port B4 high
}
else if
{
LED5=0; //will make port B4 low
}

}

this is the error comment:
----------------------------------------------------------------------
Debug build of project `C:\Users\Odin\Desktop\babe\coding\temperature sensor2.mcp' started.
Language tool versions: mpasmwin.exe v5.35, mplink.exe v4.35, mcc18.exe v3.35
Preprocessor symbol `__DEBUG' is defined.
Mon Mar 14 12:02:23 2011
----------------------------------------------------------------------
Make: The target "C:\Users\Odin\Desktop\babe\coding\temperature_sensor2.o" is out of date.
Executing: "C:\MCC18\bin\mcc18.exe" -p=18F4550 /i"C:\MCC18\h" "temperature_sensor2.c" -fo="temperature_sensor2.o" -D__DEBUG -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa-
C:\Users\Odin\Desktop\babe\coding\temperature_sensor2.c:109:Error: syntax error
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.
Mon Mar 14 12:02:25 2011
----------------------------------------------------------------------
BUILD FAILED

---------- Post added at 05:05 ---------- Previous post was at 05:03 ----------

this is the complete source code. But it still got error, i do not understand where is the error. Can is use if...else if in c compliler? can it read it?
/************************************************** ******************

LM35 Temperature Sensor INTERFACING TEST PROGRAM

---------------------------------------------------------
Simple Program to connect with LM temperature sensor using the
internal ADC of PIC MCU.

The program displays the current environment temperature on
LCD Module.

MCU: PIC18FXXXX Series from Microchip.
Compiler: HI-TECH C Compiler for PIC18 MCUs (Embedded C Compilers and Tools for Software Development: HI-TECH Software)


************************************************** ******************/
#include <p18f4550.h>
#include <math.h>

#define LED1 PortBbits.RB0
#define LED2 PortBbits.RB1
#define LED3 PortBbits.RB2
#define LED4 PortBbits.RB3
#define LED5 PortBbits.RB4

//Simple Delay Routine
void Wait(unsigned int delay)
{
for(;delay;delay--)
__delay_us(100);
}

//Function to Initialise the ADC Module
void ADCInit()
{
//We use default value for +/- Vref

//VCFG0=0,VCFG1=0
//That means +Vref = Vdd (5v) and -Vref=GEN

//Port Configuration
//We also use default value here too
//All ANx channels are Analog

/*
ADCON2

*ADC Result Right Justified.
*Acquisition Time = 2TAD
*Conversion Clock = 32 Tosc
*/

ADCON2=0b10001010;
}

//Function to Read given ADC channel (0-13)
unsigned int ADCRead(unsigned char ch)
{
if(ch>13) return 0; //Invalid Channel

ADCON0=0x00;

ADCON0=(ch<<2); //Select ADC Channel

ADON=1; //switch on the adc module

GODONE=1;//Start conversion

while(GODONE); //wait for the conversion to finish

ADON=0; //switch off adc

return ADRES;
}
void main()
{

//Initialize the ADC Module

ADCInit();


while(1)
{
unsigned int val; //ADC Value

unsigned int t; //Temperature


val=ADCRead(0); //Read Channel 0


Wait(1000);
}





TRISB=0B00000000 [ this code show an error]

else if (t=25)
{
LED1=1; //will make port B0 high
}
else if
{
LED1=0; //will make port B0 low
}



else if(t=26)
{
LED2=1; //will make port B1 high
}
else if
{
LED2=0; //will make port B1 low
}


else if(t=27)
{
LED3=1; //will make port B2 high
}
else if
{
LED3=0; //will make port B2 low
}


else if(t=28)
{
LED4=1; //will make port B3 high
}
else if
{
LED4=0; //will make port B3 low
}


else if(t=29)
{
LED5=1; //will make port B4 high
}
else if
{
LED5=0; //will make port B4 low
}

}


//the error is it because i din not define the LED??//
this is the error comment:
----------------------------------------------------------------------
Debug build of project `C:\Users\Odin\Desktop\babe\coding\temperature sensor2.mcp' started.
Language tool versions: mpasmwin.exe v5.35, mplink.exe v4.35, mcc18.exe v3.35
Preprocessor symbol `__DEBUG' is defined.
Mon Mar 14 12:02:23 2011
----------------------------------------------------------------------
Make: The target "C:\Users\Odin\Desktop\babe\coding\temperature_sen sor2.o" is out of date.
Executing: "C:\MCC18\bin\mcc18.exe" -p=18F4550 /i"C:\MCC18\h" "temperature_sensor2.c" -fo="temperature_sensor2.o" -D__DEBUG -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa-
C:\Users\Odin\Desktop\babe\coding\temperature_sens or2.c:109:Error: syntax error
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.
Mon Mar 14 12:02:25 2011
----------------------------------------------------------------------
BUILD FAILED
 

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

Thanks
 

There are two ways your problem could be resolved:
First someone else does it for you.
Second you do it yourself.
If I were in your position I'd prefer doing it myself. Firstly in the main window of MPLAB, there is an option where it is saying "DEBUG" change it to release.
Your one compile time error is gone.
Open up Microchip C18, manual and look for error 109, its a syntax error find where is the error, rectify it and hopefully your code will compile.

There is a working code for LM35, by Simon Inns.
Search Google for his name, go to his website something about waiting for friday, there is a 8x16 clock and temp sensor. Thats the one you should have a look at, it has complete source codes.
 


hey,
i'm new in this so i don really understand what is mean by u?
Cant we just use the LM35 to detect the temperature changes directly? Do we need to set the value 1st in the coding part? if i assign a temperature t to the coding then will it influence the output from LM35?
sorry if i ask silly question, cause i really confusing.
Thanks

---------- Post added at 03:00 ---------- Previous post was at 01:30 ----------

this is my current coding. But i still get some error. Can someone tell me how to solve the error?
How to define the adc part?
/********************************************************************

LM35 Temperature Sensor INTERFACING TEST PROGRAM

---------------------------------------------------------
Simple Program to connect with LM temperature sensor using the
internal ADC of PIC MCU.

The program displays the current environment temperature on LED


********************************************************************/
#include <p18f4550.h>
#include <math.h>
#include <adc.h>
#include <stdlib.h>


//*LEDs defination
#define LED1_PORT PORTB
#define LED2_PORT PORTB
#define LED3_PORT PORTB
#define LED4_PORT PORTB
#define LED5_PORT PORTB
unsigned int t; //Temperature

//Function to Initialize the ADC Module

void initADC(void)
{
//We use default value for +/- Vref

//VCFG0=0,VCFG1=0
//That means +Vref = Vdd (5v) and -Vref=GEN

//Port Configuration
//We also use default value here too
//All ANx channels are Analog

/*
ADCON2

*ADC Result Right Justified.
*Acquisition Time = 2TAD
*Conversion Clock = 32 Tosc
*/


ADCON2=0b10001010;
}




//Function to Read given ADC channel (0-13)
unsigned int ADCRead(unsigned char ch)

{
if(ch>13) return 0; //Invalid Channel

ADCON0=0;

ADCON0=(ch<<2); //Select ADC Channel

ADON=1; //switch on the adc module

GO_DONE=1;//Start conversion

while(GO_DONE); //wait for the conversion to finish

ADON=0; //switch off adc

return ADRES;
}
void main()
{

//Initialize the ADC Module

ADCInit();


while(1)
{
unsigned int val; //ADC Value




val=ADCRead(0); //Read Channel 0


Wait(1000);
return t;
}




TRISB=0b0000000;//All port B is set as output

if (t=250)
{
LED1_PORT=1; //LED1 only activated only for temperature equal to 25'c
LED2_PORT=0; //LED2 will not activated
LED3_PORT=0; //LED3 will not activated
LED4_PORT=0; //LED4 will not activated
LED5_PORT=0; //LED5 will not activated
}
else if (t=260)
{
LED1_PORT=0; //LED1 will not activated
LED2_PORT=1; //LED2 only activated only for temperature equal to 26'c
LED3_PORT=0; //LED3 will not activated
LED4_PORT=0; //LED4 will not activated
LED5_PORT=0; //LED5 will not activated
}
else if (t=270)
{
LED1_PORT=0; //LED1 will not activated
LED2_PORT=0; //LED2 will not activated
LED3_PORT=1; //LED3 only activated only for temperature equal to 27'c
LED4_PORT=0; //LED4 will not activated
LED5_PORT=0; //LED5 will not activated
}
else if(t=280)
{
LED1_PORT=0; //LED1 will not activated
LED2_PORT=0; //LED2 will not activated
LED3_PORT=0; //LED3 will not activated
LED4_PORT=1; //LED4 only activated only for temperature equal to 28'c
LED5_PORT=0; //LED5 will not activated
}
else if(t=290)
{
LED1_PORT=0; //LED1 will not activated
LED2_PORT=0; //LED2 will not activated
LED3_PORT=0; //LED3 will not activated
LED4_PORT=0; //LED4 will not activated
LED5_PORT=1; //LED5 only activated only for temperature equal to 29'c
}

}


Here is the error:
----------------------------------------------------------------------
Debug build of project `C:\Users\Odin\Desktop\babe\coding\temperature sensor2.mcp' started.
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:27 2011
----------------------------------------------------------------------
Make: The target "C:\Users\Odin\Desktop\babe\coding\temperature_sensor2.o" is out of date.
Executing: "C:\MCC18\bin\mcc18.exe" -p=18F4550 "temperature_sensor2.c" -fo="temperature_sensor2.o" -D__DEBUG -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa-
C:\Users\Odin\Desktop\babe\coding\temperature_sensor2.c:76:Error [1105] symbol 'ADON' has not been defined
C:\Users\Odin\Desktop\babe\coding\temperature_sensor2.c:76:Error [1101] lvalue required
C:\Users\Odin\Desktop\babe\coding\temperature_sensor2.c:78:Error [1105] symbol 'GO_DONE' has not been defined
C:\Users\Odin\Desktop\babe\coding\temperature_sensor2.c:78:Error [1101] lvalue required
C:\Users\Odin\Desktop\babe\coding\temperature_sensor2.c:80:Error [1105] symbol 'GO_DONE' has not been defined
C:\Users\Odin\Desktop\babe\coding\temperature_sensor2.c:82:Error [1105] symbol 'ADON' has not been defined
C:\Users\Odin\Desktop\babe\coding\temperature_sensor2.c:82:Error [1101] lvalue required
C:\Users\Odin\Desktop\babe\coding\temperature_sensor2.c:91:Warning [2058] call of function without prototype
C:\Users\Odin\Desktop\babe\coding\temperature_sensor2.c:104:Warning [2058] call of function without prototype
C:\Users\Odin\Desktop\babe\coding\temperature_sensor2.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
----------------------------------------------------------------------
BUILD FAILED
 

The solution to your problem lies within the error report and code that you have posted :

Code:
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

I have made the relevant areas "BOLD" just have a look at them, apparently ADON should be ADCON.
You need to define GO_Done.
You still have the debug option selected, I thought you would have changed it to release by now.
 

Hi susuying,

Codes in above posted links are in HI-TECH C, so you should use that compiler. If you use C18, study the steps in those codes and write your own code using C18.

See "A/D CONVERTER FUNCTIONS" section in "MPLAB C18 C compiler libraries.pdf" file (in "doc" folder of C18 installation directory)
 

Code:
#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)...
}

Hey, i get my code modified. It can successfully run by C18. But when i test with my hardware, it keep blink 1 of the LED with the wrong input. Example: when i set the temperature is 0.25 and it should be running the 2nd LED, but end up it just run the 3rd LED.

please help!!
 
Last edited:

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.
 

Hi enjunear,
Thanks a lot for the information. it really helps. Can i ask you about the adc part?
the way i write my adc part is correct?

---------- Post added at 04:54 ---------- Previous post was at 03:49 ----------

Hi,

can i know how this run? How i going to write it definition?
Code:
    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.
 


is ok enjunear.Thanks for ur help again.

---------- Post added at 05:21 ---------- Previous post was at 05:18 ----------

Code:
#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)
    }

    }
    
}

Hi,

i have assign the prototype and the definition properly and it can compile successfully.But why when i test it with my hardware, all the LED is blinking?
Any part of my coding is wrong?

How to troubleshoot the ADC part?how i going to know which part i need to improve again?

Please help. Thanks
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…