Shop OBEX P1 Docs P2 Docs Learn Events
Voltage conversion problem — Parallax Forums

Voltage conversion problem

I'm working on a simple circuit to read a voltage, run it through an ADC, process it with a BS2 and output it to a display. It works fine until I get to x.80 volts. As an example, if I input 3.79 volts, I get an output of 3.79 but if I input 3.80 volts, I get an output of 3.00. I get strange numbers anywhere between 3.80 and 3.99 then it's good once I pass 4.00 volts, again until reaching 4.80 volts. I've attached a snippet of the code I'm using, in this case just for troubleshooting. I'm guessing my problem is in the modulus calculation, it appears that a remainder of 655 or lower works fine while 656 or higher doesn't. In this example code, voltx simulates the output from the ADC, if voltx=3112 (3112/819=3 r655), my output is 3.79. If voltx=3113 (3113/819=3r656), my output is 3.00.
Any ideas what I'm doing wrong?

Comments

  • When you multiply 655 by 100, it overflows.

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2016-11-03 22:53
    Try this:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    ' -----[ Constants ]-------------------------------------------------------
    corv               CON     819      'voltage correction  4096/5=819.2 rounded to 819
    ' -----[ Variables ]-------------------------------------------------------
    volts_bat          VAR     Word     ' battery voltage converted
    voltx              VAR     Word     ' test-simulates ADC output
    '------[ Program Code ]----------------------------------------------------
    voltx = 3144
    volts_bat = voltx ** 8000
    DEBUG "voltx = ", DEC voltx,"  volts_bat = ", DEC volts_bat / 100, ".",  DEC2 volts_bat', CR
    END
    

    The 8000 above is equal to 500 * 65536 / 4096.

    -Phil
  • Thanks guys, I overlooked the 65536 limit, you got me on the right track now :-)
Sign In or Register to comment.