main
Last change
on this file since c0a8b2a was c0a8b2a, checked in by Adrien Destugues <pulkomandy@…>, 7 years ago |
Initial work on implementing a standard PS/2 keyboard controller.
Complete with key matrix scanning.
|
-
Property mode
set to
100644
|
File size:
2.1 KB
|
Line | |
---|
1 | #include <usart.h>
|
---|
2 |
|
---|
3 | #include <avr/io.h>
|
---|
4 | #include <avr/interrupt.h>
|
---|
5 | #include <avr/wdt.h>
|
---|
6 | #include <avr/pgmspace.h>
|
---|
7 | #include <util/delay.h>
|
---|
8 |
|
---|
9 | #include <string.h>
|
---|
10 | #include <stdbool.h>
|
---|
11 |
|
---|
12 | // Cnfigure the I/Os. We will use port C for columns (we put one of those to 0
|
---|
13 | // at a time) and port ?? for lines (we read this one and look for pressed keys
|
---|
14 | // in the column).
|
---|
15 | #define DDRCOL DDRC
|
---|
16 | #define PORTCOL PORTC
|
---|
17 | #define PINCOL PINC
|
---|
18 |
|
---|
19 | #define PORTLINE PORTA
|
---|
20 | #define PINLINE PINA
|
---|
21 | #define DDRLINE DDRA
|
---|
22 |
|
---|
23 | static uint8_t matrix[8];
|
---|
24 |
|
---|
25 | int main() {
|
---|
26 | // Enable the watchdog to reboot in case of crashes.
|
---|
27 | wdt_enable(WDTO_2S);
|
---|
28 |
|
---|
29 | // By default, columns are configured as input (high impedance). We force
|
---|
30 | // just one column at a time to the output low state. This way, even in
|
---|
31 | // case of keyboard interferences, there is no short-circuit situation.
|
---|
32 | PORTCOL = 0;
|
---|
33 | DDRCOL = 0;
|
---|
34 |
|
---|
35 | // configure lines as inputs, with pull-up resistors.
|
---|
36 | DDRLINE = 0;
|
---|
37 | PORTLINE = 0xFF;
|
---|
38 |
|
---|
39 | // Let's configure the USART.
|
---|
40 | USARTInit();
|
---|
41 |
|
---|
42 | // Let's rock!
|
---|
43 | uint8_t column = 0;
|
---|
44 | for(;;) {
|
---|
45 | // We are alive
|
---|
46 | wdt_reset();
|
---|
47 |
|
---|
48 | // Scan a column
|
---|
49 | DDRCOL = 1 << column;
|
---|
50 |
|
---|
51 | // Let things settle after setting up the pin. We rely on internal
|
---|
52 | // pull-ups to raise pins which are left unconnected, and this takes
|
---|
53 | // some time.
|
---|
54 | _delay_us(6);
|
---|
55 |
|
---|
56 | // - Read input (8 rows) - we invert is so that pressed key are "1"
|
---|
57 | uint8_t read = ~PINLINE;
|
---|
58 |
|
---|
59 | // Check if there were any changes since previous scan
|
---|
60 | if (read ^ matrix[column]) {
|
---|
61 | // There was a change in this colum!
|
---|
62 | // TODO scan each set bit and report press/release according to
|
---|
63 | // the keyboard matrix
|
---|
64 | USARTWriteChar('c');
|
---|
65 | USARTWriteHex(column);
|
---|
66 | USARTWriteChar(':');
|
---|
67 | USARTWriteHex(read);
|
---|
68 |
|
---|
69 | // Update the matrix for next time so we don't re-report the same keys
|
---|
70 | matrix[column] = read;
|
---|
71 |
|
---|
72 | if (read == 0) {
|
---|
73 | USARTWriteChar('\r');
|
---|
74 | USARTWriteChar('\n');
|
---|
75 | } else
|
---|
76 | USARTWriteChar(' ');
|
---|
77 | }
|
---|
78 |
|
---|
79 | // Prepare next column
|
---|
80 | column++;
|
---|
81 | if (column > 7) {
|
---|
82 | column = 0;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | return 0;
|
---|
87 | }
|
---|
88 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.