PDA

View Full Version : division problem 500000 / 133



lerameur
- 24th May 2013, 04:03
Hello,

I need to divide 500000 by my input
so 500000 / input Ex: 500000/145= 3448
Obviously this is a 20 bit number.
tried reducing to 50000, but I get stuck with a decimal, used :
input1 = 50000 / 145 '=344
output1= input1 >> 2 '=86

1) How do I combine these two numbers to create one?
2) maybe there is another and better way to do it ?

K

lerameur
- 24th May 2013, 04:21
Thought I had it,
but there is something fishy about the shifting i believe..

input1 = 50000 / 142
output1= input1 >> 2
input1= input1 * 10
output1 = output1 /10
input1 = input1 +output1

K

HenrikOlsson
- 24th May 2013, 06:14
Hi,
If you're running on a 18F series then you could enable support for LONGS in the compiler and simply be done with it. It will increase the code size quite a bit though so I understand if you're trying to avoid it.

Have you looked at the DIV32 operator? I'm not 100% sure it'll work with a variable but I think it will, try it out:

Dummy VAR WORD
Result VAR WORD
Divider VAR WORD

' Be aware that Divider must be at least 8 or Result will
' overflow and show the wrong result.
Divider = 145

Dummy = 500*1000 ' Produce intermediate result of 500000
Result = DIV32 Divider ' Divide the intermediate result

/Henrik.

lerameur
- 24th May 2013, 11:21
Hi,

I am using the 16F88 chip..
tried your code but shows 65535 on LCD


K

Dave
- 24th May 2013, 11:57
Here is a snipit form a program I use and it works great:
'************************************************* *******************
CALCF_C: 'CALCULATE DEG.F and DEG.C for LM 95231 TEMPERATURE SENSOR READING
'************************************************* *******************
CALCF:
SCRATCH = TEMPREAD * 1000
TEMPF = DIV32 17777
TEMPF = TEMPF * 10
TEMPF = TEMPF + OFFSET
CALCC:
SCRATCH = TEMPREAD * 10
TEMPC = DIV32 32
RETURN

Just make sure you don't have any interrupts going on when using DIV32.
I also think your problem may be that you are using a variable after the DIV32 keyword. Try entering the constant value and see if that doesn't work.

lerameur
- 25th May 2013, 15:31
Dummy = 500*1000 ' Produce intermediate result of 500000
Result = DIV32 133

lcdout $FE,2, "Duty:", dec Result

... still getting 65535 as output

Thats all I have in the program, no interrupt nothing else !

then I initialized my parameters this way:
a var word
b var word
a= 500
b = 1000
Dummy VAR word
Result VAR WORD
Divider con 145

Dummy = a * b ' Produce intermediate result of 500000
Result = DIV32 133

and its now working.. Dummy doesnt like numbers ;)