Hi,
I'd be grateful for any advice.
I need to output a 40 bit word serially from a single port.
I have figured out a way to do it that works but it seems very wasteful and inefficient.
What I've done is represent the 40 bit word as a string array (which uses up 40 bytes instead of 5 bytes) and then output each byte of the array in turn.
There must be a more direct way of outputting a binary number sequentially (LSB first) just can't find it / figure it out.
If you can help!
Thanks
G
Binary word serial output
Moderator: Benj
- 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: Binary word serial output
Hello G,
Could you do it like this.
Create a byte variable in the variable manager called output[5].
Then assign your binary value by passing in byte constants or variables.
output[0] = 0x5A
output[1] = byte1
Or you can simply clear the variable and then set the bits which need to be logic 1.
output[0] = 0
output[0] = 1 << 4
output[0] = 1 << 7
Once you have the data inside the 5 bytes you should be able to create a loop to stream out the values starting with the LSb from byte 0 and ending with the MSb from byte 4.
Could you do it like this.
Create a byte variable in the variable manager called output[5].
Then assign your binary value by passing in byte constants or variables.
output[0] = 0x5A
output[1] = byte1
Or you can simply clear the variable and then set the bits which need to be logic 1.
output[0] = 0
output[0] = 1 << 4
output[0] = 1 << 7
Once you have the data inside the 5 bytes you should be able to create a loop to stream out the values starting with the LSb from byte 0 and ending with the MSb from byte 4.
Code: Select all
while (byteindex < 5)
{
innerindex = 0
while (innerindex < 8)
{
if (output[byteindex] & (0x01 << innerindex))
Yes: Output 1 to Port Pin
No: Output 0 to Port Pin
delay_ms(1)
innerindex = innerindex + 1
}
byteindex = byteindex + 1
}
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