转载自本人博客:http://blog.csdn.net/chengdong1314/article/details/50061429
这篇文章讲51芯片内部各个模块的汇编程序,这里不做实验,只贴代码:
定时器模块程序:
中断地址,中断调用:
;-----------------------------------------------
ORG 0000H ;伪指令 告诉编译器从0000H开始
LJMP MAIN ;64KB无条件跳转 跳转到main处执行
ORG 0003H ;外部中断0中断入口
LJMP exter0
ORG 000BH ;T0溢出中断入口
LJMP time_0
ORG 0013H ;外部中断1中断入口
LJMP exter1
ORG 001BH ;T1溢出中断入口
LJMP time_1
ORG 0100H ;虽然这里是伪指令 但是确实有标明地址的作用
;-----------------------------------------------
主函数 main:
MAIN:
MOV SP,#3FH ;设置栈低为5FH
MOV num,#00H
MOV wei,#07H
MOV TMOD,#11H
MOV TH0,#4CH
MOV TL0,#00H
MOV TH1,#4CH
MOV TL1,#00H
SETB ET0 ;开定时中断
CLR PT0 ;设置定时器0的中断优先级为低
SETB TR0 ;开启定时器
SETB ET1
SETB PT1
SETB TR1
SETB EA ;开总中断
MOV num1,#00H ;初始化数据
MOV num2,#00H
MOV LEDTEMP,#0FEH
LCALL led_Static_display ;数码管初始化
;********************死循环**********************
中断函数:
;================================================
;定时器0中断函数
;================================================
time_0:
PUSH DPH
PUSH DPL
PUSH PSW
PUSH ACC
MOV TH0,#4CH
MOV TL0,#00H
INC num1
MOV A,num1
CJNE A,#020, OVERtime_0
INC num
MOV A,num
CJNE A,#010H,rigthnumtime_0
MOV num,#00H
rigthnumtime_0:
LCALL led_Static_display
MOV num1,#00H
OVERtime_0:
POP ACC
POP PSW
POP DPL
POP DPH
RETI ;中断结束返回
;================================================
;定时器1中断函数
;================================================
time_1:
PUSH DPH
PUSH DPL
PUSH PSW
PUSH ACC
MOV TH1,#4CH
MOV TL1,#00H
INC num2
MOV A,num2
CJNE A,#02H, OVERtime_1
MOV A,LEDTEMP
RR A
MOV LEDTEMP,A
MOV LED_P,LEDTEMP
MOV num2,#00H
OVERtime_1:
POP ACC
POP PSW
POP DPL
POP DPH
RETI ;中断结束返回
;================================================
;数码管静态显示函数
;================================================
整个程序的代码:
;-----------------------数码管脚功能定义---------------------------
LED_wei EQU P2
LED138_0 EQU P2.0
LED138_1 EQU P2.1
LED138_2 EQU P2.2
LED_duan EQU P0
LED_P EQU P1
;-------------------- 数码要显示的位、数据---------------------------
wei EQU 30H ;数码管位选
num EQU 31H ;数码管段选
num1 EQU 32H ;定时器0进中断数
num2 EQU 33H ;定时器1进中断数
LEDTEMP EQU 34H ;小灯数据暂存
;-----------------------------------------------
ORG 0000H ;伪指令 告诉编译器从0000H开始
LJMP MAIN ;64KB无条件跳转 跳转到main处执行
ORG 0003H ;外部中断0中断入口
LJMP exter0
ORG 000BH ;T0溢出中断入口
LJMP time_0
ORG 0013H ;外部中断1中断入口
LJMP exter1
ORG 001BH ;T1溢出中断入口
LJMP time_1
ORG 0100H ;虽然这里是伪指令 但是确实有标明地址的作用
;-----------------------------------------------
MAIN:
MOV SP,#3FH ;设置栈低为5FH
MOV num,#00H
MOV wei,#07H
MOV TMOD,#11H
MOV TH0,#4CH
MOV TL0,#00H
MOV TH1,#4CH
MOV TL1,#00H
SETB ET0 ;开定时中断
CLR PT0 ;设置定时器0的中断优先级为低
SETB TR0 ;开启定时器
SETB ET1
SETB PT1
SETB TR1
SETB EA ;开总中断
MOV num1,#00H ;初始化数据
MOV num2,#00H
MOV LEDTEMP,#0FEH
LCALL led_Static_display ;数码管初始化
;********************死循环**********************
while1:
LJMP while1
;================================================
;外部中断0函数
;================================================
exter0:
RETI ;中断结束返回
;================================================
;外部中断1函数
;================================================
exter1:
RETI ;中断结束返回
;================================================
;定时器0中断函数
;================================================
time_0:
PUSH DPH
PUSH DPL
PUSH PSW
PUSH ACC
MOV TH0,#4CH
MOV TL0,#00H
INC num1
MOV A,num1
CJNE A,#020, OVERtime_0
INC num
MOV A,num
CJNE A,#010H,rigthnumtime_0
MOV num,#00H
rigthnumtime_0:
LCALL led_Static_display
MOV num1,#00H
OVERtime_0:
POP ACC
POP PSW
POP DPL
POP DPH
RETI ;中断结束返回
;================================================
;定时器1中断函数
;================================================
time_1:
PUSH DPH
PUSH DPL
PUSH PSW
PUSH ACC
MOV TH1,#4CH
MOV TL1,#00H
INC num2
MOV A,num2
CJNE A,#02H, OVERtime_1
MOV A,LEDTEMP
RR A
MOV LEDTEMP,A
MOV LED_P,LEDTEMP
MOV num2,#00H
OVERtime_1:
POP ACC
POP PSW
POP DPL
POP DPH
RETI ;中断结束返回
;================================================
;数码管静态显示函数
;================================================
led_Static_display:
ANL wei, #07H
ANL LED_wei,#0F8H
MOV A,wei
ORL LED_wei,A
MOV A,num
MOV DPTR,#numtab
MOVC A,@A+DPTR ;查表取数据
MOV LED_duan,A
RET
;================================================
;各种参数的延时子函数
;================================================
DELAY100us: ;100us
MOV R6,#06H
DEL11: MOV R7,#08H
DEL12: DJNZ R7,DEL12
DJNZ R6,DEL11
RET
DELAY500us: ;500us
MOV R6,#0CH ;288
DEL21: MOV R7,#18H
DEL22: DJNZ R7,DEL22
DJNZ R6,DEL21
RET
DELAY1ms: ;1ms
MOV R6,#18H
DEL61: MOV R7,#18H
DEL62: DJNZ R7,DEL62
DJNZ R6,DEL61
RET
DELAY40ms: ;延时40ms 500得1ms
MOV R6,#100
DEL41: MOV R7,#200
DEL42: DJNZ R7,DEL42
DJNZ R6,DEL41
RET
DELAY256ms: ;延时256ms
MOV R5,#05H
DEL51: MOV R6,#100
DEL52: MOV R7,#0FFH
DEL53: DJNZ R7,DEL53
DJNZ R6,DEL52
DJNZ R5,DEL51
RET
DELAY500ms: ;延时256ms
MOV R5,#0AH
DEL81: MOV R6,#100
DEL82: MOV R7,#0FFH
DEL83: DJNZ R7,DEL83
DJNZ R6,DEL82
DJNZ R5,DEL81
RET
DELAY1s: ;1秒
MOV R5,#16H
DEL31: MOV R6,#100
DEL32: MOV R7,#0FFH
DEL33: DJNZ R7,DEL33
DJNZ R6,DEL32
DJNZ R5,DEL31
RET
DELAY10s: ;10秒
MOV R5,#0DCH
DEL71: MOV R6,#100
DEL72: MOV R7,#0FFH
DEL73: DJNZ R7,DEL73
DJNZ R6,DEL72
DJNZ R5,DEL71
RET
numtab: DB 03FH,006H,05BH,04FH,066H,06DH,07DH,007H,07FH,06FH,077H,07CH,039H,05EH,079H,071H,000H
end
keil工程整体源码:
keil工程整体源码:51汇编之双定时器数码管流水灯-汇编http://download.csdn.net/detail/chengdong1314/9303613
|