Analogue convertion and LCD display (PIC16F877)

Discussion in 'Programmer's Corner' started by airjy00, Oct 26, 2006.

  1. airjy00

    Thread Starter New Member

    Oct 26, 2006
    1
    0
    Hi , I am currently learning PIC16F877 . I am trying to write a code to convert the analogue value on the potentiometer attached to RA0 and display the scaled conversion on an LCD display attached to PORTD such that when the 0V – 5V range of RA0 is converted the display reads on line 1 “VOLTAGE = X.XX V” where XXX has a value from 000 to 500.

    here is the code i have for convertion , but i have no idea about how to combine this code to complete the above requests. Any help is appreicated =) .

    My email : microctrler@hotmail.com
    P.S. full code is always the best =P

    //analogue input on RA0 with result sent to PORTB and PORTC

    #include <pic.h>

    void init_ports(void);
    void init_a_d(void);
    void init_ports(void)
    {
    TRISB = 0x00;
    TRISC = 0x00; // set portb and portc as outputs
    TRISA = 0xFF; // set the porta bits to inputs
    }

    void init_a_d(void)
    {
    ADCS1 = 1;
    ADCS0 = 1; // A/D conversion clock derived from RC osc
    CHS2 = 0;
    CHS1 = 0;
    CHS0 = 0; //select channel 0
    ADON = 1; // A/D switched ON
    ADCON1 = 0x00; // enable all analogue inputs
    ADFM = 1; // right justified result - 6 most sig bits
    //of ADRESH read as '0'
    }

    void main(void)
    {
    int result;
    init_ports();
    init_a_d();

    while(1)
    {
    ADGO = 1; // start the conversion
    while (ADGO= =1){}; // now wait here until the conversion is done
    result = (ADRESH <<8 )+ ADRESL;
    PORTB = (result>>8) & 0x00FF;
    PORTC = (result & 0x00FF);
    }
    }
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    Hi,

    It seems that you are having problems with the scaling of the input. If your A to D has a range of 0 - 10 volts and 8 bits resolution, then a 5 volt input will give you a conversion value of 256 (decimal). If 10 bits, then 5 volts will be a conversion of 512. Divide the result by the input voltage to get the number of conversion bits per volt. It's never even, so you have to make a rounding routine for your displayed numeric value.
     
  3. sci-3d

    Well-Known Member

    Aug 22, 2006
    51
    0
    Your code looks OK, except this line

    while (ADGO= =1) should be

    while (ADGO==1)

    That is my opinion. :)
     
Loading...