counting pulses & dividing by 2 to get .5??

torana

New Member
Hi gents.. First time caller.

I hope you can help me with what I hope is some simple code.
I am trying to count input tips from a small bucket rain gauge collecting rain water calibrated to .5 mm per tip.
This will be displayed on a LCD with the temperature.. That part is ok.. its the sorting out how to display the decimal place
in ether .0 or .5 MM & count up from there. ( Maths not my strong point )


Thanks. Paul
 

erco

Senior Member
PicAxe (like many uC's) only does integer math. Fractions & decimals are simply lost. So you'll have to do some math manipulation to keep all significant values non-fractional. Multiplying everything by 10 will make your 0.5 into a 5, for instance. Then you can parse your numbers onto a display. For instance 10.5 would be internally stored as 105, then you would break that into a 10 and 5 for display purposes.
 

nick12ab

Senior Member
Follow erco's advice on storing the variable, then to insert the decimal place, use the bintoascii command to break the variable into separate ASCII characters and then the decimal point can be inserted where necessary.

Example for serial LCD: pin = pin constant for pin serial LCD is connected to, baud = serial LCD baud rate, usually N2400
Code:
bintoascii b0,b1,b2,b3
serout [I]pin[/I],[I]baud[/I],(254,128,b1,b2,".",b3)
Example for 8-bit parallel LCD: enable = pin constant for pin enable line of LCD is connected to, rs = pin constant for register select line of LCD, lcddata = port LCD data bus is connected to (e.g. pinsB)
Code:
bintoascii b0,b1,b2,b3
low [I]rs[/I]
[I]lcddata [/I]= 128 : pulsout [I]enable[/I],1
high [I]rs[/I]
[I]lcddata[/I] = b1 : pulsout [I]enable[/I],1
[I]lcddata [/I]= b2 : pulsout [I]enable[/I],1
[I]lcddata [/I]= "." : pulsout [I]enable[/I],1
[I]lcddata [/I]= b3 : pulsout [I]enable[/I],1
 

westaust55

Moderator
Welcome to the PICAXE forum.

Another method starting with erco's concept:

Count10 = BucketTips * 10 / 2
Integer = Count10 / 10
Decimal= Count10 // 10
SEROUT <pin>, <baud>, (#Integer,".",#Decimal,cr,lf)
 
Last edited:

mrburnette

Senior Member
Welcome to the PICAXE forum.

Another method starting with erco's concept:

Count10 = BucketTips * 10 / 2
Integer = Count10 / 10
Decimal= Count10 // 10
SEROUT <pin>, <baud>, (#Integer,".",#Decimal,cr,lf)
@Paul ... you can run the following in the simulator (use 08M2 chip template) to see how the math works..

Code:
Symbol Count10 = W0
Symbol BucketTips = W1
Symbol Integer = W2
Symbol Decimal = B6

; Simulate bucket tips
For BucketTips = 0 to 1000

	Count10 = BucketTips * 10 / 2
	Integer = Count10 / 10
	Decimal= Count10 // 10
	SERTXD (#Integer,".",#Decimal,cr,lf)
	
Next BucketTips
- Ray
 

torana

New Member
Thanks everyone for your help.
I have got it going... Such a simple answer. Here I was going the the wrong way with look up tables & stuff.

Thanks all again.

Paul
 
Top