PIC code help

Discussion in 'Homework Help' started by skuzzie, Mar 2, 2009.

  1. skuzzie

    Thread Starter Member

    Feb 11, 2009
    10
    0
    Is this code turning on the LEDs one by one with a timer between them??




    Code ( (Unknown Language)):
    1. ;Declare some new variables...according to the datasheet, user RAM starts at address 0x0C
    2. W_TEMP        EQU    0x0C        ;Temp storage of wreg in interupt
    3. STATUS_TEMP    EQU    0x0D        ;temp storage of STATUS reg in interupt
    4. TEMP1        EQU    0x0E        ;temp storage
    5. TEMP2        EQU    0x0F        ;temp storage
    6. SECH        EQU    0x10        ;second counter high byte
    7. SECL        EQU    0x11        ;second counter low byte
    8. T0OVFLCNT    EQU    0x12        ;timer0 overflow count
    9.  
    10. TMR0        EQU    0x01        ;timer0 reg loc is 01H
    11. INTCON        EQU    0x0B        ;INTCON reg loc is 0BH
    12. T0IF        EQU    2        ;T0IF is bit 2 in the INTCOM reg
    13. T0IE        EQU    5        ;t0IE is bit 5 in the INTCOM reg
    14. GIE        EQU    7        ;GIE is bit 7 in the INTCOM reg
    15.  
    16. ;Here, a macro will be declared so that in the code, the same instructions won't need to be typed over ;and over to complete
    17. ;this function.  Instead, wherever SUB1616 is located in the code, when the code is compiled, all these instructions
    18. ;will be inserted in that spot of the code.
    19.  
    20. ;This macro does A1:A2 - B1:B2, only to check whether or not A1:A2 is >= B1:B2
    21. SUB1616        macro    A1,A2,B1,B2
    22.  
    23.         MOVF    B2,0        ;get low byte of second word and in Wreg
    24.         SUBWF    A2,0        ;subtract low byte of second word from low ;byte of first word
    25.         MOVF    B1,0        ;get high byte of second word
    26.         BTFSS    STATUS,CARRY    ;check for positive result from sub
    27.         INCFSZ    B1,0        ;if not, borrow from higj byte by adding on ;to high bute of secnd word
    28.         GOTO    $ + 3        ;if no rollover in high byte increment, ;jump to three instructions ahead
    29.         BCF    STATUS,CARRY    ;if rollover in high byte increment, sub ;result is not positive
    30.         GOTO    $ + 2        ;sub finsihed, jump two instructions ahead
    31.         SUBWF    A1,0        ;sub high byte of second word from high ;byte of first word
    32.  
    33.         endm
    34.  
    35. ;An interrupt will be used, so the main code address and the interrupt code address must be declared
    36. ;According to the datasheet, main code goes at 0x0000, and interrupt code goes at 0x0004
    37.  
    38.         ORG     0x0000
    39.     [B]    GOTO    MAIN
    40. [/B]
    41.         ORG    0x0004
    42.         MOVWF    W_TEMP        ;save current wreg
    43.         MOVF    STATUS,0
    44.         MOVWF    STATUS_TEMP    ;save current STATUS reg
    45.  
    46. INT_TMR0    BCF    INTCON,T0IF    ;clear the TMR0 overflow interupt flag
    47.         INCF    T0OVFLCNT,1    ;increment timer0 overflow count
    48.         BTFSS    T0OVFLCNT,4    ;check bit 4 of timer0 overflow count
    49.         GOTO    INT_TMR0END    ;if 64 overflows have not occurred, return ;from interupt
    50.         CLRF    T0OVFLCNT    ;reset overflow count
    51.         INCFSZ    SECL,1        ;increment seconds, check for rollover
    52.         GOTO    INT_TMR0END    ;if no rollover, return from interrupt
    53.         INCF    SECH,1        ;if rolover, increment second count high ;byte
    54.         GOTO    INT_TMR0END    ;return from interupt
    55.  
    56. INT_TMR0END    MOVF    STATUS_TEMP,0    ;retrieve copy of STATUS reg
    57.         MOVWF    STATUS        ;restore pre-interupt STATUS reg
    58.         SWAPF    W_TEMP,1
    59.         SWAPF    W_TEMP,0    ;restore pre-interupt Wreg
    60.         RETFIE
    61.  
    62.  
    63. MAIN        CLRF     PORTB           ;all LEDs off
    64.         CLRF    STATUS        ;initailize registers
    65.         CLRF    W_TEMP
    66.         CLRF    STATUS_TEMP
    67.         CLRF    TEMP1
    68.         CLRF    TEMP2
    69.         CLRF    SECH
    70.         CLRF    SECL
    71.         CLRF    T0OVFLCNT
    72.         CLRF    TMR0
    73.         CLRF    INTCON
    74.  
    75.             BSF       STATUS,RP0      ;   select REG bank1
    76.             CLRF      TRISB           ;set PORTB to outputs
    77.         MOVLW    0x07
    78.         MOVWF    OPTREG        ;assign prescaler to TMR0
    79.         BCF    STATUS,RP0    ;select register bank 0
    80.         CLRF    TMR0        ;reset timer0
    81.         BCF    INTCON,T0IF    ;clear timer0
    82.         BSF    INTCON,T0IE    ;enable timer0 interupt flag
    83.         BSF    INTCON,GIE    ;enable global interupts
    84.         BSF    PORTB,0        ;turn on LED connected to PB0
    85.         BCF    STATUS,CARRY    ;clear CARRY
    86. A        MOVLW    0x00        ;set first timer compare at 2 seconds
    87.         MOVWF    TEMP1        ;0 for high byte
    88.         MOVLW    0x02
    89.         MOVWF    TEMP2        ;2 for low byte
    90.         BCF    INTCON,GIE    ;disable interupts
    91.         SUB1616    SECH,SECL,TEMP1,TEMP2    ;
    92.         BSF    INTCON,GIE    ;
    93.         BTFSS    STATUS,CARRY    ;check for positive result from sub
    94.         GOTO    A        ;
    95.         BCF    PORTB,0        ;clear PortB
    96. B        MOVLW    0x00        ;set first timer compare at 2 seconds
    97.         MOVWF    TEMP1        ;0 for high byte
    98.         MOVLW    0x3E
    99.         MOVWF    TEMP2        ;62 for low byte
    100.         BCF    INTCON,GIE    ;disable interupts
    101.         SUB1616    SECH,SECL,TEMP1,TEMP2    ;
    102.         BSF    INTCON,GIE    ;enable interupts
    103.         BTFSS    STATUS,CARRY    ;check for sub result >= 0
    104.         GOTO    B        ;if not, stay in loop
    105.         BSF    PORTB,1        ;if >=0, turn on LED connected to PB1
    106. C        MOVLW    0x00        ;set third timer compare at 122 seconds ;(cumulative timer)
    107.         MOVWF    TEMP1        ;0 for high byte
    108.         MOVLW    0x7A
    109.         MOVWF    TEMP2        ;122 for low byte
    110.         BCF    INTCON,GIE    ;disable interupts
    111.         SUB1616    SECH,SECL,TEMP1,TEMP2    ;
    112.         BSF    INTCON,GIE    ;enable interupts
    113.         BTFSS    STATUS,CARRY    ;check for sub result >=0
    114.         GOTO    C        ;if not rollover in high byte increment to ;three instructions ahead
    115.         BSF    PORTB,2        ;if >=0, turn on LED connected to PB2
    116. D        MOVLW    0x00        ;set second timer compare at 182 seconds
    117.         MOVWF    TEMP1        ;0 for high byte
    118.         MOVLW    0xB6
    119.         MOVWF    TEMP2        ;182 for low byte
    120.         BCF    INTCON,GIE    ;disable interupts
    121.         SUB1616    SECH,SECL,TEMP1,TEMP2    ;
    122.         BSF    INTCON,GIE    ;enable interupts
    123.         BTFSS    STATUS,CARRY    ;check for sub result >=0
    124.         GOTO    D        ;if not, stay in loop
    125.         BSF    PORTB,3        ;if >=0, turn on LED connected to PB3
    126. E        MOVLW    0x01        ;set second timer compare at 302 seconds
    127.         MOVWF    TEMP1        ;16-bit equivelant for 302 is 0x12E
    128.         MOVLW    0x2E
    129.         MOVWF    TEMP2
    130.         BCF    INTCON,GIE    ;disable interupts
    131.         SUB1616    SECH,SECL,TEMP1,TEMP2    ;
    132.         BSF    INTCON,GIE    ;enable interupts
    133.         BTFSS    STATUS,CARRY    ;check for sub result >=0
    134.         GOTO    E        ;if not, stay in loop
    135.         BSF    PORTB,4        ;if >=0, turn on LED connected to PB4
    136.  
    137.         BCF    INTCON,GIE    ;disable interupts
    138.         BCF    INTCON,T0IE    ;disable timer0
    139.         CLRF    TMR0        ;reset timer0
    140.         BCF    INTCON,T0IF    ;clear timer0 interrupt flag
    141.         GOTO    E        ;loop back to beginning
    142.  
    143.  
    144.         END
     
  2. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    If you use C for the code you will make your life easier.
     
  3. skuzzie

    Thread Starter Member

    Feb 11, 2009
    10
    0
    I cant...we are doing assembly.
     
  4. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    Then an assembly guy might help :p
     
Loading...