;	Alarm4Zone.asm	for PIC12F629   6-1-2014
;
; 

	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	


	;Files:
	
flags	equ		20h	 ;flags file	
tempA	equ		21h	
delA	equ		22h
delB	equ		23h
delC	equ		24h
dwell   equ     25h
steps   equ     26h
count   equ     27h
temp1	equ		28h
loops	equ     29h
store	equ     2Ah  ;stores triggered input No
loop30  equ     2Bh  ;loops for 5 minute siren			
		
		org	0	
		goto	SetUp
		nop
		

SetUp	
		movlw   07h           ;Set up W to turn off Comparator ports
		movwf   CMCON         ;must be placed in bank 0 
		goto	Main	  
	


;**************************
;* Delays 			*
;**************************

			
			
		
_1mS	movlw   .40      ;reduced to 40/255 of 1mS
        movwf   delA
		decfsz 	delA,f
		goto 	$-1			
		retlw 	00	


		
		
		;240mS delay
		
		
_240mS	nop
		decfsz 	delA,1
		goto 	$-2
		decfsz 	delB,1
		goto 	$-4		
		retlw 	00	
		
		
_500mS	goto	$+1
		goto	$+1
		decfsz 	delA,1
		goto 	$-3
		decfsz 	delB,1
		goto 	$-5		
		retlw 	00	

		;100mS delay
		
		
			

;****************************
;* Sub-routines		*
;****************************
		
			;beep-beep-beep to siren for soft start 
			
beep			
		movlw	.50
		movwf	tempA
		bsf		gpio,5
		movlw	.40
		movwf	delA
		nop
		decfsz	delA,f
		goto	$-2
		bcf		gpio,5
		movlw	.240
		movwf	delA
		Goto	$+1
		decfsz	delA,f
		goto	$-2	
		decfsz	tempA,f
		goto	$-13
		call	_240mS		
		retlw	00		
		
		;Charge 22n
		
Charge	bsf		status,rp0 	  ;Bank 1	
		movlw	b'00101111'
		movwf   trisio		  ;Make GP0,1,2,3,5 input   GP4 output      
		bcf		status,rp0	  ;bank 0 
		bsf		gpio,4
		call	_1mS          ;charge 22n
		bsf		status,rp0 	  ;Bank 1	
		movlw	b'00111111'
		movwf   trisio		  ;Make GP0,1,2,3,5 input   GP4 input      
		bcf		status,rp0	  ;bank 0 
		bcf		gpio,4
		call	_1mS          ;time for swA to discharge 22n     
		retlw   00	
			
			
			;Indicate
			;flash 1 time for constant wailing or 4 times for 5 minutes wailing	
			
Indicate

		bsf		status,rp0 	  ;Bank 1	
		movlw	b'00111001'
		movwf   trisio		  ;Make GP1,2 output      
		bcf		status,rp0	  ;bank 0 
		clrf    gpio		 ;make outputs low to turn off LEDs, 
		call 	read 		  ;read 0 or 1 from EEPROM location 1
		btfss   temp1,0
		goto    Ind_1        
		bsf		gpio,2       ;temp1 = 1   = contant siren
		call	_500mS
		bcf		gpio,2
		call	_500mS			
		retlw	00	
		
Ind_1	bsf		gpio,1       ;temp1 = 0 = 5 minute siren
		call	_500mS
		bcf		gpio,1
		call	_500mS
		bsf		gpio,1
		call	_500mS
		bcf		gpio,1
		call	_500mS
		bsf		gpio,1
		call	_500mS
		bcf		gpio,1
		call	_500mS
		bsf		gpio,1
		call	_500mS
		bcf		gpio,1
		call	_500mS		
		retlw	00
		
		
		;read takes the value 0 or 1 from EEPROM location 0 and puts it in temp1
		
read		
		movlw	.0
		bsf		status,rp0			
		movwf	EEADR									
		bsf		EECON1,0	;starts EEPROM read operation storing result in EEDATA						
		movf	EEDATA,w	;move read data into w
		bcf		status,rp0				
		movwf	temp1		;temp1 has the value 0 or 1 to flash a LED
		retlw	00
			
			
			;SIREN - WAILING SIREN			
						
Siren	movlw	7			;number of steps
		movwf	steps			
								
Siren1	movlw	5			;dwell		
		movwf	dwell			
		bsf     gpio,5
		movf	count,0
		movwf	tempA
		decfsz	tempA,1
		goto	$-1		
		bcf     gpio,5
		movf	count,0
		movwf	tempA
		goto	$+1
		goto	$+1
		goto	$+1
		decfsz	tempA,1
		goto	$-4		
		decfsz	dwell,1		; 
		goto	$-14									
		movlw	5				
		subwf	count,1	   	;			
		decfsz	steps,1	    ;	
		goto	Siren1									
		retlw   00
		
		
		
		;Siren Beeps for 10 seconds
		
SirenBeep		
		bsf		status,rp0 	  ;Bank 1	
		movlw	b'00011111'
		movwf   trisio		  ;Make GP0,1,2,3,4 input   GP5 output      
		bcf		status,rp0	  ;bank 0 
		movlw	.50           ;number of loops (cycles)
		movwf	tempA
SirenBeep1	
		bsf		gpio,5        ;siren BD679 HIGH 
		movlw	.40
		movwf	delA
		nop
		decfsz	delA,f
		goto	$-2
		bcf		gpio,5        ;siren BD679 LOW for: 255 delA
		nop
		decfsz	delA,f
		goto	$-2			
		decfsz	tempA,f
		goto	SirenBeep1 
		call	_240mS		 ;silence			
		retlw	00
			
				
		;exit tone to piezo buzzer 
tone						;524Hz for 250mS
		bsf		status,rp0 	  ;Bank 1	
		movlw	b'00101111'
		movwf   trisio		  ;Make GP0,1,2,3,5 input   GP4 output      
		bcf		status,rp0	  ;bank 0 
		movlw	.50           ;number of loops (cycles)
		movwf	tempA
tone1	bsf		gpio,4        ;buzzer BD679 HIGH 
		movlw	.40
		movwf	delA
		nop
		decfsz	delA,f
		goto	$-2
		bcf		gpio,4        ;buzzer BD679 LOW for: 255 delA
		nop
		decfsz	delA,f
		goto	$-2			
		decfsz	tempA,f
		goto	tone1 
		call	_240mS		 ;silence			
		retlw	00
		
		
			
		
write	
		bsf		status,rp0	;select bank1	
		movwf	EEDATA						
		bcf		status,rp0	;select bank0	
		movlw	.0
		bsf		status,rp0	;select bank1	
		movwf	EEADR			
		bsf		eecon1,wren	;enable write		
		movlw	55h 		;unlock codes
		movwf	eecon2
		movlw	0aah
		movwf	eecon2
		bsf		eecon1,wr	;write begins
		bcf		status,rp0	;select bank0		
writeA	btfss	pir1,eeif	;wait for write to complete
		goto	writeA
		bcf		pir1,eeif
		bsf		status,rp0	;select bank1
		bcf		eecon1,wren	;disable other writes
		bcf		status,rp0	;select bank0					
		retlw	00			
				
		
		

;****************************
;* Main
;****************************

	
Main	;45 second exit with button A detect 
		;exit beep produced

		clrf	store	;stores triggered input No
		movlw   .180       ; 180 x 250mS = 45 seconds       
		movwf   loops
		call	Indicate   ;flash LED 4 times	
		
		
M1		call    Charge				
		btfsc	gpio,4		;is button A pressed?
		goto	M2          ;not pressed
		call 	read		;0 or 1 will be in temp1
		btfss	temp1,0
		goto	$+4
		clrf	w
		call	write
		goto    M2		
		movlw	1
		call	write
				
M2		call	tone    ;call exit tone	        
        decfsz	loops,1
		goto	$-2
		
		
		
		;monitors 4 inputs 
		
Monitor	bsf		status,rp0	;select bank1
		movlw	b'00101111'
		movwf   trisio		  ;Make GP0,1,2,3,5 input   GP4 output 
		bcf     81h,7         ;turn on pulls-ups in option_reg
		movlw	b'00000111'
		movwf   095h          ;WPU register for GP0,1,2   
		bcf		status,rp0	  ;bank 0  
		btfsc   gpio,0
		goto	Mon1
		btfsc   gpio,1
		goto	Mon1
		btfsc   gpio,2
		goto	Mon1
		btfsc   gpio,3
		goto	Mon1
		goto    Monitor
		
Mon1	bsf		status,rp0	;select bank1
		bcf     trisio,5	;Make GP5 output 
		bcf		status,rp0	  ;bank 0 
		
		
		;soft-start siren with beeps for 10 seconds
		
		
		movlw  .40
		movwf  loops
		call   SirenBeep			        
        decfsz	loops,1
		goto	$-2
		
		;constant siren if temp1,0 = 1
		;5 minute siren if temp1,0 = 0
		
		btfss   temp1,0
		goto    Siren5min 		 
		call	Siren      ;constant siren		
		goto    $-1


Siren5min
       	
       	movlw   .30      ;30 loops
       	movwf   loop30       	
       	movlw  .185      ;inner loop = 10 seconds
		movwf  loops
		call   Siren			        
        decfsz	loops,1
		goto	$-2	
		decfsz	loop30,1
		goto	$-6			
		goto    $
		

			
;************************************
;*EEPROM     						*
;************************************
								
		org		2100h			
		
		de		00h			
							
		END
		
		
		
		
		
		
		
		
		
					

		

