PDA

View Full Version : divide



daydream
- 8th May 2012, 03:39
hi,i got x as my freq.x changes betwwen 1 and 65535.using pbp how do i divide 1,x to get the freq
and show the dec result on my lcd?
thx.

shahidali55
- 10th May 2012, 13:56
Maybe you can do something like this . . .
First, find out when x increments by 1, what is increment in frequency (call this the step size)
Second, find out what is the frequency when x = 1 (call this the minimum frequency).
Then to find actual frequency from x you can do some math like below:
actual frequency = minimum frequency + (x * step size)
Now you can print the actual frequency on the lcd using LCDOUT (assuming you are using a HD44780 based LCD display).

SteveB
- 10th May 2012, 21:08
Assuming:
1) You can use LONG variables
2) You are looking for the time of each period


Freq VAR LONG ' Length of time for each period

SELECT CASE x
CASE = 1
LCDOUT "001.000 Sec"
CASE <=1000
Freq = 1000000/ x
LCDOUT DEC3 Freq/1000,".",DEC3 Freq," mSec"
CASE ELSE
Freq = 1000000000/ x
LCDOUT DEC3 Freq/1000,".",DEC3 Freq," uSec"
END SELECT

daydream
- 11th May 2012, 06:19
hi,x is my frequency and it increments by 1.x changes betwwen 1 and 65535.i want to divide 1,x to get the period and show the period on lcd.i cant use long var.
what should i do?
thx

SteveB
- 11th May 2012, 18:17
Simple answer:
Because PBP uses integer math, you won't get any meaningful value from 1/x.

You will need to think in terms of integers and scale your values appropriately.

The other alternatives is to try the floating point routines found here: http://melabs.com/resources/fp.htm

Charlie
- 12th May 2012, 09:58
First of all, use longs because you will need the bit space. Next decide your units that will result in an integer to display. For this example, lets choose ms. Since there are 1000 ms in a second, to get period as whole ms, instead of dividing 1 by x (Hz), divide 1000 by x(Hz) which is the same as (1/x)*1000.
Obviously, if you want units of us then it's 1,000,000/x. You can see why longs are required.

daydream
- 12th May 2012, 14:19
hi steveB,i used the floating point routines and it works,so thanks.