Зеркало сайта: vivacious-stockings-frog.cyclic.app

Blink на ассемблере AVR на примере ATtiny13

разделы: AVR , ATtiny13A , АССЕМБЛЕР , дата: 18 марта 2014г.

Если разобраться в предыдущем посте: дизассемблирование blink.hex, где подробно разобрана реализация blink на ассемблере AVR, то проблем с написанием собственного варианта Blink на ассемблере не будет. Вообще-то программа там уже выложена. Осталось только разобраться с преобразованием исходников в hex файлы.

Мой вариант blink.asm:

.equ DDRB, 0x17
.equ PB0, 0x00
.equ PORTB, 0x18
.org 0x00 ; начало
        sbi     DDRB, PB0;   порт PB0 на передачу
        ldi     r25, 0x01;   r25=1
; главный цикл
loop:
        in      r24, PORTB;  r24=PORTB
        eor     r24, r25;    r24 = r24 xor r25
        out     PORTB, r24;  PORTB=r25
        ldi     r18, 0x3F;   r18=0x3F
        ldi     r19, 0x0D;   r19=0x0D
        ldi     r24, 0x03;   r24=0x03
sleep:
        subi    r18, 0x01;  (r18r19r24)-1  вычитание трехбайтного целого числа
        sbci    r19, 0x00
        sbci    r24, 0x00
        brne    sleep;     если значение в r24 не равно нулю, то переход на начало операции вычитания
        rjmp    loop;      возврат на начало главного цикла

Компиляция:

avr-as -mmcu=attiny13 -o blink.o blink.asm
avr-ld -o blink.elf blink.o
avr-objcopy --output-target=ihex blink.elf blink.hex

Смотрим, все ли в порядке:

$ avr-objdump -m avr -D blink.hex

blink.hex:     file format ihex


Disassembly of section .sec1:

00000000 <.sec1>:
   0:   b8 9a           sbi     0x17, 0 ; 23
   2:   91 e0           ldi     r25, 0x01       ; 1
   4:   88 b3           in      r24, 0x18       ; 24
   6:   89 27           eor     r24, r25
   8:   88 bb           out     0x18, r24       ; 24
   a:   2f e3           ldi     r18, 0x3F       ; 63
   c:   3d e0           ldi     r19, 0x0D       ; 13
   e:   81 e0           ldi     r24, 0x03       ; 3
  10:   21 50           subi    r18, 0x01       ; 1
  12:   30 40           sbci    r19, 0x00       ; 0
  14:   80 40           sbci    r24, 0x00       ; 0
  16:   e1 f7           brne    .-8             ;  0x10
  18:   f5 cf           rjmp    .-22            ;  0x4

Здесь нет таблицы прерываний, так как они не используются.

Если все ОК, прошиваем микроконтроллер.

# avrdude -p attiny13 -c gromov -v -P /dev/ttyS0 -b9600 -U flash:w:blink.hex

avrdude: Version 6.0.1, compiled on Nov 25 2013 at 14:33:17
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "/etc/avrdude.conf"
         User configuration file is "/root/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyS0
         Using Programmer              : gromov
         Overriding Baud Rate          : 9600
         AVR Part                      : ATtiny13
         Chip Erase delay              : 4000 us
         PAGEL                         : P00
         BS2                           : P00
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65     5     4    0 no         64    4      0  4000  4000 0xff 0xff
           flash         65     6    32    0 yes      1024   32     32  4500  4500 0xff 0xff
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          2    0      0     0     0 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00

         Programmer Type : SERBB
         Description     : serial port banging, reset=dtr sck=rts mosi=txd miso=cts

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9007
avrdude: safemode: lfuse reads as 6A
avrdude: safemode: hfuse reads as FF
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: reading input file "blink.hex"
avrdude: input file blink.hex auto detected as Intel Hex
avrdude: writing flash (26 bytes):

Writing | ################################################## | 100% 0.01s

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

Reading | ################################################## | 100% 0.01s

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

avrdude: safemode: lfuse reads as 6A
avrdude: safemode: hfuse reads as FF
avrdude: safemode: Fuses OK (H:FF, E:FF, L:6A)

avrdude done.  Thank you.

В итоге получаем размер прошивки равной 26 байтом, т.е. 13 команд. Не знаю, стоит ли сейчас вспоминать сколько весит Blink написаный на Wiring)