Creating 8 Port input\output Switch

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
PJE2017
Posts: 2
Joined: Thu Mar 02, 2017 1:52 am

Creating 8 Port input\output Switch

Post by PJE2017 » Thu Mar 02, 2017 2:10 am

Hi could anyone help please with a pic programme to look at the state of 8 inputs and give an output corresponding to one of the 8 outputs. If any input is activated the other 7 outputs are disabled.
So if any one of the 8 inputs goes low the corresponding output goes high and all other inputs\outputs are disabled. As soon as the low input goes high enables the system again.
It matters not if the input is low or high as I could use a hex inverter to get what I want signal wise if this is easier.
Any help most appreciated.
Regards
Paul

User avatar
phalanx
Non-SFE Guru
Posts: 1972
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: Creating 8 Port input\output Switch

Post by phalanx » Thu Mar 02, 2017 5:51 am

What should happen if two inputs go active at the same time?

Also, what have you written so far or are you looking for someone to write it for you?

-Bill

PJE2017
Posts: 2
Joined: Thu Mar 02, 2017 1:52 am

Re: Creating 8 Port input\output Switch

Post by PJE2017 » Thu Mar 02, 2017 6:07 am

Hi Bill, if both or more inputs go high at the same time only one should be allowed to activate the corresponding output. I assume that if the input ports were scanned like in a matrix switch bank that issue would not be a problem although I may well be incorrect. I was hoping code was out there somewhere if not to write it up.
Regards
Paul

User avatar
phalanx
Non-SFE Guru
Posts: 1972
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: Creating 8 Port input\output Switch

Post by phalanx » Thu Mar 02, 2017 7:03 am

When you say only one should be allowed to activate the output, you still need to define which one that would be. The hierarchy of pin priority needs to be clearly defined so you have a system that responds in the same manner every time. It's unlikely that you will find some code that will work exactly the way you want.

For the purposes of this exercise, assume PORTA are your inputs and PORTB are your outputs. A simple priority structure could be to use the bit position within PORTA to determine priority. You would then store the PORTA value in a variable. You can then do a logical shift left (or right) on the variable which shifts the bits through the carry flag. After each shift, check the carry flag and the first time it becomes a "1", you then toggle the corresponding bit in PORTB which will be one less than the amount of shifts that you performed (bit positions start at 0, not 1). At the next iteration of the input scan, check if the input bit that corresponds to your output is still active. If it is, exit out of the routine with no change. If it isn't active, perform the full PORTA scan and shift again to find what the new output pin should be.

This is only one of dozens of ways you could implement this but it should give you an idea of what you need to do in order to implement an appropriate algorithm. Also keep in mind that if you are doing this in assembly, then your instruction set will vary depending on the type of PIC you are using. As you move into more complicated cores, there are additional instructions that can be used to accelerate your routines.

-Bill

Post Reply