1 /* Copyright 2012 Jun Wako <wakojun@gmail.com>
3 * This is heavily based on phantom/board.{c|h}.
4 * https://github.com/BathroomEpiphanies/AVR-Keyboard
6 * Copyright (c) 2012 Fredrik Atmer, Bathroom Epiphanies Inc
7 * http://bathroomepiphanies.com
9 * As for liscensing consult with the original files or its author.
14 #include <util/delay.h>
24 static uint8_t debouncing = DEBOUNCE;
26 // bit array of key state(1:on, 0:off)
27 static matrix_row_t matrix[MATRIX_ROWS];
28 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
30 static uint8_t read_rows(void);
31 static void init_rows(void);
32 static void unselect_cols(void);
33 static void select_col(uint8_t col);
35 /* LEDs are on output compare pins OC1B OC1C
36 This activates fast PWM mode on them.
37 Prescaler 256 and 8-bit counter results in
38 16000000/256/256 = 244 Hz blink frequency.
41 /* Output on PWM pins are turned off when the timer
42 reaches the value in the output compare register,
43 and are turned on when it reaches TOP (=256). */
45 void setup_leds(void) {
46 TCCR1A |= // Timer control register 1A
47 (1<<WGM10) | // Fast PWM 8-bit
48 (1<<COM1B1)| // Clear OC1B on match, set at TOP
49 (1<<COM1C1); // Clear OC1C on match, set at TOP
50 TCCR1B |= // Timer control register 1B
51 (1<<WGM12) | // Fast PWM 8-bit
52 (1<<CS12); // Prescaler 256
53 OCR1B = 250; // Output compare register 1B
54 OCR1C = 250; // Output compare register 1C
55 // LEDs: LED_A -> PORTB6, LED_B -> PORTB7
62 uint8_t matrix_rows(void)
68 uint8_t matrix_cols(void)
73 void matrix_init(void)
75 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
79 // initialize row and col
84 // initialize matrix state: all keys off
85 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
87 matrix_debouncing[i] = 0;
91 uint8_t matrix_scan(void)
93 for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-16
95 _delay_us(3); // without this wait it won't read stable value.
96 uint8_t rows = read_rows();
97 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-5
98 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
99 bool curr_bit = rows & (1<<row);
100 if (prev_bit != curr_bit) {
101 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
103 debug("bounce!: "); debug_hex(debouncing); print("\n");
105 debouncing = DEBOUNCE;
115 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
116 matrix[i] = matrix_debouncing[i];
124 bool matrix_is_modified(void)
126 if (debouncing) return false;
131 bool matrix_is_on(uint8_t row, uint8_t col)
133 return (matrix[row] & ((matrix_row_t)1<<col));
137 matrix_row_t matrix_get_row(uint8_t row)
142 void matrix_print(void)
144 print("\nr/c 0123456789ABCDEF\n");
145 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
146 phex(row); print(": ");
147 print_bin_reverse32(matrix_get_row(row));
152 uint8_t matrix_key_count(void)
155 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
156 count += bitpop32(matrix[i]);
161 /* Row pin configuration
163 * pin: B0 B1 B2 B3 B4 B5
165 static void init_rows(void)
167 // Input with pull-up(DDR:0, PORT:1)
172 static uint8_t read_rows(void)
174 return (PINB&(1<<0) ? 0 : (1<<0)) |
175 (PINB&(1<<1) ? 0 : (1<<1)) |
176 (PINB&(1<<2) ? 0 : (1<<2)) |
177 (PINB&(1<<3) ? 0 : (1<<3)) |
178 (PINB&(1<<4) ? 0 : (1<<4)) |
179 (PINB&(1<<5) ? 0 : (1<<5));
182 /* Column pin configuration
183 * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
184 * pin: D5 C7 C6 D4 D0 E6 F0 F1 F4 F5 F6 F7 D7 D6 D1 D2 D3
186 static void unselect_cols(void)
188 // Hi-Z(DDR:0, PORT:0) to unselect
189 DDRC |= 0b11000000; // PC: 7 6
191 DDRD |= 0b11111111; // PD: 7 6 5 4 3 2 1 0
193 DDRE |= 0b01000000; // PE: 6
195 DDRF |= 0b11110011; // PF: 7 6 5 4 1 0
199 static void select_col(uint8_t col)
201 // Output low(DDR:1, PORT:0) to select