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"
31 #define LED_CONFIG (DDRD |= (1<<6))
32 #define LED_ON (PORTD &= ~(1<<6))
33 #define LED_OFF (PORTD |= (1<<6))
34 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
36 uint8_t number_keys[10]=
37 {KEY_0,KEY_1,KEY_2,KEY_3,KEY_4,KEY_5,KEY_6,KEY_7,KEY_8,KEY_9};
39 uint16_t idle_count=0;
43 uint8_t b, d, mask, i, reset_idle;
44 uint8_t b_prev=0xFF, d_prev=0xFF;
46 // set for 16 MHz clock
49 // Configure all port B and port D pins as inputs with pullup resistors.
50 // See the "Using I/O Pins" page for details.
51 // http://www.pjrc.com/teensy/pins.html
57 // Initialize the USB, and then wait for the host to set configuration.
58 // If the Teensy is powered without a PC connected to the USB port,
59 // this will wait forever.
61 while (!usb_configured()) /* wait */ ;
63 // Wait an extra second for the PC's operating system to load drivers
64 // and do whatever it does to actually be ready for input
67 // Configure timer 0 to generate a timer overflow interrupt every
68 // 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
69 // This demonstrates how to use interrupts to implement a simple
70 // inactivity timeout.
75 print("Begin keyboard example program\n");
76 print("All Port B or Port D pins are inputs with pullup resistors.\n");
77 print("Any connection to ground on Port B or D pins will result in\n");
78 print("keystrokes sent to the PC (and debug messages here).\n");
80 // read all port B and port D pins
83 // check if any pins are low, but were high previously
87 if (((b & mask) == 0) && (b_prev & mask) != 0) {
88 //usb_keyboard_press(KEY_B, KEY_SHIFT);
89 //usb_keyboard_press(number_keys[i], 0);
90 print("Port B, bit ");
95 if (((d & mask) == 0) && (d_prev & mask) != 0) {
96 //usb_keyboard_press(KEY_D, KEY_SHIFT);
97 //usb_keyboard_press(number_keys[i], 0);
98 print("Port D, bit ");
105 // if any keypresses were detected, reset the idle counter
107 // variables shared with interrupt routines must be
108 // accessed carefully so the interrupt routine doesn't
109 // try to use the variable in the middle of our access
114 // now the current pins will be the previous, and
115 // wait a short delay so we're not highly sensitive
116 // to mechanical "bounce".
123 // This interrupt routine is run approx 61 times per second.
124 // A very simple inactivity timeout is implemented, where we
125 // will send a space character and print a message to the
126 // hid_listen debug message window.
130 if (idle_count > 61 * 8) {
132 print("Timer Event :)\n");
133 //usb_keyboard_press(KEY_SPACE, 0);