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



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

.include ""

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

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

; main program
	sbi	DDRB, LED_PIN		; set LED pin as output
	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
	ldi	r20, 32			; set register, r20 = 32
	ldi	r19, 64			; set register, r19 = 64
	ldi	r18, 128		; set register, r18 = 128
	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

Leave a Comment