用回汇编语言编程8个按键控制4个LED灯
发布网友
发布时间:2022-05-02 23:37
我来回答
共1个回答
热心网友
时间:2022-06-28 10:17
没有电路图,没法写程序。
比如,按键在P0口与在P3口程序就不一样;
又比如,扫描的键盘与直接连接在Pn口的又不一样。
没有图实在是没法写程序呃
*****************************************************************
假定CPU是用51系列的单片机;
假定键盘直接连接在Pn口,从Pn0~Pn7分别对应键1~键7;
假设LED直接由Pm口驱动,从Pm0~Pm3分别对应Led1~Led4;
在这个前提下,以上的问题可以编程如下:
*****************************************************************
#define KEY1 01h ;定义键1
#define KEY2 02h ;定义键2
#define KEY3 04h ;定义键3
#define KEY4 08h ;定义键4
#define KEY5 10h ;定义键5
#define KEY6 20h ;定义键6
#define KEY7 40h ;定义键7
#define KEY8 80h ;定义键8
#define LED1 Pm0 ;定义灯1
#define LED2 Pm1 ;定义灯2
#define LED3 Pm2 ;定义灯3
#define LED4 Pm3 ;定义灯4
#define LED_ON 1 ;定义亮灯操作
#define LED_OFF 0 ;定义灭灯操作
;延迟4秒子程序
#define Timer4s 1234h ;延迟4秒的参数自己调整
Dly4s: push acc;
mov dptr, #Timer4s;
Dly_2: mov a,dpl;
Dly_1: mov dpl,a;
dec a;
jnz a,Dly_1;
mov dpl,a;
mov a,dph;
dec a;
mov dph,a;
jnz a,Dly_2;
pop acc;
ret;
;******* 主程序 ****************************
main: mov a, Pn;
je a,#KEY1, K1_Pressed;
je a,#KEY2, K2_Pressed;
je a,#KEY3, K3_Pressed;
je a,#KEY4, K4_Pressed;
je a,#KEY5, K5_Pressed;
je a,#KEY6, K6_Pressed;
je a,#KEY7, K7_Pressed;
je a,#KEY8, K8_Pressed;
ljmp main;
K1_Pressed:
mov LED1, LED_ON;
mov LED2, LED_ON;
call Dly4s;
mov LED1, LED_OFF;
mov LED2, LED_OFF;
ljmp main;
K2_Pressed:
mov LED3, LED_ON;
mov LED4, LED_ON;
call Dly4s;
mov LED3, LED_OFF;
mov LED4, LED_OFF;
ljmp main;
K3_Pressed:
mov LED1, LED_ON;
call Dly4s;
mov LED1, LED_OFF;
mov LED2, LED_ON;
call Dly4s;
mov LED2, LED_OFF;
ljmp main;
K4_Pressed:
mov LED3, LED_ON;
call Dly4s;
mov LED3, LED_OFF;
mov LED4, LED_ON;
call Dly4s;
mov LED4, LED_OFF;
ljmp main;
K5_Pressed:
mov LED1, LED_ON;
mov LED2, LED_ON;
call Dly4s;
mov LED1, LED_OFF;
mov LED2, LED_OFF;
ljmp main;
K6_Pressed:
mov LED3, LED_ON;
mov LED4, LED_ON;
call Dly4s;
mov LED3, LED_OFF;
mov LED4, LED_OFF;
ljmp main;
K7_Pressed:
mov LED1, LED_ON;
call Dly4s;
mov LED1, LED_OFF;
mov LED2, LED_ON;
call Dly4s;
mov LED2, LED_OFF;
ljmp main;
K8_Pressed:
mov LED3, LED_ON;
call Dly4s;
mov LED3, LED_OFF;
mov LED4, LED_ON;
call Dly4s;
mov LED4, LED_OFF;
ljmp main;