-
Can't multiply by 2!
I'm at a loss, and I think I need some edumacation on pbp's math system.
I'm trying to do a simple ADC voltage reading. The voltage source is a ~8 volt battery, so I made a voltage divider. With my meter I read 4.1 after the divider (which is the input to the PIC.)
Here is my code (the important parts):
Code:
Quanta con 1274
ADCIN 0, ADval ' Read A/D on RA0
ADval = (ADval*10) */ Quanta
ADval1 = ADval * 2
SEROUT2 PORTB.1,Baud,[dec ADval dig 4,".",dec4 ADval," Vdc",$0D,$0A]
SEROUT2 PORTB.1,Baud,[dec ADval1 dig 4,".",dec4 ADval1," Vdc ADVal1",$0D,$0A]
My results are:
Code:
4.1056 Vdc
1.6576 Vdc ADVal1
Obviously I'm doing something right as I'm getting my 4.1 volt measurement. My problem is trying to multiply it by 2 (as you can see...)
I also tried ADval1=ADval + ADval and ADval=ADval << 2, both resulting in wierd numbers.
I suppose I'm just not getting the whole floating point thing with pbp. Can someone enlighten me?
Thanks,
Jeff
-
4.1056 Vdc is actually 41,056.
41,056 * 2 = 82,112 which is larger than a word sized variable can hold.
82,112 - 65536 = 16,576 which is why you're seeing 1.6576 returned in AdVal1.
Try something like ADval1 = (ADval/10) * 2, and you should be back in the ball game.
This will return 8,211 so --
SEROUT2 PORTB.1,Baud,[dec ADval1 dig 3,".",dec3 ADval1," Vdc ADVal1",$0D,$0A]
-
Ahhh... that helps greatly. Thanks!!!
Jeff