Code:
	'****************************************************************
'*  Name    : KITT2000_2x-LEDs-per-port_16F630_v2.BAS           *
'*  Author  : FLOTUL                                            *
'*  Notice  : Copyright (c) 2006 Flotulopex & Co.               *
'*          : All Rights Reserved                               *
'*  Date    : 11.08.2007                                        *
'*  Version : 1.0                                               *
'*  Notes   : "Knight Rider" style stoplight                    *
'*          :  KITT = Knight Industries Two Thousand            *
'*  Tested  : yes                                               *
'*  Finished: yes                                               *
'****************************************************************
'
' Make 20 LEDs car stoplight scroll from left to right and backwards like
'  KITT2000 car.
'
' When going on brakes, the stoplight will light-up normally (all leds
'  appearing to light simultaneously).
'
' If the brakes are released for a very short time (47...100µ capa determines
'  the duration), the program switches to KITT led mode.
'
' There are two light "modes":
' 1) STOPLight: go on brakes and all leds appear to light-up simultaneously.
'   In this mode, leds are grouped in 5 pairs. They are swicthed-ON and -OFF
'   in sequence to keep the total current low (around 200mA).
' 2) KITT2000: the "Knight Rider" effet. The leds scroll from left to right and
'   vice-versa.
'
' To index the PORTs, the "trick" is to use PORTA.0 as offset point (0).
' Since all other ports are consecutive, PORTB.0 is bit 8,
'  and PORTC.0 is bit 16, etc. So, it looks like this:
'  PORTA.2 = 2,  PORTA.4 = 4,  PORTA.5 = 5
'  PORTB.3 = 11, PORTB.4 = 12, PORTB.5 = 13
'  PORTC.0 = 16, PORTC.1 = 17, PORTC.2 = 18
'
' Power OFF/ON (brake release) detect on PORTA.3
' LEDs = PORTA.1-2-4-5, PORTC.0-1-2-3-4-5
'
' Currently, I paired the leds so each ports light-up two leds simultaneously.
'-------------------------------------------------------------------------------
' Fuses
@ DEVICE PIC16F630,BOD_ON   ; Mandatory for this circuit
@ DEVICE PIC16F630,CPD_OFF
@ DEVICE PIC16F630,PROTECT_OFF
@ DEVICE PIC16F630,MCLR_OFF
@ DEVICE PIC16F630,PWRT_OFF
@ DEVICE PIC16F630,WDT_OFF
@ DEVICE PIC16F630,INTRC_OSC_NOCLKOUT
'-------------------------------------------------------------------------------
' Registers   76543210
TRISA      = %00001000 'Set Input/Output
PORTA      = %00000000 'Ports High/Low
TRISC      = %00000000 'Set Input/Output
PORTC      = %00000000 'Ports High/Low
OPTION_REG = %10000000 'PORTA Pull-Ups disabled, prescaler bits 2-0
WPUA       = %00000000 'Weak Pull-up of PORTA disabled
INTCON     = %00000000 'GIE & TMR0 disabled
OSCCAL     = %10000000 'Oscillator Calibration when Internal RC set to 4MHz
CMCON      = %00000111 'Comparator Module is OFF
'-------------------------------------------------------------------------------
' Defines
'-------------------------------------------------------------------------------
' Variables
Counter   var byte
counter   = 0
STOPSpeed var byte
STOPspeed = 5
KITTSpeed var byte
KITTSpeed = 45
Led    var byte[10]
Led[0] = 1  'PORTA.1
Led[1] = 2   'PORTA.2
Led[2] = 4    'PORTA.4
Led[3] = 5     'PORTA.5
Led[4] = 16     'PORTC.0
Led[5] = 17 'PORTC.1
Led[6] = 18  'PORTC.2
Led[7] = 19   'PORTC.3
Led[8] = 20    'PORTC.4
Led[9] = 21     'PORTC.5
'-------------------------------------------------------------------------------
' Program
MAIN:
    'STOPlight mode
    PORTA.0(1)=1 : PORTA.0(17)=1  'Leds 0 & 5
        pause stopspeed
    PORTA.0(1)=0 : PORTA.0(17)=0
    PORTA.0(2)=1 : PORTA.0(18)=1  'Leds 1 & 6
        pause stopspeed
    PORTA.0(2)=0 : PORTA.0(18)=0
    PORTA.0(4)=1 : PORTA.0(19)=1  'Leds 2 & 7
        pause stopspeed
    PORTA.0(4)=0 : PORTA.0(19)=0
    PORTA.0(5)=1 : PORTA.0(20)=1  'Leds 3 & 8
        pause stopspeed
    PORTA.0(5)=0 : PORTA.0(20)=0
    PORTA.0(16)=1 : PORTA.0(21)=1 'Leds 4 & 9
        pause stopspeed
    PORTA.0(16)=0 : PORTA.0(21)=0
    
    if PORTA.3 = 0 then 'if the brakes are released shortly...
        goto kitT2000    'go and stay there!
    endif
    goto main
end
' Initial version (103 Words)
KITT2000:
    'Scroll Led from left to right
    for counter = 0 to 9
        PORTA.0(Led[counter]) = 1 'switch led ON
        PAUSE KITTSpeed
        PORTA.0(Led[counter]) = 0 'switch led OFF
    next counter
    'Reverse order
    for counter = 8 to 1 step -1  'also try from 9 to 1
        PORTA.0(Led[counter]) = 1 'switch led ON
        PAUSE KITTSpeed
        PORTA.0(Led[counter]) = 0 'switch led OFF
    next counter
    goto kitT2000
' LOOKUP version (121 Words)
'KITT2000:
'    'Scroll Led from left to right
'    for counter = 0 to 9
'        lookup counter,[1,2,4,5,16,17,18,19,20,21],led
'        PORTA.0(led) = 1 'switch led ON
'        PAUSE KITTSpeed
'        PORTA.0(led) = 0 'switch led ON
'    next counter
'    'Reverse order
'    for counter = 0 to 7
'        lookup counter,[20,19,18,17,16,5,4,2],led
'        PORTA.0(led) = 1 'switch led ON
'        PAUSE KITTSpeed
'        PORTA.0(led) = 0 'switch led ON
'    next counter
'    goto KITT2000
 
				
Bookmarks