一千萬個為什麽

搜索

SoftwareSerial是否被禁止用於ATTiny85/84?

我想在我的ATTiny85上使用SoftwareSerial,當我谷歌時,有幾個項目聲稱他們正在使用它...我從 https://code.google.com/p/arduino-tiny/ ,但是當我嘗試添加SoftwareSerial並編譯時,我得到:

fatal error: SoftwareSerial.h: No such file or directory

將電路板切換到UNO會使問題消失,他們是否也將其排除在外?我怎樣才能解決這個問題?

最佳答案

簡短的回答 - 沒有。相反,它並沒有被排除在外。

但在解決您提到的編譯器錯誤之前,請稍微澄清一下:

我從 https://code.google.com/p/arduino-下載了硬件小/

您提供的鏈接不是硬件,而軟件稱為 ATtiny核心文件。它包含必要的文件,可以幫助您編譯和上傳代碼從Arduino IDE到ATtiny(即使使用ArduinoISP作為程序員)。

SoftwareSerial 是Arduino IDE zip/installation附帶的庫。如果您從 Arduino網站下載了最新版本的Arduino IDE,您應該能夠找到SoftwareSerial庫文件位於:

[Path to Arduino installation folder]\libraries\SoftwareSerial

您應該找到的SoftwareSerial庫文件的示例有 SoftwareSerial.cppSoftwareSerial.h

致命錯誤:SoftwareSerial.h:沒有這樣的文件或目錄

您收到的編譯器錯誤似乎暗示編譯器無法找到SoftwareSerial庫的頭文件(SoftwareSerial.h)。這確實看起來很奇怪,因為這個庫不需要任何特殊安裝,如上所述,它是Arduino IDE安裝中內置的庫的一部分。

所以,為了緩解這種情況,我建議您:

  • Make sure the library folder exists in the location mentioned above
  • If it doesn't, then you should probably re-download the Arduino IDE installation, and after that install the ATtiny core files as per the instructions (important!). Installation instructions are located in the README file inside the ATTiny core zip file (tiny\avr\README).
  • If it does exist, and you still get this compiler error, then you must have a corrupted installation (perhaps the compiler include folders is messed up). I would still recommend following the re-installation as mentioned in the previous bullet.
  • Make sure that you can find the ATtiny 85 in the board list, at Tools->Board and that it is selected.
  • Another angle that might shed more light on this error, would be to turn on the verbose output for the compiler (you can do it in the Arduino IDE, by choosing File->Preferences-> "Show verbose output during" and checking "compilation"). Verbose output can help you track down any compiler command line errors, like missing include folders.
  • Last but not least, if the SoftwareSerial folder exist, you could use the Arduino IDE to check if the IDE can identify its existance, by going to Sketch->Import Library, and looking for SoftwareSerial. If it's not there, then the folder is not located properly under the 'libraries', verify the path as mentioned above (did you move it?)

Finally, when you will be able to compile your code, try using the SoftwareSerial example that comes with the library to check it. Note that it is not going to work 'out-of-the-box' as with the Arduino Uno; The ATtiny does not have the same FTDI hardware for communicating directly with USB, so for sending and receiving serial messages using the ATtiny you will have to either use the Arduino Uno as a proxy (using the Uno's pins 0 & 1) or to use dedicated USB to Serial hardware similar to this one.

將電路板切換到UNO會使問題消失

作為旁註,切換到Uno將使大多數ATtiny問題“消失”,因為Uno比ATtiny85/4更加裝備,無論是在MCU級別還是在突破板級別上,你都可以使用Uno(和直接使用像ATtiny這樣的芯片時沒有得到的結果。此外,大多數代碼基於Uno而不是ATTiny。 因此,如果您正在使用ATtiny邁出第一步,請準備好許多與Uno一起工作的東西在使用ATtiny之前需要額外/不同的代碼(而有些代碼根本不起作用)。

您可以使用我的簡單模擬到串行代碼:

/*
Takes an input on the AtTiny85 from the VERY TRICKY analogue input pin 2 (as labeled in HLT tutorial)
 and outputs these with Software serial to the arduino uno, or Duemillanove
 Hookup
 ATTINY85 pin 3 -> Arduino Uno pin 0
 ATTINY85 pin 4 -> Arduino Uno pin 1

http://www.instructables.com/id/Easy-ATTiny-Serial-Communication-with-Tiny-AVR-Pro/
 */


#include 
// Definitions
#define rxPin 4
#define txPin 3
SoftwareSerial mySerial(rxPin, txPin);
int sensorPin = 7; //ACTUALLY PIN LABELED AS "2" on the HLT tutorial
int sensorVal = -1;


// the setup routine runs once when you press reset:
void setup() {
  pinMode(sensorPin, INPUT);
  mySerial.begin(9600);
analogWrite(0,1);//This is kind a heart beat to show the ATTINY is "alive"
}

// the loop routine runs over and over asensorpingain forever:
void loop() {
  sensorVal = analogRead(sensorPin);
  mySerial.print("Input Val: ");
  mySerial.print(sensorVal);
  mySerial.println();
}

轉載註明原文: SoftwareSerial是否被禁止用於ATTiny85/84?