Is it possible to do SoftWare Serial TX on Timer0 on 328p?

Hi!

I use the S-BUS library for UAV (Rx 100 000 bit/s on hardware serial) and the code with PWM servo generator for 8/16-channel in the my program. PWM servo generator use Timer1 & Timer2. Everything works well, there are no jitter at the PWM output. IDE 1.8.2. Not used in my code delay() , micros(), etc.

Now I needed to add data ( a few bytes from time to time) output Tx on the UART at 9600 or 4800bps. I tried to use the NeoSWserial library for data output. At the outputs of the PWM appeared big jitter. Other software serial library , as far as I learned from the Internet, are not suitable because of the busy timers 1 and 2.

Therefore, the idea came to generate time UART TX using timer 0 with minimal use of CPU time and bit-pulse accuracy as required by the standard UART. But I am not very well versed in timers. Please help, can anyone do something like that?

Thank you,

Gennady

Gennady:
Now I needed to add data ( a few bytes from time to time) output Tx on the UART at 9600 or 4800bps. I tried to use the NeoSWserial library for data output

This doesn't make sense. If you can use the UART you don't need to use SoftwareSerial or a Hardware Timer. The baud rate (and precise timing) is only relevant while one byte is being sent (or received) and the UART takes care of that. It doesn't matter what interval there between bytes.

...R

Robin2:
. If you can use the UART you don't need to use SoftwareSerial or a Hardware Timer.

I will try use Serial.end()and Serial.begin()in Loop().
Thanks!

Changing the speed of the UART is technically possible but I can see it causing all sorts of problems if you have it set for the Tx rate when a message arrives at the Rx rate. If you can avoid that situation then you can change the rate whenever you want with Serial.end() and Serial.begin().

...R

I have no easier way out. I will try. Unfortunately, the Arduino does not use 328PB on its boards, but it’s high time! Thanks!

Maybe you could use a Mega which has 3 spare hardwares serial ports or a Leonardo which has 1 spare port?

Or use SPI or I2C to connect to another '328 just so you can use its UART.

I have some code that can send at 9600 on any I/O pin without using a HardwareTimer that is on the 0 position on an I/O port, (or on any pin with modification) but it blocks interrupts while sending a byte so I suspect it is not suitable for your project.

...R

Hi!

Unfortunately, the print-board for my device has already been done and I can not use anything except one 328P ((( I will try use Serial.end()and Serial.begin()in Loop().

But, if your sketch uses a only timer 0 or 2, then it would be interesting to see. Maybe if two timers are used to regenerate the PWM, the third timer is not very disturbing for them with a short ISR. Or vice versa, T1 and T2 will not interfere strongly with lower priority T0. NeoSW lib use millis() from T0, but make jitter on PWM.

Thanks!

Gennady:
But, if your sketch uses a only timer 0 or 2, then it would be interesting to see.

As I said in Reply #5 my code does not use any Timer - it just bit-bangs the serial data while interrupts are disabled.

What Arduino pins do you have available for serial output?

I have not tried but I suspect that my code could not use Pin1 (Tx) while the UART is using Pin0 (Rx)

the print-board for my device has already been done

Next time I bet you will sort out all the problems before getting the board made :slight_smile:

...R

I think disabling interrupts with two working timer may not work. As well, in principle, many of the pins can be Tx except a few. Now I am trying to synchronize LOOP () with the work of timers in order to have time to switch the UART speed and transmit seven bytes.

Hi!

As a result of the experiments, I came to the conclusion that the FUTABA_SBUS library does not work if the Loop () method uses turning off and on the UART port. Therefore, it will also not work when switching the UART speed to 9600 and then return to 100000 for sbus.

Thanks !

Gennady