這里介紹一個簡單的51單片機與鍵盤通訊的測試小程序,它同樣可以用在 4x1, 4x2 或者 4x3 Keypad 上。希望對大家有用。(這個小程序是將鍵盤上的鍵碼通過PS2接口,由51單片機的串行接口輸出,進行測試。)
- unsigned short keydata = 0, special = 0, down = 0;
-
- // PS2 module connections
- sbit PS2_DATA at P0_0_bit;
- sbit PS2_CLOCK at P0_1_bit;
- // End PS2 module connections
- void main() {
- UART1_Init(4800); // Initialize UART module at 4800 bps
- Ps2_Config(); // Initialize PS/2 Keyboard
- Delay_ms(100); // Wait for keyboard to finish
-
- UART1_Write_Text("Ready");
- UART1_Write(10); // Line Feed
- UART1_Write(13); // Carriage return
- do {
- if (Ps2_Key_Read(&keydata, &special, &down)) {
- if (down && (keydata == 16)) {// Backspace
- UART1_Write(0x08);
- }
- else if (down && (keydata == 13)) {// Enter
- UART1_Write('r'); // send carriage return to usart terminal
- //Usart_Write('n'); // uncomment this line if usart terminal also expects line feed
- // for new line transition
- }
- else if (down && !special && keydata) {
- UART1_Write(keydata);
- }
- }
- Delay_ms(10); // debounce
- } while (1);
- }
復制代碼 相關信息:http://www.zg4o1577.cn/bbs/dpj-136722-1.html
PS2.jpg (38.71 KB, 下載次數: 60)
下載附件
2018-12-4 14:38 上傳
|