一个例子,请仔细阅读
;Assume: CpuClck EQU 12000 ;12000Khz=12Mhz TmrClck EQU CpuClck/12;1M=1000K ReferF EQU 1000;HZ=1khz ReferT EQU 1000;us=1ms WatchedF EQU 40;KHZ=0x28
CurrTH1 EQU R0 CurrTL1 EQU R1 LastTH1 EQU R2 LastTL1 EQU R3 DeltTH1 EQU R4 DeltTL1 EQU R5 Cmppare EQU R6
ORG 0000H LJMP STAR ;;;;;;;;;;;;;;;;;;;;;;;; ORG 000BH ;T0 Int PUSH PSW PUSH ACC;save waht we used CLR TR0 ACALL ReReadT1 ;T1 in R0,R1;Delta=Current-Last CLR C MOV A,CurrTL1 SUBB A,LastTL1 MOV DeltTL1,A MOV A,CurrTH1 SUBB A,LastTH1 MOV DeltTH1,A ;update MOV A,CurrTL1 MOV LastTL1,A MOV A,CurrTH1 MOV LastTH1,A ; CLR C MOV A,DeltTL1 SUBB A,#Low(WatchedF) JC Less;< ;>= MOV A,DeltTH1 SUBB A,#High(WatchedF) JC Less;< ;>= INC Cmppare; MOV A,Cmppare ; SUBB A,#5 JC Less;<5 ;if >5 do something ; MOV P3,#0; Less: ;Re Set T0 MOV TH0,#High(-ReferT) MOV TL0,#Low(-ReferT) SETB TR0 POP ACC;restore POP PSW RETI ;;;;;;;;;;;;;;;;;;;;;;;;;; ReReadT1:;T1 still counts MOV A,TH1 MOV CurrTL1,TL1 ;if a carry from low byte to high byte occures, ;we read it again. CJNE A,TH1,ReReadT1 MOV CurrTH1,A JNB TF1,NotTF1;TF1 ;TF1 CLR TF1 NotTF1: RET ;;;;;;;;;;;;;;;;;;;;;;;;;; ;main STAR: MOV SP,#70h MOV P3,0FFH MOV TMOD,#0101$0001B;T1=16 ¼ÇÊý,T0=16 ¶¨Ê± ; MOV TH0,#High(-ReferT) MOV TL0,#Low(-ReferT) ; MOV TH1,#0 MOV TL1,#0 MOV Cmppare,0 ; SETB EA ; SETB ET0 SETB TR0 SETB TR1 LOOP: #define SIMULATE ;comment above if run on target #ifdef SIMULATE ;T1+1 INC TL1 MOV A,TL1 JNZ goon ;high byte +1 INC TH1 goon:;add NOP to adjust the watched frequency NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP ;10us NOP NOP NOP NOP NOP NOP NOP NOP NOP;40KHZ #endif JMP LOOP END |
|