SoftwareSerial Library

86Duino 內建支援序列介面在腳位 0 和 1。內建的序列介面由包含在 86Duino CPU 裡的晶片產生,叫做 UART。因為有這個 UART 晶片提供序列介面,讓 86Duino 的 CPU 在處理其他運算時,也可以接收序列介面的輸入。

SofrwareSerial 函式庫提供把一般的數位腳位作為序列介面的功能,用軟體的方式提供和硬體序列介面相同的功能。如果需要產生反向的序列訊號,在初始化時可以利用參數來設定,使得序列介面的輸出為反向。

SoftwareSerial 函式庫從 86Duino Coding 103 開始支援。

限制

SofrwareSerial 函式庫有以下已知的限制:

  • 如果使用多個軟體序列介面,同時間只有一個可以接收資料。
  • 86Duino 並不是每一個數位腳位都支援中斷,所以只有以下的數位腳位可以作為 RX 信號腳位: 18, 19, 20, 33, 34, 35, 36, 37, 38, 42, 43, 44。
  • 鮑率最高支援到 19200 bps。

範例

/*
  Software serial multple serial test
 
  從硬體序列介面接收到的資料透過軟體序列介面傳送出去。
  從軟體序列介面接收到的資料透過硬體序列介面傳送出去。
 
 連接方式: 
 * RX 是腳位 42 (與其他裝置的 TX 連接)
 * TX 是腳位 11 (與其他裝置的 RX 連接)
 */

#include <SoftwareSerial.h>

SoftwareSerial mySerial(42, 11); // RX, TX

void setup()  
{
  // 開啟硬體序列介面並等待啟動完成
  Serial.begin(57600);
  while (!Serial) {
    ;
  }


  Serial.println("Goodnight moon!");

  // 設定軟體序列介面的鮑率
  mySerial.begin(4800);
  mySerial.println("Hello, world?");
}

void loop() // 重複的做以下的動作
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

函式

SoftwareSerial()
available()
begin()
isListening()
overflow()
peek()
read()
print()
println()
listen()
write()


函式庫參考主頁面

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.