;#########################
;## PROC DEFINITIONS	##
;#########################
;   Set User ID Memory
	__idlocs		0x55aa
;   Set configuration bits using definitions from the include file, p16f84.inc
	__config	_INTRC_OSC_NOCLKOUT & _WDT_OFF & _CP_OFF & _MCLRE_OFF
	
list p=12f675

; Include file, change directory if needed
include "p12f675.inc"

;#########################
;## 	PIN DEFINITIONS	##
;#########################

	cblock    0x20
	endc

;PRO zpozdeni 0,5s
ZP0051	EQU	0x21
ZP0052	EQU	0x22
ZP0053	EQU	0x23
;PRO zpozdeni 1ms
ZP01	EQU	0x24
ZP02	EQU	0x25
ZP03	EQU	0x26

poc15		EQU	0x27
pocitadlo1	EQU	0x28
pocitadlo2	EQU	0x29
pocitadlo3	EQU	0x30
pocitadlo4	EQU	0x31
pocitadlo5 	EQU 0x32

VOLTY_hi	EQU	0x33	;.522    2.55 V
;VOLTY_lo	EQU	0x34	;.491    2.39602 V
;#########################
;## 		MACROS		##
;#########################

#define		PAGE0		bcf	STATUS,RP0	; [M] Switch to page0 in RAM
#define 	PAGE1		bsf	STATUS,RP0	; [M] Switch to page1 in RAM

#define 	PULSE_H		bsf	GPIO,PULSE	; [M] GP0 _/~
#define 	PULSE_L		bcf	GPIO,PULSE	; [M] GP0 ~\_

;vse pro AD prevodnik
#define		VOLTY_MAX	.522    ;2.55 V
;#define		VOLTY_MIN	.491    ;2.39602 V



;###############################################
;###############################################
;####			S	T	A	R	T			####
;###############################################
;###############################################
;Inicializace zakladnich registru
Reset_vector	
	org		0x0000
	goto	Init
	
Irq_vector 
	org		0x0004
	retfie

;--------------------------------------
Init
	PAGE1
	call	0x3ff			; Set page 0
	movwf	OSCCAL			; Read osc calibration value and set it 

;vse pro AD

   	movlw	b'10001'        ;AN0 as analog input,conversion clock Fosc/8
   	movwf	ANSEL

	PAGE0
   	movlw	b'00000001'		;ADC on, right justified ;razeni zleva
   	movwf	ADCON0

	clrwdt					; Reset watchdog
	movlw	.7				;comparators off
	movwf	CMCON

;#########################
;## 	set GPIO		##
;#########################
	PAGE1
	movlw	b'111001'		; 1 vstup, 		
	movwf	TRISIO			; Set direction


;#########################
;## 	set TIMER O		##
;#########################
	movlw	b'10000111'		; Divider 1:256
	movwf	OPTION_REG;
;	movlw	b'110000'
;	movwf	WPU


;#########################
;## 	MAIN LOOP		##
;#########################

Unarmed:	
	PAGE0				;stav kdy jsme doma a neni zamceno
	bcf		GPIO,GP1		;led ARMED nesviti
	call 	Zpoz01
	btfss 	GPIO,GP3	;testuju signal z klavesnice
	b		Armed			;je 1 je odemceno , 0 je zamceno
	b		Unarmed		

Armed:					;ochazim a zamykam, mam 15s na to abych vypadl z domu
	movlw	.15
	movwf	poc15
A_smycka:
	bsf		GPIO,GP1		;roznu diodu
	call	Zpoz05
	bcf		GPIO,GP1		;zhasnu diodu
	call	Zpoz05
	btfsc	GPIO,GP3	;pri zamykani jsem jste neco zapomnel a odemkl
	b		Unarmed
	decfsz	poc15
	b		A_smycka
	b		Armed2			;jen tak pro poradek :)

Armed2:					;barak je zabezpecenej nikde nikdo
						;stav kdy je zamceno a zabezpeceno
	btfsc	GPIO,GP5	;kontrola zavrenych dveri(zavrene=0,otevrene=1)
	b		Otevrene		;dvere jsou otevrene
	btfsc	GPIO,GP3	;kontroluji jestli nekdo neodemkl klavesnici(zavrene=0,otevrene=1 !!!! )	
	b		Unarmed			;odemknuta klavesnice
	bsf		GPIO,GP1		;roznu diodu
	btfss	GPIO,GP4	;kontrola zavrenych oken(zavrene=1,otevrene=0)
	b		Sirena
	b		Cidla		;kontrola spravnosti cidel
ZaCidla:	
	b	Armed2
		
Otevrene:  				;cekam na otevreni dveri, majitel jde dom

	movlw	.15
	movwf	poc15
O_smycka:
	bsf		GPIO,GP1		;roznu diodu
	call	Zpoz05
	bcf		GPIO,GP1		;zhasnu diodu
	call	Zpoz05
	btfsc	GPIO,GP3		;kontroluji jestli nekdo neodemkl klavesnici(zavrene=0,otevrene=1 !!!! )
	b		Unarmed
	decfsz	poc15
	b		O_smycka
	b		Sirena			;neprilozil jsem cip do 15s ->SIRENA

Vniknuti:
	movlw	.15
	movwf	poc15
V_smycka:
	bsf		GPIO,GP1		;roznu diodu
	call	Zpoz05
	call	Zpoz05
	call	Zpoz05
	bcf		GPIO,GP1		;zhasnu diodu
	call	Zpoz05
	btfsc	GPIO,GP3		;kontroluji jestli nekdo neodemkl klavesnici(zavrene=0,otevrene=1 !!!! )
	b		Unarmed
	decfsz	poc15
	b		V_smycka
	b		Vniknuti

	
;#########################
;## 	FUNCTIONS		##
;#########################
	
Sirena:				;10s sirenuju
	movlw 	.20	
	movwf 	pocitadlo1
smycka3: 	
	movlw 	.250  
	movwf 	pocitadlo2 
smycka4: 
	bsf		GPIO,GP2
	call	Zpoz01
 							;milisekundu svitit nesvitit = f=1/2ms = 500Hz a to slysim
	bcf		GPIO,GP2
	call	Zpoz01
	btfsc	GPIO,GP3		;kontroluji jestli nekdo neodemkl klavesnici(zavrene=0,otevrene=1 !!!! )
	b		Unarmed
	decfsz 	pocitadlo2  
	b 		smycka4
	decfsz 	pocitadlo1  
	b 		smycka3 
	b		Vniknuti			;Odskakuji na Vniknuti po houkani
;	return	

Cidla:
;ctu z AD prevodniku a nastavuju ho
	banksel	ADCON0
	bsf		ADCON0,1 	;start conversion

	movlw 	.200	
	movwf 	pocitadlo5
WaitADC:
;	banksel	ADCON0
;	btfsc	ADCON0,GO
	decfsz 	pocitadlo5  
	b 		WaitADC    ;wait for ADC to finish
	b		GetADC1
 
GetADC1:
	movf	ADRESH, W    ;get high byte
	movwf	VOLTY_hi
	banksel	PIR1
	bcf		PIR1,ADIF    ;clear conversion flag

;porovnavam hodnoty
   	banksel	GPIO
	movf	VOLTY_hi,W
	sublw	VOLTY_MAX
	btfss	STATUS,C
	b		ALARM_off
	b		ALARM_on
;	movlw	VOLTY_hi
;	subwf	VOLTY_MAX,W
;	btfss	STATUS,C
;	b		$+8
;	movf	VOLTY_lo,W
;	sublw	VOLTY_MIN
;	btfss	STATUS,C
;	b		ALARM_on 
;	movlw	VOLTY_lo
;	subwf	VOLTY_MIN,W
;	btfss	STATUS,C
;	b		ALARM_on

ALARM_on:
	b		Sirena
ALARM_off:
	b		ZaCidla

Zpoz05:	;0,5sekundy
	;zpozdeni 0,5 sekundy 
    movlw 	0x03 		;3 DEC
    movwf 	ZP0053
    movlw	0x8A  		;138 DEC
    movwf 	ZP0052
	movlw 	0x56  		;86 DEC
    movwf 	ZP0051
    btfsc	GPIO,GP3		;kontrola klavesnice
	b	Unarmed
	decfsz 	ZP0051,F
    goto 	$-1
    decfsz 	ZP0052,F
    goto 	$-3
    decfsz 	ZP0053,F
    goto 	$-7
	;End of Delay
	return
	
Zpoz01:	;1milisekunda
	;Delay 1000 cycles
    movlw 	0x01  		;1 DEC
    movwf 	ZP03
    movlw 	0x05  		;5 DEC
    movwf 	ZP02
    movlw 	0x41 		;65 DEC
    movwf 	ZP01
    decfsz 	ZP01,F
    goto 	$-1
    decfsz 	ZP02,F
    goto 	$-3
    decfsz 	ZP03,F
    goto 	$-5
	;End of Delay
	return


	END