谁能帮我理解这个PIC单片机程序?

发布于 2024-12-18 17:21:45 字数 3911 浏览 1 评论 0原文

我是一名工程专业的学生,​​我对这个程序有点困惑。任何人都可以帮我填写程序中每一行的剩余注释吗?我是这个编程的新手。

#INCLUDE<P16F88.INC> ; PIC definitions

SET_UPPER   EQU 0x20
SET_LOWER   EQU 0x21
READ_UPPER  EQU 0x22
READ_LOWER  EQU 0x23
P_gain      EQU 0x24
I_gain      EQU 0x25
D_gain      EQU 0x26
Prop        EQU 0x27
Int         EQU 0x28
Diff        EQU 0x29
DError      EQU 0x30
IError      EQU 0x31
temp        EQU 0x32

    ORG 0h
;****************************
;       INITIZLIZATION
;****************************

    BANKSEL TRISA           ;SELECT TRISA BANL
    MOVLW   B'00000011'     ;MAKE PIN0  AND 1 INPUT
    MOVWF   TRISA           ;Writing to TRISA register
    BANKSEL ANSEL
    MOVLW   B'00000011'     ;MAKE PA0 AND PA1 ANALOG INPUT
    MOVWF   ANSEL
    BANKSEL PORTA
    MOVLW   0x80            ;FOSC/32 AND CHANNEL0
    MOVWF   ADCON0
    MOVLW   0x00
    CLRF    ADCON1          ;LEFT JUSTIFIED ADC VALUE
    CLRF    CCP1CON         ;PWM MODULE
    CLRF    TRISB           ;MAKE PORT B O/P FOR PWM
    MOVLW   0xFF
    MOVWF   PR2
    CLRF    CCPR1L          ;0 DUTY CYCLE AT START
    MOVLW   0x01
    MOVWF   T2CON           ;PRESCALER 4 AND POSTSCALAR 1
    BSF     T2CON,TMR2ON    ;SWITCH ON TIMER
;****************************
;           MAIN
;****************************
MAIN
    CALL    SET_POINT       ;READ SET POINT
    CALL    READ_TEMP       ;READ TEMPERTAURE
    CALL    COMPARE         ;COMPARE AND FIND GAIN VALUE
    CALL    CONTROL         ;MAP GAIN TO DUTY CYCLE
    GOTO    MAIN

;****************************
;       READ SET PT.
;****************************
SET_POINT
    MOVLW   0x80
    MOVWF   ADCON0          ;CHANNEL 0 ADC
    BSF     ADCON0,ADON     ;SWITCH ON ADC
    NOP                     ;WAIT ACQUISITION TIME
    NOP
    NOP
    NOP
    NOP
    BSF     ADCON0,GO
ADC 
    BTFSC   ADCON0,GO       ;WAIT TILL CONVERSION IS COMPLETE
    GOTO    ADC
    MOVF    ADRESH,W        ;READ ADC VALUES
    MOVWF   SET_UPPER
    BANKSEL ADRESL
    MOVF    ADRESL,W
    MOVWF   SET_LOWER
    BANKSEL PORTA
    RETURN
;****************************
;      READ TEMPERATURE
;****************************
READ_TEMP
    MOVLW   0x88            ;ADC CHANNEL 1 SELECT
    MOVWF   ADCON0
    BSF     ADCON0,ADON     ;SWITCH ON ADC
    NOP                     ;WAIT ACQUSITION TIME
    NOP
    NOP
    NOP
    NOP
    BSF     ADCON0,GO       ;START CONVERSION
ADC0    
    BTFSC   ADCON0,GO       ;WAIT TILL CONVERSION IS COMPLETE
    GOTO    ADC0
    MOVF    ADRESH,W
    MOVWF   READ_UPPER      ;READ ADC VALUE
    BANKSEL ADRESL
    MOVF    ADRESL,W
    MOVWF   READ_LOWER
    BANKSEL PORTA
    RETURN
;******************************
;   COMPUTE ERROR AND GAINS
;******************************
COMPARE
    MOVF    SET_UPPER,W
    SUBWF   READ_UPPER,W    ;SUBTRACT READ VALUE FROM SET POINT
    SUBWF   DError,F        ;COMPUTE INTEGRAL ERROR
    ADDWF   IError,F        ;COMPUTE DIFFERENTIAL ERROR

    MOVWF   Prop            ;MOVE COMPUTED ERROR TO PROP
    MOVF    P_gain,W        
    MOVWF   temp
    MOVF    Prop,W          
Prop_loop
    ADDWF   Prop,F          ;ADD PRPORTIONAL ERROR PROPORTIONAL GAIN TIMES
    DECFSZ  temp,F
    GOTO    Prop_loop

    MOVF    IError,W
    MOVWF   Int
    MOVF    I_gain,W
    MOVWF   temp
    MOVF    Int,W   
Int_loop
    ADDWF   Int,F           ;ADD INTEGRAL ERROR INTEGRAL GAIN TIMES
    DECFSZ  temp,F
    GOTO    Int_loop

    MOVF    DError,W
    MOVWF   Diff
    MOVF    D_gain,W
    MOVWF   temp
    MOVF    Diff,W  
Diff_loop
    ADDWF   Diff,F          ;ADD DIFFERENTIAL ERROR DIFFERENTIAL GAIN TIMES
    DECFSZ  temp,F
    GOTO    Diff_loop
    RETURN
;***************************
;   MAP GAIN TO PWM VALUE
;***************************
CONTROL
    MOVF    Prop,W          ;ADD ALL THREE COMPUTED VALUES
    ADDWF   Int,W
    ADDWF   Diff,W
    MOVWF   CCPR1L          ;MOVE TO CCPR1L TO MAKE IT HI TIME OF PWM
    RETURN

    END

I am an engineering student and I am a little bit confused about this program. Can any one please help me to fill the remaining comments for each lines in the program? I am a newbie to this programming.

#INCLUDE<P16F88.INC> ; PIC definitions

SET_UPPER   EQU 0x20
SET_LOWER   EQU 0x21
READ_UPPER  EQU 0x22
READ_LOWER  EQU 0x23
P_gain      EQU 0x24
I_gain      EQU 0x25
D_gain      EQU 0x26
Prop        EQU 0x27
Int         EQU 0x28
Diff        EQU 0x29
DError      EQU 0x30
IError      EQU 0x31
temp        EQU 0x32

    ORG 0h
;****************************
;       INITIZLIZATION
;****************************

    BANKSEL TRISA           ;SELECT TRISA BANL
    MOVLW   B'00000011'     ;MAKE PIN0  AND 1 INPUT
    MOVWF   TRISA           ;Writing to TRISA register
    BANKSEL ANSEL
    MOVLW   B'00000011'     ;MAKE PA0 AND PA1 ANALOG INPUT
    MOVWF   ANSEL
    BANKSEL PORTA
    MOVLW   0x80            ;FOSC/32 AND CHANNEL0
    MOVWF   ADCON0
    MOVLW   0x00
    CLRF    ADCON1          ;LEFT JUSTIFIED ADC VALUE
    CLRF    CCP1CON         ;PWM MODULE
    CLRF    TRISB           ;MAKE PORT B O/P FOR PWM
    MOVLW   0xFF
    MOVWF   PR2
    CLRF    CCPR1L          ;0 DUTY CYCLE AT START
    MOVLW   0x01
    MOVWF   T2CON           ;PRESCALER 4 AND POSTSCALAR 1
    BSF     T2CON,TMR2ON    ;SWITCH ON TIMER
;****************************
;           MAIN
;****************************
MAIN
    CALL    SET_POINT       ;READ SET POINT
    CALL    READ_TEMP       ;READ TEMPERTAURE
    CALL    COMPARE         ;COMPARE AND FIND GAIN VALUE
    CALL    CONTROL         ;MAP GAIN TO DUTY CYCLE
    GOTO    MAIN

;****************************
;       READ SET PT.
;****************************
SET_POINT
    MOVLW   0x80
    MOVWF   ADCON0          ;CHANNEL 0 ADC
    BSF     ADCON0,ADON     ;SWITCH ON ADC
    NOP                     ;WAIT ACQUISITION TIME
    NOP
    NOP
    NOP
    NOP
    BSF     ADCON0,GO
ADC 
    BTFSC   ADCON0,GO       ;WAIT TILL CONVERSION IS COMPLETE
    GOTO    ADC
    MOVF    ADRESH,W        ;READ ADC VALUES
    MOVWF   SET_UPPER
    BANKSEL ADRESL
    MOVF    ADRESL,W
    MOVWF   SET_LOWER
    BANKSEL PORTA
    RETURN
;****************************
;      READ TEMPERATURE
;****************************
READ_TEMP
    MOVLW   0x88            ;ADC CHANNEL 1 SELECT
    MOVWF   ADCON0
    BSF     ADCON0,ADON     ;SWITCH ON ADC
    NOP                     ;WAIT ACQUSITION TIME
    NOP
    NOP
    NOP
    NOP
    BSF     ADCON0,GO       ;START CONVERSION
ADC0    
    BTFSC   ADCON0,GO       ;WAIT TILL CONVERSION IS COMPLETE
    GOTO    ADC0
    MOVF    ADRESH,W
    MOVWF   READ_UPPER      ;READ ADC VALUE
    BANKSEL ADRESL
    MOVF    ADRESL,W
    MOVWF   READ_LOWER
    BANKSEL PORTA
    RETURN
;******************************
;   COMPUTE ERROR AND GAINS
;******************************
COMPARE
    MOVF    SET_UPPER,W
    SUBWF   READ_UPPER,W    ;SUBTRACT READ VALUE FROM SET POINT
    SUBWF   DError,F        ;COMPUTE INTEGRAL ERROR
    ADDWF   IError,F        ;COMPUTE DIFFERENTIAL ERROR

    MOVWF   Prop            ;MOVE COMPUTED ERROR TO PROP
    MOVF    P_gain,W        
    MOVWF   temp
    MOVF    Prop,W          
Prop_loop
    ADDWF   Prop,F          ;ADD PRPORTIONAL ERROR PROPORTIONAL GAIN TIMES
    DECFSZ  temp,F
    GOTO    Prop_loop

    MOVF    IError,W
    MOVWF   Int
    MOVF    I_gain,W
    MOVWF   temp
    MOVF    Int,W   
Int_loop
    ADDWF   Int,F           ;ADD INTEGRAL ERROR INTEGRAL GAIN TIMES
    DECFSZ  temp,F
    GOTO    Int_loop

    MOVF    DError,W
    MOVWF   Diff
    MOVF    D_gain,W
    MOVWF   temp
    MOVF    Diff,W  
Diff_loop
    ADDWF   Diff,F          ;ADD DIFFERENTIAL ERROR DIFFERENTIAL GAIN TIMES
    DECFSZ  temp,F
    GOTO    Diff_loop
    RETURN
;***************************
;   MAP GAIN TO PWM VALUE
;***************************
CONTROL
    MOVF    Prop,W          ;ADD ALL THREE COMPUTED VALUES
    ADDWF   Int,W
    ADDWF   Diff,W
    MOVWF   CCPR1L          ;MOVE TO CCPR1L TO MAKE IT HI TIME OF PWM
    RETURN

    END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年不长 2024-12-25 17:21:45

我不会对这样的程序添加任何更多评论。除非您有具体问题要问,否则对我来说这似乎很清楚。正如有人在评论中所说,它似乎是一个 PID 控制环路,根据 ADC 的输入驱动 PWM 输出。

唯一不寻常的事情似乎是循环乘法的整数增益(各种 DECFSZ temp,F; GOTO foo_loop 循环)。

I would not add any more comments to such a program. It seems pretty clear to me unless you have specific questions to ask. As someone said in the comments, it appears to be a PID control loop that drives a PWM output based on the input of an ADC.

The only unusual thing seems to be integer gains with multiplication by looping (the various DECFSZ temp,F; GOTO foo_loop loops).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文