Combining 8 bits into one byte for ic2 transfer

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
Roy Johnston
Flowcode V4 User
Posts: 220
Joined: Mon Aug 24, 2009 8:38 am
Has thanked: 2 times
Been thanked: 34 times

Combining 8 bits into one byte for ic2 transfer

Post by Roy Johnston »

I use the following calculations to extract 8 bits from one byte called "porta1"

x[0] = (porta1 AND 1) / 1
x[1] = (porta1 AND 2) / 2
x[2] = (porta1 AND 4) / 4
x[3] = (porta1 AND 8) / 8
x[4] = (porta1 AND 16) / 16
x[5] = (porta1 AND 32) / 32
x[6] = (porta1 AND 64) / 64
x[7] = (porta1 AND 128) / 128

how do I reverse the calculation , I want to combine the 8 bits back into one byte again so I can transmit it to n ic2 port.
thanks in advance

User avatar
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: Combining 8 bits into one byte for ic2 transfer

Post by Benj »

Hello,

This should work.

Code: Select all

porta1 = 0
porta1 = porta1 | (x[0] << 0)
porta1 = porta1 | (x[1] << 1)
porta1 = porta1 | (x[2] << 2)
porta1 = porta1 | (x[3] << 3)
porta1 = porta1 | (x[4] << 4)
porta1 = porta1 | (x[5] << 5)
porta1 = porta1 | (x[6] << 6)
porta1 = porta1 | (x[7] << 7)
Or you could do it via a loop to save a bit of prog memory.

Code: Select all

loop = 0
porta1 = 0
while loop < 8
porta1 = porta1 | (x[loop] << loop)
loop = loop + 1
end while
If you replace your / x with >> 0-7 in your code to get the bits then it may run a lot faster on the hardware depending on the optimisation of the compiler. A shift is a single instruction on the hardware whereas a divide depends on the compiler generating a routine.

Code: Select all

x[0] = (porta1 AND 1) >> 0
x[1] = (porta1 AND 2) >> 1
x[2] = (porta1 AND 4) >> 2
x[3] = (porta1 AND 8) >> 3
x[4] = (porta1 AND 16) >> 4
x[5] = (porta1 AND 32) >> 5
x[6] = (porta1 AND 64) >> 6
x[7] = (porta1 AND 128) >> 7

Roy Johnston
Flowcode V4 User
Posts: 220
Joined: Mon Aug 24, 2009 8:38 am
Has thanked: 2 times
Been thanked: 34 times

Re: Combining 8 bits into one byte for ic2 transfer

Post by Roy Johnston »

Thank you,
works perfectly,

Post Reply