#include <reg52.h> #include <intrins.h> #define uint unsigned int #define uchar unsigned char sbit K1 = P3^4; sbit K2 = P3^5; uchar L_leftmove[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0xff}; void delay_1ms(uchar x) { uchar j; while(x--){ for(j=0;j<125;j++) {;} } } uchar i=0,stm=1; void ROL_LED() { if(i>7){i=0;} P1=L_leftmove[7-i]; i++; } void ROL1_LED() { for(i=0;i<8;i++) { P1=L_leftmove[7-i]; delay_1ms(500); } } void KEY_Ctrl() { if(K1==0) { ROL_LED(); } if(K2==0&&stm==1) { stm=0; ROL1_LED(); } if(K2==0&&stm==0) { stm=1; P1=L_leftmove[8]; } } void main() { P3|=0xF0; while(1) { KEY_Ctrl(); delay_1ms(200); } }将其转化为汇编语言
时间: 2024-03-17 19:47:26 浏览: 150
51与DS1302程序
5星 · 资源好评率100%
由于不同的编译器生成的汇编代码可能会有所不同,下面给出的是基于 Keil C51 编译器的汇编代码示例:
```
; Include register definitions for 8051 microcontroller
#include <reg52.inc>
; Define constants
#define uint unsigned int
#define uchar unsigned char
; Define IO ports
sbit K1 = P3^4;
sbit K2 = P3^5;
; Define LED pattern
uchar L_leftmove[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0xff};
; Define delay function
delay_1ms:
mov R7, #2
D1:
mov R6, #244
D2:
djnz R6, D2
djnz R7, D1
ret
; Define variables
i: ds 1
stm: ds 1
; Define ROL_LED function
ROL_LED:
cjne i, #8, ROL_LED1
mov i, #0
ROL_LED1:
mov A, i
movc A, @A+DPTR
mov P1, A
inc i
ret
; Define ROL1_LED function
ROL1_LED:
mov i, #0
ROL1_LED1:
mov A, i
movc A, @A+DPTR
mov P1, A
acall delay_1ms
inc i
cjne i, #8, ROL1_LED1
ret
; Define KEY_Ctrl function
KEY_Ctrl:
jb K1, KEY_Ctrl1
jb K2, KEY_Ctrl2
ret
KEY_Ctrl1:
acall ROL_LED
ret
KEY_Ctrl2:
jb stm, KEY_Ctrl21
acall ROL1_LED
mov stm, #0
ret
KEY_Ctrl21:
mov P1, L_leftmove+8
mov stm, #1
ret
; Define main function
main:
mov P3, #0xF0
mov DPTR, #L_leftmove
mov i, #0
mov stm, #1
MAIN:
acall KEY_Ctrl
acall delay_1ms
sjmp MAIN
```
注意,这里使用了 Keil C51 编译器的内置函数来实现延时功能,因此需要使用 `#include <reg52.inc>` 来包含 8051 微控制器的寄存器定义。另外,Keil C51 编译器还会将程序的启动代码嵌入到生成的汇编代码中,因此可以直接在程序中使用 `main` 函数作为程序入口。
阅读全文