bal_gennady

контроллер клавиатуры

Feb 24th, 2020
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/pgmspace.h>
  3. #include <avr/interrupt.h>
  4. #include <util/delay.h>
  5. #include "usb_keyboard.h"
  6.  
  7. #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
  8.  
  9. uint8_t matrix[8][8]= {
  10.     /*B0*/  {KEY_TILDE, KEY_Q, 0, 0, KEY_W, KEY_E, KEY_R, KEY_T}, // Top row
  11.     /*B1*/  {KEY_SLASH, KEY_A, 0, 0, KEY_S, KEY_D, KEY_F, KEY_G}, // Home row
  12.     /*B2*/  {KEY_LEFT_BRACE, KEY_Z, 0, 0, KEY_X, KEY_C, KEY_V, KEY_B}, // Bottom row
  13.     /*B3*/  {0 /*Fn*/, KEY_ENTER, 0, 0,  KEY_TAB, KEY_ESC, KEY_SPACE},
  14.  
  15.     /*B6*/  {KEY_Y, KEY_U, 0, 0, KEY_I, KEY_O, KEY_P, KEY_EQUAL}, // Top row
  16.     /*B5*/  {KEY_H, KEY_J, 0, 0, KEY_K, KEY_L, KEY_SEMICOLON, KEY_QUOTE}, // Home row
  17.     /*B4*/  {KEY_N, KEY_M, 0, 0, KEY_COMMA, KEY_PERIOD, KEY_SLASH, KEY_RIGHT_BRACE}, // Bottom row
  18.     /*D7*/  {KEY_SPACE, KEY_ESC, 0, 0,  KEY_TAB, KEY_ENTER, 0 /*Fn*/}
  19. };
  20.  
  21. int main(void) {
  22.   uint8_t state=0, prev_state[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  23.  
  24.   setup_pins_and_usb();
  25.  
  26.   while (1) {
  27.     // B0
  28.     PORTB = 0b11111110;
  29.     _delay_ms(5);
  30.     state = PINF;
  31.     read_row(state, prev_state[0], matrix[0]);
  32.     prev_state[0] = state;
  33.     // B1
  34.     PORTB = 0b11111101;
  35.     _delay_ms(5);
  36.     state = PINF;
  37.     read_row(state, prev_state[1], matrix[1]);
  38.     prev_state[1] = state;
  39.     // B2
  40.     PORTB = 0b11111011;
  41.     _delay_ms(5);
  42.     state = PINF;
  43.     read_row(state, prev_state[2], matrix[2]);
  44.     prev_state[2] = state;
  45.     // B3
  46.     PORTB = 0b11110111;
  47.     _delay_ms(5);
  48.     state = PINF;
  49.     read_row(state, prev_state[3], matrix[3]);
  50.     prev_state[3] = state;
  51.  
  52.     // B6
  53.     PORTB = 0b10111111;
  54.     _delay_ms(5);
  55.     state = PINF;
  56.     read_row(state, prev_state[4], matrix[4]);
  57.     prev_state[4] = state;
  58.     // B5
  59.     PORTB = 0b11011111;
  60.     _delay_ms(5);
  61.     state = PINF;
  62.     read_row(state, prev_state[5], matrix[5]);
  63.     prev_state[5] = state;
  64.     // B4
  65.     PORTB = 0b11101111;
  66.     _delay_ms(5);
  67.     state = PINF;
  68.     read_row(state, prev_state[6], matrix[6]);
  69.     prev_state[6] = state;
  70.     PORTB = 0b11111111;
  71.     // D7
  72.     PORTD = 0b00000000;
  73.     _delay_ms(5);
  74.     state = PINF;
  75.     read_row(state, prev_state[7], matrix[7]);
  76.     prev_state[7] = state;
  77.     PORTD = 0b10000000;
  78.   }
  79. }
  80.  
  81. void read_row(uint8_t state, uint8_t prev_state, uint8_t keys[]) {
  82.   uint8_t mask, i;
  83.  
  84.   mask = 1;
  85.   for (i=0; i<8; i++) {
  86.     if(((state & mask) >> i) == 0 && ((prev_state & mask) >> i) == 1 ) {
  87.       usb_keyboard_press(keys[i], 0);
  88.     }
  89.     mask = mask << 1;
  90.   }
  91. }
  92.  
  93. void setup_pins_and_usb() {
  94.   // set for 16 MHz clock
  95.   CPU_PRESCALE(0);
  96.  
  97.   // Configure PB0-PB6, PD7 as outputs, high state
  98.   DDRB = 0b01111111;
  99.   DDRD = 0b10000000;
  100.   PORTB = 0b01111111;
  101.   PORTD = 0b10000000;
  102.  
  103.   // Configure PF0, PF1, PF4-PF7 as inputs, pullup enabled
  104.   DDRF = 0x00;
  105.   PORTF = 0xFF;
  106.  
  107.   // Initialize the USB, and then wait for the host to set configuration.
  108.   // If the Teensy is powered without a PC connected to the USB port,
  109.   // this will wait forever.
  110.   usb_init();
  111.   while (!usb_configured()) /* wait */ ;
  112.  
  113.   // Wait an extra second for the PC's operating system to load drivers
  114.   // and do whatever it does to actually be ready for input
  115.   _delay_ms(1000);
  116. }
Add Comment
Please, Sign In to add comment