elbear_arduino_bsp/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino
klassents 0fbfd77518 v0.5.3
- начальный загрузчик для плат elbear, elsomik обновлен до версии 0.2.0
- исключено появление краткосрочных просадок при работе ШИМ с максимальным коэффициентом заполнения
- добавлена возможность пропускать код стандартного обработчика прерываний при использовании пользовательского обработчика
- добавлена возможность конфигурирования вывода на вход с притяжкой к земле (INPUT_PULLDOWN)
- исправление известных ошибок
2025-08-06 08:42:39 +03:00

105 lines
3.0 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/Ace-Nano support interrupts,
so only the following can be used for RX: 2, 3, 4, 5, 8, 9
Elsomik board pins with interrupts: P0_8, P1_4, P1_5, P1_6, P1_9, P1_10, P1_15, P2_7
Start board pins with interrupts: P0_10, P0_12, P0_13, P0_14, P0_15, P1_9, P1_15
*/
#include <SoftwareSerial.h>
// software serial #1: RX = digital pin 2, TX = digital pin 6
int rx_pin_1 = 2; // P1_9 for Start/Elsomik
int tx_pin_1 = 6; // P0_2 for Start/Elsomik
SoftwareSerial portOne(rx_pin_1, tx_pin_1);
// software serial #2: RX = digital pin 8, TX = digital pin 10
int rx_pin_2 = 8; // P1_15 for Start/Elsomik
int tx_pin_2 = 10; // P1_3 for Start/Elsomik
SoftwareSerial portTwo(rx_pin_2, tx_pin_2);
// 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();
}
}