PID with 8051 assembler language

S

Thread Starter

Simon Herard

I would like to build a generic PID control loop in a 8051 microcontroller using 8051 assembling language to control an infrared sensor. Does anybody can help me regarding the control algorithms in assembling language ?

Thanks for all the help you can provide me.
Simon
 
<PRE>
;PV-->R2R3,SV-->R4R5
LSPID: LCALL NSUB ;E=PV-SV--->R2R3
MOV DPL,#ADDDR
MOVX A,@DPTR
JZ PEABS ;00(D) FF(E)
LCALL NEG1
PEABS: MOV A,R3 ;E ABS--->R6R7
MOV R7,A
MOV A,R2
MOV R6,A
JNB ACC.7,PEA
LCALL NEG2 ;ABS(EI)-->R6R7
PEA: MOV DPL,#ADDEH ;E ALARM
MOVX A,@DPTR
CLR C
SUBB A,R7
INC DPL
MOVX A,@DPTR
SUBB A,R6
JNC ENOA
SETB EALR
SJMP PENORM
ENOA: CLR EALR
PENORM: MOV R0,#ADDICM
MOVX A,@R0
MOV R5,A
INC R0
MOVX A,@R0
MOV R4,A
LCALL NADD
PESAVE: MOV R0,#ADDEI
MOV A,R3
MOVX @R0,A
INC R0
MOV A,R2
MOVX @R0,A ;EI-->R2R3
SPID1: MOV DPL,#ADDISEL ;I SEPARATE DPH=PID1
MOVX A,@DPTR
CLR C
SUBB A,R7 ;ABS(EI)-->R6R7
INC DPL
MOVX A,@DPTR
SUBB A,R6
JNC SPI
MOV R2,#00H
MOV R3,#00H
LJMP PID6
SPI: MOV DPL,#ADDTi ;Ei--->R2R3
MOVX A,@DPTR
MOV R5,A
INC DPL
MOVX A,@DPTR
MOV R4,A
LCALL DIV ;Ei/Ti-->R6R7
MOV R2,#2H ;TS=1
MOV R3,#00H
LCALL MUL ;Ts*512*Ei/Ti-->R2R3
MOV DPL,#ADDKp
MOVX A,@DPTR
MOV R5,A
INC DPL
MOVX A,@DPTR
MOV R4,A
LCALL DIV ;KP*TS*EI/TI--->R6R7
MOV R0,#ADDERR
MOVX A,@R0
ADD A,R7
MOVX @R0,A
INC R0
MOVX A,@R0
ADDC A,R6
MOV R6,A
ANL A,#81H
JNB ACC.7,PID0
ORL A,#0FEH
PID0: MOVX @R0,A ;BACK 9 BIT -->Err
MOV A,R6 ;FRONT 7 BIT -->R2R3(KP*Ei*Ts/Ti)
MOV C,ACC.7
RRC A
MOV R2,#00H
JB ACC.7,PID5
MOV R3,A
SJMP PID6
PID5: ADD A,#1
MOV R3,A
JZ PID6
MOV R2,#0FFH ;I-->R2R3
PID6: MOV PIBUFH,R2
MOV PIBUFL,R3
CLR C
MOV R0,#ADDEI1
MOVX A,@R0
MOV R5,A
INC R0
MOVX A,@R0
MOV R4,A
MOV R0,#ADDEI
MOVX A,@R0
SUBB A,R5
MOV R3,A
MOV TEMPBUFL,A
INC R0
MOVX A,@R0
SUBB A,R4
MOV R2,A ;DV-->R2R3
MOV TEMPBUFH,A ;DV-->TEMPBUF
MOV DPL,#ADDKp
MOVX A,@DPTR
MOV R5,A
INC DPL
MOVX A,@DPTR
MOV R4,A
LCALL DIV ;Kp(Ei-Ei-1)--->R6R7
MOV A,PIBUFL ;KP*(Ei-Ei-1)+KP*Ts*Ei/Ti-->R2R3
ADD A,R7
MOV PIBUFL,A
MOV A,PIBUFH
ADDC A,R6
MOV PIBUFH,A
MOV R3,PIBUFL
MOV R2,PIBUFH
MOV DPL,#ADDTD
MOVX A,@DPTR
MOV R5,A
INC DPL
MOVX A,@DPTR
ORL A,R5
JZ PIDU0
MOV DPL,#ADDFPV
MOVX A,@DPTR
JZ PIDP
MOV R3,TEMPBUFL ;DEI--> R2R3
MOV R2,TEMPBUFH
SJMP PID7
PIDU0: LJMP PIDU
PIDP: CLR C ;DPV--->R2R3
MOV R0,#ADDPV1
MOVX A,@R0
MOV R3,A
INC R0
MOVX A,@R0
MOV R2,A
MOV R0,#ADDPV
MOVX A,@R0
SUBB A,R3
MOV R3,A
INC R0
MOVX A,@R0
SUBB A,R2
MOV R2,A
MOV DPL,#ADDDR
MOVX A,@DPTR
JZ PID7
LCALL NEG1
PID7: MOV DPL,#ADDKp ;Kp-->R4R5
MOVX A,@DPTR
MOV R5,A
INC DPL
MOVX A,@DPTR
MOV R4,A
PIDD: MOV R6,#4
PIDD1: CLR C
MOV A,R4
RRC A
MOV R4,A
MOV A,R5
RRC A
MOV R5,A
DJNZ R6,PIDD1
LCALL DIV ;Kp*^Ei-->R6R7
PIDD2: MOV R0,#ADDUD1 ;Udi-1
MOVX A,@R0
ADD A,R7
MOV R3,A
INC R0
MOVX A,@R0
ADDC A,R6
MOV R2,A ;Udi-1+Kp*Kd*^Ei-->R2R3
JNB OV,PID71
JC PID72
MOV R2,#7FH
MOV R3,#0FFH
SJMP PID71
PID72: MOV R2,#80H
MOV R3,#00H
PID71: MOV DPL,#ADDTd
MOVX A,@DPTR
MOV R5,A
INC DPL
MOVX A,@DPTR
MOV R4,A
MOV A,#10H
ADD A,R5
MOV R5,A
MOV A,#00H
ADDC A,R4
MOV R4,A
LCALL DIV ;(Udi-1+Kp*Kd*^Ei)/(TD+KD)--->r6r7
MOV DPL,#ADDTd
MOVX A,@DPTR
MOV R3,A
INC DPL
MOVX A,@DPTR
MOV R2,A
LCALL MUL ;UDI=Td*(Udi-1+Kp*Kd*^Ei)/(TD+KD)--->r2r3
CLR C
MOV R0,#ADDUD1
MOVX A,@R0
MOV R5,A
MOV A,R3
MOVX @R0,A
INC R0
MOVX A,@R0
MOV R4,A
MOV A,R2
MOVX @R0,A
LCALL NSUB ;^UDI-->R2R3
MOV R5,PIBUFL ;^PI-->R4R5
MOV R4,PIBUFH
LCALL NADD ;^U=^UPI+^UD-->R2R3
PIDU: MOV R0,#ADDU1
MOVX A,@R0
ADD A,R3
MOV R3,A
INC R0
MOVX A,@R0
ADDC A,R2
MOV R2,A
JNB ACC.7,PID20
MOV R3,#00H ;LOWER U
MOV R2,#00H
SJMP PID3
PID20: ANL A,#0F0H
JZ PID3
PID4: MOV R2,#0FH ;HEIGH U
MOV R3,#0FFH
PID3: MOV R0,#ADDOCM
MOVX A,@R0
MOV R5,A
INC R0
MOVX A,@R0
MOV R4,A
LCALL NADD
PUL: MOV A,R2 ;LIMIT U-->0-1
JNB ACC.7,PUL1
MOV R3,#00H
MOV R2,#00H
SJMP PID9
PUL1: ANL A,#0F0H
JZ PID9
MOV R2,#0FH
MOV R3,#0FFH
PID9: LCALL PMV
MOV R0,#ADDU1 ;SAVE U1
MOV A,R3
MOVX @R0,A
INC R0
MOV A,R2
MOVX @R0,A ;U1-->R2R3
MOV R0,#ADDEI
LCALL BFLOW
MOV R0,#ADDPV
LCALL BFLOW
RET
BFLOW: MOVX A,@R0
MOV R5,A
INC R0
MOVX A,@R0
MOV R4,A
INC R0
MOV A,R5
MOVX @R0,A
INC R0
MOV A,R4
MOVX @R0,A
RET
;-------------------------------
</PRE>
 
O

Orsini Livio S.

Hello Simon ,
I can send You a program that performs one PID algorithm. This program is written in assembler language (Intel) for 8052. The comments are written in ITALIAN language. I can send it
without any other comments or documentation in english language. If You agree let me know and I'll send it to You.
Livio
(lsorsini @tin.it)
 
Top