Monday, October 8, 2012

Keypad (PIC16F690)

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
This is nothing more than 3 vertical lines (columns), and 4 horizontal lines (rows) which are connected with 12 push buttons. So if i apply voltage to C1 and the user press any button between 1, 4, 7 or * the corresponding row will be connected to the supplying voltage to. The only thing i have to do is to search which row is "high" and i can immediately understand which button  is pressed given that i already know which column is "high". The same logic can be used for the columns 2 and 3. Simple!!! But really smart.

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

As you can see on the flow chart, we are in an infinite loop with three stages. On the first stage we "set" the first column (C1), and we check every row if its high, and accordingly to that we "export" the user input to a  seven segment display. For example if the button 4 is pressed by the user, this will be recognized on the second decision of the first stage as you can see on the flow chart. (a better solution will have to use debounce time, but this requires a longer flow chart and more code).
Please take a look on the following schematic:
proteus simulation of keypad
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). 
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:


  1. start
  2. ; remaining code goes here
  3. call Initialization
  4. setC1
  5. bsf PORTA, 0
  6. bcf PORTA, 1
  7. bcf PORTA, 2
  8. setC1_A
  9. btfss PORTB, 4
  10. goto setC1_B
  11. movlw b'00000110'
  12. movwf PORTC
  13. goto setC1
  14. setC1_B
  15. btfss PORTB, 5
  16. goto setC1_C
  17. movlw b'01100110'
  18. movwf PORTC
  19. goto setC1
  20. setC1_C
  21. btfss PORTB, 6
  22. goto setC1_D
  23. movlw b'00000111'
  24. movwf PORTC
  25. goto setC1
  26. setC1_D
  27. btfss PORTB, 7
  28. goto setC2
  29. movlw b'01000000'
  30. movwf PORTC
  31. goto setC1
  32. setC2
  33. bcf PORTA, 0
  34. bsf PORTA, 1
  35. bcf PORTA, 2
  36. setC2_A
  37. btfss PORTB, 4
  38. goto setC2_B
  39. movlw b'01011011'
  40. movwf PORTC
  41. goto setC1
  42. setC2_B
  43. btfss PORTB, 5
  44. goto setC2_C
  45. movlw b'01101101'
  46. movwf PORTC
  47. goto setC1
  48. setC2_C
  49. btfss PORTB, 6
  50. goto setC2_D
  51. movlw b'01111111'
  52. movwf PORTC
  53. goto setC1
  54. setC2_D
  55. btfss PORTB, 7
  56. goto setC3
  57. movlw b'00111111'
  58. movwf PORTC
  59. goto setC1

  60. setC3
  61. bcf PORTA, 0
  62. bcf PORTA, 1
  63. bsf PORTA, 2
  64. setC3_A
  65. btfss PORTB, 4
  66. goto setC3_B
  67. movlw b'01001111'
  68. movwf PORTC
  69. goto setC1
  70. setC3_B
  71. btfss PORTB, 5
  72. goto setC3_C
  73. movlw b'01111100'
  74. movwf PORTC
  75. goto setC1
  76. setC3_C
  77. btfss PORTB, 6
  78. goto setC3_D
  79. movlw b'01100111'
  80. movwf PORTC
  81. goto setC1
  82. setC3_D
  83. btfss PORTB, 7
  84. goto setC1
  85. movlw b'00110110'
  86. movwf PORTC
  87. goto setC1
  88. goto    start             ; loop forever

  89. Initialization
  90. bsf STATUS, RP0
  91. bcf STATUS, RP1 ;bank 1

  92. movlw 0xff
  93. movwf TRISB ;PORTB is input
  94. clrf TRISA ;PORTA is output
  95. clrf TRISC ;PORTC is output
  96. bcf STATUS, RP0
  97. bsf STATUS, RP1 ;bank 2
  98. clrf ANSEL
  99. clrf ANSELH ;set all I/Os as digitals

  100. bcf STATUS, RP1 ;bank0

  101. clrf PORTA
  102. clrf PORTC
  103. return
        

For this project you can download all the files from here






No comments:

Post a Comment