forked from Elron_dev/elbear_arduino_bsp
- добавлена библиотека softwareSerial. Скорость работы до 57600 включительно и на отправку, и на прием. - в bootloader добавлена проверка контрольной суммы - добавлен файл wiringLL.h, в котором собраны некоторые макросы. Этими макросами заменены функции в местах, чувствительных к длительности выполнения кода. - общие функции обработки прерываний перенесены в ram память для увеличения скорости выполнения. Так же в память ram перенесены функции обработки прерываний модулей WInterrupt (прерывания от gpio), модуля tone, модуля softwareSerial - добавлен файл-заглушка util/delay.h, необходимый для некоторых библиотек
100 lines
2.6 KiB
C++
100 lines
2.6 KiB
C++
/*
|
|
Software serial multiple serial test
|
|
|
|
Receives from the two software serial ports,
|
|
sends to the hardware serial port.
|
|
|
|
In order to listen on a software port, you call port.listen().
|
|
When using two software serial ports, you have to switch ports
|
|
by listen()ing on each one in turn. Pick a logical time to switch
|
|
ports, like the end of an expected transmission, or when the
|
|
buffer is empty. This example switches ports when there is nothing
|
|
more to read from a port
|
|
|
|
The circuit:
|
|
Two devices which communicate serially are needed.
|
|
* First serial device's TX attached to digital pin 2(RX), RX to pin 6(TX)
|
|
* Second serial device's TX attached to digital pin 8(RX), RX to pin 10(TX)
|
|
|
|
Note:
|
|
Not all pins on the Elbear Ace-Uno support interrupts,
|
|
so only the following can be used for RX:
|
|
2, 3, 4, 5, 8, 9
|
|
*/
|
|
|
|
#include <SoftwareSerial.h>
|
|
|
|
// software serial #1: RX = digital pin 2, TX = digital pin 6
|
|
SoftwareSerial portOne(2, 6);
|
|
// software serial #2: RX = digital pin 8, TX = digital pin 10
|
|
SoftwareSerial portTwo(8, 10);
|
|
|
|
// active port is changed by user button pressing
|
|
volatile bool isPortOneActive = true;
|
|
void btn_pressed_callback(void)
|
|
{
|
|
// change active port
|
|
if (isPortOneActive)
|
|
{
|
|
portTwo.listen();
|
|
Serial.println("Port two is listening");
|
|
isPortOneActive = false;
|
|
}
|
|
else
|
|
{
|
|
portOne.listen();
|
|
Serial.println("Port one is listening");
|
|
isPortOneActive = true;
|
|
}
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
// Open serial communications and wait for port to open
|
|
Serial.begin(115200);
|
|
while (!Serial) {
|
|
; // wait for serial port to connect. Needed for native USB port only
|
|
}
|
|
|
|
// attach interrupt to user button
|
|
attachInterrupt(digitalPinToInterrupt(BTN_BUILTIN), btn_pressed_callback, FALLING);
|
|
|
|
// Start each software serial port
|
|
portOne.begin(57600);
|
|
portTwo.begin(57600);
|
|
|
|
// First listen to port one
|
|
portOne.listen();
|
|
isPortOneActive = true;
|
|
Serial.println("Port one is listening");
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
// check data from port one
|
|
if (portOne.available())
|
|
{
|
|
Serial.println("Data from port one:");
|
|
// while there is data coming in, read it
|
|
// and send to the hardware serial port
|
|
while (portOne.available() > 0)
|
|
Serial.write((char)portOne.read());
|
|
|
|
// blank line to separate data from the two ports
|
|
Serial.println();
|
|
}
|
|
|
|
// check data from port two
|
|
if (portTwo.available())
|
|
{
|
|
Serial.println("Data from port two:");
|
|
// while there is data coming in, read it
|
|
// and send to the hardware serial port
|
|
while (portTwo.available() > 0)
|
|
Serial.write((char)portTwo.read());
|
|
|
|
// blank line to separate data from the two ports
|
|
Serial.println();
|
|
}
|
|
}
|