How to decode a signal from a keypad
In many applications we want to give to user the possibility to use a keypad. We are going to use 4x3 keypad (1, 2, 3, 4, 5, 6, 7, 8, 9, 0, *, #). In order to be able to decode the user input we have to understand how it works. So please take a look on the schematic below:
keypad 4x3 |
In today's article we are going to use a microcontroller in order to decode the user inputs. The software will follow the flowchart below (is big but is really simple):
Keypad reading flowchart |
Please take a look on the following schematic:
There is nothing more than the micro controller, the keypad, the seven segment display and off course some resistors (pull up resistors, safety resistors for the leds).
proteus simulation of keypad |
The wiring of the seven segment display is easy if you keep in mind the picture below:
Image from Wikipedia |
The first wire is A, the second wire is B etc. The last wire is the cathode of leds.
Now let me show you the software i wrote for this project:
- start
- ; remaining code goes here
- call Initialization
- setC1
- bsf PORTA, 0
- bcf PORTA, 1
- bcf PORTA, 2
- setC1_A
- btfss PORTB, 4
- goto setC1_B
- movlw b'00000110'
- movwf PORTC
- goto setC1
- setC1_B
- btfss PORTB, 5
- goto setC1_C
- movlw b'01100110'
- movwf PORTC
- goto setC1
- setC1_C
- btfss PORTB, 6
- goto setC1_D
- movlw b'00000111'
- movwf PORTC
- goto setC1
- setC1_D
- btfss PORTB, 7
- goto setC2
- movlw b'01000000'
- movwf PORTC
- goto setC1
- setC2
- bcf PORTA, 0
- bsf PORTA, 1
- bcf PORTA, 2
- setC2_A
- btfss PORTB, 4
- goto setC2_B
- movlw b'01011011'
- movwf PORTC
- goto setC1
- setC2_B
- btfss PORTB, 5
- goto setC2_C
- movlw b'01101101'
- movwf PORTC
- goto setC1
- setC2_C
- btfss PORTB, 6
- goto setC2_D
- movlw b'01111111'
- movwf PORTC
- goto setC1
- setC2_D
- btfss PORTB, 7
- goto setC3
- movlw b'00111111'
- movwf PORTC
- goto setC1
- setC3
- bcf PORTA, 0
- bcf PORTA, 1
- bsf PORTA, 2
- setC3_A
- btfss PORTB, 4
- goto setC3_B
- movlw b'01001111'
- movwf PORTC
- goto setC1
- setC3_B
- btfss PORTB, 5
- goto setC3_C
- movlw b'01111100'
- movwf PORTC
- goto setC1
- setC3_C
- btfss PORTB, 6
- goto setC3_D
- movlw b'01100111'
- movwf PORTC
- goto setC1
- setC3_D
- btfss PORTB, 7
- goto setC1
- movlw b'00110110'
- movwf PORTC
- goto setC1
- goto start ; loop forever
- Initialization
- bsf STATUS, RP0
- bcf STATUS, RP1 ;bank 1
- movlw 0xff
- movwf TRISB ;PORTB is input
- clrf TRISA ;PORTA is output
- clrf TRISC ;PORTC is output
- bcf STATUS, RP0
- bsf STATUS, RP1 ;bank 2
- clrf ANSEL
- clrf ANSELH ;set all I/Os as digitals
- bcf STATUS, RP1 ;bank0
- clrf PORTA
- clrf PORTC
- return
For this project you can download all the files from here
No comments:
Post a Comment