PDA

View Full Version : Sorry, yet ANOTHER mod question



andywpg
- 30th January 2014, 00:54
First of all, I wish my brain and math got along with each other - but I guess I can't have everything.

I'm trying to measure the input voltage of the circuit and display it on the LCD. To that end, I have a voltage divider with the mid-point hooked up to (it so happens) ADC 2. The idea was that 15V input would give a full-scale 5V to the pin, using Vdd as the reference. It so happens that my Vdd is exactly 5V.

When my power supply is sending 14.7V to the circuit, my ADC value (in decimal) is 732. So I took 732 / 14.7 = 49.8.

This is what I did. Multiply my ADC value by 10 and divide by 498. And the decimal is the result mod 10 since I multiplied by 10. What I get for the decimal is .04 not the .70 I expect - what the heck am I doing wrong? BTW, VOLTS_IN and VOLTS_FRAC are both word variables. Below is the code I have, any help is appreciated.




MEASURE_VOLTS:
ADCIN 2, VOLTS_IN 'READ CURRENT VALUE OF INPUT VOLTAGE DIVIDER
VOLTS_IN = (VOLTS_IN * 10) / 498
VOLTS_FRAC = VOLTS_IN MOD 10

LCDOUT $FE, 1, "CURRENT INPUT"
LCDOUT $FE, $C0, "VOLTAGE IS: ", DEC2 VOLTS_IN, ".", DEC2 VOLTS_FRAC

richard
- 30th January 2014, 02:27
14 mod 10 = 4 mod gives you the remainder after the division ie ten goes into 14 once with 4 leftover , its not what you want
frac = volts*10 mod 100 is what you want ie 1469 mod 100 = 69 but
the following is easier
forget the frac
volts_in = (volts_in * 10) / 498

try lcdout $fe, $c0, "voltage is: ", volts_in/100, ".", dec2 volts_in

andywpg
- 30th January 2014, 02:56
14 mod 10 = 4 mod gives you the remainder after the division ie ten goes into 14 once with 4 leftover , its not what you want
frac = volts*10 mod 100 is what you want ie 1469 mod 100 = 69 but
the following is easier
forget the frac
volts_in = (volts_in * 10) / 498

try lcdout $fe, $c0, "voltage is: ", volts_in/100, ".", dec2 volts_in

Hmmm, neither of those seemed to work for me - didn't get what I was looking for.

Then I tried this:



MEASURE_VOLTS:
ADCIN 2, VOLTS_IN 'READ CURRENT VALUE OF INPUT VOLTAGE DIVIDER
VOLTS_VAL = (VOLTS_IN * 10) / 498
VOLTS_FRAC = (VOLTS_IN * 10) MOD 100


LCDOUT $FE, 1, "CURRENT INPUT"
LCDOUT $FE, $C0, "VOLTAGE IS: ", DEC2 VOLTS_VAL, ".", DEC2 VOLTS_FRAC


Still not right, so I modified the fraction line to: VOLTS_FRAC = ((VOLTS_IN * 10) / 498) MOD 100

But that gives me 14.14 - still not right. Any other ideas? And thanks again for the help

richard
- 30th January 2014, 02:58
got that wrong
frac = volts*1000/498 mod 100 is what you want ie 1469 mod 100 = 69 but it may cause overflows


volts_in = (volts_in * 100) / 498 (should be ok internal mult/div is 32 bit in pbp3 anyway)

try lcdout $fe, $c0, "voltage is: ", volts_in/10, ".", dec1 volts_in

Gusse
- 30th January 2014, 11:12
What kind of voltage divider you are using?
If you try to get full 0-5V scale with 0-15V input, then you should use 1:3 ratio (e.g. 22kOhm and 11kOhm resistors) to get best measurement accuracy.

With this divider 15V -> 5V (and 0V -> 0V) at ADC input pin, ADC should show 255 (8bit) or 4095 (12bit).

Math to show input voltage based on ADC value.
8bit ADC:

VOLTS_VAL = (15 * VOLTS_IN)/255

12bit ADC:

VOLTS_VAL = (15 * VOLTS_IN)/4095

andywpg
- 30th January 2014, 14:51
What kind of voltage divider you are using?
If you try to get full 0-5V scale with 0-15V input, then you should use 1:3 ratio (e.g. 22kOhm and 11kOhm resistors) to get best measurement accuracy.

With this divider 15V -> 5V (and 0V -> 0V) at ADC input pin, ADC should show 255 (8bit) or 4095 (12bit).


I'm using a 10K and a 3.3K and my ADCbits is set to 10. Here's the ADC setup (maybe I'm doing something wrong there)



DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
ADCON0 = %11000000 ' Set ADC_CLOCK to Frc,
ADCON1 = %10000000 ' Right-Justify result in ADRESH:ADRESL registers, use Vss and Vdd as voltage refernces

andywpg
- 30th January 2014, 15:24
BTW, I should mention that its a 16F886

Gusse
- 30th January 2014, 18:28
Take a look to Voltage Divider Example (http://www.picbasic.co.uk/forum/content.php?r=245-Voltage-Divider-Example).
There is everything you need.

10k and 3k3 doesn't give you 1:3 divider. Resistor ratio in your case should be 1:2 (e.g 11k and 22k).
Link above explain also divider math to calculate right values.

EDIT: Earlier post 12bit (4095) should have been 10bit (1023). My misstake!

andywpg
- 30th January 2014, 19:25
Take a look to Voltage Divider Example (http://www.picbasic.co.uk/forum/content.php?r=245-Voltage-Divider-Example).
There is everything you need.


Problem solved! When I use the example code and plug in my actual resistor values, I get a reading that is within .1V of the actual. MORE than adequate for what I need. I only wanted it because the smoker controller could be running off battery - and I want to be able to shut it down and generate an alarm if the voltage reaches 10.5V. The user can also press a button while in auto mode to see the current voltage. A tenth of a volt out is NOTHING.

Thanks!

Andy