;2.24设计,
S2 BIT P1.2
S6 BIT P1.3
S4 BIT P1.4
S5 BIT P1.5
S3 BIT P1.6
S1 BIT P1.7
BH BIT P3.7
WDG BIT P1.1
CMP BIT P1.0
FOR BIT P3.1
BACK BIT P3.0
PWM BIT 08H
FLAG BIT 09H ;0-FACT SPEED;1-GIV SPEED
FLAG0 BIT 0AH ;0-启动定时器,1-获取给定值
FLAG1 BIT 0BH ;0-启动定时器,1-获取实际转速
OPEN_FLAG BIT 0CH
INT1_FLAG BIT 0DH
H_TIME EQU 31H
L_TIME EQU 32H
SWITCH_TEMP EQU 33H
GIVEN_H EQU 34H ;给定转速
GIVEN_L EQU 35H
SPEED_H EQU 36H ;电机实际转速
SPEED_L EQU 37H
INTT0 EQU 38H
INTT1 EQU 39H
ORG 0000H ;BEGINE OF CODE
AJMP MAIN ;JUMP TO MAIN PROGRAM
ORG 0003H ;INT0 INTERRUPT
AJMP INT0_INT
ORG 000BH ;T0 INTERRUPT
AJMP T0_INT
ORG 0013H ;INT1 INTERRUPT
AJMP INT1_INT
ORG 001BH ;T1 INTERRUPT
AJMP T1_INT
;_____________________________________________________________________
ORG 0100H
MAIN:
MOV P3,#11111111B ;INITIALIZATION
MOV P1,#11111101B ;TO CLOSE DRIVE SIGNAL
MOV SP,#60H ;SET STACK POINTER
CLR OPEN_FLAG
MOV H_TIME,#206 ;PWM=60%,此初值可以设定,保证电机正确启动。
MOV L_TIME,#56
MOV TH1,#206
MOV TL1,#56
MOV TMOD,#00100001B ;T1 DEFINE TIMER1 8 BIT AUTO RELOAD MODE
CLR FLAG
CLR FLAG0 ;AND T0 16 BIT TIMER
SETB EA ;TO PERMIT CHIEF INTERRUPT
SETB ET1
SETB TR1
SETB IT0 ;INT0 AND INT1 UP-DOWN CREATE
SETB IT1
SETB EX0 ;TO PERMIT INT0 AND INT1
SETB EX1
;_____________________________________
POWJUDGE:
CPL WDG
JB CMP,POWJUDGE ;0 POW>GIV,1 POW<GIV
JNB FOR, FORWARDRUN
JNB BACK,BACKWARDRUN
SJMP POWJUDGE
;______________________________________________________________________________________
FORWARDRUN:
MOV P3,#11111111B
JNB BH,STOP ;TO CHECK BH SIGNAL
LCALL JUDGE
MOV A,H_TIME
CJNE A,#240,FORLOOP1
SETB OPEN_FLAG
LCALL FORFULL
SJMP POWJUDGE
FORLOOP1:
CLR OPEN_FLAG
LCALL FORWARD
SJMP POWJUDGE
;______________________________________________________________
STOP:
CLR EA
SETB P1.0
LCALL DELAY
CLR P1.0
SJMP STOP
;______________________________________________________________________
BACKWARDRUN:
MOV P3,#11111111B
JNB BH,STOP ;TO CHECK BH SIGNAL
LCALL JUDGE
MOV A,H_TIME
CJNE A,#240,BACKLOOP1
SETB OPEN_FLAG
LCALL BACKFULL
SJMP POWJUDGE
BACKLOOP1:
CLR OPEN_FLAG
LCALL BACKWORD
SJMP POWJUDGE
;_____________________________________________________________
INT0_INT:
PUSH PSW
PUSH ACC
JB FLAG,END_INT0 ;FLAG0=0,获取给定转速,FLAG=1跳出中断。
JNB FLAG0,GET_GIV ;FLAG1=0,跳转至启动定时器,FLAG1=1,停止计数,获取给定
CLR TR0
MOV GIVEN_H,TH0
MOV GIVEN_L,TL0
LOOP2:
CPL FLAG
SJMP ENDINT0
GET_GIV:
MOV TH0,#0
MOV TL0,#0
SETB TR0
ENDINT0:
CPL FLAG0
END_INT0:
POP ACC
POP PSW
RETI
;____________________________________________________________________________________
INT1_INT:
PUSH PSW
PUSH ACC
JNB FLAG,END_INT1
JNB FLAG1,GET_FACT
CLR TR0
MOV SPEED_H,TH0
MOV SPEED_L,TL0
CPL FLAG
SETB INT1_FLAG
SJMP ENDINT1
GET_FACT:
MOV TH0,#0
MOV TL0,#0
SETB TR0
NOP
ENDINT1:
CPL FLAG1
END_INT1:
POP ACC
POP PSW
RETI
;___________________________________________________________________________________
T0_INT:
RETI
;_________________________________________________________________________
T1_INT:
CPL PWM ;GENERATE PWM
JB OPEN_FLAG,END_T1
JB PWM,PWM_H ;PWM=1,OUTPUT SWITCH_TEMP
MOV P1,#0FFH
MOV TH1,L_TIME
SJMP END_T1
PWM_H:
MOV P1,SWITCH_TEMP
MOV TH1,H_TIME
END_T1:
RETI
;________________________________________________________________
FORWARD:
FORWARD_101:
MOV A,P3
ANL A,#00111000B
CJNE A,#00101000B,FORWARD_100
MOV SWITCH_TEMP,#01110111B
SJMP FORWARDHALL_END
FORWARD_100:
CJNE A,#00001000B,FORWARD_110
MOV SWITCH_TEMP,#01111011B
SJMP FORWARDHALL_END
FORWARD_110:
CJNE A,#00011000B,FORWARD_010
MOV SWITCH_TEMP,#10111011B
SJMP FORWARDHALL_END
FORWARD_010:
CJNE A,#00010000B,FORWARD_011
MOV SWITCH_TEMP,#10101111B
SJMP FORWARDHALL_END
FORWARD_011:
CJNE A,#00110000B,FORWARD_001
MOV SWITCH_TEMP,#11001111B
SJMP FORWARDHALL_END
FORWARD_001:
MOV SWITCH_TEMP,#11010111B
FORWARDHALL_END:
RET
;_______________________________________________
FORFULL:
FORFULL_101:
MOV A,P3
ANL A,#00111000B
CJNE A,#00101000B,FORFULL_100
MOV P1,#01110111B
SJMP FORFULLHALL_END
FORFULL_100:
CJNE A,#00001000B,FORFULL_110
MOV P1,#01111011B
SJMP FORFULLHALL_END
FORFULL_110:
CJNE A,#00011000B,FORFULL_010
MOV P1,#10111011B
SJMP FORFULLHALL_END
FORFULL_010:
CJNE A,#00010000B,FORFULL_011
MOV P1,#10101111B
SJMP FORFULLHALL_END
FORFULL_011:
CJNE A,#00110000B,FORFULL_001
MOV P1,#11001111B
SJMP FORFULLHALL_END
FORFULL_001:
MOV P1,#11010111B
FORFULLHALL_END:
RET
;________________________________________________
BACKWORD:
BWHETHER_101:
MOV A,P3
ANL A,#00111000B
CJNE A,#00101000B,BWHETHER_100
MOV SWITCH_TEMP,#10101111B
SJMP BHALL_END
BWHETHER_100:
CJNE A,#00001000B,BWHETHER_110
MOV SWITCH_TEMP,#11001111B
SJMP BHALL_END
BWHETHER_110:
CJNE A,#00011000B,BWHETHER_010
MOV SWITCH_TEMP,#11010111B
SJMP BHALL_END
BWHETHER_010:
CJNE A,#00010000B,BWHETHER_011
MOV SWITCH_TEMP,#01110111B
SJMP BHALL_END
BWHETHER_011:
CJNE A,#00110000B,BWHETHER_001
MOV SWITCH_TEMP,#01111011B
SJMP BHALL_END
BWHETHER_001:
MOV SWITCH_TEMP,#10111011B
BHALL_END:
RET
;_______________________________________________
BACKFULL:
BACKFULL_101:
MOV A,P3
ANL A,#00111000B
CJNE A,#00101000B,BACKFULL_100
MOV P1,#10101111B
SJMP BACKFULLHALL_END
BACKFULL_100:
CJNE A,#00001000B,BACKFULL_110
MOV P1,#11001111B
SJMP BACKFULLHALL_END
BACKFULL_110:
CJNE A,#00011000B,BACKFULL_010
MOV P1,#11010111B
SJMP BACKFULLHALL_END
BACKFULL_010:
CJNE A,#00010000B,BACKFULL_011
MOV P1,#01110111B
SJMP BACKFULLHALL_END
BACKFULL_011:
CJNE A,#00110000B,BACKFULL_001
MOV P1,#01111011B
SJMP BACKFULLHALL_END
BACKFULL_001:
MOV P1,#10111011B
BACKFULLHALL_END:
RET
;________________________________________
JUDGE:
JNB INT1_FLAG,END_JUDGE
CLR INT1_FLAG
MOV A,SPEED_H
CJNE A,GIVEN_H,JUDGE0
SJMP RE_JUDGE
RE_JUDGE:
MOV A,SPEED_L ;A>GIVEN_L,C=0,A<GIVEN,C=1
CJNE A,GIVEN_L,JUDGE0
SJMP END_JUDGE
JUDGE0:
JC DOWNSPEED ;C=1,跳转
SJMP UPSPEED
DOWNSPEED:
MOV A,L_TIME
CJNE A,#240,DEC_SPEED ;此处设定占空比的最小值,可以通过实验获取
SJMP END_JUDGE
UPSPEED:
MOV A,H_TIME
CJNE A,#240,INC_SPEED ;此处设定占空比的最大值
SJMP END_JUDGE
INC_SPEED:
INC H_TIME
DEC L_TIME
SJMP END_JUDGE
DEC_SPEED:
DEC H_TIME
INC L_TIME
END_JUDGE:
RET
;__________________________________________________________________
DELAY:
MOV R7,#40
DEL1:
MOV R6,#0FFH
DEL2:
MOV R5,#80H
KONG:
NOP
DJNZ R5,$
DJNZ R6,DEL2
DJNZ R7,DEL1
RET
;_____________________________________________________________________
END