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.

16f628a scan keypad store results

Status
Not open for further replies.

deansmith

New Member
Hello, can someone please help me to get my code working properly. It scans a 3x4 keypad, when a key is pressed the value is stored in a register. 4 keys are to be stored this way. The first key value is stored ok. When the program scans the second key, it stops on the last key that was pressed. It appears that the internal pull up resistor is latched low therefore holding the first key press and jumping to the second store register,storing the value of the first key press.
Problem 2. I want the PIC to go in to sleep mode and start scanning when a key is pressed. I got it to work but when i pressed a key the program started scanning but didnt find the first key to be pressed. ie i would have to press a key twice to get 1 number, once to wake up from sleep and once to scan a key. How can i do this so that the wakes up and with the same key press stores the value.
I have the rows on portB <4-7> because of interrupt on change to wake up from SLEEP. Columns on portA because i want to use RB0 interrupt and RB2 TX , If i ever get that far. :( . I guess thats all a bit confusing, please see attached code and jpg. Thanks in advace.
 

Attachments

  • key3.jpg
    key3.jpg
    220.6 KB · Views: 677
  • key3a_628.asm
    3.9 KB · Views: 245
You should use an ISR when it wakes up from sleep, at org 0x004 tell it to "goto ISR" or something like that!

I think you need to clear the bit which says an interrupt occured (I might be wrong ).
 
birdman is right. You need to add an interrupt or timer to wake up the pic from sleep. Also at the moment your pic won't sleep.
Code:
;Program starts now.

	MOVLW 	0xFF		;11110000
	movwf	PORTA
	CLRF 	PORTB 		;Clears PortB.

	
SCAN
	
;SLEEP				;sleep until one of PORTB
				;buttons are pressed

The ; in front of the sleep command makes that the compiler takes it as a comment.
When the pic enters a interrupt it jump to 0x04.

I think your code should look something like this

Code:
	LIST 	P=16F628A 	;PIC we are useing
	include "P16f628A.inc"	;Standard Header File
	ORG 0 			;start address 
	GOTO 	START 		;goto start
	
	org 0x04		; Interrupt jump here

	movwf	w_temp

	; Code goes here

	movfw	w_temp
	bcf	INTCON,1	; Enable more interrupts
	retfie			; quit interrupt

	START	

	num1	EQU	0x20	;stores first  number pressed
	num2	EQU	0x21	;stores second number pressed
	num3	EQU	0x22	;stores third  number pressed
	num4	EQU	0x23	;stores fourth number pressed
	w_temp	equ	0x24	;Stores the w register
 
Last edited:
first of all thank you for your replies. I realise that the sleep routine code is incomplete. I deleted some lines and cancelled out some of the code with the semicolon(;)because i was trying to solve the other ploblem(scanning the keypad and storing results). Also I did read somewhere that if the GIE bit is set and the RBIF bit, then the ISR jumps to to 0x04, BUT if only the RBIF bit is set then the next instruction to be executed is the one after SLEEP. hav'nt go around to trying it though, can anybody varify this ?? I was kind of hoping that some wizkid would fix my program and post it back to me. There are probably a 1001 ways to scan a keypad and store data. Id be grateful for any comments, thoughts and ideas. :)
 
The pic will wake up if an enabled (RBIE=1) interrupt bit is (or becomes) set - if GIE is also set then an interrupt will occur. Make sure you clear RBIF before sleeping.

One thing on your keypad read, you are setting one column at a time low and the others high. This can cause a problem if two keys are pressed simultaneously. Say keys 1 and 2 are both pressed and you are scanning column 1, you now have a low on RA2 and a high on RA1 connected together. The way to fix this is to set the scanned column to output (and low) and the other two to input.

I have not looked at your code, is it now working?

Mike.
 
Thanks Mike, I will try the sleep routine, but first I need sort out why I cant save the value of the key strokes in the registers(num1,2,3 and 4).
As for pressing two keys at once, well I think i understand what your saying, but cant see how that would cause a problem. for instance if i pressed numbers 1 and 2, wouldnt it scan the first number that made a contact and jump to the next instruction after BTFSS, sorry if im missing the point here. I will bear it in mind and try it your way after i have sorted out the issue on saving the numbers in the num registers. :confused: Its me who needs a sleep routine its driving me up the wall.
 
Hi Mike, Ill try and explain the problem another way. If you look at the attached thumbnail(key3.jpg) you can see the watch window. The result 0x20 num1 is 0x81. so far it done as it should have.

SCAN

column1

check1
BTFSC PORTB,4 ; number 1 has been pressed intern pullup is LOW, so skip next instuction
goto check4
MOVLW b'1000 0001 ; = 0x81
goto store
... ;so far so good
...

store
BTFSS 0x20,7 ; contents of work register to be stored at 0x20 IF it is 0x00
GOTO numbers1 .... ok nothing stored here, go to numbers1
BTFSS 0x21,7
GOTO numbers2
BTFSS 0x22,7
GOTO numbers3
BTFSS 0x23,7
GOTO numbers4
goto SCAN

numbers1
movwf num1 ;contents of work register is 1000 0001 ( now in num1)
goto SCAN ; now go and scan for a second keypress.
this is where the problem starts. The internal pullup on RB4 is latched low
so without pressing a key, number one is found again and the value is put in to num2.
the cycle starts again, intern pullup is still low, now the value is put in to num3 and again to num4. what i need to do is reset the intern pullups. after a keypress it stays LOW.
It needs to be HIGH to be able to scan the other keys.
numbers2
movwf num2
goto SCAN
numbers3
movwf num3
goto SCAN
numbers4
movwf num4
goto SCAN

END
 
Last edited:
Ive tried using resistors, same problem. I will try with a debounce. I didnt want to build a curcuit that doesnt work, although it did cross my mind that it could be a simulation problem. Ill make a hardware version and let you know how i get on. Thanks for you help its really appriciated.
 
the free versions have restrictions, limited pin connections and a few other drawbacks. Two choices, proteus LITE and the full demo version. ill send you some links so you can see what it can do ...
 
If you still care, I have built a display driver circuit (hardware) and can post the software code for you by tomorrow if you like. It reads the keypad and stores 4 numbers using RB interrupts.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top