ADC issues...
Moderator: Benj
-
- Posts: 44
- Joined: Sat Dec 11, 2010 8:50 pm
- Has thanked: 8 times
- Been thanked: 3 times
- Contact:
ADC issues...
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: ADC issues...
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
)
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
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
![Razz :P](./images/smilies/icon_razz.gif)
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
Martin
-
- Posts: 44
- Joined: Sat Dec 11, 2010 8:50 pm
- Has thanked: 8 times
- Been thanked: 3 times
- Contact:
Re: ADC issues...
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: ADC issues...
Perhaps floating point for mAs would be the answer?saakis wrote:If the ms are less than 1000 the result of mAs is zero...is it possible to work with strings?
Sakis
When I get a chance, Will see what I can come up with.
You may get someone else posting answer before I do.
Martin
-
- Posts: 594
- Joined: Thu Sep 17, 2009 7:52 am
- Location: Belgium
- Has thanked: 63 times
- Been thanked: 102 times
- Contact:
Re: ADC issues...
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'
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'
-
- Posts: 44
- Joined: Sat Dec 11, 2010 8:50 pm
- Has thanked: 8 times
- Been thanked: 3 times
- Contact:
Re: ADC issues...
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: ADC issues...
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
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
- Attachments
-
- mAs_meter16F88_modified.fcf
- (13 KiB) Downloaded 271 times
Martin
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: ADC issues...
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
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
- Attachments
-
- mAs_meter16F88_modified2.fcf
- Float variable Version
- (13 KiB) Downloaded 285 times
Martin
-
- Posts: 44
- Joined: Sat Dec 11, 2010 8:50 pm
- Has thanked: 8 times
- Been thanked: 3 times
- Contact:
Re: ADC issues...
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: ADC issues...
Hi Sakis,
(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
Float takes up a lot of room, especially when involved with calculations. You can get chips with much smaller pin count.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
(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
- Attachments
-
- mAs_meter18F4450.fcf
- changed config.
- (9.5 KiB) Downloaded 272 times
Martin
-
- Posts: 44
- Joined: Sat Dec 11, 2010 8:50 pm
- Has thanked: 8 times
- Been thanked: 3 times
- Contact:
Re: ADC issues...
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: ADC issues...
Your welcome.saakis wrote:Later today I connect it to the analog section to test the whole thing! You will have news soon...
Sakis
Lets hope it works the way you want it to
![Smile :)](./images/smilies/icon_smile.gif)
If not, I'm sure we can help you further.
Martin