;以89c51单片机为例,假定用P1控制LED:
IO_LED EQU P1;
led_0 BIT P1.0;
led_1 BIT P1.1;
led_2 BIT P1.2;
led_3 BIT P1.3;
led_4 BIT P1.4;
led_5 BIT P1.5;
led_6 BIT P1.6;
led_7 BIT P1.7;
;定义开关LED的数据,假定LED正极一端接电源,另一端通过限流电阻接I/O端口:
DATA_CloseLED EQU 11111111B;
DATA_OpenLED EQU 00000000B;
ORG 0000H
AJMP MAIN
ORG 0030H
;-----------------------
;延时0.5秒程序(注:此延时函数摘自STC-ISP)
DELAY500MS: ;12.000MHz
PUSH 30H
PUSH 31H
PUSH 32H
MOV 30H,#4
MOV 31H,#205
MOV 32H,#180
NEXT:
DJNZ 32H,NEXT
DJNZ 31H,NEXT
DJNZ 30H,NEXT
POP 32H
POP 31H
POP 30H
RET
;-----------------------
;下面是主程序
MAIN:
MOV IO_LED,#DATA_CloseLED;同时关闭8个LED
LCALL DELAY500MS;延时0.5秒
MOV IO_LED,#DATA_OpenLED;同时打开8个LED
LCALL DELAY500MS;延时0.5秒
LJMP MAIN;循环
END;
上面程序演示了8个LED间隔0.5秒同时亮和灭的变化。