您的当前位置:首页正文

直流电机PWM调速的单片机控制

2024-07-02 来源:伴沃教育
;************************************************************* ;CMD_ST indicate the rotate parameters.

;bit7: 1-motor1 rotate,0-motor1 stop. bit6: 1-CW,0-CCW,bit5~bit4 rotate mode.

;The same with bit3~bit0 for motor2.

;CMD_M1 indicate the degrees the motor1 rotate. ;CMD_M2 indicate the degrees the motor2 rotate.

;************************************************************* RADDR EQU 07H QADDR EQU 08H CMD_ST EQU 31H

ORG 0000H AJMP MAIN ;转主程序

ORG 000BH AJMP INT_T0

ORG 0023H AJMP INT_RT ;转接收中断

ORG 0030H

;TABLE: DB

00H,00H,0FH,07H,00H,00H,00H,00H,0F0H,00H,0FFH,0F7H,70H,00H,7FH,77H TABLE: DB

00H,00H,0FH,07H,00H,00H,00H,00H,0F0H,00H,0FFH,0F7H,70H,00H,7FH,77H

MAIN: MOV SP,#60H MOV SCON,#0D0H ;方式3,允许串口接收数据,接收为地址信号。Enable transmit and receive MOV TMOD,#21H ;Counter0 in Timmer mode MOV PCON,#80H ;Double the baud rate MOV TL1,#0FDH ;Set the baud rate to 19.2Kbps MOV TH1,#0FDH ;Load TH1 to TL1 automatically when Time is out. SETB EA ;Enable the CPU interrupt

SETB ET0 MOV P1,#0H MOV P3,#0FFH MOV CMD_ST,#0H

MOV DPTR,#TABLE

MOV R5,#04H MOV R6,#00H MOV R7,#00H SETB ES SETB TR1 SETB SM2 JMP $ INT_T0:

ADDC A,#0H INC R1 MOV A,R1 ANL A,#0FH MOV R1,A SUBB A,R5 JNC INT_T0_CONTIUNE

CLR ES

MOV A,CMD_ST ANL A,#0FH JZ Step_Over ANL A,#03H JZ Is_M2_Run

Is_M1_Run:

DJNZ R6,Is_M2_Run ANL CMD_ST,#0CH

Is_M2_Run: MOV A,CMD_ST ANL A,#0CH JZ Count_End DJNZ R7,Count_End ANL CMD_ST,#03H

Count_End: MOV A,CMD_ST ANL A,#0FH JZ Step_Over MOVC A,@A+DPTR MOV P1,A

;Enable the UART interrupt ;Laugh Timer 1

SETB ES MOV TL0,#00H MOV TH0,#0F0H RETI

Step_Over: MOV P1,#0H CLR TR0 SETB ES RETI

INT_T0_CONTIUNE: ANL P1,#88H MOV TL0,#00H MOV TH0,#0F0H RETI

INT_RT: JBC TI,RETURN CLR RI JNB SM2,INSDATA MOV A,SBUF CJNE A,#RADDR,INQUERY CLR SM2 MOV P1,#0H CLR ET0 MOV R0,#0H RETURN: RETI

INQUERY: CJNE A,#QADDR,RETURN CLR TB8 MOV SBUF,CMD_ST SETB SM2 RETI

INSDATA:

CJNE R0,#0H,Second

First:

MOV A,SBUF Is_M1_Cw:

;Check if the Send interrupt ;Clear the RI

;Check if it is Data ;Is address,identify it. ;Clear the Address identifier

CJNE A,#02H,Is_M1_CCw Is_M1:

ANL CMD_ST,#0CH ORL CMD_ST,A MOV R0,#01H RETI

Is_M1_CCw:

CJNE A,#03H,Is_M2_Cw JMP Is_M1

Is_M2_Cw:

CJNE A,#08H,Is_M2_CCw Is_M2:

ANL CMD_ST,#03H ORL CMD_ST,A MOV R0,#02H RETI

Is_M2_CCw:

CJNE A,#0CH,Is_Speed JMP Is_M2

Is_Speed:

CJNE A,#00H,RT_Finish MOV R0,#03H RETI

Second:

Is_M1_Angle:

CJNE R0,#01H,Is_M2_Angle MOV R6,SBUF CLR TB8 MOV SBUF,#RADDR SETB TR0

JMP RT_Finish

Is_M2_Angle:

CJNE R0,#02H,Is_SpeedH MOV R7,SBUF CLR TB8 MOV SBUF,#RADDR SETB TR0

JMP RT_Finish

Is_SpeedH:

CJNE R0,#03H,RT_Finish MOV A,SBUF ANL A,#0FH MOV R5,A CLR TB8 MOV SBUF,#RADDR

RT_Finish: SETB SM2 SETB ET0 RETI END

因篇幅问题不能全部显示,请点此查看更多更全内容