Advertisement
Guest User

Matrix

a guest
Oct 1st, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.07 KB | None | 0 0
  1. /*
  2. Copyright 2012 Jun Wako <wakojun@gmail.com>
  3.  
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16. */
  17.  
  18. /*
  19.  * scan matrix
  20.  */
  21. #include <stdint.h>
  22. #include <stdint.h>
  23. #include <stdbool.h>
  24. #include <avr/io.h>
  25. #include <util/delay.h>
  26. #include "print.h"
  27. #include "debug.h"
  28. #include "util.h"
  29. #include "matrix.h"
  30.  
  31.  
  32. #ifndef DEBOUNCE
  33. #   define DEBOUNCE 5
  34. #endif
  35. static uint8_t debouncing = DEBOUNCE;
  36.  
  37. /* matrix state(1:on, 0:off) */
  38. static matrix_row_t matrix[MATRIX_ROWS];
  39. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  40.  
  41. static matrix_row_t read_cols(void);
  42. static void init_cols(void);
  43. static void unselect_rows(void);
  44. static void select_row(uint8_t row);
  45.  
  46.  
  47. inline
  48. uint8_t matrix_rows(void)
  49. {
  50.     return MATRIX_ROWS;
  51. }
  52.  
  53. inline
  54. uint8_t matrix_cols(void)
  55. {
  56.     return MATRIX_COLS;
  57. }
  58.  
  59. void matrix_init(void)
  60. {
  61.     // initialize row and col
  62.     unselect_rows();
  63.     init_cols();
  64.  
  65.     // initialize matrix state: all keys off
  66.     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  67.         matrix[i] = 0;
  68.         matrix_debouncing[i] = 0;
  69.     }
  70. }
  71.  
  72. uint8_t matrix_scan(void)
  73. {
  74.     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  75.         select_row(i);
  76.         _delay_us(30);  // without this wait read unstable value.
  77.         matrix_row_t cols = read_cols();
  78.         if (matrix_debouncing[i] != cols) {
  79.             matrix_debouncing[i] = cols;
  80.             if (debouncing) {
  81.                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
  82.             }
  83.             debouncing = DEBOUNCE;
  84.         }
  85.         unselect_rows();
  86.     }
  87.  
  88.     if (debouncing) {
  89.         if (--debouncing) {
  90.             _delay_ms(1);
  91.         } else {
  92.             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  93.                 matrix[i] = matrix_debouncing[i];
  94.             }
  95.         }
  96.     }
  97.  
  98.     return 1;
  99. }
  100.  
  101. bool matrix_is_modified(void)
  102. {
  103.     if (debouncing) return false;
  104.     return true;
  105. }
  106.  
  107. inline
  108. bool matrix_is_on(uint8_t row, uint8_t col)
  109. {
  110.     return (matrix[row] & ((matrix_row_t)1<<col));
  111. }
  112.  
  113. inline
  114. matrix_row_t matrix_get_row(uint8_t row)
  115. {
  116.     return matrix[row];
  117. }
  118.  
  119. void matrix_print(void)
  120. {
  121.     print("\nr/c 0123456789ABCDEF\n");
  122.     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  123.         phex(row); print(": ");
  124.         pbin_reverse16(matrix_get_row(row));
  125.         print("\n");
  126.     }
  127. }
  128.  
  129. uint8_t matrix_key_count(void)
  130. {
  131.     uint8_t count = 0;
  132.     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  133.         count += bitpop16(matrix[i]);
  134.     }
  135.     return count;
  136. }
  137.  
  138. /* Column pin configuration
  139.  * col: 0   1   2   3   4   5   6   7   8   9   10  11  12  13
  140.  * pin: F0  F1  E6  C7  C6  B6  D4  B1  B0  B5  B4  D7  D6  B3  (Rev.A)
  141.  * pin:                                 B7                      (Rev.B)
  142.  */
  143. static void  init_cols(void)
  144. {
  145.     // Input with pull-up(DDR:0, PORT:1)
  146.     DDRF  &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
  147.     PORTF |=  (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
  148.     DDRD  &= ~(1<<7 | 1<<6 | 1<<5);
  149.     PORTD |=  (1<<7 | 1<<6);
  150.     DDRB  &= ~(1<<6 | 1<<5 | 1<<4);
  151.     PORTB |=  (1<<6 | 1<<5 | 1<<4);
  152.     DDRC  &= ~(1<<7 | 1<<6);
  153.     PORTC |=  (1<<7 | 1<<6);
  154. }
  155.  
  156. static matrix_row_t read_cols(void)
  157. {
  158.     return (PIND&(1<<5) ? 0 : (1<<0));
  159.            (PINC&(1<<7) ? 0 : (1<<1)) |
  160.            (PINC&(1<<6) ? 0 : (1<<2)) |
  161.            (PINB&(1<<5) ? 0 : (1<<3)) |
  162.            (PINC&(1<<6) ? 0 : (1<<4)) |
  163.            (PINF&(1<<7) ? 0 : (1<<5)) |
  164.            (PINF&(1<<6) ? 0 : (1<<6)) |
  165.            (PINF&(1<<5) ? 0 : (1<<7)) |
  166.            (PINF&(1<<4) ? 0 : (1<<8)) |
  167.            (PINF&(1<<1) ? 0 : (1<<9)) |
  168.            (PINF&(1<<0) ? 0 : (1<<10)) |
  169.            (PINB&(1<<4) ? 0 : (1<<11)) |
  170.            (PIND&(1<<7) ? 0 : (1<<12)) |
  171.            (PIND&(1<<6) ? 0 : (1<<13));
  172. }
  173.  
  174. /* Row pin configuration
  175.  * row: 0   1   2   3   4`
  176.  * pin: D0  D1  D2  D3  D5
  177.  */
  178. static void unselect_rows(void)
  179. {
  180.     // Hi-Z(DDR:0, PORT:0) to unselect
  181.     DDRD  &= ~0b00001010;
  182.     PORTD &= ~0b00001010;
  183.     DDRB  &= ~0b10000101;
  184.     PORTB &= ~0b10000101;
  185. }
  186.  
  187. static void select_row(uint8_t row)
  188. {
  189.     // Output low(DDR:1, PORT:0) to select
  190.     switch (row) {
  191.         case 0:
  192.             DDRD  |= (1<<3);
  193.             PORTD &= ~(1<<3);
  194.             break;
  195.         case 1:
  196.             DDRD  |= (1<<1);
  197.             PORTD &= ~(1<<1);
  198.             break;
  199.         case 2:
  200.             DDRB  |= (1<<7);
  201.             PORTB &= ~(1<<7);
  202.             break;
  203.         case 3:
  204.             DDRB  |= (1<<2);
  205.             PORTB &= ~(1<<2);
  206.             break;
  207.         case 4:
  208.             DDRB  |= (1<<0);
  209.             PORTB &= ~(1<<0);
  210.             break;
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement