Maker Pro
Maker Pro

24 bit math so I can read and write

R

Randy

I am writing and reading to a microcontroller.
My largest allowable variables are 16 bit and 8 bit
How can I write a 24 bit value when I am limited by 16 and 8 bit
variables?

For instance I need to send a register value of HEX 200110 which is
configured for 24 bits.
This is equal to 2,097,424 in decimal format.
How do I divide this up into a 16 and 8 bit or 3 8-bit variable values
to send and receive?
I can send it HEX or Binary.

Thanks
 
J

James Beck

I am writing and reading to a microcontroller.
My largest allowable variables are 16 bit and 8 bit
How can I write a 24 bit value when I am limited by 16 and 8 bit
variables?
You hit on it below.
You read/write 3 8 bit pieces.
For instance I need to send a register value of HEX 200110 which is
configured for 24 bits.
This is equal to 2,097,424 in decimal format.
How do I divide this up into a 16 and 8 bit or 3 8-bit variable values
to send and receive?
I can send it HEX or Binary.

Well it kinda' depends.
How is the variables laid our in memory?
If it were an HC05 part I would know the variable is most likely stored
as three 8 bit values with the lowest address holding the upper byte
address+1 is the middle byte and address+2 is the low byte.
Other CPU/MCUs do it just the opposite.
Google "big endian" or "little endian"
Now, all you have to do is send those 3 bytes and you are done.
If you are using a high level language that supports INT24s then the
task is even easier. Just do shifts and mask off the byte you want to
send.

Jim
 
N

Noway2

Randy said:
I am writing and reading to a microcontroller.
My largest allowable variables are 16 bit and 8 bit
How can I write a 24 bit value when I am limited by 16 and 8 bit
variables?

For instance I need to send a register value of HEX 200110 which is
configured for 24 bits.
This is equal to 2,097,424 in decimal format.
How do I divide this up into a 16 and 8 bit or 3 8-bit variable values
to send and receive?
I can send it HEX or Binary.

Thanks

If the platform you are using does not support numbers large enough for
your needs, then you need to roll your own routines. A while back, I
worked on an application that performed 24 bit arithmetic with an 8 bit
Motorola 65xx series processor. The math routines were modeled after
the grade school methods of long hand multiplication and division.

As far as transferring or storing data, it doesn't matter what format it
is in. To the computer it is all the same. You need to decide on the
protocol that will be used between the devices. The data doesn't
matter, the key is how it is interpreted.
 
J

John

I am writing and reading to a microcontroller.
My largest allowable variables are 16 bit and 8 bit
How can I write a 24 bit value when I am limited by 16 and 8 bit
variables?

For instance I need to send a register value of HEX 200110 which is
configured for 24 bits.
This is equal to 2,097,424 in decimal format.
How do I divide this up into a 16 and 8 bit or 3 8-bit variable values
to send and receive?
I can send it HEX or Binary.

Thanks

You might want to check on the PICAXE forum
http://www.rev-ed.co.uk/picaxe/forum/forum.asp?forum_id=30&forum_title=PICAXE+Forum

There's been recent discussion on how to do 24 (and 32) bit
arithmetic. Since the PICAXE is programmed in a dialect of BASIC, the
conversion to Stamp should be relatively simple.

John
 
J

James Beck

You might want to check on the PICAXE forum
http://www.rev-ed.co.uk/picaxe/forum/forum.asp?forum_id=30&forum_title=PICAXE+Forum

There's been recent discussion on how to do 24 (and 32) bit
arithmetic. Since the PICAXE is programmed in a dialect of BASIC, the
conversion to Stamp should be relatively simple.

John
I might have been a bit slow, BUT I think he is trying to write to some
type of device's configuration register. If that is the case just hard
coding the three bytes into the code would be good enough to get by. I
think what threw me was the "math" word.

Jim
 
Top