SoftwareSerial Library – Arduino Developer
Allows normal IO pins to be used as bit bashed UART pins
Mục lục bài viết
Resources
https://www.arduino.cc/en/Reference/SoftwareSerial
http://arduiniana.org/libraries/newsoftserial/
Example Usage
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 7); // RX=pin #, TX=pin #
//This also works:
//SoftwareSerial mySerial(PA2, PA3); // RX=pin #, TX=pin #
void audio_init (void)
{
//Set the baud rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello, world?");
}
void tx_packet (void)
{
byte tx_data[4];
mySerial.write("Hello");
tx_data[0] = 0x00;
tx_data[1] = 0x01;
tx_data[2] = 0x02;
tx_data[3] = 0x03;
mySerial.write(&tx_data[0], 4);
}
Receiving Data
Note you can define unlimited software serial ports, but only 1 can be set to receive at a time.
//Enable RX (will occur in background using irq's)
mySerial.listen();
//....
while (mySerial.available() > 0)
{
char data = mySerial.read();
}
//or:
char data = mySerial.read();
if (data != -1)
{
//Byte received
}
Using only 1 pin
Using for RX only
Defining both pins as PA5 on an ATtiny841 and using to receive worked for us.
Compatibility
ATtiny841
RX on pin 2 (RB2) would not work (no explanation found as to why).
RX on pin 5 (PA5) works.
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.