Sorry, yet ANOTHER mod question


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159

    Default Sorry, yet ANOTHER mod question

    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.


    Code:
    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
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: Sorry, yet ANOTHER mod question

    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
    Last edited by richard; - 30th January 2014 at 02:39.

  3. #3
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: Sorry, yet ANOTHER mod question

    Quote Originally Posted by richard View Post
    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:

    Code:
    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
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: Sorry, yet ANOTHER mod question

    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

  5. #5
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Default Re: Sorry, yet ANOTHER mod question

    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:
    Code:
    VOLTS_VAL = (15 * VOLTS_IN)/255
    12bit ADC:
    Code:
    VOLTS_VAL = (15 * VOLTS_IN)/4095
    Last edited by Gusse; - 30th January 2014 at 11:18.

  6. #6
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: Sorry, yet ANOTHER mod question

    Quote Originally Posted by Gusse View Post
    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)

    Code:
    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
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  7. #7
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: Sorry, yet ANOTHER mod question

    BTW, I should mention that its a 16F886
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  8. #8
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Default Re: Sorry, yet ANOTHER mod question

    Take a look to 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!
    Last edited by Gusse; - 30th January 2014 at 18:36.

  9. #9
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: Sorry, yet ANOTHER mod question

    Quote Originally Posted by Gusse View Post
    Take a look to 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
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

Similar Threads

  1. Help with Pic Delay code mod please
    By g7jiq in forum General
    Replies: 1
    Last Post: - 26th March 2009, 00:06
  2. ERROR: Macro MOD?TCB not found in macro file.
    By JohnP in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th February 2009, 19:10
  3. 18F4620 HPWM Half Bridge Mod problem
    By showtime in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 2nd November 2007, 12:55

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts