Serial communication baud rate setting and baud rate algorithm
When the serial communication of the single chip microcomputer is used, the baud rate must be set, and the baud rate should be set to 9600, 19200...? Is this a tradition? From what? The corresponding crystal oscillator is also generally 11.0592MHZ.
Is it first to set the crystal oscillator and then set the baud rate or set the baud rate first and then set the crystal oscillator. Why should this be set?
There are two more reliable statements:
1: According to the physical characteristics of electricity, transmission media, etc. combined with the requirements of serial device use
In order to ensure effective communication, according to the physical characteristics of electricity, transmission media, etc. combined with the requirements of serial device use, it is determined that the maximum transmission rate of RS232 can only be 115200, and then obtained by two steps to get 57600, 28800, 19200; to adapt to these rates, design the corresponding crystal frequency.
2: This is determined by the characteristics of the telecommunication line.
The bandpass of the telephone line is 300--3KHz. At that time, hayes first engaged in the modem, so the 2400HZ signal used, the corresponding baud rate is 2400. Since the basic frequency is determined, the improved pass adopted the method of the rate is multiplied on the basis of 2400 later, so the formation of 9600.19200. . . .
Either way, there is a baud rate and then a crystal frequency.
Calculation of baud rate of serial communication of single chip microcomputer.
In mode 0 and mode 2, the serial port baud rate is fixed, and mode 0 is Fosc/12. Mode 2 is Fosc/32 or Fosc/64, which is determined by the SMOD bit of the PCON register.
In Mode 1 and Mode 3, the baud rate is a variable value. The baud rate can be generated by Timer 1 (8052 can be generated by Timing 2). What is the baud rate? Baud rate by timer 1 overflow rate to be determined:
Baud Rate = (2SMOD/32)*(Timer 1 Overflow Rate)
Note: 2SMOD here SMOD is the index, SMOD is the baud rate double baud rate bit, this bit is in the PCON register.
When using timer 1 as the baud rate generator, it is usually necessary to configure timer 1 to operate in 8-bit auto-reload mode, and also to disable timer 1 interrupt, then overflow of timer 1 how to calculated? The overflow rate is the frequency of overflow. Assuming the crystal frequency is 12MHz and the TH1 value is 0xFE, only two clock pulses will be required to overflow, and the overflow period is 2us,then the frequency of overflow is 500KHz. According to the timer section, the timing time has the following formula:
Timing time = (maximum count value - counter initial value) * machine cycle = (maximum count value - counter initial value) * (12 / crystal frequency (Hz)) (s) = (256-TH1) * (12 / OSC_FREQ ( Hz))(s).
Then the frequency of overflow is naturally:
So the final push-to-baud rate is:
The calculation formula of the baud rate is available, and naturally, the initial value of TH1 can be derived according to the baud rate:
Assuming the crystal frequency is 11.0592MHz, the baud rate is 9600, SMOD=0, what should the initial value of TH1 be? According to the above formula, it can be calculated TH1=256-11059200/(9600*32*12)=0xFD.