  
;**********************************************************************
;  Filename: LZ4B28.asm        File Version: 4B28
;                
;  Author: Pavel Janko  jankop@volny.cz                             
;**********************************************************************
;    Files required: <P12F675.INC>                                                 
;                                                                     
;                                                                     
;**********************************************************************
;    Notes:
;  
;
;
;
; 
;
;                                                           
;**********************************************************************
	list      p=12F675  		; list directive to define processor
	#include <P12F675.INC>  ; processor specific variable definitions
	errorlevel -302
	__CONFIG  _CP_OFF &_CPD_OFF & _WDT_OFF & _MCLRE_OFF &_BODEN_OFF &_PWRTE_ON &_INTRC_OSC_NOCLKOUT 
	__idlocs 0x4B28 			; Uklada verzi do ctyr ID byte (pouze 4 spodni bity)
					    		   ; Datum je kodovano hexadecimalne xYxMdDdD
	#define	  	RAM_Start 		0x20 
	#define		counter_over	0x00	; counter flag for sampling increment to 0x0000
	#define		delay_over		0x01	; counter flag for delay decrement to 0xFF
	#define		LED1				GPIO0 ; Vystup
	#define		LED2				GPIO1 ; Vystup
	#define 		LED3				GPIO2 ; Vystup
	#define		SW3				GPIO3	; Vstup
	#define		SW2				GPIO4	; Vstup
	#define		SW1				GPIO5	; Vstup
 ;--------- CONSTANTS DEFINITIONS---------------------------------------
	#define const60msLO		0xFE	; konstanta pro 60ms
	#define const60msHI		0xFF	; konstanta pro 60ms
	#define const250msLO		0xFB	; konstanta pro 0,25s
	#define const250msHI		0xFF	; konstanta pro 0,25s
	
	;--------- VARIABLE DEFINITIONS ---------------------------------------
	CBLOCK 	RAM_Start
w_temp        	; variable used for context saving 
status_temp   	; variable used for context saving
counterLO		; citac sample x 60ms
counterHI		; 				"
ucrLO				; universalni registr LO
ucrHI				; universalni registr HI
flag				; registr priznaku
mask				; pomocny registr				
	ENDC


;----------------------------------------------------------------------
		ORG     0x000          	; Processor reset vector
reset
		goto main					; Go to beginning of program

		ORG     0x004          	; interrupt vector location

;----------- Obsluha preruseni vcetne ulozeni registru ------------
		movwf w_temp         	; save off current W register contents
		movf STATUS,W        	; move status register into W register
		movwf status_temp     	; save off contents of STATUS register
		bcf STATUS, RP0 			; Restore Bank 0
		btfsc PIR1,TMR1IF			; test preruseni od timeru casu	
		goto timer1_INT			; preruseni od timeru casu	
		goto reset					; osetreni poruchy 
;----------------------------------------------------------------------
; 				Obsluha preruseni od TMR1  
; Kazdym pruchodem je inkrementovan par registru counterLO/HI,
; pokud neni aktivni bit flag<counter_over>. Ten je nastaven, kdyz je 
; counterLO/HI incrementovan na 0x0000. Muze byt rizen i z vnejsku.
;----------------------------------------------------------------------
timer1_INT
		btfsc flag,counter_over	; preteceni registru -> 
		goto ctstop					; neincrementovat
		incf counterLO,F			;		"
		btfsc STATUS,Z				;		"
		incf counterHI,F			;		"
		btfsc STATUS,Z
		bsf flag,counter_over	; timer pro flash Roll Over
ctstop	
      bcf PIR1,TMR1IF			; mazani priznaku preruseni TMR1  
		movf status_temp,W		; retrieve copy of STATUS register
		movwf STATUS				; restore pre-isr STATUS register contents
		swapf w_temp,F
		swapf w_temp,W				; restore pre-isr W register contents
		retfie						; return from interrupt
;---------------------------------------------------------------------
; Start procesoru , konfigurace
;---------------------------------------------------------------------
main	
		
		bcf INTCON,GIE			; zakaz vsech preruseni
		call reinit_CPU		; odstaveni periferii a procesu
      bsf STATUS, RP0 		; Restore Bank 1
		call 0x3FF 				; Get the cal value
		movwf OSCCAL 			; Calibrate
		bcf STATUS,RP0 		; Bank 0
		call set_IO				; nastaveni portu
		call set_TMR1			; nastaveni casovace TMR1
		bsf INTCON,PEIE		; Enables all unmasked perif. IRQ
		bsf INTCON,GIE			; Enables all unmasked interrupts
;---------------------------------------------------------------
;                 Hlavni smycka
;---------------------------------------------------------------
		clrf mask
		bsf flag,counter_over	; stop citace 
		movlw const60msLO			; nastaveni konstant timeru
		movwf counterLO			;		"
		movlw const60msHI			; nastaveni konstant timeru
	   movwf counterHI			;		"
		bcf flag,counter_over	; start citace 
wait02
		btfss flag,counter_over ; Test uplynutí 60ms
		btfss GPIO, SW1
		bsf mask, LED1
		btfss GPIO, SW2
		bsf mask, LED2
		btfss GPIO, SW3
		bsf mask, LED3
		movf mask,W
		btfss STATUS, Z
		goto err_sub
celoop
		btfss GPIO, SW1
		goto win_SW1
		btfss GPIO, SW2
		goto win_SW2
		btfss GPIO, SW3
		goto win_SW3
		goto celoop
		
		
win_SW1
		bsf GPIO, LED1
		goto win_SW1
win_SW2
		bsf GPIO, LED2
		goto win_SW2
win_SW3
		bsf GPIO, LED3
		goto win_SW3

;------------------------------------------------------
;        Signal Missing Button
;------------------------------------------------------
err_sub
repete
	   movf mask, W
		xorwf GPIO,F
      bsf flag,counter_over	; stop citace 60ms
		movlw const250msLO						; nastaveni konstant timeru 
		movwf counterLO			;		"
		movlw const250msHI		; 		"
		movwf counterHI			;		"
		bcf flag,counter_over	; start citace 60ms
mloop672  
      btfss flag,counter_over  
	   goto mloop672
      movf mask, W
		xorwf GPIO, F
      bsf flag,counter_over	; stop citace 60ms
		movlw const250msLO		; nastaveni konstant timeru 
		movwf counterLO			;		"
		movlw const250msHI   	; 		"
		movwf counterHI			;		"
		bcf flag,counter_over	; start citace 60ms
mloop677  
      btfss flag,counter_over  
	   goto mloop677
      goto repete          ; opakuj bliknuti
      

;-----------------------------------------------------------------------
; Zastaveni cinnosti periferii a IRQ  reinicializace 
;-----------------------------------------------------------------------
reinit_CPU
		bcf STATUS, RP0 		; Restore Bank 0
		clrf INTCON				; zakaz vsech IRQ
		clrf T1CON 				; Stop Timer1,LP oscillator is off, Internal clock(F OSC /4)
		clrf ADCON0				; vypnuti ADC - snizeni spotreby
		movlw 0x07				; odpojeni komparatoru CMP
		movwf CMCON				;       "
		bsf STATUS,RP0			; Bank 1
		clrf PIE1				; zakaz vsech IRQ periferii
		clrwdt
		clrf OPTION_REG		; TMR0 internal clock source,prescaler to the TIMER0
		clrf VRCON				; vypnuti reference - snizeni spotreby
		clrf ANSEL				; odpojeni ADC
		bcf STATUS, RP0 		; Restore Bank 0
		clrf INTCON				; mazani priznaku IRQ
		clrf PIR1				; mazani priznaku IRQ periferii
		return
;----------------------------------------------------------------------
; Nastavení vstupu a vystupu.
;----------------------------------------------------------------------
set_IO
		bcf STATUS,RP0 			; Bank 0
		bcf INTCON,GPIE			; Disables the GPIO port change interrupt
		clrf GPIO					; reset outputs for future
		bsf STATUS,RP0 			; Bank 1
      clrf IOCB					; zakaz vsech INTERRUPT-ON-CHANGE
		movlw B'00110000'
		movwf WPU     				; Pull-up setings for all inputs
      bcf OPTION_REG,NOT_GPPU	; GPIO pull-ups are enabled
		bcf TRISIO,LED1			; Vystup 
		bcf TRISIO,LED2			; Vystup 
		bcf TRISIO,LED3			; Vystup
		bsf TRISIO,SW1				; Vstup
		bsf TRISIO,SW2				; Vstup
		bsf TRISIO,SW3				; Vstup
		bcf STATUS, RP0 			; Restore Bank 0
		return


;----------------------------------------------------------------------
;		Timer1 Initialization segment with run and start its interrupt only
;----------------------------------------------------------------------
set_TMR1
		clrf T1CON 					; Stop Timer1,LP oscillator is off,Internal clock (F OSC /4)
		bcf T1CON,T1CKPS1			; prescaler /1
		bcf T1CON,T1CKPS0			; prescaler /1
		bsf STATUS,RP0 			; Bank 1
		bcf PIE1,TMR1IE			; Disables the TMR1 overflow interrupt
		bcf STATUS,RP0 			; Bank 0
		bcf PIR1,TMR1IF 			; clear TMR1 IRQ flag - register overflowed
		clrf TMR1L 					; set Timer1 LO byte register to zero
		clrf TMR1H					; set Timer1 HI byte register to zero
		bsf STATUS,RP0 			; Bank 1
		bsf PIE1,TMR1IE			; Enables the TMR1 overflow interrupt
		bcf STATUS,RP0 			; Bank 0
		bsf T1CON,TMR1ON 			; Timer1 starts to increment
		return

		END                     ; directive 'end of program'

