;***************************************************************************
; Application:  Led flasher
; Author:       rue_mohr 
; Date:         
; Assembler:   
; Target:       Atmel ATtiny13.
;***************************************************************************

.device ATtiny13 

; ------------- Memory locations ---------------
;
;  .def bar =r01 ; one byte ram location
;
; ----------------------------------------------

.def A          = r16    
.def B          = r17            ; and logic instructions
.def C          = r18
.def D          = r19

; ------------- Equates ------------------

.equ INPUT      = 0
.equ OUTPUT     = 1

;========================| SYSTEM VECTORS |===========================
;
.cseg                                   ;CODE segment
.org 0x0000
    rjmp    init   ; RESET 
    reti           ; EXT_INT0  
    reti           ; PCINT0   (pin change)
    reti           ; TIM0_OVF  
    reti           ; EE_RDY  
    reti           ; ANA_COMP  
    reti           ; TIM0_COMPA  
    reti           ; TIM0_COMPB  
    reti           ; WATCHDOG  
    reti           ; ADC  

;=============================| MAIN |================================
;

.org 0x000A
init:   
        ; Set up stack
        ldi     A,   low(RAMEND)       ; load low address of ram to R16...
        out        SPL, A                 ; Set Stack Pointer to top of RAM

        ; Disable interrupts
        cli        

        ; Set up io ports
        ldi   A,    (0<<PORTB5)|(0<<PORTB4)|(0<<PORTB3)|(1<<PORTB2)|(0<<PORTB1)|(0<<PORTB0)
        out   PORTB, A
        ldi   A,    (INPUT<<DDB5)|(INPUT<<DDB4)|(INPUT<<DDB3)|(OUTPUT<<DDB2)|(INPUT<<DDB1)|(INPUT<<DDB0)
        out   DDRB,  A

MainLoup:
       ; ldi     A, $00                  ; set tempport bit 0 low
       ; out     PORTB, A                ; write to port

        sbi     PORTB, 2

        rcall   wait                        ; do the wait thing

       ; ldi     A, $04                ; set tempport bit to high
       ; out     PORTB, A                ; write to port
       
        cbi      PORTB, 2

        rcall   wait                        ; do the wait thing

        rjmp    MainLoup                ; go back and do it all over again


;========================| SUBROUTINES |==============================
;

wait:    
        ldi     B, 3              ; load count1 with decimal 200

d1:
        ldi     C, 200              ; load count2 with decimal 200

d2:
        ldi     D, 200

d3:
        dec     D
        brne    d3
        dec     C                  ; decrement count2
        brne    d2                 ; loop if not zero              
        dec     B                  ; decrement count1 if count2 is zero
        brne    d1                 ; do inside loop again if count2 nz
        ret
    


