#include<reg51.h> sbit LED1=P1^0; sbit LED2=P1^1; sbit LED3=P1^2; sbit LED4=P1^3; sbit LED5=P1^4; sbit LED6=P1^5; sbit LED7=P1^6; sbit LED8=P1^7; sbit LED9=P3^0; sbit LED10=P3^1; sbit LED11=P3^2; sbit LED12=P3^3; sbit LED13=P3^4; sbit LED14=P3^5; sbit LED15=P3^6; sbit LED16=P3^7; sbit P24=P2^4; sbit P25=P2^5; sbit P26=P2^6; sbit P27=P2^7; void delay_ms ( unsigned int ); void main ( void ) { while (1) { P2=0xfe; if (P24==0){LED1=!LED1; delay_ms (500);} else if (P25==0){LED2=!LED2; delay_ms (500);} else if (P26==0){LED3=!LED3; delay_ms (500);} else if (P27==0){LED4=!LED4; delay_ms (500);} P2=0xfd; if (P24==0){LED5=!LED5; delay_ms (500);} else if (P25==0){LED6=!LED6; delay_ms (500);} else if (P26==0){LED7=!LED7; delay_ms (500);} else if (P27==0){LED8=!LED8; delay_ms (500);} P2=0xfb; if (P24==0){LED9=!LED9; delay_ms (500);} else if (P25==0){LED10=!LED10; delay_ms (500);} else if (P26==0){LED11=!LED11; delay_ms (500);} else if (P27==0){LED12=!LED12; delay_ms (500);} P2=0xf7; if (P24==0){LED13=!LED13; delay_ms (500);} else if (P25==0){LED14=LED14; delay_ms (500);} else if (P26==0){LED15=LED15; delay_ms (500);} else if (P27==0){LED16=LED16; delay_ms (500);} } } void delay_ms ( unsigned int dt ) { register unsigned char bt,ct ; for (; dt ; dt --) for ( ct =2; ct ; ct --) for ( bt =250;-- bt ;); }将这串代码改成5x4矩阵键盘的C语言代码
时间: 2023-10-18 13:04:19 浏览: 109
以下是将代码改成使用 5x4 矩阵键盘的 C 语言代码:
```c
#include <reg51.h>
sbit LED1 = P1^0;
sbit LED2 = P1^1;
sbit LED3 = P1^2;
sbit LED4 = P1^3;
sbit LED5 = P1^4;
sbit LED6 = P1^5;
sbit LED7 = P1^6;
sbit LED8 = P1^7;
sbit LED9 = P2^0; // P3 改成了 P2
sbit LED10 = P2^1;
sbit LED11 = P2^2;
sbit LED12 = P2^3;
sbit LED13 = P2^4;
sbit LED14 = P2^5;
sbit LED15 = P2^6;
sbit LED16 = P2^7;
sbit ROW1 = P3^0; // 定义行引脚
sbit ROW2 = P3^1;
sbit ROW3 = P3^2;
sbit ROW4 = P3^3;
sbit COL1 = P2^0; // 定义列引脚
sbit COL2 = P2^1;
sbit COL3 = P2^2;
sbit COL4 = P2^3;
void delay_ms(unsigned int);
void main(void) {
while (1) {
COL1 = 0; // 第一列输入为 0
if (ROW1 == 0) { LED1 = !LED1; delay_ms(500); }
else if (ROW2 == 0) { LED5 = !LED5; delay_ms(500); }
else if (ROW3 == 0) { LED9 = !LED9; delay_ms(500); }
else if (ROW4 == 0) { LED13 = !LED13; delay_ms(500); }
COL2 = 0; // 第二列输入为 0
if (ROW1 == 0) { LED2 = !LED2; delay_ms(500); }
else if (ROW2 == 0) { LED6 = !LED6; delay_ms(500); }
else if (ROW3 == 0) { LED10 = !LED10; delay_ms(500); }
else if (ROW4 == 0) { LED14 = !LED14; delay_ms(500); }
COL3 = 0; // 第三列输入为 0
if (ROW1 == 0) { LED3 = !LED3; delay_ms(500); }
else if (ROW2 == 0) { LED7 = !LED7; delay_ms(500); }
else if (ROW3 == 0) { LED11 = !LED11; delay_ms(500); }
else if (ROW4 == 0) { LED15 = !LED15; delay_ms(500); }
COL4 = 0; // 第四列输入为 0
if (ROW1 == 0) { LED4 = !LED4; delay_ms(500); }
else if (ROW2 == 0) { LED8 = !LED8; delay_ms(500); }
else if (ROW3 == 0) { LED12 = !LED12; delay_ms(500); }
else if (ROW4 == 0) { LED16 = !LED16; delay_ms(500); }
// 恢复列引脚状态
COL1 = 1;
COL2 = 1;
COL3 = 1;
COL4 = 1;
}
}
void delay_ms(unsigned int dt) {
register unsigned char bt, ct;
for (; dt; dt--)
for (ct = 2; ct; ct--)
for (bt = 250; --bt;);
}
```
阅读全文