• AVR Freaks

Hot!Reading Analog Value

Page: < 123 > Showing page 2 of 3
Author
1and0
Access is Denied
  • Total Posts : 9257
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Reading Analog Value 2019/04/25 05:29:14 (permalink)
0
john_ive
After disabling the ADC pin and predefined value is given to input_volatge variable means,the predefined value is correctly shown in display.

Don't think so, but show your lcd_putch() code.  Also, you don't have to disable the ADC to do that; just assign a new value to input_voltage.
#21
pcbbc
Super Member
  • Total Posts : 1024
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: Reading Analog Value 2019/04/25 06:13:17 (permalink)
+1 (1)
    iv_temp1 = ((ADRESH << 8) + ADRESL);
    input_voltage = (((iv_temp1 * 5)*1000) / 1024);

The calcualtion for input_voltage will overflow for any value of iv_temp1 greater than 6.
5 * 1,000 = 5,000
5,000 * 7 (iv_temp) = 35,000
That exceeds the range of the 16 bit int type (-32768 to 32767).
#22
1and0
Access is Denied
  • Total Posts : 9257
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Reading Analog Value 2019/04/25 08:04:44 (permalink)
0
Good catch!  Try this
input_voltage = (iv_temp1 * 5005uL) >> 10;

with rounding.
 
Also, use unsigned integers instead.
 
#23
john_ive
Super Member
  • Total Posts : 194
  • Reward points : 0
  • Joined: 2017/05/22 22:22:08
  • Location: 0
  • Status: online
Re: Reading Analog Value 2019/04/26 01:06:25 (permalink)
0
Changed the ADC like this and now able to read the value.
But when connecting multimeter parallel to ADC input means there is an difference between the reading in multimeter and LCD.
How to improve the errors in output..?
#24
PStechPaul
Super Member
  • Total Posts : 2268
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: Reading Analog Value 2019/04/26 01:22:17 (permalink)
0
What are you using as ADC reference? That will determine the accuracy. You might also add a calibration factor to get readings to match, or a trimpot to calibrate it. Also, it's a good idea to average several readings or use a true RMS calculation, and take readings over an integral number of line frequency cycles, to filter noise.

 
#25
john_ive
Super Member
  • Total Posts : 194
  • Reward points : 0
  • Joined: 2017/05/22 22:22:08
  • Location: 0
  • Status: online
Re: Reading Analog Value 2019/04/26 02:13:41 (permalink)
0
PStechPaul
What are you using as ADC reference? That will determine the accuracy. You might also add a calibration factor to get readings to match, or a trimpot to calibrate it. Also, it's a good idea to average several readings or use a true RMS calculation, and take readings over an integral number of line frequency cycles, to filter noise.


VSS and VDD is used as reference value.
#26
john_ive
Super Member
  • Total Posts : 194
  • Reward points : 0
  • Joined: 2017/05/22 22:22:08
  • Location: 0
  • Status: online
Re: Reading Analog Value 2019/04/26 02:37:47 (permalink)
0
By using multimeter measured the voltage across VSS,VDD in pcb.Its 5.019
 
so I changed like this.

iv_temp1 = ((ADRESH << 8) + ADRESL);
input_voltage = ((iv_temp1 * 5019) / 1024);

 
Is this is correct..?
#27
1and0
Access is Denied
  • Total Posts : 9257
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Reading Analog Value 2019/04/26 02:53:04 (permalink)
+1 (1)
john_ive
By using multimeter measured the voltage across VSS,VDD in pcb.Its 5.019
 
so I changed like this.

 
iv_temp1 = ((ADRESH << 8) + ADRESL);
 
input_voltage = ((iv_temp1 * 5019) / 1024);
 

Is this is correct..?

The point is to get Vdd (or Vref+ - Vref-) for the max ADC reading of 1023, so try this:

input_voltage = (iv_temp1 * 5024uL) >> 10;

Edit: Added uL.
post edited by 1and0 - 2019/04/26 03:04:39
#28
john_ive
Super Member
  • Total Posts : 194
  • Reward points : 0
  • Joined: 2017/05/22 22:22:08
  • Location: 0
  • Status: online
Re: Reading Analog Value 2019/04/26 03:10:38 (permalink)
0
1and0
john_ive
By using multimeter measured the voltage across VSS,VDD in pcb.Its 5.019
 
so I changed like this.

 
 
 
iv_temp1 = ((ADRESH << 8) + ADRESL);
 
 
 
input_voltage = ((iv_temp1 * 5019) / 1024);
 
 
 

Is this is correct..?

The point is to get Vdd (or Vref+ - Vref-) for the max ADC reading of 1023, so try this:

 
input_voltage = (iv_temp1 * 5024uL) >> 10;
 

Edit: Added uL.


Can you explain this 5024uL please...?
#29
pcbbc
Super Member
  • Total Posts : 1024
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: Reading Analog Value 2019/04/26 03:12:54 (permalink)
+2 (2)
john_iveCan you explain this 5024uL please...?

5024 unsigned long
 
It forces the result of iv_temp1 * 5024uL to be 32 bits instead of the default 16.
When you do arithmetic on two int variables in C (a constant like 5024 is taken to be an int also) you get an int (16 bit in XC8) result. That means the result is truncated if it is too large to fit in the size of an int.
If however one (or both) of the values in the arithmetic is a larger type (32 bit in this case for unsigned long) the result is also of that larger type.
post edited by pcbbc - 2019/04/26 03:19:31
#30
1and0
Access is Denied
  • Total Posts : 9257
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Reading Analog Value 2019/04/26 03:18:56 (permalink)
+2 (2)
john_ive
 
Can you explain this 5024uL please...?

A 10-bit ADC has 1024 levels but with a max reading of 1023. You wanted this max reading to be 5.019 volt, correct?  So solve for the scaling factor X in
 
5019 = 1023 * X / 1024
 
gives you X = 5024.  Hence, voltage = (adres * 5024) >> 10) because right-shifting is an efficient way to divide by a power of 2 (a smart compiler will do that for you).
 
The suffix uL makes that literal constant an unsigned long integer so the multiplication is done in 32-bit math to avoid overflow; i.e. 1023 * 5024 is bigger than 16-bit, so you must be use 32-bit math.
#31
john_ive
Super Member
  • Total Posts : 194
  • Reward points : 0
  • Joined: 2017/05/22 22:22:08
  • Location: 0
  • Status: online
Re: Reading Analog Value 2019/04/26 03:32:23 (permalink)
0
Thanks a lot.
While reading the voltage in parallel with multimeter,the last digit in mutimeter is oscillating from its nearest value,
but in controller the oscillating value is +/-5 for last digit..
 
This 5 difference is changing the third digit also...
 
How to avoid these oscillating value..?
#32
1and0
Access is Denied
  • Total Posts : 9257
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Reading Analog Value 2019/04/26 03:44:56 (permalink)
+1 (1)
john_ive
Thanks a lot.
While reading the voltage in parallel with multimeter,the last digit in mutimeter is oscillating from its nearest value,
but in controller the oscillating value is +/-5 for last digit..
 
This 5 difference is changing the third digit also...
 
How to avoid these oscillating value..?

Filters ... hardware and/or software.  Easier method is to average a numbers (again power of 2) of readings in software. ;)  Or, you can also use moving average.
 
#33
john_ive
Super Member
  • Total Posts : 194
  • Reward points : 0
  • Joined: 2017/05/22 22:22:08
  • Location: 0
  • Status: online
Re: Reading Analog Value 2019/04/26 04:26:01 (permalink)
0
Suppose the voltage across VSS,VDD is varying in another one PCB means,we have to measure the voltage in that board also and have to change the value..?
#34
pcbbc
Super Member
  • Total Posts : 1024
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: Reading Analog Value 2019/04/26 04:38:16 (permalink)
+1 (1)
john_iveSuppose the voltage across VSS,VDD is varying in another one PCB means,we have to measure the voltage in that board also and have to change the value..?

Yes, if your Vss/Vdd supply is not accurate enough you will need to include a calibrated reference voltage on Vref+/-.
Or have a pot to trim the ADC voltage, and calibrate on a per PCB basis.
#35
john_ive
Super Member
  • Total Posts : 194
  • Reward points : 0
  • Joined: 2017/05/22 22:22:08
  • Location: 0
  • Status: online
Re: Reading Analog Value 2019/04/26 04:57:12 (permalink)
0
pcbbc
john_iveSuppose the voltage across VSS,VDD is varying in another one PCB means,we have to measure the voltage in that board also and have to change the value..?

Yes, if your Vss/Vdd supply is not accurate enough you will need to include a calibrated reference voltage on Vref+/-.
Or have a pot to trim the ADC voltage, and calibrate on a per PCB basis.


We can give reference voltage by externally at AN3 pin or have to use AN2 pin also..?
 
#36
pcbbc
Super Member
  • Total Posts : 1024
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: Reading Analog Value 2019/04/26 05:50:58 (permalink)
+2 (2)
john_iveWe can give reference voltage by externally at AN3 pin or have to use AN2 pin also..?

Depends on your application.
Did you look at the datasheet?  Did you see they are programmable individually as per REGISTER 11-2?
Vref+ to either Vdd or RA3/AN3
Vref- to either Vss or RA2/AN2
If your voltage reference is referenced to Vss, then you only need to connect Vref+
Otherwise connect both Vref- and Vref+
#37
1and0
Access is Denied
  • Total Posts : 9257
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Reading Analog Value 2019/04/26 05:52:02 (permalink)
+1 (1)
john_ive
 
We can give reference voltage by externally at AN3 pin or have to use AN2 pin also..?

AN3 is Vref+ and AN2 is Vref-.  There are restrictions on the voltage and range of these voltage reference -- check your PIC datasheet spec.
#38
john_ive
Super Member
  • Total Posts : 194
  • Reward points : 0
  • Joined: 2017/05/22 22:22:08
  • Location: 0
  • Status: online
Re: Reading Analog Value 2019/05/01 22:28:49 (permalink)
0
While giving the reference voltage from externally,it should be +5VDC or shall we give +3.3VDC and shall we change the calculations..?
#39
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: Reading Analog Value 2019/05/01 22:41:56 (permalink)
0
What is the maximum PEAK voltage you need to measure?
You cannot measure higher than your reference voltage, so if Vref=3.3V, you cannot measure more than 3.3V.

Nearly there...
#40
Page: < 123 > Showing page 2 of 3
Jump to:
© 2019 APG vNext Commercial Version 4.5