Thursday, 06 December 2012 16:33

最简单又最便宜的Arduino

这里我们打造一个US3.00的Arduino,只是使用一个ATTiny85(ATTiny45,ATTiny44都可以),所以称它为Tiny Arduino。
 
Tiny Arduino只有八个接脚(Pin),Pin4为接地(Gnd),Pin8为5V(Vcc)Pin1为复位(Reset),Pin2和Pin3原本用来连接Crystal,为了要尽量用完全部IO,所以使用内部振荡(RC Oscillator),因此Tiny Arduino共有五个IO。以下是Tiny Arduino的功能。
  • 五个IO,各个IO可以设成输入(Input)或者输出(Output)
  • 其中两个IO可以设成PWM
  • 三个IO可以设成Analog输入
如果功能不足够,可以通过I2C对外扩展
 
 
材料
  • 1 x 5V电源power supply
  • 1 x Breadboard (Project board)
  • 1 x ATTiny85 (这就是我们的Tiny Arduino)
  • 1 x Arduino duemilanove(用来上传Sketch至ATTiny85)
  • 1 x 10 uF 电容器capacitors(防止Arduino duemilanove自动复位)
  • 5 x 光二极管LED
  • 5 x 电阻器Resistors
  • 以及少许Wire

由于ATTiny85 本身没有串行(Serial)接口,所以我们必须借用外面的编译器来上载Sketch,这里我们使用Arduino duemilanove上载Sketch至ATTiny85。因此我们必须把Arduino duemilanove设成编译器(in-system programmer)简称为ISP。
  • 将Arduino duemilanove连接至电脑
  • 打开Arduino 软件,这里我使用arduino-0023
  • 按File>Examples>ArduinoISP
ArduinoISP
 
  • 按Tools>Board>Arduino Duemilanove or Nano w/ATmega328 或者Arduino UNO
Arduino Board
  • 单击Upload icon开始上载Sketch至Arduino duemilanove
  • 现在你的Arduino duemilanove变成ISP编译器了
 

你必须添加plugin支持ATtiny85:
  • 下载ATtiny85 Plugin
  • 找出Arduino sketchbook文件夹,按File>Peferences,我的sketchbook是在D:\My Documents\Arduino如图下:
Arduino Peferences
  • 如果sketchbook文件夹里没有Hardware文件夹,就自己创建一个
  • 把已经下载的ATtiny85 Plugin解压到Hardware文件夹里。如图下
ATtiny85 45 Plugin
完成这步骤后,你会在Tools>Board看到ATtiny85 (w/Arduino as ISP)
 

连接Arduino duemilanove至ATTiny85,接法如图下:
Connecting ATtin85 to Arduino
 

这里设计一个跑马灯电路图,五粒LED顺序地跑。
5 channel running light
 
  • 按File>New
  • 复制以下跑马灯源码
  • 按Tools>Board>ATtiny85 (w/Arduino as ISP)
Arduino as ISP
  • 单击Upload icon开始上载Sketch至ATtiny85
 
跑马灯源码,只占用了8KByte Flash Memory中的804Byte

// The sketch demonstrate a 5 channel running light using ATTiny45/85 micro controller

#define CH1 0 // ATTiny45/85 PIN5
#define CH2 1 // ATTiny45/85 PIN6
#define CH3 2 // ATTiny45/85 PIN7
#define CH4 3 // ATTiny45/85 PIN2
#define CH5 4 // ATTiny45/85 PIN3

long interval = 1000; // set the LED blink speed (1000ms= one second)

void setup()
{
pinMode(CH1, OUTPUT); // set CH1 as output
pinMode(CH2, OUTPUT); // set CH2 as output
pinMode(CH3, OUTPUT); // set CH3 as output
pinMode(CH4, OUTPUT); // set CH4 as output
pinMode(CH5, OUTPUT); // set CH5 as output
}

void loop() {
digitalWrite(CH1, HIGH); // set the LED on
delay(interval); // wait for a second
digitalWrite(CH1, LOW); // set

digitalWrite(CH2, HIGH); // set the LED on
delay(interval); // wait for a second
digitalWrite(CH2, LOW); // set the LED off

digitalWrite(CH3, HIGH); // set the LED on
delay(interval); // wait for a second
digitalWrite(CH3, LOW); // set the LED off

digitalWrite(CH4, HIGH); // set the LED on
delay(interval); // wait for a second
digitalWrite(CH4, LOW); // set the LED off

digitalWrite(CH5, HIGH); // set the LED on
delay(interval); // wait for a second
digitalWrite(CH5, LOW); // set the LED off
}

 
比较复杂的Sketch,占用了8KByte Flash Memory1508Byte

#define MAX_CHANNEL 5
#define CH1 0 // ATTiny45/85 PIN5
#define CH2 1 // ATTiny45/85 PIN6
#define CH3 2 // ATTiny45/85 PIN7
#define CH4 3 // ATTiny45/85 PIN2
#define CH5 4 // ATTiny45/85 PIN3

int Channel[MAX_CHANNEL] = {CH1, CH2, CH3, CH4, CH5};

long interval = 500; //speed = 0.5 second

void setup() {
//Serial.begin(9600);
for (byte i=0; i<MAX_CHANNEL; i++) {
pinMode(Channel[i], OUTPUT); // declare all channel as output
}

allChannelOn(); //turn on all LED for one second during startup
delay(1000);
}

void loop()
{
runDown();
runKnightRider();
for (byte i=0; i<10; i++) { // loop 10 times for random LED
runRandom();
}
}

///////////////////////// turn on all LED
void allChannelOn(void) {
for (byte i=0; i<MAX_CHANNEL; i++) {
digitalWrite(Channel[i], HIGH);
}
}

///////////////////////// turn off all LED
void allChannelOff(void) {
for (byte i=0; i<MAX_CHANNEL; i++) {
digitalWrite(Channel[i], LOW);
}
}

///////////////////////// run LED in sequence
void runDown(void) {
for (byte i=0; i<MAX_CHANNEL; i++) {
allChannelOff();
digitalWrite(Channel[i], HIGH);
delay(interval);
}
}

void runKnightRider (void) {
allChannelOff();
for (byte i=0; i<MAX_CHANNEL; i++) {
digitalWrite(Channel[i], HIGH);
delay(interval);
}
for (byte i=0; i<MAX_CHANNEL; i++) {
digitalWrite(Channel[i], LOW);
delay(interval);
}
}

///////////////////////// run LED randomly
void runRandom(void) {
int allChannelState, previousState, state;
allChannelState= 0;

for (byte i=0; i<MAX_CHANNEL; i++) {
state= random(2);
allChannelState= allChannelState + state;
digitalWrite(Channel[i], state);
}

if (allChannelState==0) { //if all LED turned off
// reserved
} else {
delay(interval);
}
}


Arduino Tiny支援以下指令
  1. pinMode()
  2. digitalWrite()
  3. digitalRead()
  4. analogRead()
  5. analogWrite()
  6. shiftOut()
  7. pulseIn()
  8. millis()
  9. micros()
  10. delay()
  11. delayMicroseconds()
  12. SoftwareSerial (Arduino 1.0才支持)
这是我的真实例子,原来我的Arduino Duemilanove也是这么简单。
My Arduino ISP
 
最后谢谢High-Low Tech 
 

 

Read 10608 times Last modified on Thursday, 04 April 2013 12:51

Leave a comment

Back to Top