# 蓝桥杯单片机综合练习4

### 功能 ： 当按下键盘的每键一个时，数码管显示不同的值。

#include <reg52.h>//包含51头文件
#include <intrins.h>//包含移位标准库函数头文件
#define uint unsigned int
#define uchar unsigned char

sbit du = P2^6;//数码管段选
sbit we = P2^7;//数码管段选

//共阴数码管段选表
uchar  code leddata[]= {
//0		1	 2     3     4     5     6     7     8
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F,
//9     A     B	   C	 D	   E	 F		H	 L
0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71, 0x76, 0x38,
//n	   u	  -	  熄灭
0x37, 0x3E, 0x40, 0x00 };

void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);

}
uint line;
uchar row;
uchar k;
uchar num;
uint keyscan()
{
P3 = 0xf0;    // 将 1111 0000 赋值与 P3
line = 0x00;	 // 用来记录行位置
row  = 0x00;	  // 用来记录列位置
if(P3!= 0xf0)
{
delay(10);	  // 软件消抖
if(P3!= 0xf0)
{
row = P3;     // 用row记录P3列变化
P3   = 0x0f; // 将 0000 1111 赋值与 P3
line = P3;   // 用row记录P3列变化
num  = line | row;
while(P3 != 0X0f);//松手检测
switch (num)
{
case 0xee:k = 0; break;
case 0xde:k = 1; break;
case 0xbe:k = 2; break;
case 0x7e:k = 3; break;

case 0xed:k = 4; break;
case 0xdd:k = 5; break;
case 0xbd:k = 6; break;
case 0x7d:k = 7; break;

case 0xeb:k = 8; break;
case 0xdb:k = 9; break;
case 0xbb:k = 10; break;
case 0x7b:k = 11; break;

case 0xe7:k = 12; break;
case 0xd7:k = 13; break;
case 0xb7:k = 14; break;
case 0x77:k = 15; break;
}
}
}
return k;
}
void main ()
{
we = 1;
P0 = 0xfe;
we = 0;

du = 1;
while(1)
{
P0 = leddata[keyscan()];
}

}



03-27 1万+

01-19 219

01-18 3173

06-12 171

02-01 52

02-26 131

02-06 107

02-01 85

02-03 316

#### 蓝桥杯单片机综合练习5（使用定时器中断控制流水灯）

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。