Hi Gang
As per subject -Something I've not done before
So, what is the procedure to formulate multibyte data and then transmit it over, say, a serial port then then additionally when receiving data the same thing in reverse (obviously using AND or OR) but nailing the new Byte and Bit position to get the required data back out.
Some background - if you look at a data sheet and various data options are shown over a number of bytes, how is this 'word' needs constructed and then the whole lot transmitted as one in either a MSB or LSB format.
I can do it with a single byte but multiples is making my brain fart!
Similarly, if various Bits need to be set programmatically do you just read the bytes then AND the new info then update original or is there a better way (in FC)
thanks
Matt
Constructing Multiple Byte Data
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
-
- Posts: 315
- Joined: Sun Jun 23, 2013 1:29 pm
- Has thanked: 28 times
- Been thanked: 61 times
- Contact:
Re: Constructing Multiple Byte Data
Hi Martin,
Yes I see , ok so if I need to go 3 or more bytes you just do multiples of the Byte value so for the third byte you'd multiply by um (scratching at brain) 2^16 (I think) and if you wanted 4bytes you'd do 2^24 etc
Further to your suggested post you have a holding variable that when constructed out of multiple bytes you then output it (to a UART say) but doesn't this have a limitation of the ULONG variable type Which is what 4Bytes?
What if you wanted more bytes.
Matt
Yes I see , ok so if I need to go 3 or more bytes you just do multiples of the Byte value so for the third byte you'd multiply by um (scratching at brain) 2^16 (I think) and if you wanted 4bytes you'd do 2^24 etc
Further to your suggested post you have a holding variable that when constructed out of multiple bytes you then output it (to a UART say) but doesn't this have a limitation of the ULONG variable type Which is what 4Bytes?
What if you wanted more bytes.
Matt
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Constructing Multiple Byte Data
Hi Matt,
Martin's example will work for up to 32-bit by using multiplications and divides. You could use shifts but unfortunately it's not defined what a shift of over 16-bits does in C. Some compilers will work and some won't.
I'm not sure if an embedded compiler would deal with a 64-bit value but if you needed a long long (64 bit variable, yes it's an awful name) then you could essentially do something like this in C. You could also do similar for 4-byte floating point variables and 8 byte double precision variables. This basically uses an offset to read/write the byte directly without having to manipulate the 8-bit value into the right offset position from the base variable address.
32-bit Union
64-bit Union
Martin's example will work for up to 32-bit by using multiplications and divides. You could use shifts but unfortunately it's not defined what a shift of over 16-bits does in C. Some compilers will work and some won't.
Yes this is correct.(scratching at brain) 2^16 (I think) and if you wanted 4bytes you'd do 2^24 etc
I'm not sure if an embedded compiler would deal with a 64-bit value but if you needed a long long (64 bit variable, yes it's an awful name) then you could essentially do something like this in C. You could also do similar for 4-byte floating point variables and 8 byte double precision variables. This basically uses an offset to read/write the byte directly without having to manipulate the 8-bit value into the right offset position from the base variable address.
32-bit Union
Code: Select all
//Supplementary Code Definition - Already declared for you in Flowcode 7.
typedef union
{
MX_UINT32 AsLong;
MX_UINT16 AsInt[2];
MX_UINT8 AsByte[4];
} MX_Union32;
//Implementation using C
MX_Union32 temp;
temp.AsLong = longvalue;
byte0 = temp.AsByte[0];
byte1 = temp.AsByte[1];
byte2 = temp.AsByte[2];
byte3 = temp.AsByte[3];
Code: Select all
//Supplementary Code Definition
typedef union
{
unsigned long long AsLongLong
MX_UINT32 AsLong[2];
MX_UINT16 AsInt[4];
MX_UINT8 AsByte[8];
} MX_Union64;
//Implementation using C
MX_Union64 temp;
temp.AsLongLong = longlongvalue;
byte0 = temp.AsByte[0];
byte1 = temp.AsByte[1];
byte2 = temp.AsByte[2];
byte3 = temp.AsByte[3];
byte4 = temp.AsByte[4];
byte5 = temp.AsByte[5];
byte6 = temp.AsByte[6];
byte7 = temp.AsByte[7];
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Constructing Multiple Byte Data
Hi Matt,
For more bytes you could check out this thread.
Just Bear in mind that was regarding FC6 which uses a different compiler, so final outcome may or may not be the same.
Martin
For more bytes you could check out this thread.
Just Bear in mind that was regarding FC6 which uses a different compiler, so final outcome may or may not be the same.
Martin
Martin