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

ATtiny45: blink на чистом Си

разделы: AVR , дата: 7 марта 2014г.

Немного раньше, я расказывал о том, как с помощью Arduino IDE программировать младшее семейство микроконтролеров AVR на примере ATtiny45. В последующем посте я обратил внимание на то, что вес прошивки составил 802 байта. Для ATtiny45 эта цифра не выглядит страшно, но в лишний раз подумаешь что бы связываться c ATtiny 25/24 с их двумя килобатами флеш-памяти. Давайте напишем Blink на Си и посмотрим на размер бинарника.

Как видно на 5-ом пине назначен порт PB0. На него и будем подключать светодиод.

Исходник:

// blink.c for AVR ATtiny 25/45/85

#include <avr/io.h>
#define F_CPU 1000000UL // частота резонатора 1МГц
#include <util/delay.h>
int main(void) {
 // макрос _BV(число) заменяет конструкцию (1 << число)
 DDRB |= _BV(PB0); // аналог pinMode(PB0,OUTPUT); в Wiring
 for (;;) {
  PORTB ^= _BV(PB0); // инвертируем состояние порта PB0
  _delay_ms(1000); // ждем 1 секунду
 }
 return 0;
}

Компиляция:

$ avr-gcc -mmcu=attiny45 -Wall -Os -o blink.elf blink.c
$ avr-objcopy -O ihex blink.elf blink.hex

Прошивка:

# avrdude -p t45 -c gromov -P /dev/ttyS0 -v -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
         AVR Part                      : ATtiny45
         Chip Erase delay              : 4500 us
         PAGEL                         : P00
         BS2                           : P00
         RESET disposition             : possible i/o
         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     6     4    0 no        256    4      0  4000  4500 0xff 0xff
           flash         65     6    32    0 yes      4096   64     64  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  9000  9000 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          2    0      0     0     0 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 = 0x1e9206
avrdude: safemode: lfuse reads as 62
avrdude: safemode: hfuse reads as DF
avrdude: safemode: efuse 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 (82 bytes):
Writing | ################################################## | 100% 0.06s

avrdude: 82 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 82 bytes
avrdude: reading on-chip flash data:

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

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

avrdude: safemode: lfuse reads as 62
avrdude: safemode: hfuse reads as DF
avrdude: safemode: efuse reads as FF
avrdude: safemode: Fuses OK (H:FF, E:DF, L:62)

avrdude done.  Thank you.

Обратите внимание на выделенную красную строку. Размер прошивки 82 байта против 802 на Wiring. Разница почти на порядок.

Еще один момент. Для программ на Си доступен весь модельный ряд микроконтроллеров AVR, а не какой-то ограниченный набор.