51单片机汇编语言定时器的使用(中断方式)
(1)COUNT_INT EQU 30HLEDVALUE EQU 31H ORG 0000H LJMP START ORG 001BH LJMP Timer1InterruptSTART: MOV SP,#60H MOV COUNT_INT,#0 MOV LEDVALUE,#01H LCALL InitTimer1LOOP: MOV A,LEDVALUE MOV P2,A LJMP LOOPInitTimer1: MOV TMOD,#10H;定时器T1为工作方式1,产生50ms定时 MOV TH1,#3CH MOV TL1,#0B0H SETB EA SETB ET1 SETB TR1 RETTimer1Interrupt: PUSH DPH PUSH DPL PUSH ACC MOV TH1,#3CH MOV TL1,#0B0H ;======================== INC COUNT_INT MOV A,COUNT_INT;每50ms加1, CJNE A,#2,Timer1Interrupt_EXIT;100ms MOV COUNT_INT,#0 MOV A,LEDVALUE RL A MOV LEDVALUE,A ;========================Timer1Interrupt_EXIT: POP ACC POP DPL POP DPH RETIEND(2)COUNT_INT_L EQU 30HCOUNT_INT_H EQU 31HLEDVALUE EQU 32H ORG 0000H LJMP START ORG 001BH LJMP Timer1InterruptSTART: MOV SP,#60H MOV COUNT_INT_L,#0 MOV COUNT_INT_H,#0 MOV LEDVALUE,#01H LCALL InitTimer1LOOP: MOV A,LEDVALUE MOV P2,A LJMP LOOPInitTimer1: MOV TMOD,#20H;定时器T1为工作方式2,产生250us定时 MOV TH1,#00H MOV TL1,#06H SETB EA SETB ET1 SETB TR1 RETTimer1Interrupt: PUSH DPH PUSH DPL PUSH ACC MOV TH1,#00H MOV TL1,#06H ;======================== INC COUNT_INT_L MOV A,COUNT_INT_L;每250us加1, CJNE A,#40,Timer1Interrupt_EXIT;250US*40=10ms MOV COUNT_INT_L,#0 INC COUNT_INT_H MOV A,COUNT_INT_H;每10Ms加1, CJNE A,#10,Timer1Interrupt_EXIT;10ms*10=100MS MOV COUNT_INT_H,#0 MOV A,LEDVALUE RL A MOV LEDVALUE,A ;========================Timer1Interrupt_EXIT: POP ACC POP DPL POP DPH RETIEND
51单片机,用定时器中断,在P1口实现1S移动一次的流水灯,用汇编语言编写的程序
//晶振12M
DSEG AT 30H
timetemp: DS 1
LED: DS 1
CSEG AT 00H
ORG 0000H
LJMP START
ORG 000BH
LJMP Timer0Interrupt
START:
MOV SP,#60H
MOV timetemp,#20
MOV P1,#0FFH;LED低电平点亮
LCALL InitTimer0
MOV LED,#0FEH
LOOP:
MOV P1,LED ;add your code here!
LJMP LOOP
InitTimer0:
MOV TMOD,#01H
MOV TH0,#HIGH(65536-50000)
MOV TL0,#LOW(65536-50000);延时50ms
SETB EA
SETB ET0
SETB TR0
RET
Timer0Interrupt:
PUSH ACC
PUSH PSW
CLR TR1
MOV A,TL1
ADD A,#LOW(65536-50000)+7
MOV TL1,A
CLR A
ADDC A,#HIGH(65536-50000)
SETB TR1
DJNZ timetemp,NEXTT1
MOV timetemp,#20
;========================
MOV A,LED
RL A
MOV LED,A;add your code here!
;========================
POP PSW
POP ACC
RETI