Pic18f1220 adc

Discussion in 'Embedded Systems and Microcontrollers' started by c_calvin, Aug 16, 2009.

  1. c_calvin

    Thread Starter New Member

    Aug 16, 2009
    4
    0
    Hi,
    I trying a simple program where the LED will turn on if the input to an ADC is above a certain voltage. I have connected some LEDs to PORTB of the microcontroller and i applied a 4V voltage directly to the ADC input which is AN0. Somehow the LED will keep on lighting even after i stop applying the 4V voltage to the ADC input. Is there anything wrong with my code? Pls help, thanks.

    Here is my code

    //program to turn on LED if voltage above the preset voltage-PIC18F1220

    #include <p18f1220.h>
    #include <delays.h>
    #pragma config WDT = OFF //watchdogtimer off
    #pragma config LVP = OFF //lowvoltageprogramming off
    #pragma config OSC = INTIO2 //select internal oscillator
    #pragma config BOR = OFF // brownout reset off
    #pragma config MCLRE = OFF //master clear off
    #pragma code


    float getVoltage(void) //func to read ADC
    {

    ADCON0bits.GO = 1; //start ADC
    Delay100TCYx (2);
    while(ADCON0bits.GO == 1); //wait for completion
    return (ADRESH*0.018); // x with 0.018 to convert back to actual value
    }


    void main (void)
    {
    OSCCON = 0x43; //set internal freq 1Mhz
    OSCTUNE = 0
    ADCON1=0x7e; //select AN0 as analog
    ADCON0 =0x0; //select Vdd Vss as reference, select input AN0
    ADCON2=0x27; // left justified, use internal conversion clock
    ADCON0bits.ADON = 1 ;


    TRISA = 1; //set portA as input
    TRISB = 0; //set port B as output
    PORTB = 0b00000000; //reset port B


    while (1)
    {


    if (getVoltage() >3||getVoltage()<5)


    PORTB =0b11111111; //LED ON

    else
    PORTB = 0b00000000; //LED OFF
    }

    }
     
  2. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    x > 3 || x < 5 will always evaluate to true.
     
  3. c_calvin

    Thread Starter New Member

    Aug 16, 2009
    4
    0
    oh my how could i not notice this simple mistake :eek:
    Thanks a lot for the help!! :D
     
Loading...