| ||
Shopping Cart Download Website |
Home | Products | Teensy | Blog | Forum |
You are here: 8051 Tools Code Library Automatic Baud Rate |
|
Automatic Baud Rate DetectionDon't like reading docs, why not just Skip Down to the Code?OverviewThis code attempts to automatically detect the user's baud rate and initialize the 8051's built-in uart. More specifically, these steps are taken:
How it worksWhen the user presses Enter, their terminal emulation program should send the byte 13, which will be transmitted as shown here:0 1 0 1 1 0 0 0 0 1 | | | | start bit----+ +--lsb msb--+ +----stop bitNote: someday I'll draw a nice picture of this to replace the cheezy ascii art. The code reads P3.0, which is the RXD pin, and waits for it to become low, which is assumed to be the start bit. When it becomes high again, timer1 is started in 16 bit mode. The code waits for the remaining transitions shown above. At the beginning of the stop bit, timer1 is halted. If everything went well, the 16 bit value in timer1 should indicate how many CPU cycles passed while the code waited for the eight data bits. The 8051 uses timer1 to generate the clock for its built-in UART. Though it is possible to use timer1 in any of its modes, using the 8-bit auto-reload makes the most sense unless an extreemly slow baud rate is needed. This code only configures the timer1 in 8-bit auto-reload mode. Though this code makes configuring the built-in UART easy, it should be noted that the 8051's UART requires timer1 to generate the baud rate clock and can't be changed without affecting the UART. The built-in UART requires 16 clock cycles (from timer1) for each bit. Because the code timed all 8 bits, the value that will be needed for initializing the timer in auto-reload mode is calculated by dividing the CPU cycle count by 128. Care must be taken to avoid a round-off error that will yield an inaccurate baud rate value. This bug appeared in PAULMON1. The code presented here fixes that bug and is identical to the automatic baud rate detection code in PAULMON2. Available Baud RatesBecause the 8051's UART requires timer1, which is clocked by the crystal (divided by 12), the only baud rates which the 8051's hardware can produce are:Baud = Crystal / ( 12 * 16 * N ) where "N" is an integer from 1 to 255. This code will select the available baud rate which is the closest to what was received. Usually, an error of about 2.5% will still manage to communicate without trouble, but much beyond that will be problematic, if it works at all. Many crystals are available which are exact multiples of the standard baud rates. Usually these crystals provide faster baud rates than simply switching to a faster crystal. For example, a 4 MHz crystal provides 1200 baud. Switching to 8 MHz only increases the maximum to 2400 baud, but switching to a 3.6864 MHz crystal will allow 19200 baud! Here is a summary of some standard crystals and the maximum standard baud rates which should work with them:
In applications where the speed is limited by the baud rate (such as development with PAULMON), using a crystal which provides a faster baud rate can be a big improvement, even if it is a lower frequency. The CodeThis code is available as plain text or in a zip file. |