Page 1 of 1

Reading An 8 Bit Port - Easier Solution..

Posted: Tue Mar 05, 2019 4:56 pm
by Sparkoids
Hi all,

With FC 5 I am reading an 8 bit port, D0-D7 as one unmasked input...

I am trying to read basically two four bit switches and depending upon their position perform a different action where the port is split in two, D0-D3 and D4-D7...

In each of the two halves the switch readouts I am looking for are the same but priority is given to D0-D3 so if the positions are the same it acts on the first first.

You will say mask the port and read it twice then act upon it but in simulation that takes an extra read and I need it in one go.

The switches are read as the following and in each four bit half there must be a minimum of two switches set (showing zero) which is causing the hassle because the whole port will give more than two set regardless if there is one set in each half.

So I'm looking for and acting upon reading in each half but polling the WHOLE port in one go.

0011
1100
0001
0100

Obviously the binary above could have four zero's before or after it depending if it's D0-D3 or D4-D7.

I am bogged down. Is there an easy way to do this does anyone know please without the need to compare every possible combination in a IF THEN scenario?


James :-}

Re: Reading An 8 Bit Port - Easier Solution..

Posted: Tue Mar 05, 2019 5:16 pm
by Benj
Hi James,

I'm not 100% with you but I can certainly offer some help.

Lets say you read the 8-bit value into the variable SwitchInFull.

You can then split the upper four bits and lower four bits into different variables like this.

SwitchInLow = SwitchInFull & 0x0F
SwitchInHigh = (SwitchInFull & 0xF0) >> 4

You now have two variables with the potential values in the range of 0-15. You could ideally pass each variable as a parameter to a macro in turn to repeat the same code on both.

Does this help?

Re: Reading An 8 Bit Port - Easier Solution..

Posted: Tue Mar 05, 2019 5:19 pm
by Sparkoids
Ben I think it does and I'll have a crack at it tomorrow in work.

Thanks as always fella :-}

Re: Reading An 8 Bit Port - Easier Solution..

Posted: Wed Mar 06, 2019 4:05 pm
by Sparkoids
You sorted it again and it works perfectly on the variable. Thank you Ben as always :-}