;#########################
;## 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
	ADC_value_lo
	ADC_value_hi
	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	0x28
pocitadlo1	EQU	0x29
pocitadlo2	EQU	0x30
pocitadlo3	EQU	0x31
pocitadlo4	EQU	0x32


;#########################
;## 		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 	LED 5
#define    	ADC 0
#define 	UPPER_LIMIT .543    ;2.64984 V
#define 	LOWER_LIMIT .491    ;2.39602 V

;------------------------------------------------------------------
;macro definitions
;------------------------------------------------------------------

;macro compares the 16 bit value stored in fr,
;fr-1 to a 16 bit literal number. It jumps to
;an address if the value in the registers is higher
;than the literal.

cjalwd    	macro fr1,L1,L2,addr
   	movf	fr1,W
   	sublw	L1
   	btfss	STATUS,C
   	goto	addr
   	movlw	L1
   	subwf	fr1,W
   	btfss	STATUS,C
   	goto	$+5
   	movf	fr1-1,W
   	sublw	L2
   	btfss	STATUS,C
   	goto	addr 
	endm


;###############################################
;###############################################
;####			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'1010001'           	;AN0 as analog input,conversion clock Fosc/8
   	movwf	ANSEL

   	movlw	b'10000001'		;ADC on, right justified
   	movwf	ADCON0

	PAGE0
	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
WaitADC:
;	banksel	PIR1
	btfss	PIR1,ADIF
	goto	WaitADC        ;wait for ADC to finish
GetADC1:
	movf	ADRESH, W    ;get high byte
	movwf	ADC_value_hi
	banksel	ADRESL
	movf	ADRESL, W    ;get low byte
	movwf	ADC_value_lo
	banksel	PIR1
	bcf		PIR1,ADIF    ;clear conversion flag
;porovnavam hodnoty
   	banksel	GPIO
   	cjalwd	ADC_value_hi,HIGH UPPER_LIMIT,LOW UPPER_LIMIT,ALARM_off    ;compare ADC value with upper limit
   	cjalwd	ADC_value_hi,HIGH LOWER_LIMIT,LOW LOWER_LIMIT,ALARM_on    ;compare ADC value with lower limit
ALARM_off:
   	b		Armed2
ALARM_on:
	b		Sirena

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