Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Keypad with Internal Pull_up

Status
Not open for further replies.

PA3040

Advanced Member level 3
Advanced Member level 3
Joined
Aug 1, 2011
Messages
883
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Activity points
6,936
Dear All
This is reference to pic 16f877a MCU keypad 4x4
I am writing a program for Keypad, so I used portb without interrupt
0 - 3 as out put and 4 - 7 as in put with PORTB internal pull_up
I masked it when I pressed number 1, the RC0 LED tern on and press 2, the RC1 LED tern on and so on

It is working with some errors, I mean when I touch the keypad LED will tern on here and there and toggle

finally I add external pull_up ( four 2.2k resistors to Vcc to RB4,RB5,RB6,RB7) You will wonder now it is working fine

Can I have Advice on this

I need to drive this CCt without external pull_ups

Thanks in advance
 

There is no reason why you should need external pull-ups if you have correctly configured port B's internal pull ups. I have done it like that.

Do you have diodes on the output lines to the keypad? You should - to prevent pressing two keys simultaneously from allowing the active output from one line to be shorted into an inactive output line.

Also, be aware that the PIC might not change output lines instantly. I have had issues where lines go high before others have gone low, and vice versa, and tried to read the inputs immediately after setting the outputs, before the outputs have settled. Both will cause mis-reads. The solution is simply to insert a delay of a few cycles between setting the output and reading the input.
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Hi wp100

My hardware does not have external pull_up
Thanks

---------- Post added at 11:33 ---------- Previous post was at 11:23 ----------

Dear FoxyRick
Thanks for reply

I configured PORTB pull_up well


Code:
Bank1
bcf  option_reg,7  ;pull-ups are enabled by individual port latch values

I also add big size delay but problem remaining same but I do not add any diode because I did not see such cct in the any web tutorials

Please advice
 

Dear BigDog,
Thanks for reply
I red all tutorials that you provided to me
lot of cct's use external pullups, that mean internal pullups does not support for keypad , I am I correct?

Please advice
 

Dear BigDog,
Thanks for reply
I red all tutorials that you provided to me
lot of cct's use external pullups, that mean internal pullups does not support for keypad , I am I correct?

Please advice



Hi,

This thread might interest you https://www.google.co.uk/imgres?imgurl=http://forum.allaboutcircuits.com/cache.php%3Furl%3Dhttp://www.astrosurf.com/astroenit/projets/images/keypad-schematic.gif&imgrefurl=http://forum.allaboutcircuits.com/showthread.php%3Ft%3D39577&usg=__KzBwhjvMIrtLuMVHVNjNUhB2ktQ=&h=301&w=461&sz=8&hl=en&start=15&zoom=1&tbnid=NoN698kHh80DvM:&tbnh=84&tbnw=128&ei=xZuNT5iBCMua0QWbzLTzDA&prev=/images%3Fq%3Dpic%2Bkeypad%2Busing%2Binternal%2Bpull%2Bups%26hl%3Den%26sa%3DG%26tbm%3Disch&itbs=1

---------- Post added at 17:52 ---------- Previous post was at 17:42 ----------

Why don't you post your keypad circuit /.dsn and the keypad code so we can see whats happening .

---------- Post added at 18:59 ---------- Previous post was at 17:52 ----------

Hi,

Just tried Nigels basic keypad code with resistors and then with the internal pull ups - both work fine.

Are you correctly selecting the internal pull ups ?

bit CLEAR to turn the pull ups ON ..... bcf OPTION_REG,RBPU

Ensure you do not change those Input Ports to outputs as that knocks off the pull ups.
Always preceed your keypad routine with the above instruction to ensure they are pulled up

Should just add that the tests were done on actual hardware , not simulation
 
Last edited:
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear All
This is reference to pic 16f877a MCU keypad 4x4
I am writing a program for Keypad, so I used portb without interrupt
0 - 3 as out put and 4 - 7 as in put with PORTB internal pull_up
I masked it when I pressed number 1, the RC0 LED tern on and press 2, the RC1 LED tern on and so on

It is working with some errors, I mean when I touch the keypad LED will tern on here and there and toggle

finally I add external pull_up ( four 2.2k resistors to Vcc to RB4,RB5,RB6,RB7) You will wonder now it is working fine

Can I have Advice on this

I need to drive this CCt without external pull_ups

Thanks in advance

Each of the PORTB pins has a weak internal pull-up. A single control bit can turn on all the pull-ups. This is performed by clearing bit RBPU (OPTION_REG<7>). The
weak pull-up is automatically turned off when the port pin is configured as an output.

ooooops, sorry. for keyboard depends upon many things
 
Last edited:
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
eak pull-up is automatically turned off when the port pin is configured as an output.

Dear papunblg thanks for reply

I set PORTB LSB as output and MSB as input. and as I Know this may not effect to disable pull_ups
can you kindly explain what is the reason that may not work when I enable the internal pull_ups and it is working well when I enable the external pull_ups

Thanks in advance
 

Dear papunblg thanks for reply

I set PORTB LSB as output and MSB as input. and as I Know this may not effect to disable pull_ups
can you kindly explain what is the reason that may not work when I enable the internal pull_ups and it is working well when I enable the external pull_ups

Thanks in advance

I will tell you what is happening. Before that do two things(its important) :-
1. Make PORTB MSB(4-7 bits) as output and LSB as input. Then enable internal pullups for LSB bits. And run the program. Observe what is happening.
2. Publish a schematic of the keyboard connection
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear papunblg,
thanks for reply

I did it opposite way MSB ( 4-7) as input and LSB as ( 0-3) as output but problem remaining same. What is the efferent this and you explained above and I will do as you explained and let you know
Thanks
 

Dear Wp100
Thank you very much for reply

Please look in to attachment for my hardware
Please advice

Code:
status		equ		0x03
portb		equ		0x06
trisb		equ		0x86
portc		equ		0x07
trisc		equ		0x87
portd		equ		0x08
trisd		equ		0x88
option_reg	equ		0x81

del1		equ		0x20
del2		equ		0x21

#include <p16f877a.inc>				;include the defaults for the chip
__config 3f39						;sets the configuration settings

			org		0x00			;org sets the origin, 0x0000 for the 16F877A
start		goto	main			;this is where the program starts running
main		clrwdt
			call	sys_init
loop		call	keypad
			goto	loop

keypad		movlw	b'11110000'		;Set all output pins low
			movwf	portb
			movwf	portb
			btfss	portb,7			;Determine Columns
			goto	row1
			btfss	portb,6			;Determine Columns
			goto	row2
			btfss	portb,5			;Determine Columns
			goto	row3
			btfss	portb,4	
			goto	row4		;Determine Columns
			return
;------------------------------
;The MCU will need to determine which of the keys is being pressed 
;WHICH KEYS CONNECTED ROW1
row1		bcf		portb,0
			bsf		portb,3
			btfsc	portb,7
			goto	one
			bcf		portb,3
			bsf		portb,2
			btfsc	portb,7
			goto	two
			bcf		portb,2
			bsf		portb,1
			btfsc	portb,7
			goto	three		
			bcf		portb,1
			bsf		portb,0
			btfsc	portb,7
			goto	A
			goto	keypad
;------------------------------
;Determine which of the keys is being pressed of row2
row2		bcf		portb,0
			bsf		portb,3
			btfsc	portb,6
			goto	one
			bcf		portb,3
			bsf		portb,2
			btfsc	portb,6
			goto	two
			bcf		portb,2
			bsf		portb,1
			btfsc	portb,6
			goto	three		
			bcf		portb,1
			bsf		portb,0
			btfsc	portb,6
			goto	A
			goto	keypad
;------------------------------
;Continuve the prgram to determine which of the keys is being pressed of row3
row3		bcf		portb,0
			bsf		portb,3
			btfsc	portb,5
			goto	one
			bcf		portb,3
			bsf		portb,2
			btfsc	portb,5
			goto	two
			bcf		portb,2
			bsf		portb,1
			btfsc	portb,5
			goto	three		
			bcf		portb,1
			bsf		portb,0
			btfsc	portb,5
			goto	A
			goto	keypad
;------------------------------
;Continuve the prgram to determine which of the keys is being pressed of row4
row4		bcf		portb,0
			bsf		portb,3
			btfsc	portb,4
			goto	one
			bcf		portb,3
			bsf		portb,2
			btfsc	portb,4
			goto	two
			bcf		portb,2
			bsf		portb,1
			btfsc	portb,4
			goto	three		
			bcf		portb,1
			bsf		portb,0
			btfsc	portb,4
			goto	A
			goto	keypad
;------------------------------
one			movlw	b'00000001'
			movwf	portc
			call	delay		;determine the key has been released.
			call	delay		
			goto	keypad

two			movlw	b'00000010'
			movwf	portc
			call	delay		;determine the key has been released.
			call	delay
			goto	keypad

three		
			movlw	b'00000100'
			movwf	portc
			call	delay		;determine the key has been released.
			call	delay
			goto	keypad

A			movlw	b'00001000'
			movwf	portc
			call	delay		;determine the key has been released.
			call	delay
			goto	keypad


;------------------------------		;Delay routines			
delay		movlw	.100		
			movwf	del1
s2			decfsz	del1
			goto	s1
			return
s1			movlw	.255
			movwf	del2
			decfsz	del2
			goto	$-1
			goto	s2
;------------------------------		;end of Delay routines

sys_init	bsf		status,5		;select bank 1
			movlw	b'11110000'		;set keypad pins
			movwf	portb			;half in, half out
			clrf	trisc			;
			clrf	trisd
                        bcf	         option_reg,7
			bcf		status,5		;select bank 0
			clrf	portc
			clrf	portd
			return
				
end
[ATTACH=CONFIG]72768._xfImport[/ATTACH]
 

Hi,


Have checked out your code, please see the comments in CAPITALS at the beginning and also at the end in the sys_init routine.

I assume the actual key routine is your own coding ? - have not gone though that as it will take far too long.

Think the main error is using PORTB instead of TRISB to set the i/o direction.
Also assume you have bSf when trying to use the internal pull ups, bSf turns them OFF


As mentioned before that WInpicprog code keypad code program 9.1 does work fine, providing you enable the internal pull ups correctly.
Perhaps tery that to prove the keypad can work with the instenal pull ups, then go back and try your code again knowing the hardware is good.

Your pcb design looks neat but is a bit difficult to see everything, can you post the schematic diagram


Code:
;status		equ		0x03    ALL DEFINED IN THE #INCLUDE FILE
;portb		equ		0x06
;trisb		equ		0x86
;portc		equ		0x07
;trisc		equ		0x87
;portd		equ		0x08
;trisd		equ		0x88
;option_reg	equ		0x81

del1		equ		0x20
del2		equ		0x21

	title	" Keypad Controller"
	list p=16F877A,r=hex,n=80,x=off,st=off

	#include <p16f877a.inc>				;include the defaults for the chip

;__config 3f39						;sets the configuration settings  BETTER TO USE THE NEXT LINE
;									SO YOU CAN SEE WHAT PARAMETERS ARE SET

	__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_OFF & _XT_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF


			org		0x00			;org sets the origin, 0x0000 for the 16F877A
start		goto	main			;this is where the program starts running


main		clrwdt					; WATCHDOG IS TURNED OFF IN THE CONFIG
			call	sys_init
loop		call	keypad
			goto	loop

keypad		movlw	b'11110000'		;Set all output pins low
			movwf	portb
			movwf	portb
			btfss	portb,7			;Determine Columns
			goto	row1
			btfss	portb,6			;Determine Columns
			goto	row2
			btfss	portb,5			;Determine Columns
			goto	row3
			btfss	portb,4	
			goto	row4		;Determine Columns
			return
;------------------------------
;The MCU will need to determine which of the keys is being pressed 
;WHICH KEYS CONNECTED ROW1
row1		bcf		portb,0
			bsf		portb,3
			btfsc	portb,7
			goto	one
			bcf		portb,3
			bsf		portb,2
			btfsc	portb,7
			goto	two
			bcf		portb,2
			bsf		portb,1
			btfsc	portb,7
			goto	three		
			bcf		portb,1
			bsf		portb,0
			btfsc	portb,7
			goto	A
			goto	keypad
;------------------------------
;Determine which of the keys is being pressed of row2
row2		bcf		portb,0
			bsf		portb,3
			btfsc	portb,6
			goto	one
			bcf		portb,3
			bsf		portb,2
			btfsc	portb,6
			goto	two
			bcf		portb,2
			bsf		portb,1
			btfsc	portb,6
			goto	three		
			bcf		portb,1
			bsf		portb,0
			btfsc	portb,6
			goto	A
			goto	keypad
;------------------------------
;Continuve the prgram to determine which of the keys is being pressed of row3
row3		bcf		portb,0
			bsf		portb,3
			btfsc	portb,5
			goto	one
			bcf		portb,3
			bsf		portb,2
			btfsc	portb,5
			goto	two
			bcf		portb,2
			bsf		portb,1
			btfsc	portb,5
			goto	three		
			bcf		portb,1
			bsf		portb,0
			btfsc	portb,5
			goto	A
			goto	keypad
;------------------------------
;Continuve the prgram to determine which of the keys is being pressed of row4
row4		bcf		portb,0
			bsf		portb,3
			btfsc	portb,4
			goto	one
			bcf		portb,3
			bsf		portb,2
			btfsc	portb,4
			goto	two
			bcf		portb,2
			bsf		portb,1
			btfsc	portb,4
			goto	three		
			bcf		portb,1
			bsf		portb,0
			btfsc	portb,4
			goto	A
			goto	keypad
;------------------------------
one			movlw	b'00000001'
			movwf	portc
			call	delay		;determine the key has been released.
			call	delay		
			goto	keypad

two			movlw	b'00000010'
			movwf	portc
			call	delay		;determine the key has been released.
			call	delay
			goto	keypad

three		
			movlw	b'00000100'
			movwf	portc
			call	delay		;determine the key has been released.
			call	delay
			goto	keypad

A			movlw	b'00001000'
			movwf	portc
			call	delay		;determine the key has been released.
			call	delay
			goto	keypad


;------------------------------		;Delay routines			
delay		movlw	.100		
			movwf	del1
s2			decfsz	del1
			goto	s1
			return
s1			movlw	.255
			movwf	del2
			decfsz	del2
			goto	$-1
			goto	s2
;------------------------------		;end of Delay routines

sys_init	bsf		status,5		;select bank 1			USE  BANKSEL TRIS etc, AGAIN MUST EASIER TO FOLLOW
			movlw	b'11110000'		;set keypad pins
			movwf	portb			;half in, half out    SHOULD BE TRISB SETTING THE PORTS TO INPUT OR OUTPUT
			clrf	trisc			;
			clrf	trisd
            bcf	         option_reg,7			;  CLEARING THE RBPU BIT TURNS THE PULLUPS ON !!  SEE THE DATASHEET - OPTION REG RBUP
												; BETTER TO CODE IT  AS BCF	OPTION_REG,RBPU THN YOU KNOW WHICH BIT YOUR ARE REFERING TO
			bcf		status,5		;select bank 0
			clrf	portc
			clrf	portd
			return

    END
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear WP100,
Thank you very much for your advice
My program now working properly and I got a lot of experience about keypad and more

Dear All,

My next step is prepare a door lock using keypad

I am plaining to use four digit user code
Can any body explain with dilogarithm, how recognise the four digit code
Please explain in Assembly code
Thanks in advance
 

I am going to be mean and say you should be coding this yourself.:)

A security code is simple enough if you have got the keypad mastered and can save the keystoke to a register.

The simple way is to have two or four registers in your code containing the desired sequence of digits for the lock.

When someone keys in one digit you Compare that with the first stored digit, if equal it then waits for the next digit, and so on. If all equal then unlock the door.

Do that simple sequence and get it working, then you can add all the extra features to make it properly functional, like storing the actual code in EEprom so it can be changed via the keypad with a master code instead of reprogramming the chip.

The code will also need key debonce routines, error exit routines and time delays to ensure the sequence is entered within a reasonable period etc.
You will also need a led to show when its ready for another sequence ready or error.

Post some .dsn when you have the basic lock working
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear WP100,
Thanks for reply and hint:lol:
All codes are above written by me, base on your and other members advice . now I can write program for keypad by my mind and it is little long and now I need to study Nigels basic keypad code that you given me
Your explanation post#15 is about big picture to me
Can I have pseudo code level explanation
Thanks in advance
 

Hi,

The thing missing from your keypad example is when you detect a key press you do not save its value and return to you 'main' code.

You want to set up something, so that when a key is pressed , its value is stored, you return to your main code and then the value is displayed on a row of leds, 7 segments or lcd.

Then you return to the keypad routine for another scan.

Once you have saved your keypad info it should become easier to see how start developing your lock idea.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top