; Flash12a
; Program for light unit
; Now at 4 MHz for 12C509 
; State 0 both inputs low  Headlight dim, rear light on
; State 1 bit 1 lo, 0 hi   Headlight full, ditch lights flashing
; State 2 bit 1 hi, 0 lo   Headlight full, dual gyralights
; State 3 bit 1 hi, 0 hi   All front lights full
; Timing OK at 4MHz
; 11/05/02  Modified rates to correspond with Rio Grande GP30/40

	
	processor 12C509A
	include "p12C509A.inc"
	 

	__CONFIG	h'000E' ; CP off, WDT on, internal clock


;Definitions


F1_in	equ	0
Op3	equ	1
Op3_msk	equ	B'00000010'
Op4	equ	2 
Op4_msk	equ	B'00000100'
F2_in	equ	3	
Op2	equ	4
Op2_msk	equ	B'00010000'
Op1	equ	5
Op1_msk	equ	B'00100000'


	CBLOCK	0x0007

	State  		;set by input combination
	Pwm1  		;pwm value for headlight
	Pwm2   		;pwm value for light 1
	Pwm3 		;pwm value for light 2
	Count1 
	Count2 
	Index  		;LUT index for ditch
	Index1       	;Antiphase index
	Indtmp
	Indtmp2
	Statmp 
	Msk1        	;for reverse counting
	Mask           	;mask for lights on / off
	Temp
	Tim1		;timimg 1
	Tim2		;timing 2
	ENDC

	org	0000	;setup  
	movwf	OSCCAL
        bcf	STATUS,PA0
	clrf	GPIO
			
	movlw	B'00001001'
	tris	GPIO		;GP0 and GP3 are the control inputs
	movlw	B'10001111'	;weak pullups, WDT on
	option
	 
	movlw	h'40' 		;dim value
	movwf	Pwm1
	movlw	h'FF'
	movwf	Pwm2
	movlw	h'1F'  		;antiphase 0 to 31
	movwf	Index1
	clrf	Index           ;0 to 31
        clrf	Count1 
	clrf	State 
	movlw	B'00011111'
	movwf	Msk1

Test	clrwdt
	decf	Count1,F        ;
        comf	Count1,W	;Is Count1 = FF
	btfss	STATUS,Z
	goto	Test1 
	call	Update
	movf	Tim1,W		;Delay value to adjust
	movwf	Count2
Dely	decfsz	Count2,F
	goto	Dely 
	incf	Index,W      	;LUT index
	andlw	B'00111111'
	movwf	Index
	incf	Index1,W 	;Antiphase LUT index
	andlw	B'00111111'
	movwf	Index1
 
	movf	State,W 
	movwf	Statmp
	movlw	h'FF' 
	addwf	Statmp,W
	btfss	STATUS,C
	goto	Test		;State 0
	btfsc	STATUS,Z
	goto	Ditch		;State 1
	andlw	1
	btfss	STATUS,Z 	;State 2 
	goto	Gyra
	goto	Test		;State 3
Ditch	movf	Index,W
	call	Load 
	movwf	Pwm3
	movf	Index1,W 
	call	Load
	movwf	Pwm2
	goto	Test 
Gyra	movf	Index,W
	call	Load1 
	movwf	Pwm3
	movf	Index,W
	movwf	Indtmp2 
	movlw	h'F'      	;offset 
	addwf	Indtmp2,W
	call	Load1
	movwf	Pwm2
	goto	Test

Test1	movlw	B'11111110'
	subwf	Count1,W
	btfss	STATUS,Z
	goto	Test2
	call	Update
	movf	Tim2,W		;Delay value to adjust
	movwf	Count2
Dely1	decfsz	Count2,F
	goto	Dely1
	clrf	State
	movf	GPIO,W		;get inputs
	movwf	Temp
	rrf	Temp,F
	rrf	Temp,F
	bcf	Temp,0
	btfsc	Temp,7
	bsf	Temp,0
	movlw	B'00000011'
	andwf	Temp,W
	addwf	PCL,F
	goto	State0
	goto	State1
	goto	State2
	goto	State3

State0	movlw	B'00100100'     ;headlamp only dim, tail on
	movwf	Mask
	bcf	GPIO,Op2
	bcf	GPIO,Op3 
	bsf	GPIO,Op4	;tail on
	movlw	.30     	;dim value
	movwf	Pwm1
	goto	Test

State1	movlw	B'00110010'     ;headlamp full, ditch, tail off
	movwf	Mask 
	bcf	GPIO,Op4	;tail off
	movlw	7
	movwf	Tim1
	movlw	6
	movwf	Tim2
	bsf	State,0 
	movlw	.253
	movwf	Pwm1

	goto	Test

State2	movlw	B'00110010'  	;headlamp full, gyra, tail off
	movwf	Mask 
	bcf	GPIO,Op4
	bsf	State,1
	movlw	3
	movwf	Tim1
	movlw	2
	movwf	Tim2
	movlw	.253
	movwf	Pwm1
	goto	Test

State3	movlw	B'00110010'   	;all full on, tail off
	movwf	Mask 
	bcf	GPIO,Op4
	movlw	3
	movwf	State
	movlw	.253
	movwf	Pwm1 		;full on
	movwf	Pwm2   		;full on
	movwf	Pwm3	      	;full on
	goto	Test 

Test2	call	Update
	movf	Tim1,W		;Delay value to adjust
	movwf	Count2
Dely2	decfsz	Count2,F
	goto	Dely2
	goto	Test

		;32 values in LUT  (ditch) 	
Load	movwf	Indtmp
	andlw	B'00011111'
	btfsc	Indtmp,5 
	subwf	Msk1,W
        
	
	addwf	PCL,F
	retlw	.5      	;0
       	retlw	.5   		;1
	retlw	.5    		;2
	retlw	.5    		;3
	retlw	.6    		;4
	retlw	.9    		;5
	retlw	.12    		;6
	retlw	.16     	;7
	retlw	.20    		;8
	retlw	.25    		;9
	retlw	.32    		;10
	retlw	.40    		;11
	retlw	.50   		;12
	retlw	.67   		;13
	retlw	.81   		;14
	retlw	.110   		;15
        retlw	.150     	;16
	retlw	.190   		;17
	retlw	.218   		;18
	retlw	.220   		;19
	retlw	.228   		;20
	retlw	.234   		;21
	retlw	.240   		;22
	retlw	.244   		;23
	retlw	.247   		;24
	retlw	.250   		;25
	retlw	.252   		;26
	retlw	.253   		;27
	retlw	.253   		;28
	retlw	.253   		;29
	retlw	.253   		;30
	retlw	.253   		;31

Load1		;32 values in LUT  (gyra)
	movwf	Indtmp
	andlw	B'00011111'
	btfsc	Indtmp,5
	subwf	Msk1,W
        
	
	addwf	PCL,F
	retlw	.40	   	;0	intermediate flash
       	retlw	.15   		;1
	retlw	.0   		;2
	retlw	.0   		;3
	retlw	.0    		;4
	retlw	.0    		;5
	retlw	.0    		;6
	retlw	.0     		;7
	retlw	.0    		;8
	retlw	.0    		;9
	retlw	.0    		;10
	retlw	.0    		;11
	retlw	.0   		;12
	retlw	.0   		;13
	retlw	.0   		;14
	retlw	.0   		;15
        retlw	.0	     	;16 
	retlw	.0   		;17
	retlw	.0   		;18
	retlw	.0   		;19
	retlw	.5  		;20
	retlw	.10   		;21
	retlw	.20   		;22
	retlw	.40   		;23
	retlw	.60   		;24
	retlw	.85   		;25
	retlw	.115   		;26
	retlw	.150   		;27
	retlw	.190   		;28
	retlw	.230   		;29
	retlw	.250   		;30
	retlw	.253   		;31


Update	movf	Count1,W
	subwf	Pwm1,W
	btfss	STATUS,C
	goto	Op1_off      	;above pwm period so turn off
	movlw	Op1_msk  	;is it on?
	andwf	Mask,W
	btfss	STATUS,Z
	bsf	GPIO,Op1     	;turn on
	goto	Up1
Op1_off	bcf	GPIO,Op1	;turn off
Up1	movf	Count1,W
	subwf	Pwm2,W
	btfss	STATUS,C
	goto	Op2_off
	movlw	Op2_msk
	andwf	Mask,W
	btfss	STATUS,Z
	bsf	GPIO,Op2
	goto	Up2
Op2_off	bcf	GPIO,Op2
Up2	movf	Count1,W
	subwf	Pwm3,W
	btfss	STATUS,C
	goto	Op3_off
	movlw	Op3_msk
	andwf	Mask,W
	btfss	STATUS,Z
	bsf	GPIO,Op3
	goto	Up3
Op3_off	bcf	GPIO,Op3 
Up3	
	
	retlw	0			
	
;	org	h'3FF'		;for development with erasable version
;	movlw	h'80'		;cal value for now.
	end
	
