Guest User

matrix.c

a guest
Jul 2nd, 2016
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. /*
  2.  * scan matrix
  3.  */
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. #include <avr/io.h>
  7. #include <util/delay.h>
  8. #include "print.h"
  9. #include "debug.h"
  10. #include "util.h"
  11. #include "matrix.h"
  12.  
  13.  
  14. #ifndef DEBOUNCE
  15. #define DEBOUNCE 5
  16. #endif
  17. static uint8_t debouncing = DEBOUNCE;
  18.  
  19. /* matrix state(1:on, 0:off) */
  20. static matrix_row_t matrix[MATRIX_ROWS];
  21. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  22.  
  23. static matrix_row_t read_cols(void);
  24. static void init_cols(void);
  25. static void unselect_rows(void);
  26. static void select_row(uint8_t row);
  27.  
  28.  
  29. inline
  30. uint8_t matrix_rows(void)
  31. {
  32.     return MATRIX_ROWS;
  33. }
  34.  
  35. inline
  36. uint8_t matrix_cols(void)
  37. {
  38.     return MATRIX_COLS;
  39. }
  40.  
  41. void matrix_init(void)
  42. {
  43.     // initialize row and col
  44.     unselect_rows();
  45.     init_cols();
  46.  
  47.     // initialize matrix state: all keys off
  48.     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  49.         matrix[i] = 0;
  50.         matrix_debouncing[i] = 0;
  51.     }
  52. }
  53.  
  54. uint8_t matrix_scan(void)
  55. {
  56.     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  57.         select_row(i);
  58.         _delay_us(30);  // without this wait read unstable value.
  59.         matrix_row_t cols = read_cols();
  60.         if (matrix_debouncing[i] != cols) {
  61.             matrix_debouncing[i] = cols;
  62.             if (debouncing) {
  63.                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
  64.             }
  65.             debouncing = DEBOUNCE;
  66.         }
  67.         unselect_rows();
  68.     }
  69.  
  70.     if (debouncing) {
  71.         if (--debouncing) {
  72.             _delay_ms(1);
  73.         } else {
  74.             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  75.                 matrix[i] = matrix_debouncing[i];
  76.             }
  77.         }
  78.     }
  79.  
  80.     return 1;
  81. }
  82.  
  83. bool matrix_is_modified(void)
  84. {
  85.     if (debouncing) return false;
  86.     return true;
  87. }
  88.  
  89. inline
  90. bool matrix_is_on(uint8_t row, uint8_t col)
  91. {
  92.     return (matrix[row] & ((matrix_row_t)1<<col));
  93. }
  94.  
  95. inline
  96. matrix_row_t matrix_get_row(uint8_t row)
  97. {
  98.     return matrix[row];
  99. }
  100.  
  101. void matrix_print(void)
  102. {
  103.     print("\nr/c 0123456789ABCDEF\n");
  104.     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  105.         phex(row); print(": ");
  106.         pbin_reverse16(matrix_get_row(row));
  107.         print("\n");
  108.     }
  109. }
  110.  
  111. uint8_t matrix_key_count(void)
  112. {
  113.     uint8_t count = 0;
  114.     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  115.         count += bitpop16(matrix[i]);
  116.     }
  117.     return count;
  118. }
  119.  
  120. static matrix_row_t read_cols(void)
  121. {
  122.     return (PINF&(1<<7) ? 0 : (1<<4)) |
  123.            (PINB&(1<<6) ? 0 : (1<<3)) |
  124.            (PINB&(1<<5) ? 0 : (1<<2)) |
  125.            (PINB&(1<<4) ? 0 : (1<<1)) |
  126.            (PIND&(1<<7) ? 0 : (1<<0)) |
  127.            (PINC&(1<<7) ? 0 : (1<<5)) |
  128.            (PINC&(1<<6) ? 0 : (1<<6)) |
  129.            (PIND&(1<<3) ? 0 : (1<<7)) |
  130.            (PIND&(1<<2) ? 0 : (1<<8)) |
  131.            (PIND&(1<<1) ? 0 : (1<<9)) |
  132.            (PIND&(1<<0) ? 0 : (1<<10)) |
  133.            (PINB&(1<<7) ? 0 : (1<<13)) |
  134.            (PINB&(1<<3) ? 0 : (1<<12)) |
  135.            (PINB&(1<<2) ? 0 : (1<<11)) ;
  136. }
  137.  
  138. /* Row pin configuration
  139.  * row: 0   1   2   3   4
  140.  * pin: D0  D1  D2  D3  D5
  141.  */
  142. static void unselect_rows(void)
  143. {
  144.     // Hi-Z(DDR:0, PORT:0) to unselect
  145.     DDRF  &= ~0b01110011;
  146.     PORTF &= ~0b01110011;
  147. }
  148.  
  149. static void select_row(uint8_t row)
  150. {
  151.     // Output low(DDR:1, PORT:0) to select
  152.     switch (row) {
  153.         case 0:
  154.             DDRF  |= (1<<6);
  155.             PORTF &= ~(1<<6);
  156.             break;
  157.         case 1:
  158.             DDRF  |= (1<<5);
  159.             PORTF &= ~(1<<5);
  160.             break;
  161.         case 2:
  162.             DDRF  |= (1<<4);
  163.             PORTF &= ~(1<<4);
  164.             break;
  165.         case 3:
  166.             DDRF  |= (1<<1);
  167.             PORTF &= ~(1<<1);
  168.             break;
  169.         case 4:
  170.             DDRF  |= (1<<0);
  171.             PORTF &= ~(1<<0);
  172.             break;
  173.     }
  174. }
  175.  
  176.  
  177. static void init_cols(void)
  178. {
  179.     // Input with pull-up(DDR:0, PORT:1)
  180.     DDRF  &= ~(1<<7);
  181.     PORTF |=  (1<<7);
  182.     DDRB  &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<3 | 1<<2);
  183.     PORTB |=  (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<3 | 1<<2);
  184.     DDRD  &= ~(1<<7 | 1<<3 | 1<<2 | 1<<1 | 1<<0 );
  185.     PORTD |=  (1<<7 | 1<<3 | 1<<2 | 1<<1 | 1<<0 );
  186.     DDRC  &= ~(1<<7 | 1<<6);
  187.     PORTC |=  (1<<7 | 1<<6);
  188. }
Advertisement
Add Comment
Please, Sign In to add comment