There are applications on an 8052 type microcontroller where the UART is in use and connected to a PC type host computer. Often the UART is setup to operate at a specific baud rate and this scheme works quite well if the oscillator frequency of the microcontroller is known at the time the UART intialization is coded. There are times however when the oscillator frequency is not known at the time of coding. Some examples of this could be that the code is embeddded in a libary for use with a multitude of different 8052 type parts, the oscillator on the 8052 may have multiple frequency selections based upon the way the product is built, or the processor frequency is locked via PLL technique to some other piece of equipment that has an unknown frequency. For these cases it can be useful to have the 8052 program measure the timing of a known character transmission from the PC host computer and then set its baud rate generator according to the measured timing.
On this page I present a coding example of how the baud rate for an 8052 can be programmed automatically. In this example the user sends a SPACE character (ASCII 0x20) from the PC to the 8052 microcontroller right after the microcontroller has entered a detection subroutine. This subroutine uses the 8052 Timer 2 to generate its baud rates for normal UART operation. The detection subroutine uses Timer 1 to measure the time span of the sequence of 6 zeros in the 0x20 SPACE character. This time span is then converted to the correct Timer 2 auto reload value to make the 8052 operate at the same baud rate. The measurement itself is done by having the microcontroller firmware directly poll the UART input via the port pin where the UART RxD pin is connected. There is a provision in the subroutoine for a shutdown process if the 0x20 character is not received within 30 seconds. Implementation specific details of how this 30 second timeout is performed are not shown here in that it does not relate directly to the autobuad process. I prepared a spreadsheet that helped guide me through the design process for the autobaud detection process. Here is an image of the spread sheet. If you want to actually download the spreadsheet then click the link below the image. The spreadsheet is in EXCEL format.
The code for the detection subroutine is shown below. This image is captured as a GIF file. A PDF of the code may be downloaded by clicking on the code image. Note that the download listing also includes a pair of integer math routines that are used for the Timer 2 reload factor computation.