Lennard Electronics

Kiwi made, innovative electronics

Bit Bashed UART in C for 8051

I've had a requirement recently for a project with 3 UARTs (one TX & RX, one TX only, one RX only).
Most 8051 based MCU's only have 1 built in UART, so you need to "bit bash" a solution for the others.

Below is example code, written in SDCC, for a Silicon Labs C8051F005.  This is a fairly powerful 8051 derivative, and one of the major differences is that you can choose to have your timers clock source as div by 12 or full frequency.  The examples here use the crystal frequency directly.
For an 8051 where the timer clock source is div by 12, you may have to change timer values for a slower baud rate.

The crystal frequency in these examples is 11.0592MHz.  Both examples are for 8N1.

Transmit UART example - Transmitting is the easy part.  This example is at 9600 baud.

Receive UART example - Receiving is a little harder.  The basic algorythm is:

1. Look for a Start bit (RX high to low transition).
2. Then, wait 1.3 bit periods, and sample the first bit.
3. Wait 1 bit period, then sample the next bit.
4. Go back to 3 until you have 8 bits.
5.  Look for the stop bit.

This example is listening to a GPS unit, and displaying the date, time, latitude, and longitude on an HD44780 based LCD.

It Works!! Although there is an error in my parsing code in this version of the firmware, which results in the GPS position being displayed incorrectly... :O
 - according to the displayed lat and long, I'm somewhere in the middle of the Cook Strait...  :D