Serial library with non standard bit rates?

Hi all,

I am searching for a serial library which can use non-standard bit rates, e.g. 19304 bit/s on a digital port. In fact I just only need to send at this speed with no start and stop bits - just one byte after another. I am not new to programing but new to Arduino and microcontrollers, so I hope someone can give me a hint where I can find such a library or how to set up such functions (I think with timers an interrupts, or?)

Thanks, Holger

The source code for the HardwareSerial class, which is what Serial is an instance of, is available on your computer. Hack away (at a copy).

If the baudrates are non standard but fixed (known in advance) you can patch the NewSoftSerial internal tables relative easily.

I recently tested and noted that standard baudrates work +/- 5%
==> So if you make a table with steps chosen carefully (5% maybe 8%) you should be able to cover any speed.

to manage a range
from 1200 - 2400 (factor 2) you need log(2) / log(1,05) = 15 steps [8% steps => 9]
from 1200 - 115200 (factor 96) you need log(96) / log(1,05) = 93 steps [8% steps => 60]

Alternative is to derive a formula for the timing parameters for NewSoftWareSerial (possible but not trivial, Excel might be helpfull)

It sounds like you are talking about synchronous communication - no start or stop bits. If that is the case, you can use the SPI function of the processor. For a 16MHz processor, you would set the baud rate register to 413 to get the baud rate you are looking for.Read the Atmel328 data sheet topic about the USART in SPI mode.

It looks like the SPI library will do what you are looking for but you'll have to modify the source for your custom baud rate.

Hi all,

thanks for your answers!
I think I will try to patch NewSoftSerial or use the SPI library. After testing I will post my results here.

Have a nice weekend,

Holger

Do you plan to use NewSoftSerial with an on chip UART or with digital pins? If you don't have a hardware UART, and you're working with sync data, you're going to be banging your head against the wall. Its easy enough to make it move data, but as soon as you try to do something with that data at the same time, its gets frustrating.

Hi skyjumper,

no UART :slight_smile:
I need to generate a signal for driving digital modell locomotives (not DCC but Motorola-Märklin). So my other end of the communication is a decoder chip inside the locomotive. I have a rough manual how to generate the frames with a serial line at 19200 bit/s but you have to do some strange timing things to get the right signal. I thaught that it is better to generate the signal without that strange things but for this you "might" have to send the frames with a non-standard bit rate (s. http://modell-zug.com/Support/HowToArticles/DigCtrlSys/DigitalTransmission.htm). I read some documentation where they don't use exactly 4800 bit/s... but I'm not sure.

Regards, Holger

It sounds like you are talking about synchronous communication - no start or stop bits. If that is the case, you can use the SPI function of the processor.

The Arduino hardware SPI is limited to a fixed set of frequencies derived from the processor clock divided by powers of two. Successive bytes may not be bit-synchronous with each other - you'd have to test that.

To generate an accurate fixed frequency the use of timer1 (which has 16bit precision) is a possible way - using a timer interrupt would be the most accurate way to use that timer to push bits out.

However the UART is probably the simplest way (if you can configure 0 start bits and 0 stop bits?)

Hi Codeman

Just a thought, have you considered using the Motorola Trinary Encoder to handle the packet generation and timing. It has its own oscillator timing.

You would have to provide the 8 bit byte to the chip, but it may simplify your processor design/programming as the chip would handle packet generation and the resend for reliability.

Quick google, and the chip appears to be in the $15-$20 US range.

Regards Bill

Codeman:
Hi skyjumper,

no UART :slight_smile:
I need to generate a signal for driving digital modell locomotives (not DCC but Motorola-Märklin). So my other end of the communication is a decoder chip inside the locomotive. I have a rough manual how to generate the frames with a serial line at 19200 bit/s but you have to do some strange timing things to get the right signal. I thaught that it is better to generate the signal without that strange things but for this you "might" have to send the frames with a non-standard bit rate (s. http://modell-zug.com/Support/HowToArticles/DigCtrlSys/DigitalTransmission.htm). I read some documentation where they don't use exactly 4800 bit/s... but I'm not sure.

Regards, Holger

Which Arduino are you using? You should be able to do this with a bunch of them. For example, the 324/644/1284 chip should be able to do this nicely with its onboard USART. These USARTs can do quite a bit more then the arduino code allows as is, but its very easy to hack the code to set up the USART as you need it. If you look at the HardwareSerial.cpp begin() method, you'll see that there is not too much to it.

I took a look at the data sheet for the system you are trying to simulate and I don't think a USART is going to do the job. The Motorola decoder expects a "binary-encoded trinary" signal with two different bit times, not the uniform bit times that conventional USARTs use. You could probably modify SoftwareSerial to accomplish what you're after.

Hi all,

thanks for your responses!

@MarkT: I hoped to find a way to avoid writing the interrupt driven code :slight_smile: but it seems that I have to write such library (which will take more time for me than I thaught). My problem is that I have no scope, so I have to do trial & error for the signal generation (that's a problem with all other solutions, too).

@Bill_Kerr: Yes, that's also a possibility to avoid the software signal generation code. But I know that there is a working(!) software implementation based on UARTs (srcpd version 2 documentation). I will keep in mind to change to the hardware signal generation when everything will fail.

@skyjumper: I will use the the Arduino UNO (328P). It seems that someone has made a Port from OpenDCC to the Arduino (http://developer.berlios.de/projects/microsrcp/) which is also based on UART output (like srcpd). But I hope to get arround the tricky timings when using the "right" bit rate to communicate with.

It seems that I have to write my own signal generation code with interrupts or I have to use the hardware encoder from Motorola...

Regards, Holger