Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 2nd December 2003, 11:04 PM   (permalink)
Default TAO of switch

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]
natbit is offline  
Old 3rd December 2003, 12:20 AM   (permalink)
Default

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
;
__________________
"Having to do with Motion Control"
motion is offline  
Old 3rd December 2003, 09:59 PM   (permalink)
Default

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
natbit is offline  
Old 4th December 2003, 01:12 AM   (permalink)
Default

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 :?:
natbit is offline  
Old 4th December 2003, 01:20 AM   (permalink)
Default

Quote:
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.
Attached Images
File Type: gif keyscan.GIF (2.2 KB, 319 views)
__________________
"Having to do with Motion Control"
motion is offline  
Old 10th December 2003, 03:25 AM   (permalink)
Default

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.
Attached Images
File Type: jpg switchdebounce1.jpg (4.8 KB, 299 views)
natbit is offline  
Reply

Bookmarks

Thread Tools
Display Modes





All times are GMT. The time now is 04:01 AM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker