Hi Ben
Can you advise simplest flowchart way to break down large integer values ie 144000000
into a separate byte array as i need to send serially to another device in bcd format
Ie re above example of 144000000
ie byte[0] = bcd of 1
ie byte[1] = bcd of 4
ie byte[2] = bcd of 4
etc etc
Also
The reverse of this ie receive in bcd and convert to a long integer
PS using DSCPIC
An example would be great
Many thanks in advance
David
BCD
-
- Posts: 140
- http://meble-kuchenne.info.pl
- Joined: Wed Dec 02, 2020 7:35 pm
- Been thanked: 18 times
-
- Valued Contributor
- Posts: 1213
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 118 times
- Been thanked: 622 times
Re: BCD
Hi David,
Why not convert to a string and pass that using the conversion routines built in. BCD (binary coded decimal) would suggest two digits per byte sent and was used to make displaying numbers in decimal easy (each 4 bits is a digit) - the dibble-dabble algorithm does this (and can be found on the matrix forum)
Martin
Why not convert to a string and pass that using the conversion routines built in. BCD (binary coded decimal) would suggest two digits per byte sent and was used to make displaying numbers in decimal easy (each 4 bits is a digit) - the dibble-dabble algorithm does this (and can be found on the matrix forum)
Martin
-
- Matrix Staff
- Posts: 1384
- Joined: Sat Dec 05, 2020 10:32 am
- Has thanked: 184 times
- Been thanked: 322 times
Re: BCD
Hi David,
If you are in control of both sides of the conversion, then there's no need to use BCD and things would be more efficient and easier to code to simply convert the large integer into its upper/lower byte values.
If you 'think' of the number in hexadecimal, this is trivial. In your example, 144000000 = 0x08954400. Each 2 hex digits comprises of a single byte, so this naturally splits up into the following 4 bytes: 0x08, 0x95, 0x44 and 0x00. 4 bytes can represent any unsigned number between 0 and 4,294,967,295 (i.e. 0x00000000 to 0xFFFFFFFF) - in Flowcode we call this a "ULONG" integer type.
To split this up, simply use masking and bit shifting. The following example can be put into a calculation icon:
and reconstituting is also trivial, again in a single icon:
Note that none of this requires the ulong to be 'written' in hexadecimal or for you be converting or displaying it as such. Any integer in the program is not defined or restricted by its visual (i.e. human-readable) format. The computer or micro running your code 'sees' every integer as a series of 1's and 0's. So don't worry about me mentioning hexadecimal (but as a programmer, it is very useful to see integers as hexadecimals because this better represents the 1's and 0's that a computer 'sees').
I've not checked that these work. But I think they should.
OTOH, if you do require specific BCD conversion, that is not too difficult either. It's just more wasteful in terms of effort, processing speed and memory. You would only need to do this if you were not in control of the other side of the conversion and were forced to convert to BCD.
HTH,
Steve.
If you are in control of both sides of the conversion, then there's no need to use BCD and things would be more efficient and easier to code to simply convert the large integer into its upper/lower byte values.
If you 'think' of the number in hexadecimal, this is trivial. In your example, 144000000 = 0x08954400. Each 2 hex digits comprises of a single byte, so this naturally splits up into the following 4 bytes: 0x08, 0x95, 0x44 and 0x00. 4 bytes can represent any unsigned number between 0 and 4,294,967,295 (i.e. 0x00000000 to 0xFFFFFFFF) - in Flowcode we call this a "ULONG" integer type.
To split this up, simply use masking and bit shifting. The following example can be put into a calculation icon:
Code: Select all
byte_array[0] = ulong AND 0xFF
byte_array[1] = (ulong AND 0xFF00) >> 8
byte_array[2] = (ulong AND 0xFF0000) >> 16
byte_array[3] = (ulong AND 0xFF000000) >> 24
Code: Select all
ulong = byte_array[0]
ulong = ulong + (byte_array[1] << 8)
ulong = ulong + (byte_array[2] << 16)
ulong = ulong + (byte_array[3] << 24)
I've not checked that these work. But I think they should.
OTOH, if you do require specific BCD conversion, that is not too difficult either. It's just more wasteful in terms of effort, processing speed and memory. You would only need to do this if you were not in control of the other side of the conversion and were forced to convert to BCD.
HTH,
Steve.
-
- Posts: 140
- Joined: Wed Dec 02, 2020 7:35 pm
- Been thanked: 18 times
Re: BCD
Hi
Many thanks to both of you for the quick responses I will investigate both today and will revert
Note Unfortunately i am not in control of both sides of the communication hence the request for help with the BCD
David
Many thanks to both of you for the quick responses I will investigate both today and will revert
Note Unfortunately i am not in control of both sides of the communication hence the request for help with the BCD
David
-
- Matrix Staff
- Posts: 1610
- Joined: Wed Dec 02, 2020 11:07 pm
- Has thanked: 567 times
- Been thanked: 531 times
Re: BCD
Hi David.
When I created RTC Flowchart, to convert from decimal to BCD I used
As an alternative for conversions from Ulong to bytes, you could use the Type conversions component (with example) for increased efficiency.
For Ulong to byte conversions, I also used the method I posted here.
When I created RTC Flowchart, to convert from decimal to BCD I used
Code: Select all
Seconds = (Seconds / 10 << 4) + (Seconds MOD 10)
For Ulong to byte conversions, I also used the method I posted here.
Martin
-
- Posts: 140
- Joined: Wed Dec 02, 2020 7:35 pm
- Been thanked: 18 times
Re: BCD
Folks
Thanks for all your help / suggestions
As i cannot change both ends I had to use BCD.
But as i already had a string representation of the long integer for other reasons in the program
i chose the first suggestion IE manipulate a string
Able to do this very simple in circa 8 lines in a calculation box within 1 loop
Also did virtually the reverse to receive data
So now all working fine
As a matter of interest this was to allow me to communicate to a XIEGU Transceiver using ICOM BCD CAT protocol
David
Thanks for all your help / suggestions
As i cannot change both ends I had to use BCD.
But as i already had a string representation of the long integer for other reasons in the program
i chose the first suggestion IE manipulate a string
Able to do this very simple in circa 8 lines in a calculation box within 1 loop
Also did virtually the reverse to receive data
So now all working fine
As a matter of interest this was to allow me to communicate to a XIEGU Transceiver using ICOM BCD CAT protocol
David