|Shopping Cart Checkout Shipping Cost Download Website|
|Home||MP3 Player||8051 Tools||All Projects||PJRC Store||Site Map|
|You are here: Teensy Teensyduino Libraries AltSoftSerial|
AltSoftSerial LibraryAltSoftSerial emulates an additional serial port, allowing you to communicate with another serial device.
AltSoftSerial is particularly useful when simultaneous data flows are needed. More details below.
AltSoftSerial is capable of running up to 57600 baud on 16 MHz AVR with up to 9 µs interrupt latency from other libraries. Slower baud rates are recommended when other code may delay AltSoftSerial's interrupt response by more than 9 µs.
Serial Port Options
The "Unusable PWM" pins can be used normally, with digitalRead() or digitalWrite(), but their PWM function controlled by analogWrite() will not work properly, because AltSoftSerial uses the timer which controls that pin's PWM feature.
Basic UsageAltSoftSerial mySerial;
Create the AltSoftSerial object. Only one AltSoftSerial can be used, with the fixed pin assignments shown above.mySerial.begin(baud);
Initialize the port to communicate at a specific baud rate.mySerial.print(anything);
Print a number or text. This works the same as Serial.print().mySerial.available();
Returns the number of bytes received, which can be read.mySerial.read();
Reads the next byte from the port. If nothing has been received, -1 is returned.
However, some libraries may disable interrupts for longer than 1 bit time. If they do, AltSoftSerial will not work properly. If you discover erroneous results from AltSoftSerial, please try testing without other libraries.
Arduino HardwareSerial Issue #776Update: Arduino 1.0.1 fixed this problem.
HardwareSerial on Arduino Uno and Arduino Mega contains inefficient code, which can interfere with AltSoftSerial or NewSoftSerial at 115200 baud. A simple fix is available to reduce the interrupt latency.
Teensyduino and Sanguino do not have this issue.
Timer UsageAltSoftSerial uses a 16 bit hardware timer to generate the transmit output and measure the receive input waveforms. Any other library which needs the same timer, and the PWM pins which depend upon that timer, will not work.
Some boards, like Teensy, Teensy++ and Arduino Mega, have more than 1 timer which is suitable for AltSoftSerial. You can configure which timer AltSoftSerial uses by editing "config/known_boards.h" within the library. This may allow AltSoftSerial to be used together with other libraries which require the timer which AltSoftSerial uses by default.
Without other libraries, on Teensy or Arduino (with the issue 776 fix), interrupt latency is about 3 to 4 µs. 115200 baud is possible.
However, the maximum baud rate is often not the most important question. Each library imposes interrupt latency on other libraries. AltSoftSerial causes approximately 2-3 µs latency. NewSoftSerial causes 10 bit times of latency for other libraries. Running at 57600 baud, that's 174 µs! This latency is the primary difference between AltSoftSerial and NewSoftSerial.
To see this in action, you can try the example that comes with SoftwareSerial in Arduino 1.0. If you type "Goodnight" in the Arduino Serial Monitor, you'll see what actually comes out of pin 3 at 4800 baud is "Goot". The characters "dnigh" are lost. The reason is because while SoftwareSerial is sending the letter "G" at 4800, the letters "oodnigh" arrive at 57600 baud. Only "oo" are held in the UART registers. The rest are lost because interrupts were disabled for too long. AltSoftSerial can handle this test easily, since it does not lock out interrupts for long times.
Using Both NewSoftSerial and AltSoftSerialIt is possible to use both NewSoftSerial and AltSoftSerial, and of course HardwareSerial, to have 3 serial ports! However, the baud rates must be chosen carefully!
Because NewSoftSerial creates 10 bit times of latency for other libraries, it should be used for a device needing high baud rate. NewSoftSerial should NOT be used at slow baud rates, because it will interfere with the other ports. NewSoftSerial can not simultaenously transmit and receive, so it should be used with a device that never sends in both directions at once.
HardwareSerial can tolerate up to 20 bit times latency for receive, or 10 bit times for non-stop transmit. To maintain full transmit speed, HardwareSerial's baud rate should be no greater than NewSoftSerial's. If continuous transmit is not needed, HardwareSerial can use a baud rate almost twice as fast as NewSoftSerial and still reliably receive. But more than twice NewSoftSerial's baud will not be able to receive reliably.
AltSoftSerial can tolerate almost 1 bit time latency, so its baud rate must be at least 10 times less than the baud rate used for NewSoftSerial.
If the baud rates are chosen wisely, all 3 can work together reliably!