|
|
Part: AN447
Category:
Description:
Company:
Datasheet: Download AN447 datasheet File size : 50 kB
Request For quote: Find where to buy AN447
Datasheet text preview:
Philips Semiconductors Microcontroller Products
Application Note
Automatic baud rate detection for the 80C51
Author: Greg Goodhue
This note documents a method to automatically establish the correct baud rate for serial communications in many 80C51 family applications. The first character received after a program is started is used to measure the baud rate empirically. This can eliminate the need to have setup switches whose settings are difficult to remember and all of the other headaches associated with applications that use multiple baud rates. One might assume that a reliable method of accomplishing this might be impossible without severely limiting the characters that could be recognized. The problem is in finding a timing interval that can be measured in a large number of possible characters under a wide variety of conditions. Measuring a single bit time would be the obvious way to quickly determine what baud rate is being received. However, many ASCII characters don't have an example of a single bit time in the RS-232 pattern. For most characters, the length of the entire transmission from the start bit to the last "visible" transition will fall within certain ranges as long as some reasonable assumptions can be made about the possible baud rates (i.e. that they are standard baud rates). Moreover, many systems now use 8 data bits and no parity for ASCII transmissions. In this format, normal ASCII characters will never have the MSB set and since UARTs send data LSB first/MSB last, the program would always be able to "see" the beginning of the stop bit. The following baud rate detection routine waits for a start bit (falling edge) on the serial input pin and then starts timer 0. At every subsequent rising edge of the serial data, the timer value is captured and saved. When the timer overflows, the last captured value will indicate the duration of the serial character from the start bit to the last 0 to 1 transition (hopefully the stop bit). The table CmpTable contains the maximum timer measurement that is accepted for each baud rate. These values were picked such that a timed interval of only 4 data bit times (plus the start bit time) will still produce the correct baud rate. There is an assumption in this method that anyone using it needs to be aware of. That is, that this technique depends on only one character being received during the sampling window, which has to be at least as long as a typical character at the slowest baud rate that can be accepted. Essentially this means that the data must normally come from someone typing at a keyboard. On our PCs, we were not able to fool the program by typing two characters in quick succession. The PC function keys did present a problem because they send two characters in a tight sequence, and fooled the program into detecting the wrong baud rate. In the example program, which is designed for a 12 MHz clock, the total sample interval is about 65 milliseconds, or about twice the duration of an RS-232 character sent at 300 baud. If parity is used, a possibility of a baud rate determination error happens when the four MSBs and the parity bit of the character received are all ones. This can happen for the lower case letters "p" through "z", plus curly brackets, vertical bar (|), tilde (~), and "delete", depending on whether the system uses odd or even parity. Note that the usual prompt characters that a user would type to get a system's attention (e.g. space, carriage
AN447
return, and escape) are NOT subject to this limitation. Because of the way this program works, the first input character that is used to detect the baud rate is lost since the UART cannot be set to the correct baud rate until after the first character has been timed. Also, most "real" programs using this technique would want to repeat the baud rate detection process if framing errors are detected at the UART during normal operation. To calculate CmpTable values for other oscillator frequencies and baud rates, use the following equation: Table entry + Osc(MHz) Baud Rate 5 12
Remember that the table entry is a two byte value, so the result of the above must be split into upper and lower bytes (easy if you have a hexadecimal calculator). It may also be possible to get the assembler to do all of the calculations for you. The above equation was derived as follows: maximum timer value + minimum recognition time machine cycle time (table entry) Minimum bits-to-recognize × byte time recognition = #-of-bits time Note: `#-of-bits' (the number of "visible" bits) is 9, and bits-to-recognize (the minimum # of bits to recognize) is 5 for 8-N-1 communication. byte time + 1 baud rate #-of-bits
machine Osc frequency cycle time + 12
June 1993
1
Philips Semiconductors Microcontroller Products
Application Note
Automatic baud rate detection for the 80C51
AN447
;***************************************************************************** ; Automatic Baud Rate Detection Test
;***************************************************************************** $Title(Automatic Baud Rate Detection Test) $Date(121691) $MOD552
;***************************************************************************** ; Definitions
;***************************************************************************** RX CharH CharL BaudRate Display BIT DATA DATA DATA EQU P3.0 30h 31h 32h P4 ;Location of serial receive pin. ;Holds high byte of frame timer result. ;Holds low byte of frame timer result. ;Holds final baud rate determination. ;Port to display result for debug.
;***************************************************************************** ; Reset and Interrupt Vectors
;***************************************************************************** ORG Start: 8000h
ACALL AutoBaud ;Go try to get a baud rate value. MOV Display,BaudRate ;Display baud rate value for debug. SJMP Start
;***************************************************************************** ; Subroutines
;***************************************************************************** ; AutoBaud Rate Detect Routine. ; Attempts to detect baud rate from first received character, by measuring ; the length of the character. Some characters may not work properly, ; primarily those that end with more than 3 (4?) ones in a row. ; Returns with ACC = baud rate pointer. AutoBaud: MOV MOV MOV MOV MOV MOV AB0: JB SETB JB JNB MOV MOV JB JB SJMP TMOD,#01h TH0,#0 TL0,#0 TCON,#0 CharH,#0 CharL,#0 RX,AB0 TR0 TF0,AB3 RX,AB1 CharH,TH0 CharL,TL0 TF0,AB3 RX,AB2 AB1 ;Initialize timer 0 (UART baud rate timer). ;Put timer 0 in 16bit counter mode.
;Initialize timer result.
;Wait for serial start bit. ;Start timer. ;Check for timer overflow. ;Check for a rising edge on serial data. ;Capture timer value at this serial edge.
AB1:
AB2:
;Check for timer overflow. ;Check for falling edge on serial data. ;Go back and repeat sampling. 2
June 1993
Philips Semiconductors Microcontroller Products
Application Note
Automatic baud rate detection for the 80C51
AN447
AB3:
CLR CLR MOV MOV MOV MOVC DEC CJNE SJMP JC DJNZ SJMP MOV MOVC CJNE SETB JC DJNZ
TR0 TF0
;Maximum sample time has expired, check result. ;Begin by stopping timer and clearing flag.
CmpLoop:
Cmp1:
BaudRate,#19 ;Set up table pointers. A,BaudRate DPTR,#CmpTable A,@A+DPTR ;Get a table entry for comparison. BaudRate A,CharH,Cmp1 ;Check result range. CmpLow ;High byte table = timed value, check low byte. CmpMatch ;A match if table value is < timed value. BaudRate,CmpLoop ;Check for end of comparison table. CmpMatch A,BaudRate A,@A+DPTR ;Get a table entry for comparison. A,CharL,Cmp2 ;Check result range. C ;Match if equal. CmpMatch ;C set if A < low byte of result. BaudRate,CmpLoop ;Check for end of comparison table. A,BaudRate C A BaudRate,A ;Comparison complete, ; get final baud rate index, ; and save.
CmpLow:
Cmp2:
CmpMatch: MOV CLR RRC MOV RET
; Compare table holds timer values for the transition points of the accepted ; baud rates. Entries are LSB, MSB. These values are for 12 MHz operation. CmpTable: DB DB DB DB DB DB DB DB DB DB END 40h,0 80h,0 0,01h 0,02h 0,04h 0,08h 0,10h 0,20h 0,40h 0,80h ;0 ;1 ;2 ;3 ;4 ;5 ;6 ;7 ;8 ;9 out of range, value too low. 38400 baud. 19200 baud. 9600 baud. 4800 baud. 2400 baud. 1200 baud. 600 baud. 300 baud. out of range, value too high.
June 1993
3
Others parts begin by an
AN-1 AN-2 AN-3 AN-4 AN-5 AN-6 AN-7 AN-8
|
|
|