                                ;**********************************************************************
; This file is a basic code template for assembly code generation *
; on the PIC16F84A. This file contains the basic code *
; building blocks to build upon. *
; *
; Refer to the MPASM User's Guide for additional information on *
; features of the assembler (Document DS33014). *
; *
; Refer to the respective PIC data sheet for additional *
; information on the instruction set. *
; *
;**********************************************************************
; *
; Filename: roc.asm*
; Date: 	12.10.2012 *
; File Version: 8.30 *
; *
; Author:  *
; Company: *
; *
; *
;**********************************************************************
; *
; Files Required: P16F84A.INC *
; *
;**********************************************************************
; *
; Notes: *
; *
;**********************************************************************


    list p=16F84A ; list directive to define processor
    #include <p16F84A.inc> ; processor specific variable definitions

    __CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC

;***** LITERAR DEFINITIONS
_OPT 	set 0
_opt	set	.4
_int	set	0a0h
_td		set	.130
;***** VARIABLE DEFINITIONS
	cblock 0ch
w_temp
status_temp
flag_r
segm_cis
segm_bar
led_r
led_g
stisk_r
code_r
citac
set_r
           ENDC

;***** BIT DEFINITIONS
#define CY_f STATUS,C

;***** MACRO DEFINITIONS
pushPSW macro

        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
        ENDM

popPSW macro

        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
        ENDM

;***** SYMBOLIC DEFINITIONS

#define bank0 bcf STATUS,RP0
#define bank1 bsf STATUS,RP0
#define T0_f	flag_r,0 
#define T1_f	flag_r,1
#define T2_f	flag_r,2 
#define T3_f	flag_r,3 
#define key_f	flag_r,4
#define svit_f	flag_r,5
#define cas_f	flag_r,6

;**********************************************************************
        ORG 0x000 ; processor reset vector
          goto main ; go to beginning of program

        ORG 0x004 ; interrupt vector location
    pushPSW
		bcf		INTCON,T0IF
		bcf		STATUS,2
		movlw	_td
		movwf	TMR0	
		btfsc	T1_f
		goto	loop_00
		bsf		T1_f
		bcf		PORTA,2
		movf	segm_cis,w
		movwf	PORTB
		movlw	5h
		movwf	PORTA
		goto	konec	
loop_00
		btfsc	T2_f
		goto	loop_01
		bsf		T2_f
		bcf		PORTA,2
		movf	segm_bar,w
		movwf	PORTB
		movlw	6h
		movwf	PORTA
		goto	konec
loop_01
		btfsc	T3_f
		goto	loop_02
		bsf		T3_f
		bcf		PORTA,3
		movf	led_r,w
		movwf	PORTB
		movlw	0Ah
		movwf	PORTA
		goto	konec
loop_02
		bcf		T1_f
		bcf		T2_f
		bcf		T3_f
		bcf		PORTA,3
		movf	led_g,w
		movwf	PORTB
		movlw	8h
		movwf	PORTA
		decfsz	citac,f
		goto	loop_07
		movlw	3Eh
		movwf	citac
		bsf		cas_f			;nahození příznaku po uplynutí 1s
loop_07
	bank1
		movlw	0F0h
		movwf	TRISB
	bank0
		andwf	PORTB,w
		movwf	stisk_r
		swapf	stisk_r,f
		bsf		PORTA,2
		movlw	0F0h
		andwf	PORTB,w
		iorwf	stisk_r,f
	bank1
		clrf	TRISB
	bank0
		comf	stisk_r,f	
		btfsc	STATUS,Z
		goto	loop_03
		goto	loop_04
loop_03
		bcf		key_f
		bcf		svit_f
		goto	konec
loop_04
		btfss	svit_f
		goto	loop_05
		bsf		key_f
		clrf	code_r
loop_06
		incf	code_r,f
		rrf		stisk_r,f
		btfss	CY_f
		goto	loop_06
		goto	konec
loop_05
		bsf		svit_f
konec
    popPSW
        retfie ; return from interrupt

main
	bank1
		movlw	_opt
		movwf	OPTION_REG
		movlw	_int
		movwf	INTCON
		clrf	TRISB
	bank0
		clrf	PORTB
		clrf	PORTA
		movlw	_td
		movwf	TMR0
null_00
		clrf	flag_r
		movlw	6h
		movwf	PORTB
		movwf	led_r
		movlw	0Ah
		movwf	PORTA			
		movlw	0Eh
		movwf	led_g
		movlw	0FFh
		movwf	segm_cis
		movwf	segm_bar
null_01
		btfss	key_f
		goto	null_01
		movlw	2h
		subwf	code_r,w
		btfss	STATUS,Z
		goto	null_02
		movlw	3Eh
		movwf	citac
		movlw	0Ah
		movwf	led_g
		movlw	29h
		movwf	segm_cis
		movlw	4h
		movwf	set_r
label_00
		call	seglut
		movwf	segm_bar
label_01
		btfss	cas_f			;test uplynutí 1s
		goto	null_03
		bcf		cas_f
		movlw	3Eh
		movwf	citac
		decfsz	set_r,f
		goto	label_00
		movlw	4h
		movwf	set_r
		goto	label_00
null_02
		movlw	8h
		subwf	code_r,w
		btfss	STATUS,Z
		goto	null_04
		movlw	0Eh
		movwf	led_r
		goto	null_04
null_03
		btfsc	key_f
		goto	label_01
		goto	null_00
null_04
		btfsc	key_f
		goto	null_04
		goto	null_00

seglut
		movf	set_r,w
		addwf	PCL
		dt 	B'11111111',B'10011001',B'10000001',B'10011000',B'10001000'
		return	
		
; remaining code goes here

        END ; directive 'end of program'