Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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
  4. *
  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:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  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
  21. * THE SOFTWARE.
  22. */
  23. #include <avr/io.h>
  24. #include <avr/pgmspace.h>
  25. #include <avr/interrupt.h>
  26. #include <util/delay.h>
  27. #include "usb_keyboard_debug.h"
  28. #include "print.h"
  29. #include "keymap.h"
  30. #define LED_CONFIG (DDRD |= (1<<6))
  31. #define LED_ON (PORTD &= ~(1<<6))
  32. #define LED_OFF (PORTD |= (1<<6))
  33. #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
  34. uint8_t number_keys[10]=
  35. {KEY_0,KEY_1,KEY_2,KEY_3,KEY_4,KEY_5,KEY_6,KEY_7,KEY_8,KEY_9};
  36. uint16_t idle_count=0;
  37. //
  38. // scan matrix
  39. //
  40. uint8_t MAX_ROW = 9;
  41. // initialize ports for matrix
  42. void port_setup(void)
  43. {
  44. // Column: input w/pullup
  45. DDRB = 0x00;
  46. PORTB = 0xFF;
  47. // Row: Hi-Z(unselected)
  48. // PD:0,1,2,3,6,7
  49. // PC:6,7
  50. // PF:7
  51. DDRD = 0x00;
  52. PORTD = 0x00;
  53. DDRC = 0x00;
  54. PORTC = 0x00;
  55. DDRF = 0x00;
  56. PORTF = 0x00;
  57. }
  58. // select a row of matrix for read
  59. void select_row(uint8_t row)
  60. {
  61. switch (row) {
  62. case 0:
  63. DDRD = (1<<0);
  64. PORTD = 0x00;
  65. DDRC = 0x00;
  66. PORTC = 0x00;
  67. DDRF = 0x00;
  68. PORTF = 0x00;
  69. break;
  70. case 1:
  71. DDRD = (1<<1);
  72. PORTD = 0x00;
  73. DDRC = 0x00;
  74. PORTC = 0x00;
  75. DDRF = 0x00;
  76. PORTF = 0x00;
  77. break;
  78. case 2:
  79. DDRD = (1<<2);
  80. PORTD = 0x00;
  81. DDRC = 0x00;
  82. PORTC = 0x00;
  83. DDRF = 0x00;
  84. PORTF = 0x00;
  85. break;
  86. case 3:
  87. DDRD = (1<<3);
  88. PORTD = 0x00;
  89. DDRC = 0x00;
  90. PORTC = 0x00;
  91. DDRF = 0x00;
  92. PORTF = 0x00;
  93. break;
  94. case 4:
  95. DDRD = (1<<6);
  96. PORTD = 0x00;
  97. DDRC = 0x00;
  98. PORTC = 0x00;
  99. DDRF = 0x00;
  100. PORTF = 0x00;
  101. break;
  102. case 5:
  103. DDRD = (1<<7);
  104. PORTD = 0x00;
  105. DDRC = 0x00;
  106. PORTC = 0x00;
  107. DDRF = 0x00;
  108. PORTF = 0x00;
  109. break;
  110. case 6:
  111. DDRD = 0x00;
  112. PORTD = 0x00;
  113. DDRC = (1<<6);
  114. PORTC = 0x00;
  115. DDRF = 0x00;
  116. PORTF = 0x00;
  117. break;
  118. case 7:
  119. DDRD = 0x00;
  120. PORTD = 0x00;
  121. DDRC = (1<<7);
  122. PORTC = 0x00;
  123. DDRF = 0x00;
  124. PORTF = 0x00;
  125. break;
  126. case 8:
  127. DDRD = 0x00;
  128. PORTD = 0x00;
  129. DDRC = 0x00;
  130. PORTC = 0x00;
  131. DDRF = (1<<7);
  132. PORTF = 0x00;
  133. break;
  134. }
  135. }
  136. uint8_t read_col(void)
  137. {
  138. return PINB;
  139. }
  140. int main(void)
  141. {
  142. uint8_t i, reset_idle;
  143. uint8_t prev_state[MAX_ROW];
  144. for (int i=0; i < MAX_ROW; i++) prev_state[i] = 0xFF;
  145. // set for 16 MHz clock
  146. CPU_PRESCALE(0);
  147. port_setup();
  148. // Initialize the USB, and then wait for the host to set configuration.
  149. // If the Teensy is powered without a PC connected to the USB port,
  150. // this will wait forever.
  151. usb_init();
  152. while (!usb_configured()) /* wait */ ;
  153. // Wait an extra second for the PC's operating system to load drivers
  154. // and do whatever it does to actually be ready for input
  155. _delay_ms(1000);
  156. // Configure timer 0 to generate a timer overflow interrupt every
  157. // 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
  158. // This demonstrates how to use interrupts to implement a simple
  159. // inactivity timeout.
  160. TCCR0A = 0x00;
  161. TCCR0B = 0x05;
  162. TIMSK0 = (1<<TOIE0);
  163. print("Begin keyboard example program\n");
  164. print("All Port B or Port D pins are inputs with pullup resistors.\n");
  165. print("Any connection to ground on Port B or D pins will result in\n");
  166. print("keystrokes sent to the PC (and debug messages here).\n");
  167. uint8_t col;
  168. uint8_t code;
  169. while (1) {
  170. reset_idle = 0;
  171. for (uint8_t r=0; r < MAX_ROW; r++) {
  172. select_row(r);
  173. // without this read unstable value.
  174. _delay_us(30);
  175. col = read_col();
  176. if (col != prev_state[r]) {
  177. prev_state[r] = col;
  178. phex(r);
  179. print(": ");
  180. pbin(col);
  181. print("\n");
  182. for (int c = 0; c < 8; c++) {
  183. if (col & 1<<c) continue;
  184. code = get_keycode(r, c);
  185. phex(code);
  186. print("\n");
  187. usb_keyboard_press(code, 0);
  188. }
  189. reset_idle = 1;
  190. }
  191. }
  192. // if any keypresses were detected, reset the idle counter
  193. if (reset_idle) {
  194. // variables shared with interrupt routines must be
  195. // accessed carefully so the interrupt routine doesn't
  196. // try to use the variable in the middle of our access
  197. cli();
  198. idle_count = 0;
  199. sei();
  200. }
  201. // now the current pins will be the previous, and
  202. // wait a short delay so we're not highly sensitive
  203. // to mechanical "bounce".
  204. _delay_ms(2);
  205. }
  206. }
  207. // This interrupt routine is run approx 61 times per second.
  208. // A very simple inactivity timeout is implemented, where we
  209. // will send a space character and print a message to the
  210. // hid_listen debug message window.
  211. ISR(TIMER0_OVF_vect)
  212. {
  213. idle_count++;
  214. if (idle_count > 61 * 8) {
  215. idle_count = 0;
  216. print("Timer Event :)\n");
  217. //usb_keyboard_press(KEY_SPACE, 0);
  218. }
  219. }