;	alarm_4Sounds.asm	for PIC12F629   29-1-2010
;
;This program has goto mode 2,3 changed to produces SPACE GUN with A0 and A1 HIGH 
;quiescent current = 100microamps
;
;       --+------------ +5v     
;		  |                  |  |
;    +--- | -----------------|[]|----+
;    |    |Vdd ---v---       |  |    |
;    |    +---|1   Gnd|      piezo   |
;    |        |       |              |
;    +--------|GP5 GP0|--------------+
;             |       |                    
;  Enable-----|GP4 GP1|----A0
;             |       |     
;        +----|GP3 GP2|----A1
;        |     -------     
;        |    PIC12F629        
;        |
;        |
;    ----+---------- 0v
;


	radix	dec
	include	"p12f629.inc"
	
		errorlevel	-302	; Don't complain about BANK 1 Registers during assembly

	__CONFIG	_MCLRE_OFF & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT  ;Internal osc.



STATUS	equ		3		
PC		equ		2			
#define	CARRY	STATUS,0				
#define	ZERO	STATUS,2				
#define	outa	GPIO,0	;			
#define	modea	GPIO,1	;			
#define	modeb	GPIO,2	;			
#define	enbf	GPIO,3	;			
#define	enbt	GPIO,4	;		
#define	outb	GPIO,5	;		
flags	equ		20h	 ;flags file	
temp	equ		21h	
delA	equ		22h
delB	equ		23h

dwell	equ		25h	; cycles
count	equ		26h	; delay
steps	equ		27h	; dwell
cnth	equ		28h		
cntl	equ		29h		
		
		
		org	0	
		goto	setup
		nop
		nop
		nop
		nop
		goto	Enable
		

SetUp	movlw	b'00001111'	;0001 1111			
		movwf	GPIO		;Make GP 0 HIGH and GP5 LOW for piezo & GP4 low
		bsf		status,rp0 	;Bank 1	
		movlw	b'00011110'
		movwf   trisio		;Make GP1,2,3,4 input GP0,5 output
		movlw	b'10000110'	;Turn off T0CKI, prescaler for TMR0 = 1:128
		movwf	option_reg  
		bsf		intcon,3	;enable GPIO state change int
		bsf		intcon,7	;enable global interrupt  GIE
		bsf		IOC,4		;enables interrupt on GPIO 4 xxxxx 
		bcf		status,rp0	;bank 0  
		movlw   07h         ;Set up W to turn off Comparator ports
		movwf   CMCON       ;must be placed in bank 0   
		clrf	delA
		movlw	.140		;for 3 minutes
		movwf	delB
			
		
					;look for enable HIGH
					
		goto	$+1
		goto	$+1
		decfsz	temp,1
		goto	$-3
		movf	GPIO,w	;clears the Int-on-change flag
		bsf		status,rp0 	;Bank 1	
		bcf		intcon,gpif
		bcf		status,rp0	;bank 0 
		nop
		sleep   
		nop		;micro goes to 	ISR at address 04!!!!!!!!!
Enable	call	_3min
		btfsc	GPIO,1	;Test A0
		goto	$+4		;A0=High
		btfsc	GPIO,2	;A0=Low  Test A1
		goto	mode3	;A0=Low  A1=high
		goto	mode0	;A0=Low  A1=low
		btfsc	GPIO,2	;A0=High  Test A1
		goto	mode2	;A0=High  A1=high   This program produces SPACE GUN
		goto	mode1	;A0=High  A1=low            with A0 and A1 HIGH
		
		
		;mode	0	CONSTANT 2.2kHz tone   A0=Low  A1=low
	
mode0	movlw	.20	; set frequency 
		movwf	count
		call	toggle		
		btfsc	GPIO,4 		;test GPIO,4 - High Input enables chip to produce tone
		goto	$-4
		goto	Enable
		
		
		;mode 1   CHIRP	  A0=High  A1=low
							
mode1		
		movlw	35		;set frequency delay	
		movwf	count	;put 35 into Count file				
		movlw	1	 		
		movwf	cnth	;put 1 in counter HIGH file					
		clrf	cntl	;clear counter LOW file					
mode1a	call	toggle					
		decfsz	cntl,1	;decrement and test counters	
		goto	mode1a						
		decfsz	cnth,1						
		goto	mode1a						
		movlw	2		;set pause delay to counters
		movwf	cnth		
		clrf	cntl		
mode1b	nop							
		nop							
		nop					
		decfsz	cntl,1	;decrement and test counters	
		goto	mode1b						
		decfsz	cnth,1						
		goto	mode1b						
		movlw	1		;init second pulsed delay to counters
		movwf	cnth					
		clrf	cntl						
mode1c	
		call	toggle	
		decfsz	cntl,1	;decrement and test counters
		goto	mode1c					
		decfsz	cnth,1					
		goto	mode1c
		btfsc	GPIO,4 		;test GPIO,4 - High Input enables chip to produce tone
		goto	mode1
		goto	Enable					
						
		
			
			;SPACE GUN	A0=Low  A1=high
					
mode2						
		movlw	7			;number of steps
		movwf	steps			
mode2a								
		movlw	5			;dwell		
		movwf	dwell								
		call	toggle
		decfsz	dwell,1		; test if dwell = 0
		goto	$-2									
		movlw	5				
		subwf	count,1	   	;increment frequency			
		btfss	GPIO,4 		;test GPIO,4 - High Input enables chip to produce tone		
		goto	Enable		;see if Enable is still HIGH and loop again
		decfsz	steps,1	    ;test if more steps	
		goto	mode2a									
		goto	mode2
		
			
		
mode3      ;mode 3 SIREN   A0=High  A1=high

 		movlw	37		;set frequency 	
		movwf	count						
		movlw	6		; set number of steps
		movwf	steps						
mode3a	movlw	0x20	; 0010 0000 set dwell 
		movwf	dwell					
mode3b	call	toggle						
		decfsz	dwell,1		; test if any more cycles	
		goto	mode3b					
		movlw	5			; step frequency delay		
		subwf	count,1					
		decfsz	steps,1		; test if last step in progress
		goto	mode3a	;
		btfsc	GPIO,4 		;test GPIO,4 - High Input enables chip to produce tone
		goto	mode3
		goto	Enable
				
							
toggle	movf	count,0
		movwf	temp
		goto	$+1
		goto	$+1
		decfsz	temp,1
		goto	$-3
		movlw	b'00100001'
		xorwf	GPIO,1		;toggle bits 0 & 5	
		retlw	00
		
_3min	decfsz	delA
		retlw	00
		decfsz	delB
		retlw	00
		goto	SetUp
		
	

		end				

		

