UART: Example

Suppose we wanted to send the character ‘q’ using a UART. Our data frame is specified as [8E1], the baudrate is 9600, and the counter clock frequency is 10 MHz. What is the data frame for sending this character, MSB first? How long will it take to send? What modulation pattern would be needed? What is the ideal sampling time for Bit 1 from the onset of the falling edge, and what is the actual sampling time and relative error (assuming the middle of the start bit is time = 0)? Let’s start with the data frame.


The very first bit is the start bit, 0. Then comes the data bits. For sending characters, oftentimes ASCII codes will be used. These codes are integers that are mapped to or represent specific characters. For instance, 65 is the ASCII code for the character ‘A’, and 97 is the ASCII code for the character ‘a’. The ASCII code for the character ‘q’, as seen below, is 113. So, in our data packet, we will need to transmit the number 113 (which is the same as transmitting ‘q’). 113 in binary is 01110001, so our data frame is currently 001110001. Next, we have the parity bit. Our frame should have even parity, and it does, so the parity bit will be 0. Finally, we have one stop bit, which will be 1, so our final data frame will be 00111000101. Keep in mind, we sent the data MSB first. If it were LSB first, then the data frame would instead look like 01000111001.

Since our data frame is 1 start bit + 8 data bits + 1 parity bit + 1 stop bit = 11 bits, and the baud rate is 9600 bits/sec, it will take 11 bits / 9600 bits/sec ≈ 1.146 ms to transmit the character ‘q’. Since the data frame’s size is constant, we could say in general that it takes approximately 1.146 milliseconds to transmit one character given these specifications.


In order to figure out the modulation pattern, we need to know the division factor. In this case, the baud rate and the counter frequency are given to us, the division factor is N = f / B = 10 MHz / 9600 ≈ 1,041.6667. Our fractional part is 0.6667, which matches up perfectly with one of the rows. The UCBRSx value is 0xD6 (11010110), so our modulation pattern would look like 1042, 1042, 1041, 1042, 1041, 1042, 1042, 1041.


Finally, the ideal sampling time for Bit 1 would at the second cycle. This is because we assumed t = 0 starts in the middle of the start bit, so Bit 0 is sampled one baud length later, and Bit 1 is sampled another baud length later. Thus, it takes two cycles for Bit 1 to be sampled. With a baud rate of 9600, it takes 1 / 9600 ≈ 104.167 µs for each cycle. Thus, it takes 2 * 104.167 µs = 208.333 µs or 208.334 µs (depending on how you approximated, but we’ll say 208.333 µs) to sample Bit 1, ideally. In actuality, the time it takes follows the modulation pattern. Bit 0 is sampled using a counter load value of 1042, and Bit 1 is also sampled using a counter load value of 1042 (but if we were to sample Bit 2, it would use a counter load value of 1041). Using the Timer32 load value formula, we can calculate the time as t = N / f = (1042 + 1042) / 10,000,000 = 208.4 µs. Taking the relative error, we get (208.4 - 208.333) / 208.333 = 0.000322.