Programming The ATtiny85(译)

Programming The ATtiny85(译)

It Packs A Punch!

说明:本文来自https://diyodemag.com/projects/programming_the_attiny85,篇名大概能译作《一文搞定Attiny85编程》。

起因是这样的,这段时间以来,一直在琢磨用Attiny13A/Attiny85做出来最小的频闪光源。或许是做事习惯的缘故——我比较喜欢做事从最困难的做起来,这样余下的就都是满满的自己对自己的鼓励了——选择了最开始的研究对象,是Attiny13A。结果很多教程和资料都因为年代久远而无法配置齐全环境,有些需要下载的东西,各种原因不能达成,但最终总算是烧录成功,实验也做成了。轮到Attiny85的时候,搜索了几篇文章就起手,没有任何的波折就结束了。

后来发现这一篇文章也在,虽然没有使用它,但是异常简单地配置、烧录等等,似乎仍有它的价值,就决定译了它。我是英语特差的那类人,担心误译了文字,故英汉同时放在一起(虽然不少机器翻译很容易操作,然而我仍不喜欢看它们)。

The ATtiny85 IC is one of the smallest microcontrollers you can get (it’s not the smallest Atmel microcontroller – the ATtiny10 has only six pins!), which makes it ideal for those applications where space is at a premium.若是你制作的某个小玩意儿空间有限时,Attiny85大概是你可能使用的最小的IC了(它不是Atmel芯片里就小的,Attiny10比它更小,只有6个引脚!)

HOW IT WORKS怎么做

Unlike an Arduino Uno, an ATtiny85 does not normally have a bootloader. This is a small program that sits in the flash memory, and is what actually runs when you first power it up. You might notice that the onboard LED on an Uno blinks a few times, no matter what sketch is loaded – this is the bootloader letting you know it is there. The bootloader listens in the first second of power-up, searching for a certain sequence of data on the serial port, and if it is detected it goes into sketch upload mode. Without this handy piece of code, we need another way of putting our program onto the chip, and as noted earlier, we can use ISP. Another advantage of not using a bootloader is that the bootloader takes up precious flash program space, and with a small microcontroller like the ATtiny85 – which has just 8kB of flash – anything saved will help.Attiny85与Arduino Uno不同之处,是ATtiny85没有Bootloader。有Bootloader的好处是可以直接在串口上传我们的程序,没有Bootloader则需要另外的方式烧录。但是Bootloader会占用一定的Attiny85的闪存空间,毕竟这个小芯片只有8 kB。

To do a basic test of ISP programming an ATtiny85 with an Uno, you will need an Uno main board, an ATtiny85 IC, a breadboard and some jumper leads. An IC socket can help too. 要对ATtiny85进行ISP编程,需要有Arduino UNO板、ATtiny85芯片、面包板和跳线,有个IC座也会有用。

The first step is to configure the Arduino IDE to work with the ATtiny85, as this is not included by default. The following assumes you are using at least version 1.6.4, as this is when the Boards Manager was introduced. 第一步要做的是配置IDE使之能收拾Attiny85,确保IDE为1.6.4以上版本,因为开发板管理器里面的介绍就这么说的。

Open the Preferences window from File>Preferences and if there are existing entries, add a comma at the end, then add: 去首选项里面填入下面的网址,如果已经有其他的,就用逗号隔开(ATtiny85自动由IDE配置开发板支持库,多已不能用,唯此文介绍竟然一次性安装成功,这表明这篇文章介绍的东西还是比较有价值的。是以译之。)

https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json

Then go to Tools>Board>Boards Manager, search for “attiny” and install the option that looks like this:然后到开发板管理器里安装David A.Mellis这个attiny库,目前是1.0.2。

After this, the Tools>Board menu should give you a few ATtiny options.之后可以选择ATtinyX5系列的开发板,并能进行简单几个设置(前面我使用的不少ATtiny85板卡支持都要进行很麻烦的设置,只有这具设置是相对简单的,对于常用的情况,似乎麻烦的设置没有必要)。

Note that there are a few sub-menu options, so make sure (when working with the ATtiny85) that the options selected are “ATtiny85” and “internal 8MHz oscillator”.注意选择ATtiny85和内部8M振荡器。Be careful not to choose an external oscillator mode, as this might cause the ATtiny85 to appear “bricked”, as it will be expecting an external clock source, which probably isn’t connected.不要选择使用外部晶振,否则在不连接外部晶振时,ATtiny85看起来象是变砖了。

Before building, open the ArduinoISP sketch from File>Examples>11.ArduinoISP>ArduinoISP, and upload it to the Uno (remembering to select Uno again for now), then connect everything up as shown, noting that pin 1 on the ATtiny85 is the top left corner with the notch to the right. If you aren’t familiar with IC pin numbering, the numbers increase anti-clockwise from pin 1.烧录之前,打开ArduinoISP.ino烧录到UNO板,然后连接UNO引脚和ATtiny85引脚。ATtiny85引脚顺序是1号位于左上角,然后逆时针依次2至8.

The connections are as follows:按以下方式连接UNO和ATtiny85的引脚

UNO PIN
ATTINY85 PIN
5V 8
GND 4
D10 1
D11 5
D12 6
D13 7

There is also an extra wire on the Uno, going from RESET to the top right pin on the ISP header – this holds the RESET pin high and keeps the Uno from resetting and being accidentally reprogrammed instead of passing on its programming instructions to the target microcontroller (the ATtiny85). 有一条额外的连线从UNO的RESET到ISP接头的右上角引脚——这使RESET引脚始终为高电平,防止Uno复位和意外重新编程,这条线并不是用来把给ATtiny85编程的。

I’ve also connected an LED between pin 2 (which is confusingly called “pin 3” under the Arduino IDE) and ground via a resistor. With this, we can test basic sketches such as “Blink”.注意面包板上的LED,它串联了一个电阻接在了ATtiny85的2和4即Arduino的D3和GND。接这些小物件,是为了一会测试Blink程序的。

下图是我补充的:

Brand new ATtiny85 ICs are set to have a 1MHz internal oscillator, and so they will appear to run slow if the sketch expects an 8MHz internal oscillator. To fix this, we make sure we have the ATtiny with 8MHz internal oscillator selected from the Tools>Board Menu, select “Arduino as ISP” as the programmer from the Tools>Programmer menu (note: there may be a similar sounding but different programmer called “ArduinoISP”), and that the Uno’s serial port is selected in the Tools>Port menu.全新的ATtiny85频率被设定为1M,这会使8M频率的ino文件运行起来显得慢一些。为了修正这个问题,在把Arduino板设置成烧录器的同时,要设置ATtiny85为内部振荡8MHz。

Click Tools>Burn Bootloader. I did mention before that we aren’t actually using a bootloader, but this is how the Arduino IDE can also set the configuration fuses on the ATtiny85.然后在工具里选择烧录引导程序。

You should see a “Done burning Bootloader” message in the Arduino IDE. If this is the case, the setup is probably correct, and you can open up the Blink sketch and change the references from pin 13 to pin 3.这个时候,在IDE里会看到引导程序烧录成功的提示信息,之后打开Blink,把里面的LED_BUILTIN改成3。

void setup() {
  pinMode(3, OUTPUT);
}
void loop() { loop() {
  digitalWrite(3, HIGH);
  delay(1000);
  digitalWrite(3, LOW);
  delay(1000);
}

Finally, click Upload (Ctrl-U or Sketch>Upload). If the LED starts flashing on for a second and off for a second, then you have a working ATtiny85 programmer on a breadboard.然后烧录到ATtiny85。If you get messages like “Programmer not found”, check that you have the correct programmer selected, that the serial port is correct, and that the ArduinoISP sketch is properly loaded onto the Uno (you might need to temporarily disconnect the wire on RESET to allow the sketch to be uploaded to the Uno).注意使用用编程器来烧录。如果提示编程器找不到,检查UNO是否正确连接,必要是需要RESET一下。

If you get a message like “Wrong microcontroller found”, then check which “board” has been selected (e.g., you might have an ATtiny25 selected by default instead of ATtiny85). If the LED isn’t flashing at the right speed, then you might have an oscillator speed mismatch between the IDE and IC, so reselect the right speed from the Tools menu and select “burn bootloader” before uploading the sketch again.如果提示微控制器不正确,看看设置;如果LED闪烁速度不正常,检查烧录时的振荡频率是否正确。

如果需要,可以使用Shield或者IC锁紧座,参考一些现成的7805稳压电路、加上滤尖峰、防反接等,焊在电路板上,或者把ATtiny85改成328P,改一改电路,自制Arduino也是可能的。

(0)

相关推荐