Asynchronous Serial Transmission
As you should already know, baud rate defines bits sent per second. But baud only has meaning if the two communicating
devices have a synchronized clock. For example, what if your microcontroller crystal has a slight deviation of .1 second,
meaning it thinks 1 second is actually 1.1 seconds long. This could cause your baud rates to break!
One solution would be to have both devices share the same clock source, but that just adds extra wires . . . All
of this is handled automatically by the UART, but if you would like to understand more, continue reading . . .
Asynchronous transmission allows data to be transmitted without the sender having to send a clock signal to the receiver.
Instead, the sender and receiver must agree on timing parameters in advance and special bits are added to each word which
are used to synchronize the sending and receiving units.
When a word is given to the UART for Asynchronous transmissions, a bit called the "Start Bit" is added to the beginning of
each word that is to be transmitted. The Start Bit is used to alert the receiver that a word of data is about to be sent,
and to force the clock in the receiver into synchronization with the clock in the transmitter. These two clocks must be
accurate enough to not have the frequency drift by more than 10% during the transmission of the remaining bits in the word.
(This requirement was set in the days of mechanical teleprinters and is easily met by modern electronic equipment.)
When data is being transmitted, the sender does not know when the receiver has 'looked' at the value of the bit -
the sender only knows when the clock says to begin transmitting the next bit of the word.
When the entire data word has been sent, the transmitter may add a Parity Bit that the transmitter generates. The Parity
Bit may be used by the receiver to perform simple error checking. Then at least one Stop Bit is sent by the transmitter.
When the receiver has received all of the bits in the data word, it may check for the Parity Bits (both sender and receiver
must agree on whether a Parity Bit is to be used), and then the receiver looks for a Stop Bit. If the Stop Bit does not
appear when it is supposed to, the UART considers the entire word to be garbled and will report a Framing Error to the host
processor when the data word is read. The usual cause of a Framing Error is that the sender and receiver clocks were not
running at the same speed, or that the signal was interrupted.
Regardless of whether the data was received correctly or not, the UART automatically discards the Start, Parity and Stop
bits. If the sender and receiver are configured identically, these bits are not passed to the host.
If another word is ready for transmission, the Start Bit for the new word can be sent as soon as the Stop Bit for the
previous word has been sent.
In short, asynchronous data is 'self synchronizing'.
The Loop-Back Test
The loop-back test is a simple way to verify that your UART is working, as well
as to locate the failure point of your UART communication setup.
For example, suppose you are transmitting a signal from your microcontroller UART
through a TTL to USB converter to your laptop and it isn't working. All it takes
is one failure point for the entire system to not work, but how do you find it?
The trick is to connect the Rx to the Tx, hence the loop-back test.
For example, to verify that the UART is outputting correctly:
- connect the Rx and Tx of the UART together
- printf the letter 'A'
- have an if statement turn on a LED if 'A' is received
If it still doesn't work, you know that your code was the failure point (if not more than one failure point).
Then do this again on the PC side using HyperTerminal,
directly connecting Tx and Rx of your USB port.
And then yet again using the TTL to USB adaptor.
You get the idea . . .
I'm willing to bet that if you have a problem getting it to work, it is because your
baud rates aren't the same/synchronized.
You may also find it useful to connect your Tx line to an oscilloscope to verify
your transmitting frequency:
Top waveform: UART transmitted 0x0F
Bottom waveform: UART received 0x0F
Adding UART Functions to AVR and your $50 Robot
To add UART functionality to your $50 robot
(or any AVR based microcontroller) you need to make a few minor modifications to your code
and add a small amount of extra hardware.
Please check out the step-by-step instructions on
how to add UART functionality to your $50 robot >>>.