Hi
Sorry - cryptic title
OK I need to read in a 32bit (8 Bytes) number from four registers of an external device, then add a 24 bit number (6 Bytes) to it and finally load the resultant back into a device.
The reading and writing bit I can do but how do I achieve the individual byte value addition with the obvious carry AND be efficient?
e.g. FF 1C FF A4
+ 15 F9 00
= FF 32 F8 A4
I know Martin posted something similar to this somewhere but I can't find it
Thanks
Howto :- 32bit addition from 4 Registers
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Howto :- 32bit addition from 4 Registers
Hi,
Do you mean 32/24bit in which case a long should do the trick?
Or 8/6 bytes (64/48bit) ?
Martin
Do you mean 32/24bit in which case a long should do the trick?
Or 8/6 bytes (64/48bit) ?
Martin
-
- Posts: 315
- Joined: Sun Jun 23, 2013 1:29 pm
- Has thanked: 28 times
- Been thanked: 61 times
- Contact:
Re: Howto :- 32bit addition from 4 Registers
Hi Martin,
32bits and 24bits brain fart from datasheetitus. Just to be clear, here's an image from the datasheet. I have to add 1440000 (15 F9 00) to the number held in the table below and then write the result back into a similar address space.
This is what I want to achieve
Ignore the dots (padding)
.........FF 1C FF A4 (an incremental number from 00000000h to FFFFFFFFh
...........+ 15 F9 00 (fixed number to be added)
.......= FF 32 F8 A4 (result to be written back)
I'm presuming when I read the data I would put it into a (4x) 1 Byte variables. what is the procedure to then combine these bytes and add 15 F9 00 then split the resultant into another 4 bytes in readiness to be sent out (MSB first)? Ideally this needs to be in a ISR so needs to be quick
Oh! its over an I2C bus if it makes a difference
Thanks
32bits and 24bits brain fart from datasheetitus. Just to be clear, here's an image from the datasheet. I have to add 1440000 (15 F9 00) to the number held in the table below and then write the result back into a similar address space.
This is what I want to achieve
Ignore the dots (padding)
.........FF 1C FF A4 (an incremental number from 00000000h to FFFFFFFFh
...........+ 15 F9 00 (fixed number to be added)
.......= FF 32 F8 A4 (result to be written back)
I'm presuming when I read the data I would put it into a (4x) 1 Byte variables. what is the procedure to then combine these bytes and add 15 F9 00 then split the resultant into another 4 bytes in readiness to be sent out (MSB first)? Ideally this needs to be in a ISR so needs to be quick
Oh! its over an I2C bus if it makes a difference
Thanks
- LeighM
- Matrix Staff
- Posts: 2178
- Joined: Tue Jan 17, 2012 10:07 am
- Has thanked: 481 times
- Been thanked: 699 times
- Contact:
Re: Howto :- 32bit addition from 4 Registers
Hi,
As Martin mentioned, you need to use unsigned long ints, e.g.
Get your 4 byte values into some ULONG variables:
ULONG_M12
ULONG_M13
ULONG_M14
ULONG_M15
Then the total long value is
ULONG_TOTAL = (ULONG_M15 << 24) + (ULONG_M14 << 16) + (ULONG_M13 << 8 ) +(ULONG_M12)
You can then add whatever you need to ULONG_TOTAL
Then extract the values to be written back:
ULONG_M12 = (ULONG_TOTAL) & 0xFF
ULONG_M13 = (ULONG_TOTAL >> 8 ) & 0xFF
ULONG_M14 = (ULONG_TOTAL >> 16) & 0xFF
ULONG_M15 =(ULONG_TOTAL >> 24) & 0xFF
Hope that helps
Leigh
As Martin mentioned, you need to use unsigned long ints, e.g.
Get your 4 byte values into some ULONG variables:
ULONG_M12
ULONG_M13
ULONG_M14
ULONG_M15
Then the total long value is
ULONG_TOTAL = (ULONG_M15 << 24) + (ULONG_M14 << 16) + (ULONG_M13 << 8 ) +(ULONG_M12)
You can then add whatever you need to ULONG_TOTAL
Then extract the values to be written back:
ULONG_M12 = (ULONG_TOTAL) & 0xFF
ULONG_M13 = (ULONG_TOTAL >> 8 ) & 0xFF
ULONG_M14 = (ULONG_TOTAL >> 16) & 0xFF
ULONG_M15 =(ULONG_TOTAL >> 24) & 0xFF
Hope that helps
Leigh
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Howto :- 32bit addition from 4 Registers
You could also use the code here viewtopic.php?f=7&t=21470
But change the real results to unsigned long. Not sure if this would be quicker than the shift and add approach. A similar technique could be used to unpack the values too.
Martin
But change the real results to unsigned long. Not sure if this would be quicker than the shift and add approach. A similar technique could be used to unpack the values too.
Martin
-
- Posts: 315
- Joined: Sun Jun 23, 2013 1:29 pm
- Has thanked: 28 times
- Been thanked: 61 times
- Contact:
Re: Howto :- 32bit addition from 4 Registers
Thank you both so much.
To model this in FC without the actual hardware I'm guessing I could use,say, 4 rotary switches configured to read 0 to FF - any suggestions on how to see the result of the addition
.
Thanks again
To model this in FC without the actual hardware I'm guessing I could use,say, 4 rotary switches configured to read 0 to FF - any suggestions on how to see the result of the addition
.
Thanks again
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Howto :- 32bit addition from 4 Registers
You can just view the variable(s) in the simulation debugger (tick the display as hex box)- or you can use NumberToHex$ to convert to a string...
Here: I convert to a string and then display on a lcd_16x2 - this displays the result in the simulated display correctly (but that is one awful font!)
Martin
Here: I convert to a string and then display on a lcd_16x2 - this displays the result in the simulated display correctly (but that is one awful font!)
Martin