PDA

View Full Version : Latched buttons



peu
- 23rd January 2006, 22:20
Hi, im building an application with a lot of loops and subroutines and I need to know every now and then if a button has been pressed.

I know that this can be accomplished by reading the port as often is possible, but I tought that by latching the port, when I have time to look at it and view if it changed from the last time it would be easier for my program structure.

to be clear, here is the metacode:

buton state=0
loop:
program runs
button is pressed (new state=1), buttons keeps this state
program continues
button is sensed and checked if it changed
loop

I know that at any given time the button may be pressed twice and I could miss that. This is something I can live with.

is there a simple way of accomplishing this?

thanks

mister_e
- 24th January 2006, 00:10
i still prefer to use interrupts. Once a button is pressed, it jump automatically to a X routine.

if you want to monitor few button, you can use Interrupt on PORTx change.

What else the program will do?

Ioannis
- 24th January 2006, 10:04
may be in your tight loop you could read all 8 bits of a port, store in a temp_port byte and process it later. The delay is minimum.

Of course that way you will miss the fast double clicks, but as you said you can tolerate this.

Best is interrupts as Steve stated.

Ioannis

J. Mark Wolf
- 24th January 2006, 11:58
I have such a circuit, and can email you a PDF later today. This circuit also handles debouncing, which the other suggestions don't allow time for.

peu
- 24th January 2006, 12:11
good ideas posted here, never tough of implementing that way, maybe because I never used the On Interrupt instruction before... :)

Will try, will tell

J. Mark: can you attach the PDF to a message, so all ppl see it?

Thanks

Archilochus
- 24th January 2006, 14:17
If you don't need instant response to the button press, you can get away with not using interrupts.
Just test the appropriate interrupt flag bit when you want to know if the button was pressed.
Remeber to clear the bit after checking it.

peu
- 24th January 2006, 14:50
I just wrote this program to start learning on interrupt, as expected... it didn't work :)




@ device pic12F675, intrc_osc_noclkout, wdt_on, mclr_off, protect_off

DEFINE OSC 4

trisio =%00110000
ansel =%00000000
cmcon =%00000111 'Comparators Off
intcon =%10001100 'interrupts enable / tmr0 overflow / gpio change interrupt enable
OPTION_REG = %11010101 ' Set TMR0 configuration

but1 var gpio.4
but2 var gpio.5
led1 var gpio.1
led2 var gpio.2
ledwork var gpio.0

but1st var bit
but2st var bit

conta var byte


on interrupt goto interrupcion

loop:

for conta=0 to 255 step 16 'working led rise routine
pwm ledwork, conta, 10
disable
if but1st=1 then
but1st=0
high led1
endif
if but2st=1 then
but2st=0
high led2
endif
enable

next conta
high ledwork

for conta =0 to 100
pause 10
next conta

low led1 'set status buttons leds off
low led2

for conta=0 to 255 step 16 'Fall routine
pwm ledwork, 255-conta, 10
disable
if but1st=1 then
but1st=0
high led1
endif
if but2st=1 then
but2st=0
high led2
endif
enable

next conta
low ledwork

low led1 'set button status leds off
low led2

goto loop


disable
interrupcion:
but1st=but1
but2st=but2
intcon =%10001101
resume
enable



Any help appreciated


Pablo

peu
- 24th January 2006, 15:10
I just changed the previous code so ledwork uses gpio.2 instead of gpio.3 because the payload of the program wasnt functioning properly (no idea why). Now it PWMs the led properly.

Im still reading the datasheet trying to figure out what I missed for the interrupt part


Pablo

peu
- 24th January 2006, 15:24
I fiddled with the intcon/option reg values and it seems to work, at least in the simulation :)

please tell me if you see someting blattantly wrong in the code:



@ device pic12F675, intrc_osc_noclkout, wdt_on, mclr_off, protect_off

DEFINE OSC 4

trisio =%00110000
ansel =%00000000
cmcon =%00000111 'Comparators Off
intcon =%10101100 'interrupts enable
OPTION_REG = %11010001 ' Set TMR0 configuration

but1 var gpio.4
but2 var gpio.5
led1 var gpio.1
led2 var gpio.2
ledwork var gpio.0

but1st var bit
but2st var bit

conta var byte


on interrupt goto interrupcion

loop:

for conta=0 to 255 step 16 'working led rise routine
pwm ledwork, conta, 10
; disable
if but1st=1 then
but1st=0
high led1
endif
if but2st=1 then
but2st=0
high led2
endif
; enable

next conta
high ledwork

for conta =0 to 100
pause 10
next conta

low led1 'set status buttons leds off
low led2

for conta=0 to 255 step 16 'Fall routine
pwm ledwork, 255-conta, 10
; disable
if but1st=1 then
but1st=0
high led1
endif
if but2st=1 then
but2st=0
high led2
endif
; enable

next conta
low ledwork

low led1 'set button status leds off
low led2

goto loop


disable
interrupcion:
but1st=but1
but2st=but2
intcon =%10001101
resume
enable



Pablo

J. Mark Wolf
- 25th January 2006, 00:22
The attached PDF file is a circuit similar to one that was published in an old EDN Design Ideas. Originally it used a 74HCT374 and eight switches.

The advantage is that it latches and debounces the momentary switches. The KYBD_INT signal can be used for interrupting or polling. Once you've read the state of the switches, clear the latch and you're primed for the next keypress.

I've found it useful in the past, maybe you will to.