
; Assembly source line config statements

; CONFIG
 __CONFIG _WDT_OFF & _CP_OFF & _MCLRE_OFF & _IntRC_OSC

#include "p12F508.inc"
      
    cblock 0x07
		del1   ;registry pro cekaci subrutiny
		del2
		del3
		numMilis
		i
		j
		tmp
    endc

FALSE			equ 0x00
TRUE			equ 0x01

         
#define BUTTON		GPIO, GP3
#define BUZZER		GPIO, GP2
      
;****** START ****************************************
    
	org 0x00
	goto main
	
;*****************************************************	
;***** MAIN ******************************************    
;*****************************************************
main	
	movwf   OSCCAL      ; loads factory calibration value into OSCCAL reg returned in W from reset vector

	;*** NASTAVENI PORTU ***
	;GP3 - input - tlacitko
	;GP2 - buzzer - output

	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
	
	clrf GPIO
	
;***** INIT ****************************************** 

										
;***** HLAVNI SMYCKA *******************************

loop	
	btfss BUTTON		;mame stisk tlacitka?
	goto loop
	call wait10ms
	btfss BUTTON
	goto loop
waitbE	
	btfsc BUTTON
	goto $-1

	movlw b'00000000'
	call morse_generate_number
	movlw b'11100000'
	call morse_generate_number
	movlw b'11000000'
	call morse_generate_number

	call wait1s
		
	goto loop
		
;***** HLAVNI SMYCKA - KONEC ***********************


;Vstup: W-REG, zajimaji nas bity [7:3], 1-dash, 0-dot
morse_generate_number
	movwf tmp
	movlw .5				; 5 nejlevejsich bitu pouze
	movwf i	
morse_generate
	bcf STATUS, C
	rlf tmp, f
	btfss STATUS, C			; C=dash, NC=dot
	goto morse_gen_dot

	;morse_dash
	bsf BUZZER
	call wait100ms
	call wait100ms
	call wait100ms
	bcf BUZZER
	
	goto morse_generate_next
		
morse_gen_dot

	;morse_dot
	bsf BUZZER
	call wait100ms
	bcf BUZZER		
	
morse_generate_next	
	
	;morse_space_parts				;mezera mezi teckou/carkou
	call wait100ms
	
	decfsz i, f
	goto morse_generate

	;morse_space_letter				;mezera po 1 znaku
	call wait100ms
	call wait100ms
	call wait100ms
				
	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 = 0.1 seconds
; Clock frequency = 4 MHz

; Actual delay = 0.1 seconds = 100000 cycles
; Error = 0 %

wait100ms
			;99993 cycles
	movlw	0x1E
	movwf	del1
	movlw	0x4F
	movwf	del2
wait100ms_0
	decfsz	del1, f
	goto	$+2
	decfsz	del2, f
	goto	wait100ms_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
	

; *** KONEC SOUBORU *************************************************	
	end