ADC to LCD doesn't convert over 2.5v???

Discussion in 'Embedded Systems and Microcontrollers' started by harsha3842, Jul 5, 2013.

  1. harsha3842

    Thread Starter New Member

    Jul 5, 2013
    24
    0
    i'm using pic18f4520 and coding from mplab c18 using C
    the below code works absolutely fine
    only prob is it doesn't convert values above 2.5v i.e 511
    wats the error????????

    Code ( (Unknown Language)):
    1.  
    2. #include <p18cxxx.h>
    3. #include <adc.h>
    4. #include <stdlib.h>
    5. #include <delays.h>
    6. #include <stdio.h>
    7.  
    8. #define rel_staff PORTA
    9. #define  E LATDbits.LATD7
    10. #define  RS  LATDbits.LATD6
    11. #define lcdport LATB
    12. #define _XTAL_FREQ 20000000
    13.  
    14. #pragma config WDT=OFF, LVP=OFF, DEBUG=ON, MCLRE = OFF
    15. #pragma config OSC=HS
    16.  
    17. int adc_out=0;
    18. float adc_value=0;
    19. unsigned char voltage[5];
    20.  
    21. void ftoa(float, char *);
    22. void lcdcomm(unsigned char);
    23. void lcddata(unsigned char);
    24. void lcdout(unsigned char);
    25. void lcdmsg(const rom char *);
    26. void lcdstr(unsigned char *);
    27. void lcdblank(void);
    28. void lcdblink(void);
    29. void lcdclear(void);
    30. void lcdinit(void);
    31.  
    32. void main (void)
    33. {
    34.     TRISC = 0x00;
    35.     TRISA = 0x0F;
    36.     TRISB = 0x00;
    37.     TRISD = 0x00;
    38.     lcdinit();
    39.     lcdcomm(0x80);
    40.     lcdmsg("RELISYS");
    41.     lcdcomm(0xC0);
    42.     lcdmsg("TECHNOLOGIES");
    43.     Delay10KTCYx(1000);
    44.     Delay10KTCYx(1000);
    45.     lcdblank();
    46.     lcdcomm(0x80);
    47.     lcdmsg("Channel 1:");
    48.     lcdcomm(0x8F);
    49.     lcdmsg("V");
    50.     lcdcomm(0xC0);
    51.     lcdmsg("Channel 2:");
    52.     lcdcomm(0xCF);
    53.     lcdmsg("V");
    54.     OpenADC(ADC_FOSC_2 &
    55.         ADC_RIGHT_JUST &
    56.         ADC_2_TAD,
    57.         ADC_CH0 &
    58.         ADC_INT_OFF &
    59.         ADC_VREFPLUS_VDD &
    60.         ADC_VREFMINUS_VSS,ADC_2ANA);
    61.     do
    62.     {
    63.         SetChanADC (ADC_CH1);
    64.         Delay10KTCYx(10);
    65.         ConvertADC();
    66.         while(BusyADC());
    67.         adc_out=ReadADC();
    68.         adc_value=(adc_out*5.0)/1024;
    69.         ftoa(adc_value,voltage);
    70.         lcdcomm(0x8A);
    71.         lcdstr(voltage);
    72.  
    73.         SetChanADC (ADC_CH2);
    74.         Delay10KTCYx(10);
    75.         ConvertADC();
    76.         while(BusyADC());
    77.         adc_out=ReadADC();
    78.         adc_value=(adc_out*5.0)/1024;
    79.         ftoa(adc_value,voltage);
    80.         lcdcomm(0xCA);
    81.         lcdstr(voltage);
    82.         Delay10KTCYx(1000);
    83.         lcdclear();
    84.     }
    85.     while(1);
    86. }
    87.  
    88. void lcdmsg(const rom char *Message)
    89. {
    90.     const rom char *Pos = Message;
    91.     while(*Pos!=0)
    92.         lcddata(*Pos++);
    93. }
    94. void lcdstr(unsigned char *str)
    95. {
    96.     int i=0;
    97.     while(str[i]!='\0')
    98.     {
    99.         lcddata(str[i]);
    100.         i++;
    101.     }
    102. }
    103.  
    104. void lcdcomm(unsigned char i)
    105. {
    106.     RS =0;
    107.     lcdout(i);
    108.     Delay1KTCYx(2);
    109. }
    110.  
    111. void lcddata(unsigned char i)
    112. {
    113.     RS =1;
    114.     lcdout(i);
    115.     Delay1KTCYx(2);
    116. }
    117.  
    118. void lcdout(unsigned char dat)
    119. {
    120.     lcdport = dat;
    121.     E = 1;
    122.     Delay1TCY();
    123.     E = 0;
    124. }
    125.  
    126. void lcdinit(void)
    127. {
    128.     lcdport=0x00;
    129.     Delay1KTCYx(40);
    130.     lcdout(0x30);
    131.     Delay1KTCYx(15);
    132.     lcdout(0x20);
    133.     Delay1KTCYx(15);
    134.  
    135.     lcdcomm(0x38);
    136.     lcdcomm(0x0C);
    137.     lcdcomm(0x01);
    138.     lcdcomm(0x06);
    139.     lcdcomm(0x80);
    140. }
    141.  
    142. void lcdblank(void)
    143. {
    144.     lcdcomm(0x80);
    145.     lcdmsg("                ");
    146.     lcdcomm(0xC0);
    147.     lcdmsg("                ");
    148. }
    149.  
    150. void lcdclear(void)
    151. {
    152.     lcdcomm(0x8A);
    153.     lcdmsg("     ");
    154.     lcdcomm(0xCA);
    155.     lcdmsg("     ");
    156. }
    157.  
    158. void lcdblink(void)
    159. {
    160.     Delay10KTCYx(200);
    161.     lcdcomm(0x89);
    162.     lcdmsg("       ");
    163.     Delay10KTCYx(200);
    164. }
    165.  
    166. void ftoa(float value, char *string)
    167.  {
    168.      if (value < 0) {
    169.          *string++ = '-';
    170.          value = -value;
    171.      }
    172.      sprintf(string, (const far rom char *) "%lu.%03u",
    173.                      (long) value,
    174.                      (int) ((value - (long) value) * 1000. + 0.5));
    175.  }
    176. [/i][/i]
     
    Last edited by a moderator: Jul 5, 2013
  2. donpetru

    Active Member

    Nov 14, 2008
    186
    25
    Do not show or do not convert higher values ​​of 2.56V because the ADC reference voltage is 2.56V. Show the circuit diagram to see if the reference is 2.56V or not.
    Then, what is the maximum voltage you want to measure?

    LATER EDIT: In your software try to use instead:

    int adc_out = 0;

    this:

    unsigned int
    adc_out = 0;
     
    Last edited: Jul 5, 2013
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    It would appear your are assuming a 5V reference, which is attached to your Vdd, now, the question becomes, are you actually using a 5V supply?

    Try debugging the code and ensure the result returned from the ADC is as expected...

    I would urge you to not use so many compiler-specific operations - the C18 compiler is on its way to obsolescence, and your code would need to be refactored, if ever you had to rebuild this code in years to come. This is, of course, advice, and you may our may not agree, but that is my two cents on the matter...
     
  4. harsha3842

    Thread Starter New Member

    Jul 5, 2013
    24
    0
    Isn't the pic18f range from 0 to 5v?
    Yes im giving an external supply
    The cinversion works absolitely gine till 2.5v and it displays 2.495v on my lcd
    But any value over 2.5 I don get any variation
     
  5. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    It could be your ReadADC() function being faulty, returning only 9 of the 10 ADC bits.

    A good method is to display the 10bit ADC value (adc_out), for debugging.
     
  6. harsha3842

    Thread Starter New Member

    Jul 5, 2013
    24
    0
    the 10 bit ADC value(adc_out) gives max of 511 only
     
  7. LDC3

    Active Member

    Apr 27, 2013
    920
    160
    A 1 in the 10th bit (0x200) gives a value of 512, so 0x3FF is 1023.
     
  8. harsha3842

    Thread Starter New Member

    Jul 5, 2013
    24
    0
    my prob is why am i not getting that 1 in the 10th place???
    wat is stopping the value from reaching there???
     
  9. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    ...this is what we are trying to ascertain. Have you tried to debug your code yet?
     
  10. harsha3842

    Thread Starter New Member

    Jul 5, 2013
    24
    0
    yes everything seems ok
    not able to find out that single bit
    any config errors?
     
  11. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    So, what does your read function return? There is a point that your program is misinterpreting the value, single-step your code and check the value at each break...
     
  12. harsha3842

    Thread Starter New Member

    Jul 5, 2013
    24
    0
    the code works now
    thank you all for the assistance
    [​IMG]

    Code ( (Unknown Language)):
    1.  
    2. #include <p18cxxx.h>
    3.  #include <adc.h>
    4.  #include <stdlib.h>
    5.  #include <delays.h>
    6.  #include <stdio.h>
    7.  
    8.  #define E LATDbits.LATD7
    9.  #define RS  LATDbits.LATD6
    10.  #define lcdport LATB
    11.  #define _XTAL_FREQ 20000000
    12.  
    13.  #pragma config WDT=OFF,LVP=OFF,DEBUG=ON
    14.  
    15.  unsigned int adc_out=0;
    16.  float adc_value=0;
    17.  unsigned char voltage[5];
    18.  
    19.  void ftoa(float, char *);
    20.  void lcdcomm(unsigned char);
    21.  void lcddata(unsigned char);
    22.  void lcdout(unsigned char);
    23.  void lcdmsg(const rom char *);
    24.  void lcdstr(unsigned char *);
    25.  void lcdblank(void);
    26.  void lcdclear(void);
    27.  void lcdinit(void);
    28.  
    29.  void main (void)
    30.  {
    31.      TRISA = 0xFF;
    32.      TRISB = 0x00;
    33.      TRISD = 0x00;
    34.      lcdinit();
    35.      lcdcomm(0x80);
    36.      lcdmsg("RELISYS");
    37.      lcdcomm(0xC0);
    38.      lcdmsg("TECHNOLOGIES");
    39.      Delay10KTCYx(1000);
    40.      Delay10KTCYx(1000);
    41.      lcdblank();
    42.      lcdcomm(0x80);
    43.      lcdmsg("Channel 1:");
    44.      lcdcomm(0x8F);
    45.      lcdmsg("V");
    46.      lcdcomm(0xC0);
    47.      lcdmsg("Channel 2:");
    48.      lcdcomm(0xCF);
    49.      lcdmsg("V");
    50.      OpenADC(ADC_FOSC_32 &
    51.              ADC_RIGHT_JUST &
    52.              ADC_4_TAD,ADC_CH1 &
    53.              ADC_INT_OFF &
    54.              ADC_VREFPLUS_VDD &
    55.              ADC_VREFMINUS_VSS, 13);
    56.      while(1)
    57.      {
    58.          SetChanADC (ADC_CH0);
    59.          Delay10KTCYx(10);
    60.          ConvertADC();
    61.          while(BusyADC());
    62.          adc_out=ReadADC();
    63.          adc_value=(adc_out*5.0)/1024;
    64.          ftoa(adc_value,voltage);
    65.          lcdcomm(0x8A);
    66.          lcdstr(voltage);
    67.  
    68.          SetChanADC (ADC_CH1);
    69.          Delay10KTCYx(10);
    70.          ConvertADC();
    71.          while(BusyADC());
    72.          adc_out=ReadADC();
    73.          adc_value=(adc_out*5.0)/1024;
    74.          ftoa(adc_value,voltage);
    75.          lcdcomm(0xCA);
    76.          lcdstr(voltage);
    77.          Delay10KTCYx(1000);
    78.          lcdclear();
    79.      }
    80.      CloseADC();  
    81.  }
    82.  
    83.  void lcdmsg(const rom char *Message)
    84.  {
    85.      const rom char *Pos = Message;
    86.      while(*Pos!=0)
    87.          lcddata(*Pos++);
    88.  }
    89.  void lcdstr(unsigned char *str)
    90.  {
    91.      int i=0;
    92.      while(str[I]!='\0')
    93.      {
    94.          lcddata(str[I]);
    95.          i++;
    96.      }
    97.  }
    98.  
    99.  void lcdcomm(unsigned char i)
    100.  {
    101.      RS =0;
    102.      lcdout(i);
    103.      Delay1KTCYx(2);
    104.  }
    105.    
    106.  void lcddata(unsigned char i)
    107.  {
    108.      RS =1;
    109.      lcdout(i);
    110.      Delay1KTCYx(2);
    111.  }
    112.    
    113.  void lcdout(unsigned char dat)
    114.  {
    115.      lcdport = dat;
    116.      E = 1;
    117.      Delay1TCY();
    118.      E = 0;
    119.  }
    120.    
    121.  void lcdinit(void)
    122.  {
    123.      lcdport=0x00;  
    124.      Delay1KTCYx(40);
    125.      lcdout(0x30);
    126.      Delay1KTCYx(15);
    127.      lcdout(0x20);
    128.      Delay1KTCYx(15);
    129.    
    130.      lcdcomm(0x38);
    131.      lcdcomm(0x0C);
    132.      lcdcomm(0x01);
    133.      lcdcomm(0x06);
    134.      lcdcomm(0x80);
    135.  }
    136.  
    137.  void lcdblank(void)
    138.  {
    139.      lcdcomm(0x80);
    140.      lcdmsg("                ");
    141.      lcdcomm(0xC0);
    142.      lcdmsg("                ");
    143.  }
    144.  
    145.  void lcdclear(void)
    146.  {
    147.      lcdcomm(0x8A);
    148.      lcdmsg("     ");
    149.      lcdcomm(0xCA);
    150.      lcdmsg("     ");
    151.  }
    152.  
    153.  void ftoa(float value, char *string)  
    154.   {  
    155.       if (value < 0) {  
    156.           *string++ = '-';  
    157.           value = -value;  
    158.       }  
    159.       sprintf(string, (const far rom char *) "%lu.%03u",  
    160.                       (long) value,  
    161.                       (int) ((value - (long) value) * 1000. + 0.5));  
    162.   }
    163. [/I][/I]
     
    Last edited by a moderator: Jul 6, 2013
  13. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    That's good news...

    ....so what was the problem?
     
  14. harsha3842

    Thread Starter New Member

    Jul 5, 2013
    24
    0
    error was with OpenADC() function
    its resolved and works absolutely fine...

    Regards
    Harsha
     
  15. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    ...that's where the problem was... what was the problem?
     
  16. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    Yeah, what he said.
     
Loading...