极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 29617|回复: 11

How to use ATtiny13 with Arduino IDE

[复制链接]
发表于 2012-4-11 13:27:31 | 显示全部楼层 |阅读模式
本帖最后由 Micky 于 2013-1-5 18:25 编辑

本帖译自 eLAZB,感谢原作者及翻译过程中所有提供过帮助的朋友。


Lots of different MCUs can be worked on using the Arduino IDE, not just the Arduino boards!
Arduino IDE 支持很多不同的 MCU,不是只有官方 Arduino 板才能用。

Arduino IDE (Integrated Development Environment), currently in version 1.0, is a great system for programming the Arduino boards. It has an easy-to-learn intuitive interface and comes with all the necessary settings for the most common Arduino boards – Uno, Duemilanove, Nano and others based on ATmega168 and ATmega328 microcontrollers. The latest version also includes some of the most recent ATmega1280 and ATmega2560 boards.
Arduino 编译系统 Arduino IDE (Integrated Development Environment) 目前最高版本为1.0。具有直观易用的界面,和常用 Arduino 板的必要设置-Uno、Duemilanove、Nano 和其他基于ATmega168 和 ATmega328 的MCU。最新版的 IDE 还提供了 ATmega1280 和 ATmega2560 的支持。

But you wouldn’t be using all of its great capabilities if you had to switch back to a text editor every time you needed to program a smaller Atmel chip, such as ATtiny13 for example. The engines behind the Arduino IDE – AVR-GCC compiler for C and C++ and avrdude – an in-system programming software for Atmel’s AVR series of micros – are already there and capable of coding for and programming the smaller chips, we just need to add a little bit of configuration magic to the Arduino IDE to be able to switch hardware freely without leaving your favorite Integrated Development Environment. Here is how:
但是如果你需要为烧录一个小的 Atmel 芯片而频繁切换到你需要的编辑器,就无法使用 IDE 提供的全部功能了,比如 ATtiny13。Arduino IDE 背后的引擎 - C 和 C++ 的 AVR-GCC 编译器 和 avrdude - ATmel AVR 系列 MCU 编程系统,已经提供了对小芯片的支持,我们只需要在 Arduino IDE 的设备配置文件中添加一小段代码来实现自如地切换硬件设置而不用离开你心爱的 IDE。具体操作如下:

Arduino IDE keeps the hardware-specific C libraries and other necessary configuration files in a directory called hardware which can be either under the IDE’s root directory (from this point on I’ll call it [arduino_home] for brevity) or the user’s sketchbook directory. I am using a Ubuntu linux machine and my sketchbook directory is ~/sketchbook/. It can be a different location on a Windows PC – please check the manual for Win version.
Arduino IDE 用于配置硬件信息的 C 库文件和必要设置的文件位于 IDE 所在文件夹下的 hardware 文件夹里(本文将简称此文件夹为【arduino_home】),配置信息也可能位于用户自己的 sketchbook 目录下。我所使用的是 Linux Ubuntu 系统,我的 sketchbook 目录位于 ~/sketchbook/。在 Windows 系统中可能位于不同的目录中,请根据Windows 版本自行检查。

The hardware-specific libraries are called cores and they are located under ~/sketchbook/hardware/xxxx/cores where xxxx can be either arduino or a hardware name that the developer of the libraries settled on – tiny, for example. It can also be under [arduino_home]/hardware/arduino/cores/
硬件配置库文件称为 cores,位于 ~/sketchbook/hardware/xxxx/cores,(xxxx 可以是 arduino,或者是开发者指定的硬件名,比如 - tiny)。cores 位于[arduino_home]/hardware/arduino/cores/。

Another file crucial for IDE’s understanding of what hardware you’re working on is a file called boards.txt which is also located at ~/sketchbook/hardware/xxxx/boards.txt or /[arduino_home]/hardware/arduino/boards.txt The file contains information about the hardware (the “board” can be a stand-alone MCU, too ), such as the clock speed of the processor, high and low fuse bytes – the configuration settings that get “burned” into the microprocessor and define the clock source, timer prescaler and other important settings that don’t need to be updated every time you re-upload your sketch. boards.txt file can also prescribe how fast the programmer should talk to the MCU – an important issue for MCUs such as ATtiny that can be slowed down greatly by using an internal clock source.
IDE 中另一个重要的文件 - 让 IDE 知道你正在给什么样的 MCU 编写程序 - boards.txt,位于 ~/sketchbook/hardware/xxxx/boards.txt 或者 /[arduino_home]/hardware/arduino/boards.txt。这个文件包含的硬件(或者可以只是一个单独的芯片)信息包括:时钟频率、高低熔丝位、MCU 烧录配置和定义时钟源、预分频器和其他一些不必每次重新上传都更新的必要设置。boards.txt 还可以指定编译器应该以多快的速度与微控制器对话 - MCU (如ATtiny)的一个重要特性 - 可以使用内置时钟源大幅放缓。

So far I came across three different sets of libraries suitable for programming of ATtiny microcontrollers:
到目前为止,我收集到三套可用的可以编译 ATtiny 的库:

Damellis’es ATtiny cores (can support ATtiny13 with additional configuration changes)
Damellis’es ATtiny cores (通过添加设置支持 ATtiny13)
Arduino-Tiny Cores (does not seem to support ATtiny13)
Arduino-Tiny Cores (似乎不支持 ATtiny13)
smeezekitty’s core13 (ATtiny13 only)
smeezekitty’s core13 (只支持 ATtiny13)

Most of them contain versions of several basic Arduino functions that can run on MCUs with such small Flash and SRAM sizes as the ATtiny. Most often supported functions include:
上述大多支持一些 Arduino 的基本功能,可以在像 ATtiny 这样小 Flash 和 SRAM 的 MCU 上运行。通常支持的功能包括:

millis()
micros() (not really a true microsecond)
delay()
delayMicroseconds() (not really a true microsecond)
analogRead()
analogWrite()
pinMode()
digitalRead()
digitalWrite()

Keep this set of functions in mind when writing your software – more complex functions such as math and trigonometry functions may work but will probably exhaust resources of the little MCU too quickly.
在编写你的程序时应该注意 - 一些更高级的功能,比如数学运算和三角函数也许是可用的,但很可能会很快耗尽 MCU 的有限资源。

All ATTiny cores install in a similar way:
上述所有 ATtiny cores 的引入方式都是类似的:

1.Shut down the Arduino IDE
1.关闭 Arduino IDE

2.Identify the sketchbook directory (on Linux look for ~/sketchbook/) and create a new directory called hardware: ~/sketchbook/hardware.
2.定位到 sketchbook 目录(在我的 Linux 上是 ~/sketchbook/),新建 hardware 文件夹,像这样 ~/sketchbook/hardware。

3.If the core is zipped with the directory structure, simply unzip the content of the zip file into the ~/sketchbook/hardware/ directory – it will create the subdirectory needed. Else, manually create a subdirectory called something like “tiny” (or “core13″ as smeezekitty suggests for his core13 core. The name of the directory can be rather arbitrary, as long as it is the same as set up in boards.txt (below)
3.如果 cores 是按目录结构压缩的,直接解压到 ~/sketchbook/hardware/ 即可,所需目录会自动创建。否则,手动创建子目录,比如 “tiny”(或者“core13” - smeezekitty 给 core13 的命名。目录名称可以随意,只要和boards.txt 中定义的一致就可以了,见下文)

4.Inside the ~/sketchbook/hardware/tiny/ directory, edit or create a new file called boards.txt (see the format below)
4.在 ~/sketchbook/hardware/tiny/ 目录中,编辑或新建 boards.txt(格式见下文)

The boards.txt file for ATTiny should contain this data (core13 core is used, replace with your core name if it’s different)
ATtiny 的 boards.txt 文件应该包含这些信息(core13 已经被用了,如果 core 不同请自行起名)
  1. ###########################################################################

  2. attiny13.name=Attiny13 @ 128 KHz (internal watchdog oscillator)

  3. attiny13.upload.using=arduino:arduinoisp
  4. # attiny13.upload.protocol=avrispv2
  5. # attiny2313at1.upload.using=pololu

  6. attiny13.upload.maximum_size=1024
  7. attiny13.upload.speed=250 # important for not losing connection to a slow processor

  8. attiny13.bootloader.low_fuses=0x7B
  9. attiny13.bootloader.high_fuses=0xFF

  10. attiny13.bootloader.unlock_bits=0x3F
  11. attiny13.bootloader.lock_bits=0x3F

  12. attiny13.build.mcu=attiny13
  13. attiny13.build.f_cpu=128000
  14. attiny13.build.core=core13

  15. ###############################################################

  16. attiny13at4.name=ATtiny13 @ 4.8MHz (internal 4.8 MHz clock)
  17. attiny13at4.bootloader.low_fuses=0x69
  18. attiny13at4.bootloader.high_fuses=0xff
  19. attiny13at4.upload.maximum_size=1024
  20. attiny13at4.build.mcu=attiny13
  21. attiny13at4.build.f_cpu=600000
  22. attiny13at4.build.core=core13
  23. ###############################################################

  24. attiny13.name=ATtiny13 @ 9.6MHz (internal 9.6 MHz clock)
  25. attiny13.bootloader.low_fuses=0x7a
  26. attiny13.bootloader.high_fuses=0xff
  27. attiny13.upload.maximum_size=1024
  28. attiny13.build.mcu=attiny13
  29. attiny13.build.f_cpu=9600000L
  30. attiny13.build.core=arduino:arduino
  31. attiny13.build.variant=tiny8
  32. ###########################################################################
复制代码

Arduino IDE 1.0 Pick the right Programmer before uploading the software
Arduino IDE 1.0 上传前选择正确的编程器

5.Once the configuration files are edited, start the IDE. In the Programmer section select the programmer you will be using (see picture on the left). I am using the Atmel AVRISP mk II USB programmer but Arduino itself can also be used for ICSP programming of AVR MCUs
5.编辑 boards.txt 之后启动 Arduino IDE,在 Programmer 菜单中选择将使用的编程器(见上图),我使用的是 Atmel AVRISP mk II USB ,Arduino 可以使用 ICSP 方式为 AVR MCU 编程。

6.In the Tools->Board menu select the MCU and the clock speed you will be using (see picture at the top of the page).
6.在 Tools -> Board 菜单中选择 MCU 类型和时钟频率(见上图)。

7.Before uploading any actual code, run “Burn Bootloader” – a menu item right next to “Programmer” under “Tools”. There is no Arduino bootleader for ATtiny because of the small memory and I/O size but the feature actually burns the low and high fuse bytes – the configuration step needed for making sure the MCU runs at the speed that you assume it does.
7.在上传程序之前,运行 "Tools" 菜单下的 “Burn Bootloader”。虽然因为内存太小和 I/O 口数量太少,选项中没有ATtiny 的 bootloader,但是这个功能的确是把高低熔丝位给烧进去了 - 上面的配置步骤对于确认MCU在你设定的速度运行是必须的。

8.Write some code while keeping in mind the hardware limitations – for ATtiny13 it’s 1024Byte FLASH and 64 byte SRAM and, obviously, only 5 x I/Os if you are planning to load the code via ICSP – the most convenient way to load the code. If you are going to use a different programming technique (which Arduino IDE won’t do, however), you can reuse the Reset pin as a 6th I/O.
8.写点代码,别忘了那些硬件限制 - ATtiny13 只有 1024Byte 的 FLASH 和 64 byte 的 SRAM,最方便的上传代码的方式是通过 ICSP,如果你计划用 ICSP,显然,只有 5 个 I/O 口可用。但如果是用其他方式上传代码的话,可以将 Reset 脚复用做第六个 I/O 口。


Connect ATtiny13 to the programmer as shown on this diagram
按此图所示连接 ATtiny13 和 编程器

9.At this point in time, you should connect your ATtiny13 MCU to the programmer’s ICSP 6-pin header using the pinout in the picture on the left. If you’re building your project on a breadboard, it is always handy to have 6 x 18AWG colored wires soldered to a dual row 6-pin male header – it will make the programmer connection easy and can be reused from project to project. +1.9V to +5.5V power should be applied to Vcc before programming the chip.
9.现在,你应该如上图所示将 ATtiny13 MCU 和 编程器的 ICSP 6 引脚连接好。如果你是用面包板连接的,最好用 6 条彩色 18AWG 线做一条 6-pin 的 ICSP 线公头 - 这将使软硬件连接变得简单,并且是重复使用的。注意 Vcc 脚应该接 +1.9V 到 +5.5V。

10.After you (think) you are done with the program, use the Upload Using Programmer [Ctrl-Shift-U] to compile the program and load it into the MCU. Remember that the power should be applied to the MCU before programming it!
10.程序写好后,按 [Ctrl-Shift-U] 编译并上传到 MCU。别忘了电压应该和 MCU 相匹配。


Your program is now running! Or not … If it has bugs in it. If you’re not getting the intended operation of the MCU, verify the program and upload it again (steps 8-10).
现在程序应该成功运行了,除非是有 bug。如果没有得到预期的结果,仔细检查然后重新上传。

One of the things to keep track of while writing a code for ATtiny13 is the amount of RAM your program is using. For example, each declared variable gets a space in RAM, and you should always use the shortest possible variable type. For example, if you have a counter that does not go over 255, there’s no need to use a 2-byte int type if 1-byte byte type will suffice. With only 64 bytes of RAM every byte counts!
保证代码在 ATtiny13 上正常运行所应该考虑到的一个问题就是程序正在使用的 RAM 数量。比如,每个声明的变量得到一个内存空间,所以声明变量时应该用占用空间最小的类型。例如程序中用到一个计数器,不会超过 255 个,就没有必要使用 2 个字节的 int 类型,使用 1 个字节的 byte 类型就足够了。每个字节计数器只有 64 个字节的 RAM!

If you’re declaring considerable size constants, such as arrays or text strings, consider using PROGMEM to move them from RAM to FLASH space, where you might have much more space to work with.
如果你声明了很多常量,比如数组或字符串,记得用 PROGMEM 将他们从 RAM 移到 FLASH,来保证有足够的 RAM用来流畅地运行程序。

I’ve had great fun programming ATtiny13 chips with Arduino IDE. The IDE provides a very comfortable environment to work with and the limitations of the small MCUs like ATtiny13 make the programming even more interesting. If you haven’t yet, I think you should definitely add the ATtiny to your projects.
用 Arduino IDE 给 ATtiny13 芯片编程非常有意思。IDE 提供了非常舒服的编程环境,ATtiny13 这样的小型 MCU 的种种限制也使得编程更具挑战性。如果你还没有尝试过,我建议你试着玩一玩 ATtiny。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

评分

参与人数 1 +1 收起 理由
幻生幻灭 + 1 赞一个!

查看全部评分

回复

使用道具 举报

发表于 2012-4-11 13:51:37 | 显示全部楼层
偶要用的,先占个沙发.
回复 支持 反对

使用道具 举报

发表于 2012-4-12 08:42:20 | 显示全部楼层
很给力的翻译,但是还没弄清楚,这又是啥IDE?
回复 支持 反对

使用道具 举报

发表于 2012-4-16 08:24:04 | 显示全部楼层
Randy 发表于 2012-4-12 08:42
很给力的翻译,但是还没弄清楚,这又是啥IDE?

IDE是Arduino IDE可以在http://arduino.cc/en/Main/Software下载
回复 支持 反对

使用道具 举报

发表于 2012-4-16 08:49:28 | 显示全部楼层
Tim_Clancy 发表于 2012-4-16 08:24
IDE是Arduino IDE可以在http://arduino.cc/en/Main/Software下载

好的,谢谢哦。新出的IDE?还是早就有了!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-4-19 11:46:49 | 显示全部楼层
译完了,顶下贴哈
回复 支持 反对

使用道具 举报

发表于 2012-5-26 23:19:23 | 显示全部楼层
attiny13 1k 太小了,很不好折腾
回复 支持 反对

使用道具 举报

发表于 2012-7-2 17:43:19 | 显示全部楼层
tiny13用Arduino来开发不合适, 编译出来的程序太大了, 我一般用WINAVR来编译
回复 支持 反对

使用道具 举报

发表于 2012-7-29 17:03:33 | 显示全部楼层
..........
回复 支持 反对

使用道具 举报

发表于 2013-1-8 18:27:32 | 显示全部楼层
第一个支持包已经找不到了,不能下载了,第三个只能选4.8MHZ
回复 支持 反对

使用道具 举报

发表于 2013-7-16 15:50:48 | 显示全部楼层
有没有支持ATtiny26的IDE?
回复 支持 反对

使用道具 举报

发表于 2013-10-4 23:17:39 | 显示全部楼层
好贴!好贴!好贴!好贴!好贴!好贴!好贴!好贴!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-29 09:35 , Processed in 0.048050 second(s), 30 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表