Reading An 8 Bit Port - Easier Solution..

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Sparkoids
Posts: 267
Joined: Mon Sep 14, 2009 10:34 am
Has thanked: 30 times
Been thanked: 19 times
Contact:

Reading An 8 Bit Port - Easier Solution..

Postby Sparkoids » Tue Mar 05, 2019 4:56 pm

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 :-}

User avatar
Benj
Matrix Staff
Posts: 14151
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4330 times
Been thanked: 4079 times
Contact:

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

Postby Benj » Tue Mar 05, 2019 5:16 pm

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?
These users thanked the author Benj for the post:
Sparkoids (Wed Mar 06, 2019 4:05 pm)
Rating: 5.56%
 

Sparkoids
Posts: 267
Joined: Mon Sep 14, 2009 10:34 am
Has thanked: 30 times
Been thanked: 19 times
Contact:

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

Postby Sparkoids » Tue Mar 05, 2019 5:19 pm

Ben I think it does and I'll have a crack at it tomorrow in work.

Thanks as always fella :-}
These users thanked the author Sparkoids for the post:
Benj (Tue Mar 05, 2019 5:21 pm)
Rating: 5.56%
 

Sparkoids
Posts: 267
Joined: Mon Sep 14, 2009 10:34 am
Has thanked: 30 times
Been thanked: 19 times
Contact:

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

Postby Sparkoids » Wed Mar 06, 2019 4:05 pm

You sorted it again and it works perfectly on the variable. Thank you Ben as always :-}
These users thanked the author Sparkoids for the post:
Benj (Wed Mar 06, 2019 4:33 pm)
Rating: 5.56%