Saturday, May 16, 2015

Blinking LED in ATtiny13A in Assembler


After read the ATtiny13A datasheet, I use the code below as the base. Under OS X in my Macbook Air. Using USBASP programmer. 

.include "../tn13Adef.inc"
;Pin assignments
;.equ LED = PB0 ;pin 5 of ATtiny13
.cseg
.org 0x00
rjmp setup
setup:
;CONFIGURE I/O PINS
ldi r16, (1 << PB0)
out PORTB, r16
out DDRB, r16
loop:
rjmp loop

Compile, build and upload it



Last login: Wed May 13 22:50:32 on ttys000

Nugrohos-MacBook-Air:~ nugroho$ avrdude -p t13 -P usb -c usbasp -n

avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9007

avrdude: safemode: Fuses OK (H:FF, E:FF, L:6A)

avrdude done.  Thank you.

Nugrohos-MacBook-Air:~ nugroho$ 


Nugrohos-MacBook-Air:coba1 nugroho$ avrdude -p t13 -c usbasp -P usb -U flash:w:coba.hex 

avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9007
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: reading input file "coba.hex"
avrdude: input file coba.hex auto detected as Intel Hex
avrdude: writing flash (10 bytes):

Writing | ################################################## | 100% 0.03s

avrdude: 10 bytes of flash written
avrdude: verifying flash memory against coba.hex:
avrdude: load data flash data from input file coba.hex:
avrdude: input file coba.hex auto detected as Intel Hex
avrdude: input file coba.hex contains 10 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.02s

avrdude: verifying ...
avrdude: 10 bytes of flash verified

avrdude: safemode: Fuses OK (H:FF, E:FF, L:6A)

avrdude done.  Thank you.

Nugrohos-MacBook-Air:coba1 nugroho$ 

Trying to get slightly advanced template 

.nolist
.include "../tn13Adef.inc"
.list

;.def rmp = r16

.dseg
.org 0x0060

.cseg
.org $0000
rjmp main;reset vector
reti;int vector 1
reti;int vector2
reti;...
reti
reti
reti
reti
reti
reti
main:
;init stack
ldi r16, low(ramend)
out spl, r16

;init portb
ldi r16,(1<<ddb2)|(1<<ddb1)|(1<<ddb0)
out ddrb, r16
;rutin
ldi r16, 1<<SE;enable sleep
out mcucr, r16
sei

loop:
sleep;goto sleep
nop;dummy wakeup
rjmp loop

compile, build and upload it


Nugrohos-MacBook-Air:coba2 nugroho$ ls
template.s  template.s.eep.hex template.s.obj
template.s.cof  template.s.hex
Nugrohos-MacBook-Air:coba2 nugroho$ avra -o template.hex template.s
AVRA: advanced AVR macro assembler Version 1.3.0 Build 1 (8 May 2010)
Copyright (C) 1998-2010. Check out README file for more info

   AVRA is an open source assembler for Atmel AVR microcontroller family
   It can be used as a replacement of 'AVRASM32.EXE' the original assembler
   shipped with AVR Studio. We do not guarantee full compatibility for avra.

   AVRA comes with NO WARRANTY, to the extent permitted by law.
   You may redistribute copies of avra under the terms
   of the GNU General Public License.
   For more information about these matters, see the files named COPYING.

Pass 1...
Pass 2...
done

Used memory blocks:
   Code      :  Start = 0x0000, End = 0x0013, Length = 0x0014

Assembly complete with no errors.
Segment usage:
   Code      :        20 words (40 bytes)
   Data      :         0 bytes
   EEPROM    :         0 bytes
Nugrohos-MacBook-Air:coba2 nugroho$ ls
template.s  template.s.eep.hex template.s.obj
template.s.cof  template.s.hex

Nugrohos-MacBook-Air:coba2 nugroho$ avrdude -p t13 -c usbasp -P usb -U flash:w:template.s.hex

avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9007
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: reading input file "template.s.hex"
avrdude: input file template.s.hex auto detected as Intel Hex
avrdude: writing flash (40 bytes):

Writing | ################################################## | 100% 0.05s

avrdude: 40 bytes of flash written
avrdude: verifying flash memory against template.s.hex:
avrdude: load data flash data from input file template.s.hex:
avrdude: input file template.s.hex auto detected as Intel Hex
avrdude: input file template.s.hex contains 40 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.03s

avrdude: verifying ...
avrdude: 40 bytes of flash verified

avrdude: safemode: Fuses OK (H:FF, E:FF, L:6A)

avrdude done.  Thank you.

Nugrohos-MacBook-Air:coba2 nugroho$

.
Two codes above have no loop feature, the first code just turning LED connected to PortB0 on. No blink. The second turning LED on on PB0 PB1 and PB2

Try to update the first code to turning on LED connected to PB0, PB1 and PB2

.include "../tn13Adef.inc"
;Pin assignments
;.equ LED = PB0 ;pin 5 of ATtiny13
.cseg
.org 0x00
rjmp setup
setup:
;CONFIGURE I/O PINS
ldi r16, (1 << PB0)|(1 << PB1)|(1<<PB2)
out PORTB, r16
out DDRB, r16
loop:
rjmp loop

and try to disassembly it 


Nugrohos-MacBook-Air:coba1 nugroho$ vavrdisasm coba.s.hex 
   0: c0 00       rjmp .+0 ; 0x2
   2: e0 07       ldi R16, 0x07
   4: bb 08       out $18, R16
   6: bb 07       out $17, R16
   8: cf ff       rjmp .-2 ; 0x8
Nugrohos-MacBook-Air:coba1 nugroho$ 
.
very pretty, :)

Now, how about blinking it?

Just add timer subroutine


.include "../tn13Adef.inc"
;Pin assignments
;.equ LED = PB0 ;pin 5 of ATtiny13
.cseg
.org 0x00
rjmp setup
setup:
;CONFIGURE I/O PINS
ldi r16, (1 << PB0)|(1 << PB1)|(1<<PB2)
out PORTB, r16
out DDRB, r16
loop:
    sbis PORTB,0
    rjmp ledOn
    rjmp ledOff
    
    rally:
    rcall timer
    rjmp loop

timer:
    ldi r17,0x00
    testtimer:
        cpi r17,0xf0
        brsh endtimer   ;branch if same or higher
        ldi r18,0x00
        testtimer2:
            cpi r18,0xf0
            brsh endtimer2 
            inc r18
            rjmp testtimer2
        endtimer2:
        inc r17
        rjmp testtimer
    endtimer:
ret
    
ledOn:
    sbi PORTB,0
    rjmp rally
ledOff:
    cbi PORTB,0
    rjmp rally


compile, build, disassembly it (it’s not necessary; just for fun :) ), and then upload it


Nugrohos-MacBook-Air:coba1 nugroho$ avra -o coba.hex coba.s
AVRA: advanced AVR macro assembler Version 1.3.0 Build 1 (8 May 2010)
Copyright (C) 1998-2010. Check out README file for more info

   AVRA is an open source assembler for Atmel AVR microcontroller family
   It can be used as a replacement of 'AVRASM32.EXE' the original assembler
   shipped with AVR Studio. We do not guarantee full compatibility for avra.

   AVRA comes with NO WARRANTY, to the extent permitted by law.
   You may redistribute copies of avra under the terms
   of the GNU General Public License.
   For more information about these matters, see the files named COPYING.

Pass 1...
Pass 2...
done

Used memory blocks:
   Code      :  Start = 0x0000, End = 0x000B, Length = 0x000C

Assembly complete with no errors.
Segment usage:
   Code      :        12 words (24 bytes)
   Data      :         0 bytes
   EEPROM    :         0 bytes
Nugrohos-MacBook-Air:coba1 nugroho$ vavrdisasm coba.s.hex 
   0: c0 00       rjmp .+0 ; 0x2
   2: e0 07       ldi R16, 0x07
   4: bb 08       out $18, R16
   6: bb 07       out $17, R16
   8: d0 01       rcall .+2 ; 0xc
   a: cf fe       rjmp .-4 ; 0x8
   c: e0 10       ldi R17, 0x00
   e: 3f 10       cpi R17, 0xf0
  10: f4 10       brcc .+4 ; 0x16
  12: 95 13       inc R17
  14: cf fc       rjmp .-8 ; 0xe
  16: 95 08       ret 
Nugrohos-MacBook-Air:coba1 nugroho$ avrdude -p t13 -c usbasp -P usb -U flash:w:coba.s.hex

avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9007
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: reading input file "coba.s.hex"
avrdude: input file coba.s.hex auto detected as Intel Hex
avrdude: writing flash (24 bytes):

Writing | ################################################## | 100% 0.03s

avrdude: 24 bytes of flash written
avrdude: verifying flash memory against coba.s.hex:
avrdude: load data flash data from input file coba.s.hex:
avrdude: input file coba.s.hex auto detected as Intel Hex
avrdude: input file coba.s.hex contains 24 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.02s

avrdude: verifying ...
avrdude: 24 bytes of flash verified

avrdude: safemode: Fuses OK (H:FF, E:FF, L:6A)

avrdude done.  Thank you.

Nugrohos-MacBook-Air:coba1 nugroho$ 

The timer's maybe little weird, just my approach to delay by make r17 and r18 to count several time. The code below is tidier timer, :)

.include "../tn13Adef.inc"
.org 0                         

init:
    ldi r16,0b00000100         
    out DDRB,r16                ; PB2 as output

loop:
    sbi PORTB,2                 
    rcall timer                 
    cbi PORTB,2                 
    rcall timer                
    rjmp loop

timer:
    ldi r16,0                   ; these are timer counters
    ldi r17,0
    ldi r18,5

timer2:
    inc r16                     ; do 256 iterations
    brne timer2
    inc r17                     ; do 256 times
    brne timer2
    dec r18
    brne timer2                 ; do 5 times
    ret                         ; return



I love assembler, :)





Kode pertama di atas digunakan untuk menyalakan LED di PortB,0 di ATTiny13A. Kode kedua digunakan untuk menyalakan LED di Pin B0, B1 dan B2.

Kode ketiga menggunakan pola/gaya yang sama dengan kode pertama namun yang dinyalakan kali ini adalah LED di Pin B0, B1 dan B2

Ketiga kode di atas hanya digunakan untuk menyalakan LED sekali saja dan terus menyala, memang ada loop tapi loop tersebut tidak melakukan apapun. 

Untuk membuat led berkedip dalam selang waktu tertentu, kita tambahkan subrutin yang bertindak sebagai time. Bagi saya  agak rumit untuk membuat timer di assembler. Saya belum bisa menggunakan model interrupt, sehingga  saya pakai model loop dengan menyuruh r17 dan r18 berhitung dari 0 sampai f0. Juga tambahkan toggle agar jika led nyala maka dia mati dan sebaliknya