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.

switch how do you save the key press

Status
Not open for further replies.

be80be

Well-Known Member
I can't for the life of me fine how to save a key press. I know how to use a switch and debounce it. I want to save it. like key 1 pressed save to SaveKeyPress key2 pressed save to SaveKeyPress and so on for 4 buttons
I tried it like this
Code:
    cblock 0x20
    SaveKeyPress
    endc

Start:
        banksel	TRISIO
        movlw	~(1<<GP1)
        movwf	TRISIO
Getkey:
	banksel	GPIO
	movlw	~(1<<GP3)   ;make GPIO,3 high
	movwf	GPIO
        btfss         GPIO,GP3        ; if button pressed (GP3 low)
        movf         SaveKeyPress  ;saves key press ? I think. I know it set it  
        goto         Getkey            ;to  f7 in mplab sim but is this right?
     end
If this part is right how do I read SaveKeyPress to turn on say GPIO,5
 
If the 4 keys read
Code:
GPIO,0    b'111110'
GPIO,1    b'111101'
GPIO,2    b'111011'
GPIO,3    b'110111'
I want to save that to SaveKeyPress
Code:
         btfss   GPIO,GP0
         btfss   GPIO,GP1
         btfss   GPIO,GP2
         btfss   GPIO,GP3
         movlw   GPIO
         movf    SaveKeyPress
Then SaveKeyPress would read b'11000'
 
I tried that in mplab sim and it moves the bits the way I think it should. But I was hoping for some input. thank's
 
I want to save 4 made by 4 buttons. I'll Google for circular buffer maybe that why I couldn't find any thing. See I'm trying to read 4 switches save that then check it if I pressed the right 4 in a roll then the program will go to the start of what i want it to do.

Thanks Blueroomelectronics
 
Last edited:
Hi be80be,
Do you mean like this

Code:
do_if_nokey
 	nop		;?????
Getkey:
	movfw	GPIO
	andlw	b'11101'	;check for GP0,GP2,GP3,GP4  note: GP1 set as output
	bz	do_if_nokey	;no keypressed go back
[COLOR="RoyalBlue"]
         ;if had one or more key pressed 
[/COLOR]
        movwf     SaveKeyPress  ;saves key press 
[COLOR="RoyalBlue"]
     ; check multi key at one say check GP0 & GP2
[/COLOR]	movfw	SaveKeyPress
	andlw	b'00000101'		;get GP0,GP2 mask
	xorlw	b'00000101'		;Is both key pressed (GP0 and GP2 go high)
	btfsc	STATUS,Z
	call	Multi_Key_pressed
[COLOR="RoyalBlue"]
	 ;check key one by one[/COLOR]
	btfsc    SaveKeyPress,GP0  ; if button pressed (GP0 high)
	call	Keypressed
        btfsc 	SaveKeyPress,GP2   ; if button pressed (GP2 high)
	call	Keypressed
	btfsc	SaveKeyPress,GP3   ; if button pressed (GP3 high)
	call	Keypressed
	btfsc	SaveKeyPress,GP4   ; if button pressed (GP4 high)
	call	Keypressed

	goto	Getkey   
       
Multi_Key_pressed
        nop
Keypressed
        nop
        return
 
I was trying some thing like that I was going to use GP5 as output 0 to 3 as inputs and my switches are high open low closed. Like a lock when the key pressed in right order the main program will start. So after I press the right buttons a green led on GP5 will come on.
Then the main program will reuse the inputs to control GP4. I need to save the state of all 4 buttons to SaveKeyPress
Code:
Getkey:
	banksel	GPIO
	movlw	b'001111'    ;makes GP5-4 outputs GP0-3 inputs
	movwf	GPIO
        btfss         GPIO,GP0    ;input is high skip if low put a 0 in SaveKeypress
        ADDCF       SaveKeyPress         ;?how do I move the 0 to SaveKeyPress
        btfss         GPIO,GP1    ;input is high skip if low put a 0 in SaveKeypress
        ????
        btfss         GPIO,GP2    ;input is high skip if low put a 0 in SaveKeypress
        ????
        btfss         GPIO,GP3    ;input is high skip if low put a 0 in SaveKeypress
        ????
        movf    SaveKeyPress
 
I keep reading this thread and I still can't work out what you are trying to do, why won't this work?
Code:
	movfw	GPIO		;get state of keys
	movwf	SaveKeyPress	;save them

Are you trying to make a combination lock or something?

Mike.
 
It would till i let go of the key then it would change. I want to
press button 1 save that press button 2 save that press button 3 save that and button 4
then move it to SaveKeyPress. So I would end up with a BCD of say 0000 in SaveKeyPress that I would check to see if it is right and if so the rest of my program will run. You can call It a combination lock But what I don't get is how do you keep the input state when you press the button so you can press the next or skip it and then press the next.
 
Last edited:
Like this maybe
Code:
        btfss   GPIO,GP0       ; if button pressed (GP0 low)
        bsf     sGPIO,GP0        ;   move to shollow copy
        btfss   GPIO,GP1       ; if button pressed (GP1 low)
        bsf     sGPIO,GP1       ;   move to shollow copy
        btfss   GPIO,GP2       ; if button pressed (GP2 low)
        bsf     sGPIO,GP2        ;   move to shollow copy
        btfss   GPIO,GP3       ; if button pressed (GP3 low)
        bsf     sGPIO,GP3       ;   move to shollow copy
        movfw	sGPIO		;get state of keys
	movwf	SaveKeyPress	;save them

I think that will work it did in mplab sim I don't need the SaveKeyPress just read sGPIO
 
Last edited:
The way to do this is into a buffer using the FSR register.

First get the debounced key press, then store it into key key buffer.
Code:
	cblock
KeyBuffer:4
Keys
Previous
	endc

		clrf	Previous
		movlw	KeyBuffer
		movwf	FSR
KeyLoop		call	Delay10mS	;debounce delay
		movfw	Keys		;keep copy of previous keys
		movwf	Previous
		movfw	GPIO		;get key state
		xorlw	0xff		;1 = key pressed
		andlw	0x0f		;keep only 0-3 bits  [COLOR="Blue"]<-added[/COLOR]
		movwf	Keys		;save for later
		xorwf	Previous,W	;find keys that have changed
		andwf	Keys,W		;and are currently pressed
		btfsc	STATUS,Z	
		goto	KeyLoop
		movwf	INDF		;store the key
		incf	FSR,f		;move pointer forward
		movfw	FSR		;see if we have 4 keys yet
		xorlw	KeyBuffer+4
		btfss	STATUS,Z
		goto	KeyLoop

;here KeyBuffer will contain 4 key values.

Mike.
 
Last edited:
I'll try it that way It looks better then what I came up with. If it was a stamp I be done
but it would of cost me $50.00 for the stamp. I like the $1.10 for the 12f683 I'm going to get this assembly language one bit at a time maybe lol thanks all
 
I try defined 4 keys, store to buffer
then test back 4 keys order in buffer,
but my problem is don't know how to process keys in mplab
Code:
cblock
  key:4
  endc
GetKey
	movfw	GPIO	;no keypressed input pin=0
	andlw	b'00001111' ;Mark every input key GP0-GP4
	bz	GetKey	;loop until keypressed
	return

Save_new_key
	call	GetKey	;Setkey 1
	movwf	key
	call	GetKey	;Setkey	2
	movwf	key+1
	call	GetKey	;Setkey 3
	movwf	key+2
	call	GetKey	;Setkey 4
	movwf	key+3
	;savekey to EEProm 

Check_if_key_in_order
	;getkey from EEProm if needed
	call	GetKey	;Check Key 1
	andwf	key,W	;get only bit of key[]
	xorwf	key,W	;is key press
	bnz	Wrongkey

	call	GetKey	;check key 2
	andwf	key+1,W	;need result to Wreg
	xorwf	key+1,W
	bnz	Wrongkey
	call	GetKey	;check key 3
	andwf	key+2,W
	xorwf	key+2,W
	bnz	Wrongkey
	call	GetKey	;check key 4
	andwf	key+3,W
	xorwf	key+3,W
	bnz	Wrongkey
All_keys_Match
	; all key match do what you want
	nop
Wrongkey
	nop
	;wrong key try again or stop
 
Have you tried Swordfish Basic ! It requires an 18F which are dirt cheap compared to a stamp.

Microcontrollers | Newark.com This is a list of the through hole parts. The higher pin count parts are SMDs.

Everyone needs to understand the machine at assembler level but as you said it can be slow.

I'll try it that way It looks better then what I came up with. If it was a stamp I be done
but it would of cost me $50.00 for the stamp. I like the $1.10 for the 12f683 I'm going to get this assembly language one bit at a time maybe lol thanks all
 
Last edited:
I just ordered about 10 PIC18F1220-I/P and 2 PIC18F4450-I/P last night I used mikrobasic but you have to buy it to code any thing big 50 words limit i'm going to try Swordfish Basic
now.
 
I'll try it that way It looks better then what I came up with. If it was a stamp I be done
but it would of cost me $50.00 for the stamp. I like the $1.10 for the 12f683 I'm going to get this assembly language one bit at a time maybe lol thanks all

You do not have to use a stamp to use basic or other higher language. See the SourceBoost series of compilers. Free for up to 2k programs, quite reasonable for unlimited.

Russ
 
Thanks all just tried this to read the buffer
Code:
       	cblock 0x20
    KeyBuffer:4
    Keys
    Previous
	sGPIO 
	d1
	d2
	lock
	lock1
	lock2
	lock3
	lockopen
	SaveKeyPress 
	endc
		org   	0x0000      	; org sets the origin, 0x0000
        goto    Start   		; go to beginning of program 
		org	  	0x0004			;interrupt vector
		goto 	Start

Start		

		movlw   07h            		; Set GPIO <2:0) to
                movwf   CMCON0          	; digital I/O
		clrf   	ANSEL           	        ; digital I/O
		movlw   b'1110101'
                movwf   OSCCON          	;8MHz internal oscillator
		movlw	b'001111'
		banksel	TRISIO
		movwf	TRISIO
		banksel	GPIO
		clrf	GPIO	  
		


		clrf	Previous
		movlw	KeyBuffer
		movwf	FSR
KeyLoop		
		call	Delay10mS	;debounce delay
		movfw	Keys		;keep copy of previous keys
		movwf	Previous
		movfw	GPIO		;get key state
		xorlw	0xff		;1 = key pressed
		movwf	Keys		;save for later
		xorwf	Previous,W	;find keys that have changed
		andwf	Keys,W		;and are currently pressed
		btfsc	STATUS,Z	
		goto	KeyLoop
		movwf	INDF		;store the key
		incf	FSR,f		;move pointer forward
		movfw	FSR		    ;see if we have 4 keys yet
		xorlw	KeyBuffer+4
		btfss	STATUS,Z
		goto	KeyLoop
  if Keys==0xB            ;If config==0xB is true,
        banksel GPIO      ;set to bank 0 GPIO
        bsf     GPIO,GP5  ;make led come on GP5
	goto	Main	  ;let main program run
  else
        goto    KeyLoop   ;if not a mach go back and cheack buffer
  endif


;here KeyBuffer will contain 4 key values.
Main:      ; what happens when you get it unlocked 


Delay10mS                         ; delay W x 10ms
      		;9998 cycles
	movlw	0xCF
	movwf	d1
	movlw	0x08
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0

			;2 cycles
	goto	$+1
  

        return				



	END                       ; directive 'end of program'
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top