the value from ADC not consistence

Discussion in 'Embedded Systems and Microcontrollers' started by mermaid09, Mar 15, 2010.

  1. mermaid09

    Thread Starter New Member

    Nov 21, 2009
    Hello everyone,
    i need some help about reading voltage that going to ADC of PIC18F252 and then display that value to input of adc is from current sensor that produce output in term of voltage

    My range input voltage should be 4.00V to 4.10v only,and the output produce at lcd also in that range but the problem here at certain time the output at lcd go outside that range (but in short time). Why its happen?. I really need precision value to calculate some formula.

    And second problem,the output that display at lcd are change from one to another are to fast (about 1 second).

    below is my coding

    unsigned char ch;
    unsigned int adc_rd;
    char *text;
    long tlong,;

    void LCD()

    Delay_ms(500);// need delay
    adc_rd = ADC_Read(2); // A/D conversion. Pin RA2 is an input.
    Lcd_Out(2,1,text); // Write result in the second line
    tlong = (long)adc_rd * 4800; // Convert the result in millivolts
    tlong = (tlong / 1024)
    ch = tlong / 1000; // Extract volts (thousands of millivolts)
    // from result

    Lcd_Chr(2,9,48+ch); // Write result in ASCII format
    ch = (tlong / 100) % 10; // Extract hundreds of millivolts
    Lcd_Chr_CP(48+ch); // Write result in ASCII format
    ch = (tlong/ 10) % 10; // Extract tens of millivolts
    Lcd_Chr_CP(48+ch); // Write result in ASCII format
    ch = tlong % 10; // Extract digits for millivolts
    Lcd_Chr_CP(48+ch); // Write result in ASCII format


  2. symqwerty


    Feb 22, 2010
    What is your Vref+ and Vref- for ADC?
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    I am not sure the internal ADC in PIC MCU is made for such resolution at all. Each bit will have a bit weight equal to 98uV. I think the internal noise is much higher than this. And how about the noise in the source signal, and the reference. You could try putting the MCU to sleep during the ADC cycle. That will reduce the noise some. But to be honest I think your project specifications do not match your current hardware at all
  4. Markd77

    Senior Member

    Sep 7, 2009
    Have you considered using op amps to amplify this voltage range? I'm not sure if this would give a better result but it might be worth a try.
  5. mermaid09

    Thread Starter New Member

    Nov 21, 2009
    symqwerty :
    my vref+=+5v and my vref- = ground,

    what do u say my project specifications do not match with current hardware. can u give same idea how to correct my mistake?i really weak about microcontroller

    how to take average reading?im not understand,how to implement at coding C?how the coding lool like to sort?can u give me some example either in c programming or something else that can me to understand about your suggestion?

    thank u so much for your time and kind to help me.:)
  6. retched

    AAC Fanatic!

    Dec 5, 2009
  7. symqwerty


    Feb 22, 2010
    i think you are using MikroC as your editor, right?
    It's true and ADC_READ() function will take care of it..

    Taking average reading means you need to store every digital value, add it, and divide by n-number of value..So, you could modify your code little bit.
    For example, instantiate an array of size 10. Then, after getting digital ADC value, keep it in array of index 0, and the next execution or looping, keep it in index 1,and repeat until 10...then, sums it up and divide by 10 to get the 'mean' value before displayed it on LCD.

    But the best practice is to keep shifting out old data in the array every-time you get new ones or in other words, using FIFO concept.