Page 1 of 1

ADC issues...

Posted: Thu Apr 07, 2011 12:00 am
by saakis
Hello Flowcode people,
I have an issue with a program I'm trying to do, which measure a voltage in the input -1000mV in the input corespond to 1000mA- and also measure the time that voltage applied in ms. The problem is when in simulation it is fine but when I programm the 16F88 and put it on the HP-480 evaluation board, I have negative values for the product mAs=(mA*time)/1000 because time is in ms. Also when I tranfer the circuit on a test board I made, the value of mA never going more than 99;it supposed could measure 5000mV. Does anybody has an idea what is going wrong? And could you help me in the multiplication to have one decimal poind to be more accurate?
Thank you
Sakis
mAs_meter16F88.fcf
(8.5 KiB) Downloaded 298 times

Re: ADC issues...

Posted: Thu Apr 07, 2011 8:45 pm
by medelec35
Hi saakis,
Reason your number is going negative on your hardware but appears OK on simulator is because they calculate in different ways. Hardware is using singed integers whereas simulator is using 16bit maths, then converting the result into signed integer. (well I think I read that some where :P )

In reality this means on real hardware you cannot multiply numbers together if the result will exceed 32767 even if you divide after to reduce total (as in your formula). If you do the variable will roll over and a negative number will be produced.

Difference with simulator, it will allow formula to go over 32767 and divide down again.

Try this:
mAs = mA*(time/1000)

Martin

Re: ADC issues...

Posted: Thu Apr 07, 2011 10:44 pm
by saakis
Hello Martin,
thank you for your advice, I tested but a new problem came up...If the ms are less than 1000 the result of mAs is zero...is it possible to work with strings?
Sakis

Re: ADC issues...

Posted: Fri Apr 08, 2011 12:34 am
by medelec35
saakis wrote:If the ms are less than 1000 the result of mAs is zero...is it possible to work with strings?
Sakis
Perhaps floating point for mAs would be the answer?
When I get a chance, Will see what I can come up with.
You may get someone else posting answer before I do.

Re: ADC issues...

Posted: Fri Apr 08, 2011 1:32 pm
by Spanish_dude
Multiply your ADC value by 10, this will give you a 0.1 precision.

An integer can't have point numbers: 5 / 2 = 2 (for an integer)
So multiplying it by 10 would give : (5 * 10) / 2 = 25

Next thing you'll need to do is put a '.' between '2' and '5'

Re: ADC issues...

Posted: Fri Apr 08, 2011 6:24 pm
by saakis
Hi,
The problem with that is that the product mAs=(mA*time)/1000 goes to high and I have negative numbers when it gets more than 32767...One way is to increase the delay to 10 ms but in that way I have redused resolution...
Sakis

Re: ADC issues...

Posted: Sat Apr 09, 2011 9:47 am
by medelec35
Hi Sakis,

Try attached flowchart.
It's the simplest solution I can think of.
Hopefully will work OK?
Not had chance to test it properly.
If values are low, there will be rounding issues

As said previously, floating point is probably give the best results, but I'm not that good with manipulating just a single floating point variable.
If I suss it out, then will post another flowchart.

Martin

Re: ADC issues...

Posted: Sat Apr 09, 2011 11:00 am
by medelec35
I have tried with the float version (on a 18F4455) as it is really accurate.
However there is a catch.
It will not fit onto a 16F88.
If you want to use the float version then you will have to use a chip with more memory.

Martin

Re: ADC issues...

Posted: Sun Apr 10, 2011 8:43 am
by saakis
Good morning Martin,
I tried to compile the secont Flowcode to 16F88 but it wasn't possible due to limits(?) of the programm version I have. Any way I compile it to 18F4450 but I don't know why the programm didn't run at all...What I did
wrong?
Sakis
mAs_meter18F4450.fcf
(9.5 KiB) Downloaded 317 times

Re: ADC issues...

Posted: Sun Apr 10, 2011 10:55 am
by medelec35
Hi Sakis,
medelec35 wrote: It will not fit onto a 16F88.
If you want to use the float version then you will have to use a chip with more memory.

Martin
Float takes up a lot of room, especially when involved with calculations. You can get chips with much smaller pin count.
(see http://www.matrixmultimedia.com/mmforum ... 40&p=22201 )
E.g 16F1827 that will work, but the catch with that chip is, until next planned update, you will need to use the hitec compiler


Reason your program did not run is down to the configuration settings.
LCD is connected to portB, yet the low voltage programming was enabled, which makes pin RB4 unusable (only available for low voltage programming).
LCD on port B requires the use of RB4, Therefore you would have no display.
I personally leave port B as the last resort for connecting LCD's or LED's etc. That way I can leave RB6/RB7 free for ICD debugging.
That has helped me loads.

If you wanted to time how long current is detected and do other things at the same time, e.g flash LED, have something different displayed e.g value of detected current etc.
Instead of a loop while current>1, you could have a timer interrupt for timing.
This would not effect the accuracy, no matter what the rest of program was doing during the timing period.

See if attached program works OK.


Martin

Re: ADC issues...

Posted: Mon Apr 11, 2011 12:43 am
by saakis
Good morning Martin,
now it works!!! Thank you.Later today I connect it to the analog section to test the whole thing! You will have news soon...
Sakis

Re: ADC issues...

Posted: Tue Apr 12, 2011 7:19 am
by medelec35
saakis wrote:Later today I connect it to the analog section to test the whole thing! You will have news soon...
Sakis
Your welcome.
Lets hope it works the way you want it to :)

If not, I'm sure we can help you further.