PDA

View Full Version : converting long integer



jra
- 24th September 2004, 23:10
Hello,

Wondering if anyone can provide an algorithm for converting
a long integer ( up to 32 bit ) into two 16 bit words. The integer
value will be sent serially to the pic. I then need to convert that value into two word variables... a high word and a low word.

This is easy enough to do in Visual basic on a pc but I need to do it on the pic.

Is there some binary arithmetic trick or hex conversion i can use ?

Thanks

Jay

carl_schell
- 25th September 2004, 16:03
Hi Jay -

It is hard for me to see exactly what you are looking for, but one thought that crosses my mind is this...but maybe this will give you an idea to solve your particular problem...I used a similar routine to solve a problem where I needed to divide using 1 million...so you multiply two larger numbers to give you this, which you cannot store, but you can opperate on...let me demonstrate briefly

You have the ability to multiply two 16-bit variables...this then places the full 32-bit result internally...you can the use the DIV32 command (you must ensure than no other operations occur before using the DIV32 command and should disable any interrupts you may be using)

ie:
'2^16 => 65535 so this is the largest number availible you can store in a variable
'However!...you can deal with larger numbers easily

j var word
k var word
dummy var word


j = 1000

Disable

dummy = j * j ' 1,000 * 1,000 = 1,000,000
k = DIV32 50 ' 1,000,000 / 50 = 20,000

Enable

Sorry, I am out of time for now (late for my daughter's swimming lesson), but start with this and look at the manual under the DIV32 command for more info...and write back as to any further ideas...i will adress them when i get more time...

Carl

jra
- 25th September 2004, 17:03
Thanks for the reply Carl

I am aware of the DIV32 command . Actually what i am trying to do is use the pic as a pulse generator. I want to serially input a decimal value between 0 and say 100,000,000 and have the pic generate square wave output pulses equal to the number that was serially input. So, If the pc sends 789267 to the pic it spits out that many pulses.

This is simple if i had 32 bit variables.

I think I have figured out a simple solution. I'll post the code
if it works.

However, If anyone has a solution it would be appreciated

Thanks

Jay

Bruce
- 25th September 2004, 23:33
Why not just use one word value as the multiplier for the second in a FOR NEXT loop, and not even mess with large numbers?

Receive two words from your PC, then send (A*B) number of pulses.


A VAR WORD
B VAR WORD
X VAR WORD ' Outter loop
Y VAR WORD ' Inner loop

Out_Pulse:
FOR X = 0 TO A ' Send A*B pulses
FOR Y = 0 TO B
SEND PULSE HERE
NEXT Y
NEXT X
GOTO ?


You have up to 65,536 * 65,536 pulses without a drop of math.

jra
- 26th September 2004, 00:14
Thanks Bruce,

I have already considered this solution but it's a pain in the neck for the end user. I don't want the user to have to convert the number of pulses into 2 16 bit words. They may be sending pulse info via hyperterminal. Just want to send a decimal number of pulses for ease of use.

I have come up with a simple solution.

Thanks for your help

Jay

Ingvar
- 27th September 2004, 17:02
Perhaps this will be to your liking.

/Ingvar


@ device pic16F876, hs_osc

DEFINE OSC 20

TxPin VAR PortA.0
RxPin VAR PortA.1
PulsPin VAR PortA.2

HiWord VAR WORD
LoWord VAR WORD

Dummy1 VAR WORD
Dummy2 VAR WORD

Dummy VAR BYTE

ADCON1 = 7
TrisA = %11111010
WHILE 1
SEROUT2 TxPin,84,["Enter number of pulses(9 digits)!",13,10]
'Recieve Hi and Low part of pulsnumber
'Number must always be transmitted as 9 digits(leading zeroes) and
'not greater than 655359999.
SERIN2 RxPin,84,[DEC5 HiWord]
SERIN2 RxPin,84,[DEC4 LoWord]
'Debuginfo
SEROUT2 TxPin,84,["Sending ",DEC5 HiWord, DEC4 LoWord, " pulses.",13,10]

'Send the pulses
For Dummy1 = 1 to HiWord
For Dummy2 = 0 to 9999
PulsPin = 1
PulsPin = 0
NEXT
NEXT
For Dummy1 = 1 to LoWord
PulsPin = 1
PulsPin = 0
NEXT
WEND


END