51 單片機(jī) C 語言編程:矩陣鍵盤輸入,顯示在數(shù)碼管
通過矩陣鍵盤輸入,并顯示在一位數(shù)碼管上。
#include
#define uint unsigned int
#define uchar unsigned char
sbit duanx = P2^6;
sbit weix = P2^7;
uchar key, k_tmp, view;
uchar show[] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40};
void keyscan(void)
{
key = 16;
P3 = 0xf0; k_tmp = P3 & 0xf0;
if (k_tmp == 0x70) key = 0;
else if(k_tmp == 0xb0) key = 1;
else if(k_tmp == 0xd0) key = 2;
else if(k_tmp == 0xe0) key = 3;
P3 = 0x0f; k_tmp = P3 & 0x0f;
if (k_tmp == 0x07) key = key + 12;
else if(k_tmp == 0x0b) key = key + 8;
else if(k_tmp == 0x0d) key = key + 4;
else if(k_tmp == 0x0e) key = key + 0;
view = show[key];
}
void display(uchar view)
{
P0 = view; duanx = 1; duanx = 0;
P0 = 0xfe; weix = 1; weix = 0;
}
main()
{
while(1) {
keyscan();
display(view);
}
}