Basic analog to digital calc volts subroutine
Campeck
Posts: 111
Hey guys. Taking a look at this subroutine here to calculate voltage from the 8bit ADC I was thinking that technically the second IF statement and everything that follows
Isn't necessary correct?
Because when V2 reaches 100 the calculation for V already increments the ones place.
Calc_Volts:
v = 5 * adcBits / 255
r = 5 * adcBits // 255
v2 = 100 * r / 255
v3 = 100 * r // 255
v3 = 10 * v3 / 255
IF (v3 >= 5) THEN v2 = v2 + 1
IF (v2 >= 100) THEN
v = v + 1
v2 = 0
ENDIF
RETURN
Just making sure I'm not missing anything important.
Thanks.
Isn't necessary correct?
Because when V2 reaches 100 the calculation for V already increments the ones place.
Calc_Volts:
v = 5 * adcBits / 255
r = 5 * adcBits // 255
v2 = 100 * r / 255
v3 = 100 * r // 255
v3 = 10 * v3 / 255
IF (v3 >= 5) THEN v2 = v2 + 1
IF (v2 >= 100) THEN
v = v + 1
v2 = 0
ENDIF
RETURN
Just making sure I'm not missing anything important.
Thanks.
Comments
That said, that subroutine is a roundabout way to do the calculation on a Stamp. Where did you find it and what is the ADC? Here is a method that uses the PBASIC */ operator...
This assumes that the formula for conversion is V = adcbit / 256, rather than V = adcbits / 255. You will see a 255 there in a lot of texts, but I would argue that the denominator should in fact be 256 for ADCs such as the ADC0831, which has its final riser 1.5 bits below the maximum value.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
And this is from the Parallax Analog and Digital book.
what is this riser you speak of?
Post Edited (Campeck) : 1/24/2010 4:14:05 AM GMT
Why not try it and see?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
The remainder from the first division is r = adcbits * 5 // 255. Because of the factor of 5 on both sides of the congruence, the value of r will also be a multiple of 5, and less than 255. So the highest value of r will be 250. Then moving to v2 = 100 * r / 255, if the maximum value of r is 250, we have, v3 = 100 * 250 / 255 = 98.04, which the Stamp rounds down to 98. The upshot is that the second IF statement is not only incorrect in the part about (v2>=100), but also the statement corrected to (v2=99) is superfluous, because v2 will never equal 99. The first IF statement however will be able to round up a 98 to 99, so that step does in fact improve the resolution.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
The transfer function relates the input voltage to the binary adc code, and that is often given as a graph in the data sheet. The code when the voltage equals zero is of course zero. The code becomes 1 when the voltage is 1/2 bit higher. So with converter that resolves 20 mV per bit, the code would go from 0 to 1 when the input is 10 mV, and it stays there until the voltage hits 30 mV, where it moves up to code 2. At the high end, the code moves from 254 to 255 when the input is 1.5 bits below maximum and stays there for the remaining 1.5 bits of the range. In this example, that would be the top 30 milliVolts of the input. There would in theory be a transition from 255 to 256 at 1/2 bit(10 mV) below maximum, but the converter cannot output a code of 256, because that wraps around to zero. The point is that the implied maximum of the transfer function is 256, not 255.
So what? Consider a simpler ADC, one that resolves only 2 bits, with output codes of 0,1, 2 and 3. Suppose this ADC has a full scale reference voltage of 4 volts. The diagram below shows the standard transfer function in black, with the input voltage on the x axis and the binary code from the ADC on the y axis. The binary code of course takes on only 4 distinct values, 0,1,2 & 3.. The first riser is at 0.5 bit, 0.5 volt, and the last riser is 1.5 bit below the maximum, at 2.5 volts.
If you use the formula, Va = 4 * adc / 4, then the 4 values of Va will be 0, 1, 2 and 3 volts, and the reading will be correct (meaning Va= true input voltage) at the points shown as red dots on the transfer function with error symetrical around those points. On the other hand, the formula Vb = 4 * adc / 3, will give output voltages of 0, 1.33, 2.67 and 4 volts, with a transfer function shown in blue, and the reading will be correct (again meaning Vb = actual input voltage) at the dots shown in blue. It is true that Vb will end up at the full scale 4 volts, but the match to the actual transfer function is not good. This is exactly the same as the situation with an 8 bit converter that outputs codes from 0 to 255. The best formula will be Va = Vfs * adc / 256, not Vb = Vfs = Vfs * adc / 255. The more bits there are, the lower the error, but an error it is. Not only that, math with the factor of 256 (or in general a power of two) is much easier to deal with.
I hope that helps.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Post Edited (Tracy Allen) : 1/25/2010 12:55:38 AM GMT