
.nolist
.include "1200def.inc"
.list 


;*************************Naudojami registrai************************
.def	save_sreg	      = r0
.def	iwr0		      = r1
.def	key_ct0		      = r2
.def	key_ct1		      = r3
.def	key_state 	      = r4
.def	key_press	      = r5
.def    min_vnt_ind           = r6
.def    min_des_ind           = r7
.def    sek_vnt_ind           = r8
.def    sek_des_ind           = r9

.def    kodas_ind             = r16
.def	wr0		      = r17
.def    laikinas              = r18
.def    sk_20                 = r19              ; 20 taimerio pertraukimu skaitiklis 20 pertraukimu - 1 s
.def    pert_sk_low           = r20
.def    pert_sk_high          = r21
.def    min_vnt               = r22
.def    min_des               = r23
.def    sek_vnt               = r24
.def    sek_des               = r25
.def    pozymiai              = r26
.def    T5ms_VIDINIS_CIKLAS   = r27
.def    T5ms_ISORINIS_CIKLAS  = r28
.def    decode                = r29



 

;********************************************************************
;    a
;   ----
;  |    |
; f| g  |b
;  |----|
; e|    |c
;  | d  |
;   ----   * t


;* segments a|e|d|t|c|g|f|b


 .equ          Segm_0              = 0b00010100		
 .equ          Segm_1              = 0b11110110		
 .equ          Segm_2              = 0b00011010		
 .equ          Segm_3              = 0b01010010		
 .equ          Segm_4              = 0b11110000		
 .equ          Segm_5              = 0b01010001		
 .equ          Segm_6              = 0b00010001		
 .equ          Segm_7              = 0b01110110		
 .equ          Segm_8              = 0b00010000		
 .equ          Segm_9              = 0b01010000	
 
 .equ          pirmas_skaicius     = PD0
 .equ          antras_skaicius     = PD1
 .equ          trecias_skaicius    = PD2
 .equ          taskas = PB4

 .equ	       key_port 	   = pind
 .equ          START_STOP          = PD3
 .equ          MINUTES             = PD4
 .equ          SEKUNDES            = PD5
 .equ          OUT                 = PD6
 

 

;********************************************************************
 

 .cseg
 .org 0

  rjmp   RESET 
  reti                                ;Nenaudojamas isorinis pertraukimas
  rjmp   TIMER_INTERRUPT              ;Taimerio pertraukimas 
  reti                                ;Nenaudojamas komparatoriaus pertraukimas
 
;-------------------------------------------------------------------------


RESET:
        ldi laikinas,0b11111111         ; Inicializuojame PortB.Ikrauname i r16 skaiciu 0xFF
        out DDRB,laikinas               ; Visi PortB isvadai bus OUT
        ldi laikinas,0xFF               ; 
        out PORTB,laikinas              ; PortB pinuose "1"
        clr POZYMIAI                    ; Apnuliname pozymiu registra
        ldi laikinas,0b01000111         ; Inicializuojame PortD.
        out DDRD,laikinas               ; PD0,PD1,PD2,PD6 - OUT. PD3,PD4,PD5 - IN
        ldi laikinas,0b00111111         ; Isejimuose: PD6 - "0" ; PD0,PD1,PD2 - "1"
        out PORTD,laikinas              ; Iejimuose:  PD3,PD4,PD5 - pull-up
        sbi ACSR,ACD                    ; Isjungiame komparatoriaus maitinima
        ldi laikinas,0x5                ; 00000101
        out TCCR0,laikinas              ; Nustatome dalikli CK/1024
        ldi laikinas,0x2                ; 00000010
        out TIMSK,laikinas              ; Leisti taimerio/skaitiklio pertraukima
;  ldi laikinas,0xF                ; 00001111
;  out WDTCR,laikinas              ; wdr po 256K ciklu
        ldi laikinas,0x3d          
        out TCNT0,laikinas


        clr key_state
        clr key_press
        clr key_ct0
        dec key_ct0
        mov key_ct1, key_ct0
  
        ldi  T5ms_VIDINIS_CIKLAS, 255
        ldi  T5ms_ISORINIS_CIKLAS, 26
        ldi  sk_20,20
   
 
      

	
  sei                             ; Leisti pertraukimus
  	
	
	
	
	
;*********************************************************
;**               PAGRINDINE PROGRAMA	                **
;*********************************************************
	
main:
        rcall gesinimas
        tst   min_des_ind
        brne  min_des_yra
        
        mov   decode,min_vnt_ind
        rcall dekodavimas
        cbr   kodas_ind,0b00010000
        out   portb,kodas_ind
        cbi   portd,pirmas_skaicius 
        rcall velinimas_5ms
        
        rcall gesinimas
        mov   decode,sek_des_ind
        rcall dekodavimas
        out   portb,kodas_ind
        cbi   portd,antras_skaicius 
        rcall velinimas_5ms
        
        rcall gesinimas
        mov   decode,sek_vnt_ind
        rcall dekodavimas
        out   portb,kodas_ind
        cbi   portd,trecias_skaicius 
        rcall velinimas_5ms
        rjmp  pabaiga


min_des_yra:
        mov   decode,min_des_ind
        rcall dekodavimas
        out   portb,kodas_ind
        cbi   portd,pirmas_skaicius 
        rcall velinimas_5ms
      
        rcall gesinimas
        mov   decode,min_vnt_ind
        rcall dekodavimas
        cbr   kodas_ind,0b00010000
        out   portb,kodas_ind
        cbi   portd,antras_skaicius 
        rcall velinimas_5ms
        
        rcall gesinimas
        mov   decode,sek_des_ind
        rcall dekodavimas
        out   portb,kodas_ind
        cbi   portd,trecias_skaicius 
        rcall velinimas_5ms
        rjmp  pabaiga
        
        

pabaiga:
        cli
        mov   min_des_ind, min_des
        mov   min_vnt_ind, min_vnt
        mov   sek_des_ind, sek_des
        mov   sek_vnt_ind, sek_vnt     
        sei        
        rjmp	main
;*********************************************************






	
;*********************************************************	
;**                       PAPROGRAMES                   **
;*********************************************************              


;*********************************************************
velinimas_5ms:
        dec     T5ms_VIDINIS_CIKLAS         ; 
        brne    velinimas_5ms               ; 
        ldi     T5ms_VIDINIS_CIKLAS, 255    ; 
        dec     T5ms_ISORINIS_CIKLAS        ; 
        brne    velinimas_5ms               ; 
        ldi     T5ms_ISORINIS_CIKLAS, 26    ; 
        ret                                
;********************************************************* 
 
 
;********************************************************* 
gesinimas:
        ldi    laikinas,0xFF
        out    portb,laikinas
        sbi    portd,pirmas_skaicius
        sbi    portd,antras_skaicius
        sbi    portd,trecias_skaicius 
        ret
;*********************************************************   
 
 
;*********************************************************   
dekodavimas:
        ldi kodas_ind, Segm_0
        andi decode, 0x0F
        breq Dec_End

        ldi kodas_ind, Segm_1
        dec decode
        breq Dec_End

        ldi kodas_ind, Segm_2
        dec decode
        breq Dec_End

        ldi kodas_ind, Segm_3
        dec decode
        breq Dec_End

        ldi kodas_ind, Segm_4
        dec decode
        breq Dec_End

        ldi kodas_ind, Segm_5
        dec decode
        breq Dec_End

        ldi kodas_ind, Segm_6
        dec decode
        breq Dec_End

        ldi kodas_ind, Segm_7
        dec decode
        breq Dec_End

        ldi kodas_ind, Segm_8
        dec decode
        breq Dec_End

        ldi kodas_ind, Segm_9


        dec decode
        breq Dec_End
        ldi kodas_ind,0xFF  ; Jei >10, gesiname
Dec_End:
        ret
;*********************************************************



      
;------------------------------------------------------------------------------ 
; R5 registre fiksuojamas perëjimas ið "1" i "0" su debouncingu


;this is a simple example to debounce up to 8 keys and detect key pressing.
;The keys are connected to GND (low active).
;All keys are fully separate handled.
;The register key_state contain the debounced state inverted (1 = key pressed).
;The registers key_ct0, key_ct1 are counters bit wise (ct0^0, ct1^0 are the counter for key0).
;If this counter rolls over, 4 times the opposite key state was detected and only then the bit0 in the register key_state was changed to.

;*************************************************************************
;*									 *
;*		Testing Read and debounce up to 8 keys			 *
;*		Bulletproof: 4 equal samples needed			 *
;*									 *
;*************************************************************************


TIMER_INTERRUPT:
	in	save_sreg, SREG
	ldi laikinas,0x3d          
        out TCNT0,laikinas
       
        
	ldi     laikinas,0b11000111    
	in	iwr0, key_port
	or      iwr0,laikinas           ;apnuliname nereikalingus bitus
	com	iwr0			;low active
	eor	iwr0, key_state
	and	key_ct0, iwr0		;reset counter
	and	key_ct1, iwr0
	com	key_ct0			;count
	eor	key_ct1, key_ct0
	and	iwr0, key_ct0		;input AND last counterstate
	and	iwr0, key_ct1
	eor	key_state, iwr0		;toggle state
	and	iwr0, key_state		;0-1-transition
	or	key_press, iwr0		;store key press detect
		
;
;			insert other timer functions here
;
     
  
                                      ; 7 bitas, ijungiantis ir isijungiantis laiko skaiciavima.
 
        eor    pozymiai,key_press
        sbrc   pozymiai,START_STOP   ; Praleisti sekanti jei bitas nenustatytas 
        rjmp   timer_on
        
;*****************************************************************************
;**                             TIMER OFF                                   **
;*****************************************************************************
        cbi   portd, OUT 
;        tst   key_press
;        breq  pert_pab                ; Jei nei vienas nenuspaustas, iseiti is pertraukimo
      
        ldi   laikinas,0b00110000
        cp    key_press,laikinas
        brne  minutes_paspausta
     
      
        clr   sek_vnt
        clr   sek_des
        clr   min_vnt
        clr   min_des
        ldi   sk_20,20
        rjmp  pert_pab
        
        
minutes_paspausta:
        sbrc  key_press,SEKUNDES
        rjmp  didink_sekundes
        sbrc  key_press,MINUTES
        rjmp  didink_minutes
        rjmp  pert_pab
     
didink_sekundes:     
        inc   sek_vnt
        cpi   sek_vnt,10
        brlo  pert_pab                ; Jei sek_vnt < 10, iseiti           
        clr   sek_vnt                 ; Jei sek_vnt = 10, ikrauname 0
        inc   sek_des                 ; Ir padidiname 1 sek_des
        cpi   sek_des,6
        brlo  pert_pab                ; Jei sek_des < 6 , iseiti 
        clr   sek_des                 ; Jei sek_des = 6, ikrauname 0
didink_minutes:       
        inc   min_vnt                 ; Ir padidiname 1 min_vnt
        cpi   min_vnt,10
        brlo  pert_pab                ; Jei min_vnt < 10 , iseiti
        clr   min_vnt                 ; Jei min_nnt = 10, ikrauname 0
        inc   min_des                 ; Ir padidiname 1 min_des
        cpi   min_des,10              ;
        brlo  pert_pab                ; Jei min_des < 10, iseiname
        clr   min_des                 ; Jei min_des = 10, ikrauname 0
        rjmp  pert_pab                ; Ir iseiname
                                       
                                       
 
 
;*****************************************************************************
;**                            TIMER ON                                     **
;*****************************************************************************                                      
timer_on:
        tst    sek_vnt
        brne   laikas_nesibaiges    
        tst    sek_des              ; Ar sek_des =0
        brne   laikas_nesibaiges               ; Laikas dar nesibaige
        tst    min_vnt              ; Ar min_vnt = 0 
        brne   laikas_nesibaiges    
        tst    min_des              ; Ar min_des = 0
        brne   laikas_nesibaiges    
        rjmp   isjunk
        
laikas_nesibaiges:
        sbi    portd,OUT            ; Ijungiame isejima             
tikrink:	 
        dec     sk_20               ; Sumaziname vienetu 20 pertraukimu skaitikli
        brne    pert_pab            ; Jei pertraukimu skaitiklis ne 0, iseiname is pertraukimo 
        ldi     sk_20,20            ; Jei pertraukimu skaitiklis = 0, uzkrauname i ji 20  
        dec     sek_vnt             ; Reiskia praejo sekunde, sumaziname sekundziu skaiciu
        breq    ar_pabaiga          ; Jei sek_vnt = 0, tikriname gal jau laikas baigesi ir visi registrai = 0
dar_ne: 
        tst     sek_vnt             ;
        brpl    pert_pab            ; Jei sek_vnt >= 0, iseiname is pertraukimo                      
        ldi     sek_vnt,9           ; Jei sek_vnt < 0, uzkrauname 9 i sek_vnt
        dec     sek_des             ; Sumaziname sek_des vienetu
        brpl    pert_pab            ; Jei sek_des >= 0, iseiname is pertraukimo 
        ldi     sek_des,5           ; Jei sek_des < 0, uzkrauname 9 i sek_des
        dec     min_vnt             ; Sumaziname min_vnt vienetu
        brpl    pert_pab            ; Jei min_vnt >= 0, iseiname is pertraukimo 
        ldi     min_vnt,9           ; Jei min_vnt < 0, uzkrauname 9 i min_vnt
        dec     min_des             ; Sumaziname min_des vienetu
        rjmp    pert_pab            ; Iseiname is pertraukimo                                
ar_pabaiga:
        tst    sek_des              ; Ar sek_des =0
        brne   dar_ne               ; Laikas dar nesibaige
        tst    min_vnt              ; Ar min_vnt = 0 
        brne   dar_ne               ; Laikas dar nesibaige
        tst    min_des              ; Ar min_des = 0
        brne   dar_ne               ; Laikas dar nesibaige
isjunk:                             ; Laikas jau baigesi
        cbr  pozymiai,0b00001000    ; Isvalome START_STOP pozymi
        cbi  portd, OUT             ; Ir isjungiame isejima
;*****************************************************************************


pert_pab:       
        clr     key_press
	out	SREG, save_sreg
	reti