1 /* Keyboard example with debug channel, for Teensy USB Development Board
2 * http://www.pjrc.com/teensy/usb_keyboard.html
3 * Copyright (c) 2008 PJRC.COM, LLC
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include <avr/pgmspace.h>
26 #include <avr/interrupt.h>
27 #include <util/delay.h>
28 #include "usb_keyboard_debug.h"
32 #define LED_CONFIG (DDRD |= (1<<6))
33 #define LED_ON (PORTD &= ~(1<<6))
34 #define LED_OFF (PORTD |= (1<<6))
35 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
37 uint8_t number_keys[10]=
38 {KEY_0,KEY_1,KEY_2,KEY_3,KEY_4,KEY_5,KEY_6,KEY_7,KEY_8,KEY_9};
40 uint16_t idle_count=0;
49 // initialize ports for matrix
52 // Column: input w/pullup
56 // Row: Hi-Z(unselected)
68 // select a row of matrix for read
69 void select_row(uint8_t row)
147 uint8_t read_col(void)
154 uint8_t i, reset_idle;
155 uint8_t prev_state[MAX_ROW];
156 for (int i=0; i < MAX_ROW; i++) prev_state[i] = 0xFF;
158 // set for 16 MHz clock
164 // Initialize the USB, and then wait for the host to set configuration.
165 // If the Teensy is powered without a PC connected to the USB port,
166 // this will wait forever.
168 while (!usb_configured()) /* wait */ ;
170 // Wait an extra second for the PC's operating system to load drivers
171 // and do whatever it does to actually be ready for input
174 // Configure timer 0 to generate a timer overflow interrupt every
175 // 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
176 // This demonstrates how to use interrupts to implement a simple
177 // inactivity timeout.
182 print("Begin keyboard example program\n");
183 print("All Port B or Port D pins are inputs with pullup resistors.\n");
184 print("Any connection to ground on Port B or D pins will result in\n");
185 print("keystrokes sent to the PC (and debug messages here).\n");
192 for (uint8_t r=0; r < MAX_ROW; r++) {
195 // without this read unstable value.
199 if (col != prev_state[r]) {
206 for (int c = 0; c < 8; c++) {
207 if (col & 1<<c) continue;
208 code = get_keycode(r, c);
211 usb_keyboard_press(code, 0);
220 // if any keypresses were detected, reset the idle counter
222 // variables shared with interrupt routines must be
223 // accessed carefully so the interrupt routine doesn't
224 // try to use the variable in the middle of our access
230 // now the current pins will be the previous, and
231 // wait a short delay so we're not highly sensitive
232 // to mechanical "bounce".
237 // This interrupt routine is run approx 61 times per second.
238 // A very simple inactivity timeout is implemented, where we
239 // will send a space character and print a message to the
240 // hid_listen debug message window.
244 if (idle_count > 61 * 8) {
246 print("Timer Event :)\n");
247 //usb_keyboard_press(KEY_SPACE, 0);