调用矩阵键盘驱动,通过矩阵键盘设置0~99的任意整数并在控制台显示
时间: 2023-04-03 15:00:52 浏览: 79
您好,可以使用以下代码调用矩阵键盘驱动,通过矩阵键盘设置~99的任意整数并在控制台显示:
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit ROW1 = P1^;
sbit ROW2 = P1^1;
sbit ROW3 = P1^2;
sbit ROW4 = P1^3;
sbit COL1 = P1^4;
sbit COL2 = P1^5;
sbit COL3 = P1^6;
sbit COL4 = P1^7;
uchar code table[] = {x3f, x06, x5b, x4f, x66, x6d, x7d, x07, x7f, x6f};
void delay(uint xms)
{
uint i, j;
for(i=xms; i>; i--)
for(j=110; j>; j--);
}
uchar KeyScan()
{
uchar keyvalue = xff;
ROW1 = ;
if(COL1 == ) keyvalue = 1;
if(COL2 == ) keyvalue = 2;
if(COL3 == ) keyvalue = 3;
if(COL4 == ) keyvalue = 10;
ROW1 = 1;
ROW2 = ;
if(COL1 == ) keyvalue = 4;
if(COL2 == ) keyvalue = 5;
if(COL3 == ) keyvalue = 6;
if(COL4 == ) keyvalue = 11;
ROW2 = 1;
ROW3 = ;
if(COL1 == ) keyvalue = 7;
if(COL2 == ) keyvalue = 8;
if(COL3 == ) keyvalue = 9;
if(COL4 == ) keyvalue = 12;
ROW3 = 1;
ROW4 = ;
if(COL1 == ) keyvalue = 14;
if(COL2 == ) keyvalue = ;
if(COL3 == ) keyvalue = 15;
if(COL4 == ) keyvalue = 13;
ROW4 = 1;
return keyvalue;
}
void main()
{
uchar keyvalue, ten, unit;
uint num;
while(1)
{
keyvalue = KeyScan();
if(keyvalue != xff)
{
if(keyvalue == 10) // 确认键
{
num = ten*10 + unit;
P2 = table[ten];
delay(500);
P2 = table[unit];
delay(500);
P2 = x00;
}
else if(keyvalue == 11) // 清零键
{
ten = ;
unit = ;
P2 = x00;
}
else if(keyvalue == 12) // 退格键
{
unit = ;
P2 = table[ten];
delay(500);
P2 = table[unit];
delay(500);
P2 = x00;
}
else if(keyvalue == 13) // 取消键
{
P2 = x00;
}
else if(keyvalue == 14) // 十位键
{
ten = ;
P2 = table[ten];
delay(500);
P2 = x00;
}
else if(keyvalue == 15) // 个位键
{
unit = ;
P2 = table[unit];
delay(500);
P2 = x00;
}
else // 数字键
{
if(ten == )
{
ten = keyvalue;
P2 = table[ten];
delay(500);
P2 = x00;
}
else if(unit == )
{
unit = keyvalue;
P2 = table[unit];
delay(500);
P2 = x00;
}
}
}
}
}
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)