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