PDA

View Full Version : Decimal value



leonel
- 6th April 2005, 17:31
If I have one ADC value of 819 thatīs one value of 15.9 in my corrent whatīs the code to measure with one decimal my corrent. Iīm having a little difficulties to measure with one decimal. Iīm using PIC16F872 and 10 ADC bits.

Melanie
- 6th April 2005, 19:33
If ADC value of 819 equals say 15.9mA then just do some math to convert 819 into 159 and remember there is one decimal place in your answer which needs attention whenever you display...

819*1942/10000=159

so...

The variables...

ADCValue var Word
TempA var Word
TempB var Word
Result var Word

The Calculation (see PBP manual section 4.17.8 DIV32 command)...

TempA=1942
TempB=ADCValue*TempA
Result=DIV32 10000

The display (see PBP manual 4.17.7 DIG command)...

LCDOut $FE,1
If Result>99 then LCDOut #Result DIG 2
LCDout #Result DIG 1,".",#Result DIG 0,"mA"

Your display will show... 15.9mA

leonel
- 7th April 2005, 08:21
Ok thankīs a lot.
I was forgetting DIG command. I was using / and // to give me the values, but itīs much simple DIG command.
Regards
Leonel Monteiro

leonel
- 7th April 2005, 15:51
I have one problem:
I achieve my best performance in low corrents doing this code:
ADCIN 0, ADC_corrente
pause 10
x = ADC_corrente * 2360
res = DIV32 10000
if res > 99 then
unid_corrente = res DIG 0
dec_corrente = res / 10
else
unid_corrente = res DIG 1
dec_corrente = res DIG 0
endif

but when ADC value * 2360 is biggest then 65535 i lost my upper 16bits, because the operator * returns the lower 16 bits of a 32 bits result.
What i have to do to doesnīt lost bits?
I know ** returns the upper 16 bits but there are any way to have the result in one variable?

Melanie
- 7th April 2005, 16:24
>> any way to have the result in one variable?

Yes, the ONE VARIABLE answer is in RES.

Look...

The maximum value for your 10-bit ADC is 1024...

Multiply this by 2360 gives 2416640...

divide this by 10,000 gives 241

everything is within range of PBP's capabilities...

The dummy WORD value 'x' when used as x=ADC*2360 is irrelevant, you follow that immediately with your DIV32 statement so the correct value of 241 ends up in RES.

Remember that RES is a factor of TEN greater... ie RES=241 is actually 24.1 so just do the math to extract what you need.

You can't have floating points in integer math, sometimes you have to "think outside of the box" in order to achieve what you want.

leonel
- 7th April 2005, 16:39
Your right. I had a wrong code, so i though it was because multiplication has an 32 bits result.
The correct code is:
ADCIN 0, ADC_corrente
pause 10
x = ADC_corrente * 2360
res = DIV32 10000
if res > 99 then
dec_corrente = res DIG 0
unid_corrente = res / 10
else
unid_corrente = res DIG 1
dec_corrente = res DIG 0
endif