2.5 16个点阵按键控制8个LED

相关下载:KEIL原工程

 1 //--------------------------------------------------------------------------
2 //
3 //程序名称: 16个点阵按键控制8个LED
4 //晶振: 11.0592MHz;
5 //MCU型号: STC89C52RC;
6 //电路板型号: EBVn和MVn组合8051实验板;
7 //--------------------------------------------------------------------------
8
9
10
11
12 //--------------------------------------------------------------------------
13 //硬件连接:(格式说明:端口名表示"板名-{模片名}-器件名-[端口名]",连接表示"->")
14 //EBVn-[VCC] -> MVn-[--VCC--]
15 //EBVn-[GND] -> MVn-[//GND//]
16
17 //EBVn-[P10] -> MVn-{EX2}-[L1]
18 //EBVn-[P11] -> MVn-{EX2}-[L2]
19 //EBVn-[P12] -> MVn-{EX2}-[L3]
20 //EBVn-[P13] -> MVn-{EX2}-[L4]
21 //EBVn-[P14] -> MVn-{EX2}-[L5]
22 //EBVn-[P15] -> MVn-{EX2}-[L6]
23 //EBVn-[P16] -> MVn-{EX2}-[L7]
24 //EBVn-[P17] -> MVn-{EX2}-[L8]
25 // -> MVn-{EX2}-(VCC)
26
27 //EBVn-[P27] -> MVn-{EX12}-[C4]
28 //EBVn-[P26] -> MVn-{EX12}-[C3]
29 //EBVn-[P25] -> MVn-{EX12}-[C2]
30 //EBVn-[P24] -> MVn-{EX12}-[C1]
31 //EBVn-[P23] -> MVn-{EX12}-[R1]
32 //EBVn-[P22] -> MVn-{EX12}-[R2]
33 //EBVn-[P21] -> MVn-{EX12}-[R3]
34 //EBVn-[P20] -> MVn-{EX12}-[R4]
35 //--------------------------------------------------------------------------
36
37
38
39
40 //--------------------------------------------------------------------------
41 //实验结果:
42 //对16个按键MVn-{EX12}-SA1..SA16控制时,8个LED灯MVn-{EX2}-L1..L8亮或灭
43 //--------------------------------------------------------------------------
44
45
46
47
48 //------------------------------------------------------
49 //头文件;
50 #include <AT89X52.H>
51
52 #define KEY P2 //将P2定义别名为KEY
53 #define KEY_C1 P2_4 //将P2_4定义别名为KEY_C1
54 #define KEY_C2 P2_5
55 #define KEY_C3 P2_6
56 #define KEY_C4 P2_7
57 #define KEY_R1 P2_3
58 #define KEY_R2 P2_2
59 #define KEY_R3 P2_1
60 #define KEY_R4 P2_0
61
62
63 //------------------------------------------------------
64 //程序入口;
65 void main(void)
66 {
67 while(1)
68 {
69 P1 = 0XFF; //将LED复位为不亮
70 KEY = 0XFF; //按键KEY复位
71 KEY_R1 = 0; //扫描KEY_R1行
72 if(KEY_C1 == 0) P1 = ~0X01;//如果MVn-{EX12}-SA1按下,MVn-{EX2}-L1亮
73 if(KEY_C2 == 0) P1 = ~0X02;//如果MVn-{EX12}-SA2按下,MVn-{EX2}-L2亮
74 if(KEY_C3 == 0) P1 = ~0X04;//如果MVn-{EX12}-SA3按下,MVn-{EX2}-L3亮
75 if(KEY_C4 == 0) P1 = ~0X08;//如果MVn-{EX12}-SA4按下,MVn-{EX2}-L4亮
76 KEY = 0XFF; //完成KEY_C1扫描后,复位KEY
77 KEY_R2 = 0; //然后对KEY_C2扫描
78 if(KEY_C1 == 0) P1 = ~0X10;//如果MVn-{EX12}-SA5按下,MVn-{EX2}-L5亮
79 if(KEY_C2 == 0) P1 = ~0X20;//如果MVn-{EX12}-SA6按下,MVn-{EX2}-L6亮
80 if(KEY_C3 == 0) P1 = ~0X40;//如果MVn-{EX12}-SA7按下,MVn-{EX2}-L7亮
81 if(KEY_C4 == 0) P1 = ~0X80;//如果MVn-{EX12}-SA8按下,MVn-{EX2}-L8亮
82 KEY = 0XFF;
83 KEY_R3 = 0; //然后对KEY_C3扫描
84 if(KEY_C1 == 0) P1 = 0X01; //如果MVn-{EX12}-SA9按下,MVn-{EX2}-L1灭
85 if(KEY_C2 == 0) P1 = 0X02; //如果MVn-{EX12}-SA10按下,MVn-{EX2}-L2灭
86 if(KEY_C3 == 0) P1 = 0X04; //如果MVn-{EX12}-SA11按下,MVn-{EX2}-L3灭
87 if(KEY_C4 == 0) P1 = 0X08; //如果MVn-{EX12}-SA12按下,MVn-{EX2}-L4灭
88 KEY = 0XFF;
89 KEY_R4 = 0; //然后对KEY_C4扫描
90 if(KEY_C1 == 0) P1 = 0X10; //如果MVn-{EX12}-SA13按下,MVn-{EX2}-L5灭
91 if(KEY_C2 == 0) P1 = 0X20; //如果MVn-{EX12}-SA14按下,MVn-{EX2}-L6灭
92 if(KEY_C3 == 0) P1 = 0X40; //如果MVn-{EX12}-SA15按下,MVn-{EX2}-L7灭
93 if(KEY_C4 == 0) P1 = 0X80; //如果MVn-{EX12}-SA16按下,MVn-{EX2}-L8灭
94 }
95 }



AKEY副本

 

AKEY

posted on 2012-01-05 14:20  mculove  阅读(1719)  评论(0编辑  收藏  举报

导航