Please help me to understand this..

Discussion in 'Embedded Systems and Microcontrollers' started by ajmalsalim, Nov 27, 2011.

  1. ajmalsalim

    Thread Starter New Member

    Nov 27, 2011
    2
    0
    Hi, I am an engineering student. Please help me to understand this program. Can anyone help me by adding relevant comments to each lines of the program?
    :)

    Code ( (Unknown Language)):
    1. #INCLUDE<P16F88.INC> ; PIC definitions
    2.  
    3. SET_UPPER   EQU 0x20
    4. SET_LOWER   EQU 0x21
    5. READ_UPPER  EQU 0x22
    6. READ_LOWER  EQU 0x23
    7. P_gain      EQU 0x24
    8. I_gain      EQU 0x25
    9. D_gain      EQU 0x26
    10. Prop        EQU 0x27
    11. Int         EQU 0x28
    12. Diff        EQU 0x29
    13. DError      EQU 0x30
    14. IError      EQU 0x31
    15. temp        EQU 0x32
    16.    
    17.     ORG 0h
    18. ;****************************
    19. ;       INITIZLIZATION
    20. ;****************************
    21.    
    22.     BANKSEL TRISA           ;SELECT TRISA BANL
    23.     MOVLW   B'00000011'     ;MAKE PIN0  AND 1 INPUT
    24.     MOVWF   TRISA           ;
    25.     BANKSEL ANSEL
    26.     MOVLW   B'00000011'     ;MAKE PA0 AND PA1 ANALOG INPUT
    27.     MOVWF   ANSEL
    28.     BANKSEL PORTA
    29.     MOVLW   0x80            ;FOSC/32 AND CHANNEL0
    30.     MOVWF   ADCON0
    31.     MOVLW   0x00
    32.     CLRF    ADCON1          ;LEFT JUSTIFIED ADC VALUE
    33.     CLRF    CCP1CON         ;PWM MODULE
    34.     CLRF    TRISB           ;MAKE PORT B O/P FOR PWM
    35.     MOVLW   0xFF
    36.     MOVWF   PR2
    37.     CLRF    CCPR1L          ;0 DUTY CYCLE AT START
    38.     MOVLW   0x01
    39.     MOVWF   T2CON           ;PRESCALER 4 AND POSTSCALAR 1
    40.     BSF     T2CON,TMR2ON    ;SWITCH ON TIMER
    41. ;****************************
    42. ;           MAIN
    43. ;****************************
    44. MAIN
    45.     CALL    SET_POINT       ;READ SET POINT
    46.     CALL    READ_TEMP       ;READ TEMPERTAURE
    47.     CALL    COMPARE         ;COMPARE AND FIND GAIN VALUE
    48.     CALL    CONTROL         ;MAP GAIN TO DUTY CYCLE
    49.     GOTO    MAIN
    50.  
    51. ;****************************
    52. ;       READ SET PT.
    53. ;****************************
    54. SET_POINT
    55.     MOVLW   0x80
    56.     MOVWF   ADCON0          ;CHANNEL 0 ADC
    57.     BSF     ADCON0,ADON     ;SWITCH ON ADC
    58.     NOP                     ;WAIT ACQUISITION TIME
    59.     NOP
    60.     NOP
    61.     NOP
    62.     NOP
    63.     BSF     ADCON0,GO
    64. ADC
    65.     BTFSC   ADCON0,GO       ;WAIT TILL CONVERSION IS COMPLETE
    66.     GOTO    ADC
    67.     MOVF    ADRESH,W        ;READ ADC VALUES
    68.     MOVWF   SET_UPPER
    69.     BANKSEL ADRESL
    70.     MOVF    ADRESL,W
    71.     MOVWF   SET_LOWER
    72.     BANKSEL PORTA
    73.     RETURN
    74. ;****************************
    75. ;      READ TEMPERATURE
    76. ;****************************
    77. READ_TEMP
    78.     MOVLW   0x88            ;ADC CHANNEL 1 SELECT
    79.     MOVWF   ADCON0
    80.     BSF     ADCON0,ADON     ;SWITCH ON ADC
    81.     NOP                     ;WAIT ACQUSITION TIME
    82.     NOP
    83.     NOP
    84.     NOP
    85.     NOP
    86.     BSF     ADCON0,GO       ;START CONVERSION
    87. ADC0   
    88.     BTFSC   ADCON0,GO       ;WAIT TILL CONVERSION IS COMPLETE
    89.     GOTO    ADC0
    90.     MOVF    ADRESH,W
    91.     MOVWF   READ_UPPER      ;READ ADC VALUE
    92.     BANKSEL ADRESL
    93.     MOVF    ADRESL,W
    94.     MOVWF   READ_LOWER
    95.     BANKSEL PORTA
    96.     RETURN
    97. ;******************************
    98. ;   COMPUTE ERROR AND GAINS
    99. ;******************************
    100. COMPARE
    101.     MOVF    SET_UPPER,W
    102.     SUBWF   READ_UPPER,W    ;SUBTRACT READ VALUE FROM SET POINT
    103.     SUBWF   DError,F        ;COMPUTE INTEGRAL ERROR
    104.     ADDWF   IError,F        ;COMPUTE DIFFERENTIAL ERROR
    105.    
    106.     MOVWF   Prop            ;MOVE COMPUTED ERROR TO PROP
    107.     MOVF    P_gain,W       
    108.     MOVWF   temp
    109.     MOVF    Prop,W         
    110. Prop_loop
    111.     ADDWF   Prop,F          ;ADD PRPORTIONAL ERROR PROPORTIONAL GAIN TIMES
    112.     DECFSZ  temp,F
    113.     GOTO    Prop_loop
    114.    
    115.     MOVF    IError,W
    116.     MOVWF   Int
    117.     MOVF    I_gain,W
    118.     MOVWF   temp
    119.     MOVF    Int,W  
    120. Int_loop
    121.     ADDWF   Int,F           ;ADD INTEGRAL ERROR INTEGRAL GAIN TIMES
    122.     DECFSZ  temp,F
    123.     GOTO    Int_loop
    124.  
    125.     MOVF    DError,W
    126.     MOVWF   Diff
    127.     MOVF    D_gain,W
    128.     MOVWF   temp
    129.     MOVF    Diff,W 
    130. Diff_loop
    131.     ADDWF   Diff,F          ;ADD DIFFERENTIAL ERROR DIFFERENTIAL GAIN TIMES
    132.     DECFSZ  temp,F
    133.     GOTO    Diff_loop
    134.     RETURN
    135. ;***************************
    136. ;   MAP GAIN TO PWM VALUE
    137. ;***************************
    138. CONTROL
    139.     MOVF    Prop,W          ;ADD ALL THREE COMPUTED VALUES
    140.     ADDWF   Int,W
    141.     ADDWF   Diff,W
    142.     MOVWF   CCPR1L          ;MOVE TO CCPR1L TO MAKE IT HI TIME OF PWM
    143.     RETURN
    144.    
    145.     END
     
    Last edited by a moderator: Nov 27, 2011
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    It's already well commented, I suggest downloading the PIC16F88 datasheet from microchip.com and reading through the "instruction set summary" section.
     
  3. ajmalsalim

    Thread Starter New Member

    Nov 27, 2011
    2
    0
    Hello Markd77, Please help me to add comments. Its really urgent. Some of the instructions could not be found:

    "BANKSEL ANSEL" etc

    Please. please .please help me..
     
  4. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Banksel is a compiler directive that generates a couple of lines of assembler to change the RP0 and RP1 bits of STATUS so that the bank that ANSEL is in is selected.
    ANSEL is the register that controls most of the analog settings.
    See section 2.2 and 12 of the datasheet.
     
Loading...