找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2483|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

一個簡單的51單片機與鍵盤通訊的測試小程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:406093 發(fā)表于 2018-12-4 14:44 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
這里介紹一個簡單的51單片機與鍵盤通訊的測試小程序,它同樣可以用在 4x1, 4x2 或者 4x3 Keypad 上。希望對大家有用。(這個小程序是將鍵盤上的鍵碼通過PS2接口,由51單片機的串行接口輸出,進行測試。)


  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




評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表