;************************************************************************
;			File: MAIN.ASM
;************************************************************************

; Interrupt routine does all timer work


MAIN				;Start in Main menu state
	BCF	PORTA,BEEPER	;Turn piezo OFF


	BTFSC	SEMAPHORE,2	;Check SLEEP has been requested
	GOTO	GO2SLEEP


	;The interrupt routine sets the SEMAPHORE to indidicate beeping required
	;This part of the code produces the frequency of the beep.
	;All the brains of the code are in the interrupt routine.
TONE
	MOVLW	3		;check bit 1:0 of SEMAPHORE
	ANDWF	SEMAPHORE,w
	BTFSC	STATUS,Z	;Check if Z=false either bit is true
	GOTO	MAIN		;no beeping required

				;Put extra delays here for other frequencies

	MOVLW	BEEPER
	XORWF	PORTA,f		;Toggle voltage to piezo
	
	GOTO	TONE


GO2SLEEP

	;disable interrupts

	MOVLW	b'00000000'	;Interrupt control register loading
				;bit 7  GIE=0 Global Interrupts disabled
				;bit 6 PEIE=0 Peripheral intrpt disabled
				;bit 5 T0IE=0 TMR0 interrupt disabled
				;bit 4 INTE=0 RB0/INT Ext intrpt disabled
				;bit 3 RBIE=0 RB port change disabled
				;bit 2 T0IF=flag
				;bit 1 INTF=flag
				;bit 0 RBIF=flag
	MOVWF	INTCON

	CLRF	PORTA		;Turn off all outputs
	CLRF	PORTB

	SLEEP
	NOP

	
;************************************************************************


