Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

matrix.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * scan matrix
  3. */
  4. #include <avr/io.h>
  5. #include <util/delay.h>
  6. #include "keymap.h"
  7. #include "matrix.h"
  8. #include "print.h"
  9. uint8_t *matrix;
  10. uint8_t *prev_matrix;
  11. static uint8_t _matrix0[MATRIX_ROWS];
  12. static uint8_t _matrix1[MATRIX_ROWS];
  13. static uint8_t read_col(void);
  14. static void select_row(uint8_t row);
  15. void matrix_init(void)
  16. {
  17. // Column: input w/pullup
  18. DDRB = 0x00;
  19. PORTB = 0xFF;
  20. // Row: Hi-Z(unselected)
  21. // PD:0,1,2,3,6,7
  22. // PC:6,7
  23. // PF:7
  24. DDRD = 0x00;
  25. PORTD = 0x00;
  26. DDRC = 0x00;
  27. PORTC = 0x00;
  28. DDRF = 0x00;
  29. PORTF = 0x00;
  30. for (int i=0; i < MATRIX_ROWS; i++) {
  31. _matrix0[i] = 0xFF;
  32. _matrix1[i] = 0xFF;
  33. }
  34. matrix = _matrix0;
  35. prev_matrix = _matrix1;
  36. }
  37. uint8_t matrix_scan(void)
  38. {
  39. uint8_t row, state;
  40. uint8_t *tmp;
  41. tmp = prev_matrix;
  42. prev_matrix = matrix;
  43. matrix = tmp;
  44. for (row = 0; row < MATRIX_ROWS; row++) {
  45. select_row(row);
  46. _delay_us(30); // without this wait read unstable value.
  47. state = read_col();
  48. matrix[row] = state;
  49. }
  50. return 1;
  51. }
  52. static uint8_t read_col(void)
  53. {
  54. return PINB;
  55. }
  56. static void select_row(uint8_t row)
  57. {
  58. switch (row) {
  59. case 0:
  60. DDRD = (1<<0);
  61. PORTD = 0x00;
  62. DDRC = 0x00;
  63. PORTC = 0x00;
  64. DDRF = 0x00;
  65. PORTF = 0x00;
  66. break;
  67. case 1:
  68. DDRD = (1<<1);
  69. PORTD = 0x00;
  70. DDRC = 0x00;
  71. PORTC = 0x00;
  72. DDRF = 0x00;
  73. PORTF = 0x00;
  74. break;
  75. case 2:
  76. DDRD = (1<<2);
  77. PORTD = 0x00;
  78. DDRC = 0x00;
  79. PORTC = 0x00;
  80. DDRF = 0x00;
  81. PORTF = 0x00;
  82. break;
  83. case 3:
  84. DDRD = (1<<3);
  85. PORTD = 0x00;
  86. DDRC = 0x00;
  87. PORTC = 0x00;
  88. DDRF = 0x00;
  89. PORTF = 0x00;
  90. break;
  91. case 4:
  92. DDRD = (1<<6);
  93. PORTD = 0x00;
  94. DDRC = 0x00;
  95. PORTC = 0x00;
  96. DDRF = 0x00;
  97. PORTF = 0x00;
  98. break;
  99. case 5:
  100. DDRD = (1<<7);
  101. PORTD = 0x00;
  102. DDRC = 0x00;
  103. PORTC = 0x00;
  104. DDRF = 0x00;
  105. PORTF = 0x00;
  106. break;
  107. case 6:
  108. DDRD = 0x00;
  109. PORTD = 0x00;
  110. DDRC = (1<<6);
  111. PORTC = 0x00;
  112. DDRF = 0x00;
  113. PORTF = 0x00;
  114. break;
  115. case 7:
  116. DDRD = 0x00;
  117. PORTD = 0x00;
  118. DDRC = (1<<7);
  119. PORTC = 0x00;
  120. DDRF = 0x00;
  121. PORTF = 0x00;
  122. break;
  123. case 8:
  124. DDRD = 0x00;
  125. PORTD = 0x00;
  126. DDRC = 0x00;
  127. PORTC = 0x00;
  128. DDRF = (1<<7);
  129. PORTF = 0x00;
  130. break;
  131. }
  132. }