0
\$\begingroup\$

I'm working on a little project using micro controllers (PIC 16f84A) and got stuck at a point. I have 4 switches and those are the external interrupts now the idea is I don't understand how can I know which switch caused the interrupt to occur?

Say I have four switches SW1 , SW2 , SW3, SW4 now all those have a common external interrupt mode and flag RBIE and RBIF. If someone pushes the SW2 switch then I should do a function "2" how can I know that SW2 was the one pushes and not SW1 ?

\$\endgroup\$
  • \$\begingroup\$ I thought about checking the bits of PORTB but if someone pressed and removed doesn't the bit turn low again? \$\endgroup\$ – Hasan Hammoud Apr 20 '18 at 4:10
  • \$\begingroup\$ You could probably use an RC circuit on the input of each of the switch pins. Keep it with a low time constant so the pic registers the logic high before the user lets go of the switch. After the lets go the rc circuit will hold the logic state for a short while and give you enough time to read the bits. What's the clock on the pic ? \$\endgroup\$ – Simeon R Apr 20 '18 at 4:50
0
\$\begingroup\$

Read the port would be the answer.

I found it useful to simply calculate this everytime, as I then get flags for all the states that I might want to deal with, as often it is Pushed or Released I want to know

ThisPort = PORTB   //note you only read the actual port in one place
ThisPort = NOT ThisPort  //if switches pull low
Changed = ThisPort XOR LastPort  //bit set if change of state
Pushed = ThisPort AND Changed    //bit set if change was pushing
Released = NOT ThisPort AND Changed
LastPort = ThisPort
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.