 INCLUDE "p16f688.inc"
 __CONFIG    _CP_OFF & _CPD_OFF & _BOD_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_ON & _FCMEN_OFF & _IESO_OFF & _INTOSCIO
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Cory Helmuth Snowfall Tubes
; 8x7 Charlieplexed for 56 Falling LED's
; Looping Through The LED's
; Pinouts as Follows
;
;1 = RA4
;2 = RA0
;3 = RA1
;4 = RA2
;5 = RC0
;6 = RC1
;7 = RC2
;8 = RC3
;
;1-2 Done;1-3 Done;1-4 Done;1-5 Done;1-6 Done;1-7 Done;1-8 Done
;2-3 Done;2-4 Done;2-5 Done;2-6 Done;2-7 Done;2-8 Done
;3-4 Done;3-5 Done;3-6 Done;3-7 Done;3-8 Done
;4-5 Done;4-6 Done;4-7 Done;4-8 Done
;5-6 Done;5-7 Done;5-8 Done
;6-7 Done;6-8 Done
;7-8 Done
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Notes :
;  MUST LEAVE BANK BEFORE INCREMENTING NUMBER
;  SUBLW and SUBWF Important Commands To Know
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ORG 0x00	;On reset or power-up, program will branch here 
 GOTO Start
 ORG 0x04	;On interrupt program will branch here
 GOTO Interupt

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;  Main Program
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Start
;; Turn Off Analog 
 MOVLW b'00000000'  
 MOVWF ANSEL  
 MOVLW b'00000111'
 MOVWF CMCON0  
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Enable Interupts & Timer0 Interupt
 MOVLW b'10100000'               ;Turn On Interupts
 MOVWF INTCON
 BSF STATUS, 5                   ;Goto Bank 1
 MOVLW b'00000000'               ;Set Prescaler & Timer0 Source
 MOVWF OPTION_REG
 BCF STATUS, 5                   ;Goto Bank 0
 MOVLW d'6'                      
 MOVWF TMR0                      ;Timer0 starting point, writing this will start timer after two ins. cyc.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

PC		equ 02                   ;Sets PC equal to the PC Counter

 cblock 0xF0                     ;Start cblock at 0x20
 d1                              ;d1 variable used in delay sequence
 d2                              ;d2 variable used in delay sequence
 tick_cnt1                       ;tick_cnt1 used to offset TRISA and TRISC datatables
 tick_cnt2                       ;tick_cnt2 used to offset PORTA and PORTC datatables
 COUNT                           ;used to loop through the LoopLED Routine 28 times
 W_TEMP							 ;Save W Temp for Interupt
 STATUS_TEMP					 ;Save STATUS Temp for Interupt
 Checker						 ;Flag for Checking Whether to send to the next LoopLED
 endc                            ;Ends the cblock

ResetLED                         ;Start of Main Program
 CLRF tick_cnt1                  ;Clear Tick_cnt1
 CLRF tick_cnt2                  ;Clear Tick_cnt2
 MOVLW 0x1C                      ;Sets/Resets Bits so count loops 28 times.
 MOVWF COUNT                     ;Move W Register to COUNT
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Start LED Loops
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LoopLED	                         ;Start of LoopLED SubRoutine
 BSF Checker, 0                  ;Set Checker Bit so Interupt does not Restart LoopLED Accidently
 MOVFW tick_cnt1                 ;Move tick_cnt1 Into W Register
 Call StatusA                    ;Call StatusA to get and Set TRISA
 MOVFW tick_cnt1                 ;Move tick_cnt1 Into W Register
 Call StatusC                    ;Call StatusC to get and Set TRISC
 incf tick_cnt1, 1               ;Increment tick_cnt1 by 1
 MOVFW tick_cnt2                 ;Move tick_cnt2 Into W Register
 Call TableBitA                  ;Call TableBitA to get Bit's for Setting PORTA
 MOVWF   PORTA                   ;Set PORTA High & Low
 MOVFW tick_cnt2                 ;Move tick_cnt2 Into W Register
 Call TableBitC                  ;Call TableBitC to get Bit's for Setting PORTC
 MOVWF   PORTC                   ;Set PORTC High & Low
 incf  tick_cnt2,1               ;Increment tick_cnt2 by 1
 Call Delay                      ;Call Delay between LED movements
 MOVFW tick_cnt2                 ;Move tick_cnt2 Into W Register
 Call TableBitA                  ;Call TableBitA to get Bit's for Setting PORTA
 MOVWF   PORTA                   ;Set PORTA High & Low
 MOVFW tick_cnt2                 ;Move tick_cnt2 Into the W Register
 Call TableBitC                  ;Call TableBitC to get Bit's for Setting PORTC
 MOVWF   PORTC                   ;Set PORTC High & Low
 incf  tick_cnt2,1               ;Increment tick_cnt2 by 1
 Call Delay                      ;Call Delay between LED movements
 BCF Checker, 0                  ;Clear Checker Bit so Interupt can Restart LoopLED Now
 decfsz   COUNT,1                ;Decrease COUNT by 1 if not 0 and Continue to LoopLED Else Skip to ResetLED
 Goto Loop                       ;Go back up to LoopLED
 Goto ResetLED                   ;Once Finished with LoopLED Start Over

Loop  
 Call Delay                      ;Infinite Loop Waiting for TMR0 Interupt
 goto Loop 

StatusA                          ;StatusA SubRoutine For Setting TRISA Bits
 BSF STATUS, 5                   ;Goto Bank 1
 Call TableSetA                  ;Call TableSetA subroutine get bits for TRISA
 MOVWF TRISA                     ;Move W Register to TRISA 
 BCF STATUS, 5                   ;Goto Bank 0
 return                          ;Return from SubRoutine

StatusC                          ;StatusC SubRoutine For Setting TRISC Bits
 BSF STATUS, 5                   ;Goto Bank 1
 Call TableSetC                  ;Call TableSetC subroutine get bits for TRISC
 MOVWF TRISC                     ;Move W Register to TRISC 
 BCF STATUS, 5                   ;Goto Bank 0
 return                          ;Return from SubRoutine

; Delay between LED changes
Delay
	movlw	0x1E
	movwf	d1
	movlw	0x0D
	movwf	d2
    movfw   tick_cnt1
    addwf   d2, 1
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0

	goto	$+1
	nop
	return

TableSetA	addwf	PC, 1      ;Datatable for TrisA
 retlw     b'101110'
 retlw     b'101101'
 retlw     b'101011'
 retlw     b'101111'
 retlw     b'101111'
 retlw     b'101111'
 retlw     b'101111'
 retlw     b'111100'
 retlw     b'111010'
 retlw     b'111110'
 retlw     b'111110'
 retlw     b'111110'
 retlw     b'111110'
 retlw     b'111001'
 retlw     b'111101'
 retlw     b'111101'
 retlw     b'111101'
 retlw     b'111101'
 retlw     b'111011'
 retlw     b'111011'
 retlw     b'111011'
 retlw     b'111011'
 retlw     b'111111'
 retlw     b'111111'
 retlw     b'111111'
 retlw     b'111111'
 retlw     b'111111'
 retlw     b'111111'
 return

TableSetC	addwf	PC, 1     ;Datatable for TrisC
 retlw     b'111111'
 retlw     b'111111'
 retlw     b'111111'
 retlw     b'111110'
 retlw     b'111101'
 retlw     b'111011'
 retlw     b'110111'
 retlw     b'111111'
 retlw     b'111111'
 retlw     b'111110'
 retlw     b'111101'
 retlw     b'111011'
 retlw     b'110111'
 retlw     b'111111'
 retlw     b'111110'
 retlw     b'111101'
 retlw     b'111011'
 retlw     b'110111'
 retlw     b'111110'
 retlw     b'111101'
 retlw     b'111011'
 retlw     b'110111'
 retlw     b'111100'
 retlw     b'111010'
 retlw     b'110110'
 retlw     b'111001'
 retlw     b'110101'
 retlw     b'110011'
 return

TableBitA	addwf	PC, 1     ;Datatable for PortA
 retlw     b'010000'
 retlw     b'000001'
 retlw     b'010000'
 retlw     b'000010'
 retlw     b'010000'
 retlw     b'000100'
 retlw     b'010000'
 retlw     b'000000'
 retlw     b'010000'
 retlw     b'000000'
 retlw     b'010000'
 retlw     b'000000'
 retlw     b'010000'
 retlw     b'000000'
 retlw     b'000001'
 retlw     b'000010'
 retlw     b'000001'
 retlw     b'000100'
 retlw     b'000001'
 retlw     b'000000'
 retlw     b'000001'
 retlw     b'000000'
 retlw     b'000001'
 retlw     b'000000'
 retlw     b'000001'
 retlw     b'000000'
 retlw     b'000010'
 retlw     b'000100'
 retlw     b'000010'
 retlw     b'000000'
 retlw     b'000010'
 retlw     b'000000'
 retlw     b'000010'
 retlw     b'000000'
 retlw     b'000010'
 retlw     b'000000'
 retlw     b'000100'
 retlw     b'000000'
 retlw     b'000100'
 retlw     b'000000'
 retlw     b'000100'
 retlw     b'000000'
 retlw     b'000100'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 return

TableBitC	addwf	PC, 1     ;Datatable for PortC
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000001'
 retlw     b'000000'
 retlw     b'000010'
 retlw     b'000000'
 retlw     b'000100'
 retlw     b'000000'
 retlw     b'001000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000001'
 retlw     b'000000'
 retlw     b'000010'
 retlw     b'000000'
 retlw     b'000100'
 retlw     b'000000'
 retlw     b'001000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000000'
 retlw     b'000001'
 retlw     b'000000'
 retlw     b'000010'
 retlw     b'000000'
 retlw     b'000100'
 retlw     b'000000'
 retlw     b'001000'
 retlw     b'000000'
 retlw     b'000001'
 retlw     b'000000'
 retlw     b'000010'
 retlw     b'000000'
 retlw     b'000100'
 retlw     b'000000'
 retlw     b'001000'
 retlw     b'000001'
 retlw     b'000010'
 retlw     b'000001'
 retlw     b'000100'
 retlw     b'000001'
 retlw     b'001000'
 retlw     b'000010'
 retlw     b'000100'
 retlw     b'000010'
 retlw     b'001000'
 retlw     b'000100'
 retlw     b'001000'
 return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Interrupt Service Routine
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Interupt
 MOVWF	W_TEMP				; Save off current W register contents
 MOVF	STATUS, w			; Move status register into W register
 MOVWF	STATUS_TEMP			; Save off contents of STATUS register
 MOVLW b'10100000'          ; Turn Interupts Back On.
 MOVWF INTCON
 MOVLW d'6'
 MOVWF TMR0                 ;Start Timer0 again from 6
 BTFSS Checker, 0           ;Check for Checker Bit Set If Set Don't Do LoopLED because you interupted it already
 goto LoopLED               ;Start LoopLED again if not already in one.

 MOVF STATUS_TEMP, w		; retrieve copy of STATUS register
 MOVWF STATUS				; restore pre-isr STATUS register contents
 SWAPF W_TEMP, f
 SWAPF W_TEMP, w			; restore pre-isr W register contents
 RETFIE						; return from interrupt


Finish                ; Escape Route To End Program
 End
