This post provides a simple method to interface any keypad (e-g 4Γ4 or 4Γ3 etc)Β with PIC12F675 microcontroller. The code for PIC12F675 is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the βDownloadsβΒ section at the bottom of this page.
In this post, it is assumed that you know,
- How to use ADC of PIC12F675 microcontroller. If you donβt then please readΒ this page.
- How to interface LCD with PIC12F675 microcontroller. If you donβt then please readΒ this page.
The circuit required to interface keypad with PIC12F675 is shown below.
The result of simulating the code inΒ ProteusΒ is shown above in the figure. This code is written in such a way that when you press any key from the keypad, then the value of that key is displayed on the LCD. For example, in the above figure LCD screen is displaying β1β, because this picture was taken after pressing β1β from the keypad.
In the above circuit, PIC12F675 is running on itβs internalΒ 4MHzΒ oscillator. GP0, GP1 and GP2 pins are used to interface LCD through 4094 IC as explained in the βPIC12F675 LCD Interfacing Code + Proteus Simulationβ post. LCD is used here just to show the pressed key value. Usually 8 pins are used to interface 4Γ4 keypad with any microcontroller. 4 pins are used to attach columns and 4 pins are used to attach rows and scanning algorithm code is used to check for any pressed key.
But PIC12F675 doesnβt have 8 pins. By using some diodes we can generate different voltage on each pressed key on a single wire (as shown above in the circuit). And then by using built-in ADC of PIC12F675 we can read this voltage value to determine pressed key easily. In this way by using only one pin of PIC12F675 we can successfully interface keypad with it.Β To interface keypad, only GP4(AN3)Β pinΒ is being used in the circuit shown above.
In the above circuit, rows of the keypad have 1N4007 diode attached with them. If you donβt have 1N4007 diode, then any simple 0.7 volt[1] diode can be used here. RowA has 3 diodes, RowB has 2 diodes, RowC has one diode and RowD has no diode attached with it. These diodes are pulled-up from one side by a 470 ohm resistor. Column1 wire of keypad is attached with the ground. Column2 is attached with a schottky diode[2].Β Column3 is attached with a 2.7v zener diode.Β Column4 is attached with a 3.0v zener diode. Using this circuit you can easily interface keypad with PIC12F675 microcontroller.
Code
The code of the main function is shown below.
Downloads
Keypad interfacing code using PIC12F675 was compiled inΒ MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10.Β To download code andΒ ProteusΒ simulationΒ click here.
For more detail: How to interface keypad with PIC12F675