Using "Night Rider" code to fade through rgb led colours

Discussion in 'Programmer's Corner' started by gregted, Feb 14, 2009.

  1. gregted

    Thread Starter New Member

    Feb 2, 2006
    2
    0
    I have this code from the net to simulate the "Night Rider" led chasing effect and want to us it to fade through the colours on a rgb led.


    This is the "night Rider" code.

    LIST P=16C84

    ; Night Rider simulation - original
    ;
    PORTB EQU 6
    TRISB EQU 86H
    OPTREG EQU 81H
    STATUS EQU 3
    CARRY EQU 0
    RP0 EQU 5
    MSB EQU 3 ; .................BIT POSITION OF LEFTMOST LED
    ;
    CLRF PORTB ; ...............ALL LEDS OFF
    BSF STATUS,RP0 ;.........SELECT REGISTER BANK 1
    CLRF TRISB^80H ;.........SET PORTB TO ALL OUTPUTS
    MOVLW 0AH
    MOVWF OPTREG^80H ;..ASSIGN PRESCALER (1:4) TO WDT
    BCF STATUS,RP0 ;........SELECT REGISTER BANK 0
    INCF PORTB,F ;............TURN ON RIGHTMOST LED
    BCF STATUS,CARRY ;....CLEAR CARRY
    LEFT SLEEP ;...............WAIT FOR WDT TIMEOUT
    RLF PORTB,F ;..............TURN ON LED TO LEFT
    BTFSS PORTB,MSB ;......REACHED LEFTMOST?
    GOTO LEFT ;...............LOOP IF NOT
    RIGHT SLEEP ;.............WAIT FOR WDT TIMEOUT
    RRF PORTB,F ;.............TURN ON LED TO RIGHT
    BTFSS PORTB,0 ;..........REACHED RIGHTMOST?
    GOTO RIGHT ;..............LOOP IF NOT
    GOTO LEFT ;...............START NEW CYCLE
    END
    .......................................................................

    If I change the value of
    MSB EQU 3
    to
    MSB EQU 2

    This should only give me 3 leds chasing instead of 4.
    These 3 leds are of course included in the one package with 4 pins.
    This works fine but the change between colours is too fast and the change is on, off and not a slow fade.

    I know I need to include a delay, something like this...

    Delay1 MOVLW 82H ;...130 loops
    MOVWF 1A
    Delx DECFSZ 1B ;........256 Increments
    GOTO Delx ;...............2uS instruction
    DECFSZ 1A
    GOTO Delx
    RETLW 00

    Do I include this after left sleep and right sleep as follows..

    LEFT SLEEP ;............WAIT FOR WDT TIMEOUT
    RLF PORTB,F ;...........TURN ON LED TO LEFT
    CALL Delay1 ; ..........Start delay loop
    BTFSS PORTB,MSB ;...REACHED LEFTMOST?
    GOTO LEFT ;.............LOOP IF NOT
    RIGHT SLEEP ;..........WAIT FOR WDT TIMEOUT
    RRF PORTB,F ;..........TURN ON LED TO RIGHT
    CALL Delay1 ; ..........Start delay loop
    BTFSS PORTB,0 ;......REACHED RIGHTMOST?
    GOTO RIGHT ;..........LOOP IF NOT
    GOTO LEFT ;............START NEW CYCLE
    END

    I will try this soon but am not sure how to fade on and off.

    Would I be better trying a completely different code altogether.

    Any help appreciated.

    Thanks.
     
    Last edited: Feb 14, 2009
Loading...