標(biāo)題: 一個簡單的51單片機(jī)與鍵盤通訊的測試小程序 [打印本頁]

作者: oldspring    時間: 2018-12-4 14:44
標(biāo)題: 一個簡單的51單片機(jī)與鍵盤通訊的測試小程序
這里介紹一個簡單的51單片機(jī)與鍵盤通訊的測試小程序,它同樣可以用在 4x1, 4x2 或者 4x3 Keypad 上。希望對大家有用。(這個小程序是將鍵盤上的鍵碼通過PS2接口,由51單片機(jī)的串行接口輸出,進(jìn)行測試。)


  1. unsigned short keydata = 0, special = 0, down = 0;

  2. // PS2 module connections
  3. sbit PS2_DATA  at P0_0_bit;
  4. sbit PS2_CLOCK at P0_1_bit;
  5. // End PS2 module connections

  6. void main() {

  7.   UART1_Init(4800);         // Initialize UART module at 4800 bps
  8.   Ps2_Config();             // Initialize PS/2 Keyboard
  9.   Delay_ms(100);            // Wait for keyboard to finish
  10.   
  11.   UART1_Write_Text("Ready");
  12.   UART1_Write(10);          // Line Feed
  13.   UART1_Write(13);          // Carriage return

  14.   do {
  15.     if (Ps2_Key_Read(&keydata, &special, &down)) {
  16.       if (down && (keydata == 16)) {// Backspace
  17.          UART1_Write(0x08);
  18.       }
  19.       else if (down && (keydata == 13)) {// Enter
  20.         UART1_Write('r');               // send carriage return to usart terminal
  21.         //Usart_Write('n');             // uncomment this line if usart terminal also expects line feed
  22.                                          // for new line transition
  23.       }
  24.       else if (down && !special && keydata) {
  25.         UART1_Write(keydata);
  26.       }
  27.     }
  28.     Delay_ms(10);            // debounce
  29.   } while (1);
  30. }
復(fù)制代碼
相關(guān)信息:http://www.torrancerestoration.com/bbs/dpj-136722-1.html









歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1