不就是通过循环让端口输出低电平,再通过查表看是哪个按下,再查找显示代码显示出来. 我把代码整理了一下:
#include<reg52.h> #include<intrins.h> #define display P1 #define keyboardCode P2
#define uint unsigned int #define uchar unsigned char
uchar code initialKeyboardCode[] = { 0xef, 0xdf, 0xbf, 0x7f }; // Set the initial keyboard code.
uchar code getKeyboardCode[] = { 0xee, 0xed, 0xeb, 0xe7, 0xde, 0xdd, 0xdb, 0xd7, // When the keyboard pressed 0xbe, 0xbd, 0xbb, 0xb7, 0x7e, 0x7d, 0x7b, 0x77 }; // down ,the mcu will get one of them.
uchar code theActionCode[] = { 0xf7, 0xf4, 0xf1, 0xf0, 0xf8, 0xf5, 0xf2, 0xf0, // The display code, you can rewrite 0xf9, 0xf6, 0xf3, 0xf0, 0xf8, 0xf5, 0xf2, 0xf0 }; // them if you want . But the code you rewrite must correspondence // with the ”getkeyboardCode”.
void KeyboardScan(void) { uchar sender = 0x00; uchar counterOne = 0x00; uchar counterTwo = 0x00;
for(counterOne=0x00; counterOne<4; counterOne++) // 轮流输出低电平 { keyboardCode = initialKeyboardCode[counterOne]; // Load the initialKeyboardCode sender = keyboardCode; // Get the keyboardCode when the keyboard press down
// perhaps a delay here. for(counterTwo=0x00; counterTwo<16; counterTwo++) { // When a geyKeyboardC ode equal to the sender , if(sender==getKeyboardCode[counterTwo]) // 查表按键 { // Then load the display code from the theActioncode display = theActionCode[counterTwo]; // 查表显示代码 // You can write you control functions here too. } } } }
// The test main function. void main(void) { display = 0x00; while(1) { KeyboardScan(); } } // You can refer to the circuit below. |