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.

valid signal on pic ports

Status
Not open for further replies.

flawed

New Member
Hi. could really do with some help.

i've built a breadboard circuit around a pic16f876a with some features, but now i would like to take a 5 bit value from 5 switches connected to portb, then use a lookup table to compaire a stored value (36 of them) to the input,
but i cant figure out how to make the signal valid, if anybody could start me off of where to go with this i'd be very grateful.

I thought maybe i could use a 6th bit as the valid signal, where the state of portb wouldnt be read until the 6th bit is pulled high, but if it can be done with only 5 it would be so much better.

thanks for looking
 
Are you saying you'd like to read the five switches concurrently and read a 5 bit value of 0..31? If so, that's certainly a challenging question...

Mike
 
hi Mike,
unfortunatly i do!! and have very limited pic knowledge so please bear with me. this is all i have so far (with regard to reading the inputs)

Code:
movlw	0x1f			;only using bottom 5 bits 
	movwf	Mask
	movf	SWITCH_PORT, w
	movwf	old

Nochange	movf   	SWITCH_PORT, w                 
  		andlw  	Mask                   ;  Mask out unused bits
  		xorwf  	old, w                 ;  Compare to previous value
  		btfsc  	STATUS, Z              ;  If Zero set, bits are the Same
   		goto  	(some label to compaire input state to saved value)	
  		xorwf 	 old 
		goto	Nochange       	       ;Bits are different, Store New pattern in "old"
              			


compaire......

could i use a "vaild" input bit, or is all the debouncing going to get overly complicated, because i really need a 5 bit input.
 
Gosh, that's a tough one.

Since it's practically impossible to press more than one switch at exactly the same moment, you stand a good chance of getting seperate and different readings whenever you try to put in some multi-switch value.

If you don't want to use another switch to signal that you have valid switch settings set up then I suspect you'll have to use some other "trick". I can think of a couple ways but they're such kludges and would be so difficult to code that I hesitate to mention them...

If you're using discrete push button switches instead of a multiplexed keypad then you might consider wiring up 6 switches in a Charlieplexed configuration (using only three pins) and dedicating one of those switches as the <enter> or <valid> key.

Mike
 
Last edited:
Do you know of a way i could only record the NUMBER of pins which are high as opposed to the port's bit value. So a sequence of keys can be pressed concurrently, and sampling portb only occurs when one of the inputs goes low. all inputs are then reset. ie, the number of high pins are accumulated.

charileplexing looks cool, like an inverse keyboard matrix. I will look into that but unfortunatly i have a project to be in 2 weeks time, and this is a part of it, so i think im gunna have to stick with
 
If you wait for a key to be released with proper debounce then the previous state should be valid.

Something like,
Code:
WaitNoKey	call	Delay10mS	;required for debounce
		movfw	PORTB		;get keys
		andlw	0x1f		;keep only bottom 5 bits
		btfss	STATUS,Z	;are any keys pressed
		goto	WaitNoKey	;yes, so carry on waiting

		clrf	Keys		;indicate no key pressed
WaitValid	call	Delay10mS	;wait 10mS for debounce
		movfw	Keys		;gets keys from last time
		movwf	OldKeys		;keep copy for later
		movfw	PORTB		;get new key state
		andlw	0x1f		;keep only bottom 5 bits
		movwf	Keys		;save for next time around
		xorwf	OldKeys,w	;keep only keys that have changed
		andwf	OldKeys,w	;and were pressed last time around
		btfsc	STATUS,Z	;will be non zero if a key was released
		goto	WaitValid

		movfw	OldKeys		;contains the valid key combination

	;do whatever with valid code

		goto	WaitNoKey	;go around and do it all again

With the above code, you could press keys 0,1 & 3 in any order but as soon as any one or more keys are released then W will contain 01011.

Mike.
 
Maybe a different kind of switch

Perhaps you could use a gang of dip switches. They come in various number. The one with 8 is pretty common. Use them along with a push button.

Set the dip switches as required. Push the button.

Then you could read the port where the dip switches are hooked to.

Dont have to debounce the dip switches, but you should do it for the push button.


;)
 
You would have to have one state (Make it NO buttons pushed) to be the idle or so called invalid state that you ignore at all times. Then you'd have to use a debounce routine that has a relatively long time frame of say 1 second. Your code would only accept an input from the port once it has been stable for 1 second or more. Any button combinations that last less than 1 second should be ignored. This will give you 31 button combinations using five momentary switches.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top