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.

TAO of switch

Status
Not open for further replies.

natbit

New Member
Hi all,

I'm sure everybody here has at some stage dealt with switch debounce...

I tried my first switch (membrane) a while back with method 1, using a pull-up (10k) across switch and +5V and other switch side to RB4
now I want to use portB weak internal pull-ups and pull down ( 10k) to gnd on other microswitch side, method 2.

I understand the concept of switch debounce and have writen code (MICROCHIP .asm) that would use a set count value to debounce the switch, not working.

Code:
SW1:
	movlw	b'01111111'	
	movwf	PORTB
	;PAGE1
	;bsf	TRISB, 7
	;bcf	OPTION_REG, 7
	;PAGE0
	
	btfss	PORTB, 7
	goto	endswitch
	incf	SW1count
	movf	SW1count, 0
	xorlw	DEB_VAL	
	btfss	STATUS, Z
	goto	endswitch
	clrf	SW1count
	bcf	STATUS, Z
	movf	DISP_Num, 0
	xorlw	d'9'
	btfss	STATUS, Z
	goto	$+3
	clrf	DISP_Num
	goto	$+1
	incf	DISP_Num
	return 				
endswitch:
	PAGE1
	clrf	TRISB
	PAGE0
	return

So I would put it to all those with experience out there, is there anything a newbie might not have considered when doing this simplest of functions.

natbit[/img]
 
To avoid switch bounce, I recommend scanning the key switches at a very slow sampling rate from 10-40 times per second. The slower the better but not too slow so that the buttons appear to be sluggish. A person will not perceive a 50msec delay in response from the buttons.

Code:
READ_KEYPORT:
         DECFSZ   KEY_TIMER,F
         RETURN
;
         COMF     KEYBUFF,W
         MOVWF   KEYEDGE
;
         MOVF     KEY_PORT,W
         MOVWF   KEYBUFF
         ANDWF   KEYEDGE,F
;
; <--- Insert code here to process key press
;
         RETURN
;
 
motion your code looks amazingly simple, are you intimating that the KEY_TIMER register is set beforehand, or is it on a 1/256 loop at which point it takes the compliment of the KEYBUFF and moves it into KEYEDGE, reads port activity to KEYBUFF then AND's it with KEYEDGE, saving it in KEYEDGE.

I havn't figured out what part of the STATUS register I should go hunting in yet but I think I get it, it needs to have identical consecutive readings which over a 25 to 100 ms interval, constitutes a valid read. :wink:

nice one
 
I implemented this routine and it's the Z flag in STATUS that one checks.

motion how do the RB weak internall pull-ups operate? Should one set the TRISA input first, or the RPBU' pull-ups...and when set, does it continue to try and hold the RB pins high exept when grounded, 'cause I want to understand the methodology of connecting the switch to gnd, does one need a 10k pull-down to gnd :?:
 
motion your code looks amazingly simple, are you intimating that the KEY_TIMER register is set beforehand, or is it on a 1/256 loop at which point it takes the compliment of the KEYBUFF and moves it into KEYEDGE, reads port activity to KEYBUFF then AND's it with KEYEDGE, saving it in KEYEDGE.

The READ_KEYPORT routine is called every 256usec from the main program loop which polls the TIMER0 for overflow. Since this is too fast for key scanning, the KEY_TIMER further scales this down to 65msec.

The routine looks for the falling edge and sets the corresponding bit in KEYEDGE. This allows simultaneous scanning of 8 switches.

Below, illustrates how the routine filters out key bounce. The scanning rate is slower than the frequency of bounce and checks the switches after the key switch has stopped bouncing.
 

Attachments

  • keyscan.GIF
    keyscan.GIF
    2.2 KB · Views: 621
I know I am going on about this, but it is realy annoying me...here are the two methds that I proposed previously, method 2 being the preferred option using weak internal pull-ups inside '84a. I have tried setting the TRISB and OPTION RPBU' bit's in all combinations, but I cannot read an input with this method, at all. Could it be due to the battery pack I am prototyping with (low power) or is it most likely that my chip is rooted :cry:

I've read everything on the net and tried so many things, I have tons of code I've already writen that is weeping for these buttons...damn electrons.
 

Attachments

  • switchdebounce1.jpg
    switchdebounce1.jpg
    4.8 KB · Views: 593
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top