title "Beomaster daughter board"
	LIST	P=16F688, F=INHX8M
#include 
	__CONFIG  _INTOSCIO & _WDT_OFF

;Declare variables
volumedata equ 0x20 ;Stores the value of the Beomaster 8000's volume control lines
comparison equ 0x21 ;Stores two bits which are used as a conditional AND
                    ;statement to determine if two IF statements are BOTH true.

	org 0x00
	;global interrupt enable - BCF INTCON, GIE (disabled by default at power up)
	
	;Enable internal low frequency oscillator
	;****************************************
	;Bit# Name  Description
	;---- ----  -----------
	; 7         Unimplemented (read as 0)
	; 6   IRCF2 Internal Oscillator Frequency Select bits:
	; 5   IRCF1 111=8MHz  110=4MHz(default) 101=2MHz 
	; 4   IRCF0 100=1MHz  011=500kHz  010=250kHz  001=125kHz  000=31kHz(LFINTOSC)
	; 3   OSTS  Oscillator Startup Timeout Status: 1=external clock   0=internal oscillator
	; 2   HTS   HFINTOSC stable bit: 1=stable  0=not stable
	; 1   LTS   LFINTOSC stable bit: 1=stable  0=not stable
	; 0   SCS   System Clock Select bit: 1=internal  0=external
	MOVLW B'00000111' ;Internal clock, 31kHz, stable
	BANKSEL OSCCON
	MOVWF OSCCON
	
	;PORTC (used as input) is used to read the volume level set by the Beomaster 8000
	;computer. At power up, TRISC defaults to all inputs, so there's no need to set it
	;*********************************************************************************
	BANKSEL PORTC
	CLRF PORTC
	BANKSEL CMCON0
	MOVLW B'00000111'
	MOVWF CMCON0      ;Configure PORTC pins as digital I/O (turn off comparators)
	BANKSEL ANSEL
	CLRF ANSEL        ;Disable analog inputs on PORTC and PORTA
	
	;PORTA (used as output) (RA2, RA4 & RA5) are used to control the CMOS analog switch
	;**********************************************************************************
	BANKSEL PORTA
	CLRF PORTA        ;Initialize PORTA
	BANKSEL TRISA
	MOVLW B'00001011' ;Configure RA2, RA4 & RA5 as outputs, the rest as inputs (the inputs aren't used)
	MOVWF TRISA
	
	CLRW
	MOVWF PORTA       ;Turn all analog switches ON (Effectively turning loudness OFF)
	
	BANKSEL PORTC

WaitForNothing
	;Read the data at PORT C and rearrange the bits to
	;match what the Beomaster volume data lines are saying
	;*****************************************************
	MOVLW B'00000000'
	MOVWF volumedata
	BTFSC PORTC, 3
	BSF volumedata, 0
	BTFSC PORTC, 0
	BSF volumedata, 1
	BTFSC PORTC, 1
	BSF volumedata, 2
	BTFSC PORTC, 2
	BSF volumedata, 3
	BTFSC PORTC, 5
	BSF volumedata, 4
	BTFSC PORTC, 4
	BSF volumedata, 5

	;If volumedata is <= 00011111 then set all switches open
	;*******************************************************
	MOVFW volumedata
	SUBLW B'00011111'
	BTFSC STATUS, 0
	CALL AllOpen

	;If volumedata is between 00100000 and 00100111 (inclusive) then set S1 closed
	;*****************************************************************************
	MOVLW B'00000000'
	MOVWF comparison
	;If volumedata is > 00011111
	MOVFW volumedata
	SUBLW B'00011111'
	BTFSS STATUS, 0
	BSF comparison,0
	;If volumedata is <= 00100111
	MOVFW volumedata
	SUBLW B'00100111'
	BTFSC STATUS, 0
	BSF comparison,1
	;If BOTH checks were true then set S1 closed
	MOVLW B'00000011'
	XORWF comparison, 0
	BTFSC STATUS, 2
	CALL S1Closed

	;If volumedata is between 00101000 and 00111011 (inclusive) then set S2 closed
	;*****************************************************************************
	MOVLW B'00000000'
	MOVWF comparison
	;If volumedata is > 00100111
	MOVFW volumedata
	SUBLW B'00100111'
	BTFSS STATUS, 0
	BSF comparison,0
	;If volumedata is <= 00111011
	MOVFW volumedata
	SUBLW B'00111011'
	BTFSC STATUS, 0
	BSF comparison,1
	;If BOTH checks were true then set S2 closed
	MOVLW B'00000011'
	XORWF comparison, 0
	BTFSC STATUS, 2
	CALL S2Closed

	;If volumedata is > 00111011 then set S3 closed
	;**********************************************
	MOVFW volumedata
	SUBLW B'00111011'
	BTFSS STATUS, 0
	CALL S3Closed

	GOTO WaitForNothing ;Go back to the beginning and read PORTC again to see if it has changed
	
;Subroutine to open all switches
;*******************************
AllOpen
	BSF PORTA, 2
	BSF PORTA, 5
	BSF PORTA, 4
	RETURN

;Subroutine to close S1 and open the others
;******************************************
S1Closed
	BCF PORTA, 2
	BSF PORTA, 5
	BSF PORTA, 4
	RETURN

;Subroutine to close S2 and open the others
;******************************************
S2Closed
	BSF PORTA, 2
	BCF PORTA, 5
	BSF PORTA, 4
	RETURN

;Subroutine to close S3 and open the others (mutes the loudness
;circuit when the volume control is set to maximum attenuation).
;***************************************************************
S3Closed
	BSF PORTA, 2
	BSF PORTA, 5
	BCF PORTA, 4
	RETURN
	
	END