;******************************************************************************
;
;			VU-METER a 4 LED
;
;			Federico Battaglin
;
;			Copyright @ 19 September 2001
;
;			www.febat.com  -  info@febat.com
;
;******************************************************************************

	title "Vu-mtr_4"
	processor pic12c672

;******************************************************************************
;       Verify Processor
;******************************************************************************
        IFNDEF __12C672
           MESSG "Processor-header file mismatch.  Verify selected processor."
        ENDIF

;******************************************************************************
;       Register Definitions
;******************************************************************************
w			equ     0x0000
f			equ     0x0001

;----- Register Files------------------------------------------------------
INDF			equ     0x0000
TMR0			equ     0x0001
PCL			equ     0x0002
STATUS			equ     0x0003
FSR			equ     0x0004
GPIO			equ     0x0005
PCLATH			equ     0x000A
INTCON			equ     0x000B
PIR1			equ     0x000C
ADRES			equ     0x001E
ADCON0			equ     0x001F

OPTION_REG		equ     0x0081
TRISIO			equ     0x0085
PIE1			equ     0x008C
PCON			equ     0x008E
OSCCAL			equ     0x008F
ADCON1			equ     0x009F

;----- STATUS Bits --------------------------------------------------------
IRP			equ     0x0007
RP1			equ     0x0006
RP0			equ     0x0005
NOT_TO			equ     0x0004
NOT_PD			equ     0x0003
Z			equ     0x0002
DC			equ     0x0001
C			equ     0x0000

;----- ADCON0 Bits --------------------------------------------------------
ADCS1			equ     0x0007
ADCS0			equ     0x0006
CHS1			equ     0x0004
CHS0			equ     0x0003
GO			equ     0x0002
NOT_DONE		equ     0x0002
GO_DONE			equ     0x0002
ADON			equ     0x0000

;----- INTCON Bits --------------------------------------------------------
GIE			equ     0x0007
PEIE			equ     0x0006
T0IE			equ     0x0005
INTE			equ     0x0004
GPIE			equ     0x0003
T0IF			equ     0x0002
INTF			equ     0x0001
GPIF			equ     0x0000

;----- PIR1 Bits ----------------------------------------------------------
ADIF			equ     0x0006

;----- OPTION Bits --------------------------------------------------------
NOT_GPPU		equ     0x0007
INTEDG			equ     0x0006
T0CS			equ     0x0005
T0SE			equ     0x0004
PSA			equ     0x0003
PS2			equ     0x0002
PS1			equ     0x0001
PS0			equ     0x0000

;----- PIE1 Bits ----------------------------------------------------------
ADIE			equ     0x0006

;----- PCON Bits ----------------------------------------------------------
NOT_POR			equ     0x0001

;----- OSCCAL Bits --------------------------------------------------------
CAL3			equ     0x0007
CAL2			equ     0x0006
CAL1			equ     0x0005
CAL0			equ     0x0004
CALFST			equ     0x0003
CALSLW			equ     0x0002

;----- ADCON1 Bits --------------------------------------------------------
PCFG2			equ     0x0002
PCFG1			equ     0x0001
PCFG0			equ	0x0000

;******************************************************************************
;       RAM Definition
;******************************************************************************
        __MAXRAM 0xFF
        __BADRAM 0x06-0x09, 0x0D-0x1D
        __BADRAM 0x86-0x89, 0x8D, 0x90-0x9E, 0xC0-0xEF

;******************************************************************************
;		Configuration Bits
;******************************************************************************
_MCLRE_ON		equ     0x3FFF
_MCLRE_OFF		equ     0x3F7F
_CP_ALL			equ     0x009F
_CP_75			equ	0x15BF
_CP_50			equ	0x2ADF
_CP_OFF			equ	0x3FFF
_PWRTE_OFF		equ	0x3FFF
_PWRTE_ON		equ	0x3FEF
_WDT_ON			equ	0x3FFF
_WDT_OFF		equ	0x3FF7
_LP_OSC			equ	0x3FF8
_XT_OSC			equ	0x3FF9
_HS_OSC			equ	0x3FFA
_INTRC_OSC		equ	0x3FFC
_INTRC_OSC_NOCLKOUT	equ	0x3FFC
_INTRC_OSC_CLKOUT	equ	0x3FFD
_EXTRC_OSC		equ	0x3FFE
_EXTRC_OSC_NOCLKOUT	equ	0x3FFE
_EXTRC_OSC_CLKOUT	equ	0x3FFF

	__CONFIG _INTRC_OSC & _PWRTE_OFF & _WDT_ON & _CP_OFF & _MCLRE_OFF

;******************************************************************************
;			VARIABLES
;******************************************************************************
AUX_DATA	equ	0x0020
AUX_GPIO	equ	0x0021

;******************************************************************************
;			CONSTANTS
;******************************************************************************
LED_0		equ	0x02	;0b 0000 0010
LED_1		equ	0x04	;0b 0000 0100
LED_2		equ	0x10	;0b 0001 0000
LED_3		equ	0x20	;0b 0010 0000
nLEDS		equ	0xc9	;0b 1100 1001

;******************************************************************************
;			SCHEMATICS
;******************************************************************************
;	GP0=	adc input
;	GP1=	LED 0
;	GP2=	LED 1
;	GP3=	nc
;	GP4=	LED 2
;	GP5=	LED 3
;******************************************************************************
;			MAIN PROGRAM
;******************************************************************************
	org	0x0000

	clrwdt
	bcf	STATUS,RP0
	clrf	GPIO
	clrf	AUX_GPIO

	bsf	STATUS,RP0
	bcf	OPTION_REG,T0CS
	movlw	nLEDS
	movwf	TRISIO
	movlw	0x06
	movwf	ADCON1

	bcf	STATUS,RP0
	movlw	0x01
	movwf	ADCON0
	nop
	nop


vm_0	clrwdt
	bsf	ADCON0,GO


vm_1	clrwdt
	btfsc	ADCON0,NOT_DONE
	goto	vm_1

;******************************************************************************
	clrwdt
	movf	ADRES,w
	movwf	AUX_DATA
	swapf	AUX_DATA,f
	rrf	AUX_DATA,f
	rrf	AUX_DATA,w
	andlw	0x03

	call	table

	movwf	AUX_DATA
	movf	AUX_GPIO,w
	andlw	nLEDS
	iorwf	AUX_DATA,w
	movwf	AUX_GPIO
	movwf	GPIO
	nop
	nop
	goto	vm_0

;******************************************************************************
table
	addwf	PCL,f
	retlw	LED_0
	retlw	LED_1
	retlw	LED_2
	retlw	LED_3

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

	End

