Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help

Status
Not open for further replies.

Shohadawy

New Member
I'm using 4 switches connected to a PIC16F877... What I want to ask about is how can I perform the following function:
when a switch is pressed and a user press another one the switch that was pressed is opened ( In software or in hardware it's the same for me ... I only want to solve this problem)
 
You asked the same question yesterday and we did not answer it because it sound like a homework question.

We do not do homework programs. If it is not homework I/we apologize.

Homework style help:
Try reading the port and using bitwise logic (AND OR XOR) to compare it to the last value read from the port.
 
The question seems a bit vague to me. Sorry.

I suspect 3V0 is on the right track. Use a switch state latch so that you can easily detect a new switch press or a new switch release while ignoring the other portions of a switch cycle.

For example, assuming you have four active low switches on RB0..RB3, you can process the switches in parallel and detect whichever portion of the switch cycle you wish by executing the following few instructions each time you poll your switches;

Code:
;
;  SwState = PORTB ^ 255              ' invert active low switches
;  SwState = SwState & 15             ' mask for switches on RB3..RB0
;  Changed = SwState ^ SwLatch        ' changed (press or release)
;  NewPress = Changed & SwState       ' a 'new' switch press
;  NewRelease = Changed & SwLatch     ' a 'new' switch release
;  SwLatch = SwState                  ' update switch state latch
;
;  use the following instruction to pass along a new switch press to main
;
;  SwFlags = SwFlags ^ NewPress       ' pass along new press to main
;
;  or use the following instruction to pass along a new switch release
;
;  SwFlags = SwFlags ^ NewRelease     ' pass along new release to main
;
Any '1' bit in the SwFlags variable (bits 3 through 0) represents a new switch press (or release) which you can act upon any way you like.

Normally I poll the switches and perform the switch management logic in an interrupt service routine (using switch press portion of switch cycle). My main code would test for switch press input something like this;
Code:
;
;  Dim Switch1 As SwFlags.bit0
;  Dim Switch2 As SwFlags.bit1
;  Dim Switch3 As SwFlags.bit2
;  Dim Switch4 As SwFlags.bit3
;
;  If(Switch1 = 1)                    ' if Switch1 pressed
;    Switch1 = 0                      ' clear the flag bit and
;    Sw1code                          ' perform Switch1 code here
;  endif
;  If(Switch2 = 1)                    ' if Switch2 pressed
;    Switch2 = 0                      ' clear the flag bit and
;    Sw2code                          ' perform Switch2 code here
;  endif
;
I hope the example switch management logic helps.

Regards, Mike
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top