PIC code help

Thread Starter

skuzzie

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




Rich (BB code):
;Declare some new variables...according to the datasheet, user RAM starts at address 0x0C
W_TEMP        EQU    0x0C        ;Temp storage of wreg in interupt
STATUS_TEMP    EQU    0x0D        ;temp storage of STATUS reg in interupt
TEMP1        EQU    0x0E        ;temp storage
TEMP2        EQU    0x0F        ;temp storage
SECH        EQU    0x10        ;second counter high byte
SECL        EQU    0x11        ;second counter low byte
T0OVFLCNT    EQU    0x12        ;timer0 overflow count

TMR0        EQU    0x01        ;timer0 reg loc is 01H
INTCON        EQU    0x0B        ;INTCON reg loc is 0BH
T0IF        EQU    2        ;T0IF is bit 2 in the INTCOM reg
T0IE        EQU    5        ;t0IE is bit 5 in the INTCOM reg
GIE        EQU    7        ;GIE is bit 7 in the INTCOM reg

;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
;this function.  Instead, wherever SUB1616 is located in the code, when the code is compiled, all these instructions
;will be inserted in that spot of the code.

;This macro does A1:A2 - B1:B2, only to check whether or not A1:A2 is >= B1:B2
SUB1616        macro    A1,A2,B1,B2

        MOVF    B2,0        ;get low byte of second word and in Wreg
        SUBWF    A2,0        ;subtract low byte of second word from low ;byte of first word
        MOVF    B1,0        ;get high byte of second word
        BTFSS    STATUS,CARRY    ;check for positive result from sub
        INCFSZ    B1,0        ;if not, borrow from higj byte by adding on ;to high bute of secnd word
        GOTO    $ + 3        ;if no rollover in high byte increment, ;jump to three instructions ahead
        BCF    STATUS,CARRY    ;if rollover in high byte increment, sub ;result is not positive
        GOTO    $ + 2        ;sub finsihed, jump two instructions ahead
        SUBWF    A1,0        ;sub high byte of second word from high ;byte of first word

        endm

;An interrupt will be used, so the main code address and the interrupt code address must be declared
;According to the datasheet, main code goes at 0x0000, and interrupt code goes at 0x0004

        ORG     0x0000
        GOTO    MAIN
 
        ORG    0x0004
        MOVWF    W_TEMP        ;save current wreg
        MOVF    STATUS,0
        MOVWF    STATUS_TEMP    ;save current STATUS reg

INT_TMR0    BCF    INTCON,T0IF    ;clear the TMR0 overflow interupt flag
        INCF    T0OVFLCNT,1    ;increment timer0 overflow count
        BTFSS    T0OVFLCNT,4    ;check bit 4 of timer0 overflow count
        GOTO    INT_TMR0END    ;if 64 overflows have not occurred, return ;from interupt
        CLRF    T0OVFLCNT    ;reset overflow count
        INCFSZ    SECL,1        ;increment seconds, check for rollover
        GOTO    INT_TMR0END    ;if no rollover, return from interrupt
        INCF    SECH,1        ;if rolover, increment second count high ;byte
        GOTO    INT_TMR0END    ;return from interupt

INT_TMR0END    MOVF    STATUS_TEMP,0    ;retrieve copy of STATUS reg
        MOVWF    STATUS        ;restore pre-interupt STATUS reg
        SWAPF    W_TEMP,1
        SWAPF    W_TEMP,0    ;restore pre-interupt Wreg
        RETFIE


MAIN        CLRF     PORTB           ;all LEDs off
        CLRF    STATUS        ;initailize registers
        CLRF    W_TEMP
        CLRF    STATUS_TEMP
        CLRF    TEMP1
        CLRF    TEMP2
        CLRF    SECH
        CLRF    SECL
        CLRF    T0OVFLCNT
        CLRF    TMR0
        CLRF    INTCON

            BSF       STATUS,RP0      ;   select REG bank1
            CLRF      TRISB           ;set PORTB to outputs
        MOVLW    0x07
        MOVWF    OPTREG        ;assign prescaler to TMR0
        BCF    STATUS,RP0    ;select register bank 0
        CLRF    TMR0        ;reset timer0
        BCF    INTCON,T0IF    ;clear timer0
        BSF    INTCON,T0IE    ;enable timer0 interupt flag
        BSF    INTCON,GIE    ;enable global interupts
        BSF    PORTB,0        ;turn on LED connected to PB0
        BCF    STATUS,CARRY    ;clear CARRY
A        MOVLW    0x00        ;set first timer compare at 2 seconds
        MOVWF    TEMP1        ;0 for high byte
        MOVLW    0x02
        MOVWF    TEMP2        ;2 for low byte
        BCF    INTCON,GIE    ;disable interupts
        SUB1616    SECH,SECL,TEMP1,TEMP2    ;
        BSF    INTCON,GIE    ;
        BTFSS    STATUS,CARRY    ;check for positive result from sub
        GOTO    A        ;
        BCF    PORTB,0        ;clear PortB
B        MOVLW    0x00        ;set first timer compare at 2 seconds
        MOVWF    TEMP1        ;0 for high byte
        MOVLW    0x3E
        MOVWF    TEMP2        ;62 for low byte
        BCF    INTCON,GIE    ;disable interupts
        SUB1616    SECH,SECL,TEMP1,TEMP2    ;
        BSF    INTCON,GIE    ;enable interupts
        BTFSS    STATUS,CARRY    ;check for sub result >= 0
        GOTO    B        ;if not, stay in loop
        BSF    PORTB,1        ;if >=0, turn on LED connected to PB1
C        MOVLW    0x00        ;set third timer compare at 122 seconds ;(cumulative timer)
        MOVWF    TEMP1        ;0 for high byte
        MOVLW    0x7A
        MOVWF    TEMP2        ;122 for low byte
        BCF    INTCON,GIE    ;disable interupts
        SUB1616    SECH,SECL,TEMP1,TEMP2    ;
        BSF    INTCON,GIE    ;enable interupts
        BTFSS    STATUS,CARRY    ;check for sub result >=0
        GOTO    C        ;if not rollover in high byte increment to ;three instructions ahead
        BSF    PORTB,2        ;if >=0, turn on LED connected to PB2
D        MOVLW    0x00        ;set second timer compare at 182 seconds
        MOVWF    TEMP1        ;0 for high byte
        MOVLW    0xB6
        MOVWF    TEMP2        ;182 for low byte
        BCF    INTCON,GIE    ;disable interupts
        SUB1616    SECH,SECL,TEMP1,TEMP2    ;
        BSF    INTCON,GIE    ;enable interupts
        BTFSS    STATUS,CARRY    ;check for sub result >=0
        GOTO    D        ;if not, stay in loop
        BSF    PORTB,3        ;if >=0, turn on LED connected to PB3
E        MOVLW    0x01        ;set second timer compare at 302 seconds
        MOVWF    TEMP1        ;16-bit equivelant for 302 is 0x12E
        MOVLW    0x2E
        MOVWF    TEMP2
        BCF    INTCON,GIE    ;disable interupts
        SUB1616    SECH,SECL,TEMP1,TEMP2    ;
        BSF    INTCON,GIE    ;enable interupts
        BTFSS    STATUS,CARRY    ;check for sub result >=0
        GOTO    E        ;if not, stay in loop
        BSF    PORTB,4        ;if >=0, turn on LED connected to PB4

        BCF    INTCON,GIE    ;disable interupts
        BCF    INTCON,T0IE    ;disable timer0
        CLRF    TMR0        ;reset timer0
        BCF    INTCON,T0IF    ;clear timer0 interrupt flag
        GOTO    E        ;loop back to beginning


        END
 
Top