Advertisement
Tempist

DriverModule [Scan-KBD]

Oct 22nd, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.97 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////
  2. ////                  Generic keypad scan driver                       ////
  3. ////                                                                   ////
  4. ////  kbd_init()   Must be called before any other function.           ////
  5. ////                                                                   ////
  6. ////  c = kbd_getc(c)  Will return a key value if pressed or /0 if not ////
  7. ////                   This function should be called frequently so as ////
  8. ////                   not to miss a key press.                        ////
  9. ////                                                                   ////
  10. ///////////////////////////////////////////////////////////////////////////
  11. ///////////////////////////////////////////////////////////////////////////
  12.  
  13. ////////////////// The following defines the keypad layout on port D
  14.  
  15. // Un-comment the following define to use port B
  16. // #define use_portb_kbd TRUE
  17.  
  18. // Make sure the port used has pull-up resistors (or the LCD) on
  19. // the column pins
  20.  
  21.  
  22. #if defined(__PCH__)
  23. #if defined use_portb_kbd
  24.    #byte kbd = 0xF81                   // This puts the entire structure
  25. #else
  26.    #byte kbd = 0xF83                   // This puts the entire structure
  27. #endif
  28. #else
  29. #if defined use_portb_kbd
  30.    #byte kbd = 6                  // on to port B (at address 6)
  31. #else
  32.    #byte kbd = 8                 // on to port D (at address 8)
  33. #endif
  34. #endif
  35.  
  36. #if defined use_portb_kbd
  37.    #define set_tris_kbd(x) set_tris_b(x)
  38. #else
  39.    #define set_tris_kbd(x) set_tris_d(x)
  40. #endif
  41.  
  42.  
  43.  
  44. //Keypad connection:   (for example column 0 is B2)
  45. //                Bx:
  46.  
  47. #ifdef blue_keypad  ///////////////////////////////////// For the blue keypad
  48. #define COL0 (1 << 2)
  49. #define COL1 (1 << 3)
  50. #define COL2 (1 << 6)
  51.  
  52. #define ROW0 (1 << 4)
  53. #define ROW1 (1 << 7)
  54. #define ROW2 (1 << 1)
  55. #define ROW3 (1 << 5)
  56.  
  57. #else ////////////////////////////////////////////////// For the black keypad
  58. #define COL0 (1 << 5)
  59. #define COL1 (1 << 6)
  60. #define COL2 (1 << 7)
  61.  
  62. #define ROW0 (1 << 1)
  63. #define ROW1 (1 << 2)
  64. #define ROW2 (1 << 3)
  65. #define ROW3 (1 << 4)
  66.  
  67. #endif
  68.  
  69. #define ALL_ROWS (ROW0|ROW1|ROW2|ROW3)
  70. #define ALL_PINS (ALL_ROWS|COL0|COL1|COL2)
  71.  
  72. // Keypad layout:
  73. char const KEYS[4][3] = {{'1','2','3'},
  74.                          {'4','5','6'},
  75.                          {'7','8','9'},
  76.                          {'*','0','#'}};
  77.  
  78. #define KBD_DEBOUNCE_FACTOR 33    // Set this number to apx n/333 where
  79.                                   // n is the number of times you expect
  80.                                   // to call kbd_getc each second
  81.  
  82.  
  83. void kbd_init() {
  84. }
  85.  
  86. char kbd_getc( ) {
  87.    static BYTE kbd_call_count;
  88.    static int1 kbd_down;
  89.    static char last_key;
  90.    static BYTE col;
  91.  
  92.    BYTE kchar;
  93.    BYTE row;
  94.  
  95.    kchar='\0';
  96.    if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) {
  97.        switch (col) {
  98.          case 0   : set_tris_kbd(ALL_PINS&~COL0);
  99.                     kbd=~COL0&ALL_PINS;
  100.                     break;
  101.          case 1   : set_tris_kbd(ALL_PINS&~COL1);
  102.                     kbd=~COL1&ALL_PINS;
  103.                     break;
  104.          case 2   : set_tris_kbd(ALL_PINS&~COL2);
  105.                     kbd=~COL2&ALL_PINS;
  106.                     break;
  107.        }
  108.  
  109.        if(kbd_down) {
  110.          if((kbd & (ALL_ROWS))==(ALL_ROWS)) {
  111.            kbd_down=FALSE;
  112.            kchar=last_key;
  113.            last_key='\0';
  114.          }
  115.        } else {
  116.           if((kbd & (ALL_ROWS))!=(ALL_ROWS)) {
  117.              if((kbd & ROW0)==0)
  118.                row=0;
  119.              else if((kbd & ROW1)==0)
  120.                row=1;
  121.              else if((kbd & ROW2)==0)
  122.                row=2;
  123.              else if((kbd & ROW3)==0)
  124.                row=3;
  125.              last_key =KEYS[row][col];
  126.              kbd_down = TRUE;
  127.           } else {
  128.              ++col;
  129.              if(col==3)
  130.                col=0;
  131.           }
  132.        }
  133.       kbd_call_count=0;
  134.    }
  135.   set_tris_kbd(ALL_PINS);
  136.   return(kchar);
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement