;******************************************************************************
;                                                                             *
;    Filename:      Uloha 3                                                   *
;    Date:          13.4.2010                                                  *
;    File Version:  1.0                                                       *
;                                                                             *
;    Author:        Miroslav Chomat                                           *
;    Company:       FEL CVUT                                                  *
;	 Descriptin:	Binarni stopky po 0.1s											  *
;                                                                             * 
;******************************************************************************
;                                                                             *
;    Files Required: P18F87J11.INC                                            *
;                                                                             *
;******************************************************************************

	LIST P=18F87J11		;directive to define processor
	#include <P18F87J11.INC>	;processor specific variable definitions

;******************************************************************************
;Configuration bits
;Microchip has changed the format for defining the configuration bits, please 
;see the .inc file for futher details on notation.  Below are a few examples.



;   Oscillator Selection:
    CONFIG	FOSC = HS            ;HS oscilator
	CONFIG	WDTEN = OFF;         ;nepouzivame Watchdog
	CONFIG	XINST = OFF;		 ;nepouzivame rozsireny instrukcni soubor

;Preddefinovane konstanty
#define PRESSED		1
#define NOTPRESSED	0

#define TMR1_PRELOAD_HIGH	0x85
#define TMR1_PRELOAD_LOW	0xee+0x01

#define STATE_STOP			0
#define STATE_RUN			1


;******************************************************************************
;Variable definitions
; These variables are only needed if low priority interrupts are used. 
; More variables may be needed to store other special function registers used
; in the interrupt routines.

		CBLOCK	0x000
		WREG_TEMP	;variable used for context saving 
		STATUS_TEMP	;variable used for context saving
		BSR_TEMP	;variable used for context saving

		RB0_STATUS	;promenna pro predani informace o zmacknuti tlacitka
		STATE		;promenna udrzujici informaci o stavu
		TIME		;hodiny po 0.1s
		ENDC

;******************************************************************************
;Reset vector
; This code will start executing when a reset occurs.

		ORG	0x0000			;startovaci adresa po resetu

		goto	Main		;skok na hlavni cast programu

;******************************************************************************
;High priority interrupt vector
; This code will start executing when a high priority interrupt occurs or
; when any interrupt occurs if interrupt priorities are not enabled.

		ORG	0x0008			;vektor preruseni pro vysokou prioritu

		goto	HighInt		;skok na ISR pri preruseni s vysokou prioritou

;******************************************************************************
;Low priority interrupt vector and routine
; This code will start executing when a low priority interrupt occurs.
; This code can be removed if low priority interrupts are not used.

		ORG	0x0018			;vektor preruseni pro nizkou prioritu

		movff	STATUS,STATUS_TEMP	;save STATUS register
		movff	WREG,WREG_TEMP		;save working register
		movff	BSR,BSR_TEMP		;save BSR register

		movlw	NOTPRESSED
		btfss	PORTB, 0, 0			;Je RB0 stisknuto?
		movlw	PRESSED
		movwf	RB0_STATUS			;Nastav aktualni informaci o zmacknuti tlacitka
		bcf		INTCON, TMR0IF, 0	;vynuluj priznak prave osetreneho preruseni


		movff	BSR_TEMP,BSR		;restore BSR register
		movff	WREG_TEMP,WREG		;restore working register
		movff	STATUS_TEMP,STATUS	;restore STATUS register
		retfie						;navrat z ISR

;******************************************************************************
;High priority interrupt routine
; The high priority interrupt code is placed here to avoid conflicting with
; the low priority interrupt vector.

HighInt:
		movlw	TMR1_PRELOAD_HIGH	;nastaveni pocatecni hodnoty Timeru 1
		movwf	TMR1H
		movlw	TMR1_PRELOAD_LOW
		movwf	TMR1L

		movlw	STATE_STOP			;pokud v STATE_RUN zvys hodnotu TIME
		cpfseq	STATE
		bra		HighIntTimeRunning
		bra		HighIntEnd

HighIntTimeRunning:		
		incf	TIME
		movff	TIME, PORTD			;Zobraz aktualni stav na LEDkach

HighIntEnd:		
		bcf		PIR1, TMR1IF, 0		;vynuluj priznak prave osetreneho preruseni
		retfie	FAST				;navrat z ISR s obnovenim kontextu

;******************************************************************************
;Start of main program
; The main program code is placed here.

Main:
		call 	Init

Loop:
		call	StateMachine
		goto	Loop				;nekonecna smycka


Init:	clrf	TRISD, 0		;Konfiguruace PORTD jako vystup
		clrf	PORTD, 0		;Inicializace hodnot na PORTD
		clrf	RB0_STATUS		;Inicializace uzivatelskych promennych
		clrf	STATE
		clrf	TIME

		movlw	b'00000001'		;Konfigurace PORTB jako vystup az na bit0
		movwf	TRISB

		movlw	b'00000000'		;Timer0 ma 16b, z internich hodin, s 1:1 prescalerem
		movwf	T0CON

		movlw	b'10110000'		;Timer1 ma 16b, z internich hodin, s 1:8 prescalerem
		movwf	T1CON

		movlw	b'11100000'		;Preruseni od preteceni Timer0 povoleno
		movwf	INTCON

		movlw	b'00000000'		;Preruseni od Timer0 ma nizkou prioritu
		movwf	INTCON2

		movlw	b'10000000'		;Povoleni priorit u preruseni
		movwf	RCON

		bsf 	IPR1, TMR1IP, 0	;Preruseni od Timer1 ma vysokou prioritu

		bsf		PIE1, TMR1IE, 0	;Povol preruseni od Timer1

		bsf		T1CON, TMR1ON, 0 ;Spust Timer1
		bsf		T0CON, TMR0ON, 0 ;Spust Timer0

		return

StateMachine:
		movlw	NOTPRESSED
Press1:	cpfseq	RB0_STATUS		;Bylo RB0 uvolneno pri preruseni?
		bra		Press1
		movlw	PRESSED
Press2:	cpfseq	RB0_STATUS		;Bylo RB0 zmacknuto pri preruseni?
		bra		Press2

		btg		STATE, 0, 0	;Zmen stav RUN/STOP
			
		return

		END