本帖最后由 dengm 于 2011-8-23 10:28 编辑
软件: 用p3.2 门控, Timer0 , 上/下边缘都中断; P3.3 下边缘中断
$MOD51
c_Delay equ 10 ; 延时采样
Status data 20h
f_DIR BIT Status.2
f_ST0 BIT Status.0 ;00 01 02/03
F_ST1 BIT Status.1
A_BAK DATA 30H
CntL data 31h
CntH data 32h
;P3.3 "0" "0"
; ==>> INC Cnt Setb f_Dir
;P3.2 "0" "1"
;P3.3 "0" "0"
; ==>> DEC Cnt clr f_Dir
;P3.2 "1" "0"
ORG 0000H
NOP
AJMP START
ORG 0003H ; INT 0 P3.2 =0
MOV TL0, #(256-c_Delay)
SETB TR0
SJMP L_UP_DN
ORG 000BH ; TIMER 0 P3.2=1
CLR TR0
SJMP L_UP_DN
ORG 0013H ; Int 1 P3.3 = 0
SJMP L_UP_DN
ORG 0050H
L_UP_DN:
MOV A_BAK, A
MOV A, P3
ANL A, #1100B
JNB f_ST1, L_UP_DN_0_1
MOV C, ACC.2
MOV F_ST0, C
MOV C, ACC.3
MOV F_ST1, C
SJMP L_UP_DN_EX
L_UP_DN_0_1:
JNB f_ST0, L_UP_DN_00
Cjne A, #0000B, L_NO_DN
CLR F_DIR
CLR F_ST0 ; ==> 00
MOV A, CntL
JNZ $+4
DEC CntH
DEC CntL
SJMP L_UP_DN_EX
L_UP_DN_00:
JZ L_UP_DN_EX
CJNE A, #0100B, L_NO_UP
SETB F_DIR
SETB F_ST0 ; ==> 01
Inc CntL
MOV A, CntL
JNZ $+4
INC CntH
SJMP L_UP_DN_EX
L_NO_UP:
L_NO_DN:
SETB f_ST1
L_UP_DN_EX:
MOV A, A_BAK
RETI
START:
; Configure Timer 0
; - Mode = 2 ; - Interrupt= ENABLED
; - Clock Source = INTERNAL
; - Enable Gating Control = ENABLED
anl TMOD, #0F0H ;clear Timer 0
orl TMOD, #0Ah ;
mov TL0, #(256-c_Delay) ;value set by user
mov TH0, #0 ;value set by user
SETB ET0
SETB IT0 ;边缘
SETB IT1
SETB EX0
SETB EX1
MOV CntL, #00
MOV CntH, #00
SETB F_DIR
SETB F_ST1
ACALL L_UP_DN
SETB EA
;-------------------------------
MainLoop:
nop
nop
sjmp MainLoop
end |