PDA

View Full Version : detecting switch release.



jmgelba
- 26th November 2012, 14:42
Greetings all,

I'm trying to get a push button switch to only increment a variable on release. I've tried a few.loops but can't seem to get it working right. Anyone have a code sample I can pick over?

Thanks.

mackrackit
- 26th November 2012, 15:36
Look up interrupt on change in you chips data sheet.

peterdeco1
- 26th November 2012, 18:30
Try this

input portb.0 'input with pullup resistor
clear 'all variables = 0

start:
if portb.0 = 1 then start 'stay here until pushbutton pushed

waitforrelease:
if portb.0 =1 then pause 50 ‘debounce
if portb.0 = 0 then waitforrelease 'stay here until button released
let (your variable) = (your variable + 1) 'button released
goto start 'wait for another push

Mike, K8LH
- 27th November 2012, 14:37
I would recommend using a switch state latch bit and simple logic to filter out all but a "new press" or "new release" state without having to wait for the opposite state. You didn't mention if you're using active-low or active-high switch signals so I'll provide (C code) examples for both using parallel switch state logic (below).



/*
* K8LH parallel switch state logic
*
* sample active lo switches with a "new press" filter or
* sample active hi switches with a "new release" filter
*
* swnew ___---___---___---___ sample switches
* swold ____---___---___---__ switch state latch
* swnew ___-__-__-__-__-__-__ changes, press or release
* swnew ___-_____-_____-_____ filter for desired state
* flags ___------______------ toggle flag bits for main
*
*/
swnew = ~portb; // sample active lo switches
swnew ^= swold; // changes, press or release
swold ^= swnew; // update switch state latch
swnew &= swold; // filter for desired state
flags ^= swnew; // toggle flag bits for main



/*
* K8LH parallel switch state logic
*
* sample active hi switches with a "new press" filter or
* sample active lo switches with a "new release" filter
*
* swnew ___---___---___---___ sample switches
* swold ____---___---___---__ switch state latch
* swnew ___-__-__-__-__-__-__ changes, press or release
* swnew ___-_____-_____-_____ filter for desired state
* flags ___------______------ toggle flag bits for main
*
*/
swnew = portb; // sample active lo switches
swnew ^= swold; // changes, press or release
swold ^= swnew; // update switch state latch
swnew &= swold; // filter for desired state
flags ^= swnew; // toggle flag bits for main


Execute this code in an ISR or in a main program loop at some "debounce" interval, typically 10 to 20 milliseconds. Test "flags" bit 4 in the main program for a "new press" or "new release", whichever you're using, for the switch connected to RB4. Clear the flag bit after using it.

To emulate a toggle switch, where you press a switch to toggle its flag from on-to-off or from off-to-on, simply test the flag bit for that switch without clearing the flag afterwards.

The switch state latch variable (swold) represents the real time (plus debounce interval) debounced state of the switches on portb and can be used for selective "repeat" key operation, if desired.

Good luck with your project and Happy Holidays to everyone.

Cheerful regards, Mike

jmgelba
- 29th November 2012, 03:41
Thank you for the responses. I now have a functioning routine and everything is working perfectly.

On to the next issue: how to display text on an LCD from a word or byte.

mackrackit
- 29th November 2012, 08:08
Have you seen the LCDOUT command?

jmgelba
- 29th November 2012, 13:47
Yes sir I use it all the time.

I am trying to display a couple of different messages depending on the values of 2 variables.

This is exactly what I want to happen but I dont think you can store strings in variables like this. At least not in 2.60A.

If Latch = 0 then LATCH1 = "MO"
endif
IF LATCH = 1 then
LATCH1 = "MA"
endif
IF DIM = 0 then
DIM1 = "OFF"
endif
IF DIM = 1 then
DIM1 = "DIM 1"
endif
IF DIM = 2 then
DIM1 = "DIM 2"
endif
IF DIM = 3 then
DIM1 = "BOOST"
endif

LCDOUT $FE, 2 LATCH1, " ", DIM1

I cannot put all the combinations into if then loops which include the LCDOUT routine as it takes too long to run through the loop and the timer does not update on the display every second. It actually updates every 3 seconds.

mackrackit
- 30th November 2012, 08:14
SELECT CASE
Might help you out.