	LIST   P=PIC12F508

; CONFIG
 __CONFIG _WDT_OFF & _CP_OFF & _MCLRE_OFF & _IntRC_OSC

;C26 calibration value

#include "p12F508.inc"
      
    cblock 0x07
	d		;hodnota cislice
	io		;co pujde na vystup
	del1		;registry pro cekaci subrutiny
	del2
	del3
	loByte		;2 bajty pro 16bit counter
	hiByte
    endc

FALSE			equ 0x00
TRUE			equ 0x01

 
;   aaaa    
;   f  b
;   f  b
;   gggg
;   e  c
;   e  c
;   dddd
            
#define BUTTON		GPIO, GP3;
#define ANODE		GPIO, GP2; 

#define BCD_A		GPIO, GP0;
#define BCD_B		GPIO, GP1;
#define BCD_C		GPIO, GP5;
#define BCD_D		GPIO, GP4;

      
;****** START ****************************************
    
	org 0x00
	goto main
	
;*****************************************************	
;***** MAIN ******************************************    
;*****************************************************
main	
	movwf   OSCCAL      ; loads factory calibration value into OSCCAL reg returned in W from reset vector

	;*** NASTAVENI PORTU ***
	;GP0, GP1, GP2, GP4 - pro BCD decoder
	;GP3 - input - tlacitko
	;GP5 - output - LED anoda

	movlw b'11001000'   ; interni oscilator, prescaler 1:1 (PSA = 1)
	OPTION    	    ; prikaz zkopiruje W -> OPTION REGISTRU

	movlw b'11001000'   ; pouze GP3 jako vstupni, ostatni output
	TRIS GPIO	    ; provadi se instrukci TRIS
	
;***** INIT ****************************************** 
				
;TEST
				
;***** HLAVNI SMYCKA *******************************

loop		btfss BUTTON		;mame stisk tlacitka?
		goto loop
		call wait10ms
		btfss BUTTON
		goto loop
		call handleButton	;Ohandluj BUTTON								
		btfsc BUTTON
		goto $-1
		goto loop		

;***** HLAVNI SMYCKA - KONEC ***********************

; *** ROUTINA NA ZOBRAZOVANI CISLIC NA LED DISPLEJI ****************	
handleButton	
	movf TMR0, w;
	andlw b'111';	;0-7
	movwf d		;Ulozime
	btfss STATUS, Z ;Pri 0 upravime na 1
	goto nonZero
	incf d, f
	goto zobraz
nonZero	movlw b'111'	;Test na 7
	subwf d, w
	btfss STATUS, Z
	goto zobraz
	decf d, f	;Upravim 7 na 6
	
zobraz	clrf io 	
	clrf GPIO	;Nejprve vyresetuji vsechny bity

	movf d, w	
bit0	andlw b'001'	
	btfsc STATUS, Z
	goto bit1	
	bsf io, 0	;A je na GP0
bit1	movf d, w
	andlw b'010'
	btfsc STATUS, Z
	goto bit2
	bsf io, 1	;B je na GP1
bit2	movf d, w
	andlw b'100'
	btfsc STATUS, Z
	goto finish
	bsf io, 5	;C je na GP5

finish	bcf io, 4	;D segment bude 0 vzdy (cisla < 8)
	bsf io, 2	;Zapneme LED displej
	movf io, w
	movwf GPIO	;Posli na vystup

	call wait1s	;Zobraz cislo 1 sekundy
	
	clrf io		;Vypni LED displej
	movf io, w
	movwf GPIO
	retlw 0


; *** SUBRUTINA, CEKEJ 10ms ******************************************

; Delay = 0.01 seconds
; Clock frequency = 4 MHz

; Actual delay = 0.01 seconds = 10000 cycles
; Error = 0 %

wait10ms
			;9993 cycles
	movlw	0xCE
	movwf	del1
	movlw	0x08
	movwf	del2
wait10ms_0
	decfsz	del1, f
	goto	$+2
	decfsz	del2, f
	goto	wait10ms_0

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	retlw 0
		
; Delay = 1 seconds
; Clock frequency = 4 MHz

; Actual delay = 1 seconds = 1000000 cycles
; Error = 0 %

wait1s
			;999990 cycles
	movlw	0x07
	movwf	del1
	movlw	0x2F
	movwf	del2
	movlw	0x03
	movwf	del3
wait1s_0
	decfsz	del1, f
	goto	$+2
	decfsz	del2, f
	goto	$+2
	decfsz	del3, f
	goto	wait1s_0

			;6 cycles
	goto	$+1
	goto	$+1
	goto	$+1

			;4 cycles (including call)
	retlw 0
	

; *** SUBRUTINY pro 16bitovy counter ********************************	
; *** Nastavi Zero, pokud jsme na nule

incHL	incfsz loByte, f
	retlw 0	
	incf hiByte, f
	retlw 0

decHL	movf loByte, f
	btfsc STATUS, Z ;je loByte na nule?
	goto decHL2
	decf loByte, w	;kopie pro nasledny OR (test na 0)
	decf loByte, f
	iorwf hiByte, w ;Z flag nastaven pri 0 (loByte OR hiByte)
	retlw 0
decHL2	decf loByte, f
	decf hiByte, f
	movf d, f 	;Potrebuju vynulovat Zero necim nikdy nenulovym
	retlw 0

	
; *** KONEC SOUBORU *************************************************	
	end