;***********************************************************************************
;
;			SUPERCAR 6
;
;***********************************************************************************
;			Federico Battaglin
;			Copyright @1999
;			www.febat.com
;			info@febat.com
;***********************************************************************************


;****************************************************************************
;			PROCESSOR SELECT
;****************************************************************************
	title "SuperCar 6"


	IFDEF __16F84
		processor pic16f84
		include P16F84.inc
	ENDIF


	IFDEF __16F84A
		processor pic16f84A
		include P16F84A.inc
	ENDIF
	
	__CONFIG _RC_OSC & _PWRTE_OFF & _WDT_ON & _CP_OFF


;****************************************************************************
;			PINOUT
;****************************************************************************
;	RA0:	a4s: abilitazione di lampeggio dopo 4 secondi di frenata.
;	RA1:	sql: abilitazione delle sequenze luminose.
;	RA2:	sqg: abilitazione delle sequenze luminose anche di giorno.
;	RA3:	ingresso luci dei freni.
;	RA4:	ingresso luci di posizione e anabaglianti.

;	RB0:	led 0
;	RB1:	led 1
;	RB2:	led 2
;	RB3:	led 3
;	RB4:	led 4
;	RB5:	led 5
;	RB6:	led 6
;	RB7:	led 7


;****************************************************************************
;			ELEMENTS IN TABLE
;****************************************************************************
;	n°=0:	numero di dati per sequenza
;	n°=1:	dato 1
;	n°=2:	dato 2
;	...	...
;	n°=k:	dato k


;***********************************************************************************
;			REGISTERS
;***********************************************************************************
INDF		equ	0x00
TMR0		equ	0x01
PCL		equ	0x02
STATUS		equ	0x03
FSR		equ	0x04
PORTA		equ	0x05
PORTB		equ	0x06
EEDATA		equ	0x08
EEADR		equ	0x09
PCLATH		equ	0x0a
INTCON		equ	0x0b
OPTIONR		equ	0x81
TRISA		equ	0x85
TRISB		equ	0x86
EECON1		equ	0x88
EECON2		equ	0x89


;***********************************************************************************
;			BITS
;***********************************************************************************
Z		equ	0x02
C		equ	0x00
RP0		equ	0x05
eoc		equ	006h


;***********************************************************************************
;			BYTES
;***********************************************************************************
a4s		equ	B'00000001'
sql		equ	B'00000010'
sqg		equ	B'00000100'
freni		equ	B'00001000'
luci		equ	B'00010000'
in		equ	B'00011111'
mark		equ	0xff
nfr		equ	0x08
t_4s		equ	0x18


;***********************************************************************************
;			VARIABLES
;***********************************************************************************
buff		equ	0x10
dly_0		equ	0x12
dly_1		equ	0x13
dly_4s		equ	0x14
dly_st		equ	0x15
elements	equ	0x16
n_frenate	equ	0x17
out		equ	0x18
pos		equ	0x19
seq		equ	0x1a


;***********************************************************************************
;			MAIN PROGRAM
;***********************************************************************************
main
	org	0x0000

		clrwdt
		bsf	STATUS,RP0
		movlw	B'00000000'
		movwf	TRISB
		bcf	STATUS,RP0
		clrf	PORTB
		clrf	seq

		movlw	nfr
		movwf	n_frenate


;***********************************************************************************
m_0		clrf	PORTB	
		clrf	pos
		clrf	dly_st
		movlw	t_4s
		movwf	dly_4s

m_1		clrwdt
		call	keys
		movf	buff,w
		andlw	freni
		btfss	STATUS,Z
		goto	m_4		;freni=1, Z=0
					;freni=0, Z=1, (freni azionati)
		decfsz	n_frenate,f
		goto	m_2		;n_frenate<>0
					;n_frenate=0
		clrwdt
		movlw	nfr
		movwf	n_frenate

		movf	seq,w
		addlw	0x01
		andlw	B'00000111'
		movwf	seq
		clrf	pos


m_2		clrwdt
		movlw	B'11111111'
		movwf	PORTB

		call	keys
		movf	buff,w
		andlw	a4s
		btfss	STATUS,Z
		goto	m_1		;a4s=off, a4s=1, Z=0
					;a4s=on, a4s=0, Z=1, (abilit. lamp.)
		movf	dly_st,w
		btfss	STATUS,Z
		goto	m_3		;dly_st<>0, Z=0, (luce lamp.)
					;dly_st=0, Z=1, (luce fissa)
		call	dly_aux
		decfsz	dly_4s,f
		goto	m_1

		movlw	0xff
		movwf	dly_st

m_3		clrwdt
		movlw	B'00000000'
		movwf	PORTB
		call	dly_lamp
		movlw	B'11111111'
		movwf	PORTB
		call	dly_lamp
		goto	m_1


;***********************************************************************************
m_4		clrwdt
		call	keys
		movf	buff,w
		andlw	sql		;abilitazione seq. lum.
		btfss	STATUS,Z
		goto	m_0		;sql=Off, sql=1, Z=0
					;sql=On, sql=0, Z=1


		call	keys
		movf	buff,w
		andlw	sqg		;abilitazione seq. lum. di giorno
		btfsc	STATUS,Z
		goto	m_5		;sqg=On, sqg=0, Z=1
					;sqg=Off, sqg=1, Z=0


		call	keys
		movf	buff,w
		andlw	luci
		btfss	STATUS,Z
		goto	m_0		;luci=1, Z=0, (luci spente)
					;luci=0, Z=1, (luci accese)

m_5		call	sequenza
		goto	m_1


;***********************************************************************************
;			SUBRPUTINES
;***********************************************************************************
dly_lamp	clrwdt
		movlw	0x6f
		movwf	dly_0

dl_0		movlw	0xff
		movwf	dly_1

dl_1		clrwdt
		nop
		decfsz	dly_1,f
		goto	dl_1
		decfsz	dly_0,f
		goto	dl_0
		return



;***********************************************************************************
dly_aux		clrwdt
		movlw	0x1f
		movwf	dly_0

da_0		movlw	0xff
		movwf	dly_1

da_1		clrwdt
		nop
		decfsz	dly_1,f
		goto	da_1
		decfsz	dly_0,f
		goto	da_0
		return


;***********************************************************************************
keys		clrwdt
		movf	PORTA,w
		andlw	in
		movwf	buff

		movlw	0x3f
		movwf	dly_0
k_0		clrwdt
		decfsz	dly_0,f
		goto	k_0

		movf	PORTA,w
		andlw	in
		subwf	buff,w
		btfsc	STATUS,Z
		return			;Key pressed, Z=1
					;Key strong, Z=0
		movlw	0xff
		movwf	buff
		return


;***********************************************************************************
sequenza
s_0		clrwdt
		call	table
		movwf	out

		movf	pos,w
		btfss	STATUS,Z
		goto	s_1		;pos<>0, Z=0
					;pos=0, Z=1
		movf	out,w
		movwf	elements
		incf	pos,f
		goto	s_0

s_1		movf	out,w
		movwf	PORTB
		call	dly_lamp

		movf	pos,w
		subwf	elements,w
		btfss	STATUS,Z
		goto	s_2		;pos<>elements, Z=0

		clrf	pos		;pos=elements, Z=1
		return

s_2		incf	pos,f
		return


;***********************************************************************************
;			TABLES
;***********************************************************************************
table		clrwdt
		movf	seq,w
		btfss	STATUS,Z
		goto	sq_1
		
		movf	pos,w
		addwf	PCL,f
sq_0		retlw	0x0e
		retlw	B'00000001'	;1
		retlw	B'00000010'	;2
		retlw	B'00000100'	;3
		retlw	B'00001000'	;4
		retlw	B'00010000'	;5
		retlw	B'00100000'	;6
		retlw	B'01000000'	;7
		retlw	B'10000000'	;8
		retlw	B'01000000'	;9
		retlw	B'00100000'	;a
		retlw	B'00010000'	;b
		retlw	B'00001000'	;c
		retlw	B'00000100'	;d
		retlw	B'00000010'	;e



sq_1		movf	seq,w
		sublw	0x01
		btfss	STATUS,Z
		goto	sq_2

		movf	pos,w
		addwf	PCL,f
		retlw	0x10
		retlw	B'00000000'	;01
		retlw	B'00000001'	;02
		retlw	B'00000011'	;03
		retlw	B'00000111'	;04
		retlw	B'00001111'	;05
		retlw	B'00011111'	;06
		retlw	B'00111111'	;07
		retlw	B'01111111'	;08
		retlw	B'11111111'	;09
		retlw	B'01111111'	;0a
		retlw	B'00111111'	;0b
		retlw	B'00011111'	;0c
		retlw	B'00001111'	;0d
		retlw	B'00000111'	;0e
		retlw	B'00000011'	;0f
		retlw	B'00000001'	;10



sq_2		movf	seq,w
		sublw	0x02
		btfss	STATUS,Z
		goto	sq_3
		
		movf	pos,w
		addwf	PCL,f
		retlw	0x1e
		retlw	B'00000000'	;01
		retlw	B'00000001'	;02
		retlw	B'00000011'	;03
		retlw	B'00000111'	;04
		retlw	B'00001111'	;05
		retlw	B'00011111'	;06
		retlw	B'00111111'	;07
		retlw	B'01111111'	;08
		retlw	B'11111111'	;09
		retlw	B'11111110'	;0a
		retlw	B'11111100'	;0b
		retlw	B'11111000'	;0c
		retlw	B'11110000'	;0d
		retlw	B'11100000'	;0e
		retlw	B'11000000'	;0f
		retlw	B'10000000'	;10
		retlw	B'11000000'	;11
		retlw	B'11100000'	;12
		retlw	B'11110000'	;13
		retlw	B'11111000'	;14
		retlw	B'11111100'	;15
		retlw	B'11111110'	;16
		retlw	B'11111111'	;17
		retlw	B'01111111'	;18
		retlw	B'00111111'	;19
		retlw	B'00011111'	;1a
		retlw	B'00001111'	;1b
		retlw	B'00000111'	;1c
		retlw	B'00000011'	;1d
		retlw	B'00000001'	;1e



sq_3		movf	seq,w
		sublw	0x03
		btfss	STATUS,Z
		goto	sq_4

		movf	pos,w
		addwf	PCL,f
		retlw	0x16
		retlw	B'00000000'	;1
		retlw	B'00000001'	;2
		retlw	B'00000010'	;3
		retlw	B'00000101'	;4
		retlw	B'00001010'	;5
		retlw	B'00010100'	;6
		retlw	B'00101000'	;7
		retlw	B'01010000'	;8
		retlw	B'10100000'	;9
		retlw	B'01000000'	;a
		retlw	B'10000000'	;b
		retlw	B'00000000'	;c
		retlw	B'10000000'	;d
		retlw	B'01000000'	;e
		retlw	B'10100000'	;f
		retlw	B'01010000'	;10
		retlw	B'00101000'	;11
		retlw	B'00010100'	;12
		retlw	B'00001010'	;13
		retlw	B'00000101'	;14
		retlw	B'00000010'	;15
		retlw	B'00000001'	;16



sq_4		movf	seq,w
		sublw	0x04
		btfss	STATUS,Z
		goto	sq_5

		movf	pos,w
		addwf	PCL,f
		retlw	0x0e
		retlw	B'00000001'	;1
		retlw	B'10000000'	;2
		retlw	B'00000010'	;3
		retlw	B'01000000'	;4
		retlw	B'00000100'	;5
		retlw	B'00100000'	;6
		retlw	B'00001000'	;7
		retlw	B'00010000'	;8
		retlw	B'00001000'	;9
		retlw	B'00100000'	;a
		retlw	B'00000100'	;b
		retlw	B'01000000'	;c
		retlw	B'00000010'	;d
		retlw	B'10000000'	;e



sq_5		movf	seq,w
		sublw	0x05
		btfss	STATUS,Z
		goto	sq_6

		movf	pos,w
		addwf	PCL,f
		retlw	0x10
		retlw	B'00001001'	;01
		retlw	B'00000110'	;02
		retlw	B'00010010'	;03
		retlw	B'00001100'	;04
		retlw	B'00100100'	;05
		retlw	B'00011000'	;06
		retlw	B'01001000'	;07
		retlw	B'00110000'	;08
		retlw	B'10010000'	;09
		retlw	B'01100000'	;0a
		retlw	B'01001000'	;0b
		retlw	B'00011000'	;0c
		retlw	B'00100100'	;0d
		retlw	B'00001100'	;0e
		retlw	B'00010010'	;0f
		retlw	B'00000110'	;10




sq_6		movf	seq,w
		sublw	0x06
		btfss	STATUS,Z
		goto	sq_7

		movf	pos,w
		addwf	PCL,f
		retlw	0x0c
		retlw	B'00000000'	;1
		retlw	B'11111111'	;2
		retlw	B'00000000'	;3
		retlw	B'11100111'	;4
		retlw	B'00000000'	;5
		retlw	B'11000011'	;6
		retlw	B'00000000'	;7
		retlw	B'10000001'	;8
		retlw	B'00000000'	;9
		retlw	B'11000011'	;a
		retlw	B'00000000'	;b
		retlw	B'11100111'	;c



sq_7		movf	pos,w
		addwf	PCL,f
		retlw	0x0c
		retlw	B'00000000'	;1
		retlw	B'00000011'	;2
		retlw	B'00000000'	;3
		retlw	B'00001100'	;4
		retlw	B'00000000'	;5
		retlw	B'00110000'	;6
		retlw	B'00000000'	;7
		retlw	B'11000000'	;8
		retlw	B'00000000'	;9
		retlw	B'00110000'	;a
		retlw	B'00000000'	;b
		retlw	B'00001100'	;c


;***********************************************************************************
;			PROGRAM END
;***********************************************************************************

	End

