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.

PIC keypad (i'm newbie)

Status
Not open for further replies.
4X4 Keypad

blueroomelectronics said:
A 4x3 keypad normally uses 7 I/O pins. 4 outputs and 3 inputs. PORTB is handy because you can enable weak pullups, it even has a very handy interrupt / wakeup on change mode for bits 4 thru 7.

A good programmer don't need to use interrupts, I use a 4x4 keypad on all 8 bits of portb and drive a LCD display with 6 bits of porta then I still have two AD channels and if you want it, I can give you a full function 101 keyboard with one wire, the best skipper is always ashore! what more documents do you want? there is a full description of PICDEV on the home page and that was published last knight!!
 
Toys for boys HUH, why not try something practical? say a thumb print scanner or writing data to a hard drive that will keep you busy for some years, that is without PICDEV!
 
4X4 Keypad

Hi everybody ASM and PICBasic this routine work PortB.0 to PortB.3 on X's and PortB.4 to PortB.7 on Y's direct to a 4X4 keypad no resistors
Code:
Keyscan:
		'FlagS[0]="C"
	Debounce=1				' Setup the initial value for Debounce
	Key=0					' Clear the variable KEY, prior to scanning
'	TRISA=%00100000
'	TRISB = %00001111
'	Option_Reg.7=0				' Enable Internal PortB Pullup Resistors	
		PORTB.4=0
		PORTB.5=1
		PORTB.6=1
		PORTB.7=1				' Pull the fourth Row line LOW
	GoSub Scancol				' Scan the columns
	IF K_Flag=1 Then GoTo Map		' If a key is pressed then map it 
		PORTB.4=1
		PORTB.5=0
		PORTB.6=1
		PORTB.7=1				' Pull the fourth Row line LOW
	GoSub Scancol				' Scan the columns
	IF K_Flag=1 Then GoTo Map		' If a key is pressed then map it 
		PORTB.4=1
		PORTB.5=1
		PORTB.6=0
		PORTB.7=1				' Pull the fourth Row line LOW
	GoSub Scancol				' Scan the columns
	IF K_Flag=1 Then GoTo Map		' If a key is pressed then map it 
		PORTB.4=1
		PORTB.5=1
		PORTB.6=1
		PORTB.7=0				' Pull the fourth Row line LOW
	GoSub Scancol				' Scan the columns
	IF K_Flag=1 Then GoTo Map		' If a key is pressed then map it 
	D_Flag=0				' No key pressed, so Reset debounce flag
	Debounce=0 			        ' No key pressed, so Reset key Debounce flag
	GoTo Exit				' Exit from the subroutine
' Do the following code if a key has been pressed
Map:	  IF D_Flag=1 Then Exit 		' Already responded to this press, so exit
	  D_Flag=1				' Set Debounce flag
	  Debounce=0 				' Reset key Debounce flag
	LookUp Key,[68,67,66,65,35,9,6,3,0,8,5,2,43,7,4,1,128],Keyval ' Map of the keypad legends for numeric output
' **To convert the output to an ascii value comment the line above and uncomment the line below
 	LookUp Key,["D","C","B","A","#","9","6","3","0","8","5","2","*","7","4","1",32],Key ' Map of the keypad legends for ascii output
' This subroutine scans the columns
' The bit, K_Flag returns a 1 if a key is pressed, and 0 if no key pressed
' Also, if no key is pressed the variable KEY will return with the value of 16
Exit:	Return
Scancol:
	K_Flag=1				' Set K_Flag initially to 1
	IF PORTB.0=0 Then DEB_release 'S_Exit		' Return if a key on the first column is pressed
	Key=Key+1				' Else increment KEY, and try another row
	IF PORTB.1=0 Then DEB_release 'S_Exit		' Return if a key on the second column is pressed
	Key=Key+1				' Else increment KEY, and try another row
	IF PORTB.2=0 Then DEB_release 'S_Exit		' Return if a key on the third column is pressed
	Key=Key+1				' Else increment KEY, and try another row
	IF PORTB.3=0 Then DEB_release 'S_Exit		' Return if a key on the fourth column is pressed
	Key=Key+1				' Else increment KEY, KEY now equals 16
	K_Flag=0				' Set the K_Flag to indicate no key pressed
S_Exit:	Return			             ' And exit the subroutine

DEB_release:
	'Check to see if key is released
	
	counter=256
DEB_release_l1:
	IF PORTB.0=0 Then DEB_release
	IF PORTB.1=0 Then DEB_release
	IF PORTB.2=0 Then DEB_release
	IF PORTB.3=0 Then DEB_release
	
	counter=counter-1
	IF counter=0 Then S_Exit
	
	GoTo	DEB_release_l1
 
Want to see what makes the Swordfish compiler fun.. Well sometime ago a very similar topic raised, so I made my own SF Library;

its use;

Code:
Device = 18F452
Clock = 20
#option KEYPAD_PORT = PORTD
Include "Keypad12.bas"
Include "usart.bas"
Include "Convert.bas"
Dim 
    Variable As Byte
USART.SetBaudrate(br19200)    
    
While True   
    Variable = 0                        // Clear the variable
    While Variable = 0                  // Wait for a key press
        Variable = Keypad12.Value       //  ...
    Wend

    // Write to USART    
    USART.Write("Key Pressed: ", Convert.DecToStr(Variable), 13) 

    // Wait for the button to be depressed
    Keypad12.Debounce
Wend

Wiring;
**broken link removed**

Download the Library here; **broken link removed**

I'm uploading a video tutorial on SF libraries now... I'll link it when done
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top