Novice : Need help to use ADC to measure 0 - 15V voltage

General discussion on mikroC.
Author
Message
crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

#16 Post by crocu » 03 Feb 2010 12:03

Thanks for the details.

If i do not use resitor divider and set AN0 input to Pic Vcc voltage = 5.20V, then ADC value displays 1022. ( not so bad, but shouldn't be 1023 ? )

The issue is that AN0 displays = 4.98V with ADC = 1022, instead expected 5.19V ....

( 1022 x 5,20 ) / 1023 = 5,19V

Do you know why there are 0,21 difference here ?
About 4% error ...

crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

Re: Novice : Need help to use ADC to measure 0 - 15V voltage

#17 Post by crocu » 19 Feb 2010 19:31

Hello

I'm still experiencing problem to convert a 0-15 Volt with ADC. ( with RA1 pin )

I set a resistor divider with 1000 & 500 ohms, in order to divide voltage per 3.
i used potentiometers so divider is very occurate.

Then i use that code :

Code: Select all

void Init(void)
 {
TRISA = 0xFF; //PORTA as input
ADCON1 = 7;  // Configure AN pins as digital I/O
CMCON |= 7; // Disable comparators
}
...

Code: Select all

// Reads AN0 voltage and display it on the LCD ( line 3 )
// LCD line 4 will show the Numéric value.

hold = ADC_Read(0);
IntToStr(hold, txt);

LCD_Chr(3,1,5*hold/1024+48);
LCD_Chr(3,2,46);
LCD_Chr(3,3,(50*hold/1024)%10+48);
LCD_Chr(3,4,(20*hold/41)%10+48); //20/41 =~ 500/1024
LCD_Chr(3,5,86);
Lcd_Out(4,1,txt);
When i apply Pic Vcc voltage to the divider input ( 5.30V ) , i get 1,76V as output, this is normal.
However the LCD displays 1.66V :shock:

Is there something wrong in code ?
What should i need to change to get value displayed on the LCD ?

Many thanks for your help,

Acetronics
Posts: 715
Joined: 27 Dec 2006 14:33
Location: Le Tréport , FRANCE

Re: Novice : Need help to use ADC to measure 0 - 15V voltage

#18 Post by Acetronics » 19 Feb 2010 20:55

My Frriend Crocu ...

shouldn't you write

Code: Select all


LCD_Chr(3,1, 26/5 *hold/1024 +48);

with 26/5 = your EXACT ref voltage

instead of

Code: Select all

LCD_Chr(3,1,5*hold/1024+48);
... exactly 4% difference ...

Alain

crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

Re: Novice : Need help to use ADC to measure 0 - 15V voltage

#19 Post by crocu » 01 Apr 2010 18:08

Hello,

I'm using resitor divider with ratio 4.

So when i apply 20V as input, i get 5V at this output of the divider.

My ADC sees 5V and displays 5V, it is fine.


But i would like the code multiplies the displayed by 4 in order to display real measured value : 20V

i tried the following but it won't work, can you tell me what is wrong ?

Code: Select all

    Lcd_Out(4,12,("Battery "));

    adc_rd  = ADC_read(1);                 // get ADC value from 1st channel ( RA1 )
    IntToStr(adc_rd, val_adc);

    tlong = (long)adc_rd * 5000;        // convert adc reading to milivolts
    tlong = tlong / 1023;                  // 0..1023 -> 0-5000mV

    ch     = (tlong / 1000);              // extract volts digit   (* 4 is resistors divider bridge ratio)
    LCD_Chr(4,17,48+(ch*4));         // write ASCII digit at 4nd row, 16th column ( (multiplies result* 4 is resistors divider bridge ratio)
etc ... for all others digits ...

I tried to multiply ch * 4 but it displays no readable characters.
Can you help me please ?

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: Novice : Need help to use ADC to measure 0 - 15V voltage

#20 Post by Sparky1039 » 02 Apr 2010 00:48

It looks like the source of your error is in the scaled value of the ADC quanta.

Code: Select all

tlong = (long)adc_rd * 5000;        // convert adc reading to milivolts
Your post specified 5.30V as the PIC vcc value which I assume is also the ADC reference.
This reference sets the ADC conversion slope so it will effect the final result accuracy. Since the ADC reference is not 5.00V (5000mV) like in your code, but is actually 5.30V (5300mV) then you should change this value in your equation.

Code: Select all

tlong = (long)adc_rd * 5300;        // convert adc reading to milivolts
ex.
340 counts x 5000mV / 1023 = 1661mV (1.661V)
340 counts x 5300mV /1023 = 1761mV (1.761V)

Also to rescale the output to reflect the actual input voltage it's best to calculate the resistor voltage divider reduction coefficient by...
Rc= (R2 / (R1+R2)) x 10000 (the x 10000 is a scaling factor so you can use integer math vs FP in the code)
and apply it into the ADC conversion formula.

Actual applied input (mV) = (((ADC counts x 5300) x Rc) / 1023) / 1000

This method alows you to apply any resistor divider combination without having to worry about possible fractional components.
A reduction of 3.00 (1/3) is easy because it's an even integer. However a resistor combination that results is a reduction of say 3.3875 is much harder to deal with in code (unless using FP which eats up code space).

crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

Re: Novice : Need help to use ADC to measure 0 - 15V voltage

#21 Post by crocu » 02 Apr 2010 06:37

Hello,

My resistor divider divides input value by 4.

(R2 / (R1+R2) = 0.25

I do not understand well what is scaling factor ( you mentionned. x 10000 )
In my case, what calculation should i do ?


I've done this, but it stills returning non readables characterson the LCD :

Code: Select all

tlong = (long)adc_rd * 5300 * 4;
tlong = tlong / 1023;
Many thanks for your help,

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: Novice : Need help to use ADC to measure 0 - 15V voltage

#22 Post by Sparky1039 » 02 Apr 2010 07:20

My resistor divider divides input value by 4.
(R2 / (R1+R2) = 0.25
I do not understand well what is scaling factor ( you mentionned. x 10000 )
In my case, what calculation should i do ?
Simple 0.25 x 10000 = 2500
All this does is scale up the R-divider coefficient by 10000 so that you don't have to use floating point math (fp) in your code. FP slows down the PIC by performing excessive number crunching. In your case the r-divider coefficient is a whole number and it's a simple process to multiply everything by 4. But what if it wasn't a 1/4 ratio? Or you wanted to change scale or range? This is why I included the extra math step.
I've done this, but it stills returning non readables characterson the LCD :
Sounds like a value to text string conversion problem. Why not use the mikroC built in conversion library for this task? (IntToStr or LongToStr)
Better yet break up the mV result into decimal and integer values and post these to the LCD.

Code: Select all

unsigned char decimal = 0;
unsigned int integer = 0;

integer = tlong / 1000; // get the integer value of the voltage measurement
decimal = tlong % 1000; // get the fractional result of the voltage measurement using modulus division
now convert these to text using the mikroC conversion library functions and display them on the LCD
(sorry i don't remember the mikroC syntax anymore since I'm using mikroC Pro these days)
psuedo code...

lcd_out integer;
lcd_out ".";
lcd_out decimal;

sahu
Posts: 85
Joined: 11 Apr 2010 19:37

Re: Novice : Need help to use ADC to measure 0 - 500V AC vol

#23 Post by sahu » 24 Jul 2011 18:00

Hello


Can someone put me on the way to use AN1 & AN2 with 16F676 , i would like to measure a voltage from 0V to 500V AC and display the result on a LCD.

Can you show me a code example that would set RA1 & RA2 as AN1 & AN2 input and do calculations for display the measured voltage ?

I will appreciate your help,
I am trying cod as ........

Code: Select all

            ADC_Value = ADC_Read(1);

                     DisplayVolt = ADC_Value * 2;

                     volt[0] = DisplayVolt/100 + 48;

                     volt[1] = (DisplayVolt/10)%10 + 48;

                     volt[3] = (DisplayVolt/1)%10 + 48;

                     Lcd_Out(1,7,volt);

                     Lcd_Out(2,1,Message2);             

                     Lcd_Chr(1,12," ");

                     ADC_Value = ADC_Read(2);

                     DisplayVolt = ADC_Value * 2;

                     volt[0] = DisplayVolt/100 + 48;

                     volt[1] = (DisplayVolt/10)%10 + 48;

                     volt[3] = (DisplayVolt/1)%10 + 48;

                     Lcd_Out(2,8,volt);
                     Delay_ms(5000);

                     Lcd_Cmd(_LCD_CLEAR);
                     Lcd_Cmd(_LCD_CURSOR_OFF); 
Attachments
ADC INPUT.JPG
ADC INPUT.JPG (240.63 KiB) Viewed 3734 times
SAHU

Post Reply

Return to “mikroC General”