Page 1 of 1

Binary word serial output

Posted: Sat Sep 22, 2012 5:02 pm
by graemec
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

Re: Binary word serial output

Posted: Mon Sep 24, 2012 10:46 am
by Benj
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.

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
}

Re: Binary word serial output

Posted: Mon Sep 24, 2012 11:08 am
by graemec
Thanks Ben, I'll give that a try.
Regards
Graeme