ATtiny13 – blinky with delay function (assembler version)

This tiny project shows how to interface LED to the microcontroller ATtiny13 and write a simple program in anssembler to make it blink. The code is on Github, click here.

LEDs are widely used for various display functions and can be directly (not quite directly, through the resistor) driven from the pins of AVR chip. In our circuit a LED is connected to PB0 and it is made to blink for roughly every second by using instruction set to delay.

Parts Required

Circuit Diagram

blinky-one-led

Software

This code is written in assembler and can be compiled using the avra compiler. More details on how to compile this project is here.

.nolist
.include "tn13def.inc"
.list

; define constant
.equ LED_PIN = PB0			; use PB0 as LED pin

; start vector
.org 0x0000
	rjmp	main			; jump to main label

; main program
main:
	sbi	DDRB, LED_PIN		; set LED pin as output
loop:
	sbic	PINB, LED_PIN		; if bit of LED pin is clear, skip next line
 	cbi	PORTB, LED_PIN		; if 1, turn the LED off
	sbis	PINB, LED_PIN		; if bit of LED pin is set, skip next line
 	sbi	PORTB, LED_PIN		; if 0, light the LED up
delay_500ms:
	ldi	r20, 32			; set register, r20 = 32
delay2:
	ldi	r19, 64			; set register, r19 = 64
delay1:
	ldi	r18, 128		; set register, r18 = 128
delay0:
	dec	r18			; decrement register, r18 = r18 - 1
	brne	delay0			; if r18 != 0, jump to label delay0
	dec	r19			; decrement register, r19 = r19 -1
	brne	delay1			; if r19 != 0, jump to label delay1
	dec	r20			; decrement register, r20 = r20 -1
	brne	delay2			; if r20 != 0, jump to label delay2
	rjmp	loop			; if r20 == 0, jump to label loop

One thought on “ATtiny13 – blinky with delay function (assembler version)

  1. Hello
    How to make this code in j.c, code in ASM?
    I am asking for such a code.
    best regards
    Alek
    code:
    #include //Attiny 2313A ,16 MHz
    #include
    main ()
    {
    DDRB = 255;
    PORTB = 170;
    DDRD = 255;
    PORTD = 170;
    while(1)
    {
    PORTB ^= 255;
    PORTD ^= 255;
    _delay_us (128000000);
    }
    }

Leave a Comment