51 MCU - Baud Rate Calculation & Timer Initial Value Calculation


foreword

本文承接上篇,留下的悬念,我们来对串口通信中的波特率进行详细讲解
Previous link: Serial communication

For example, if I want to configure the corresponding serial port baud rate, how to calculate the initial value of the timer. Knowing the initial value of the timer, how to calculate the baud rate through the initial value of the timer

Note here, in fact, the calculation of the baud rate can be stc-isp软件directly generated, as shown in the figure below,Select a baud rate of 4800, since we are passingMode 2 of Timer 1To generate the baud rate, select mode 2 in the part of the baud rate generatorOctet Reload Module. chooseBaud rate double speed, ie SMOD=1.The timer clock is 12T. This generates the initial state of the serial port baud rate.
Here is an explanation of the various registers configured

  • PCON | =0x80 , the highest bit of SMOD is 1 , and the baud rate is doubled.
  • SCON=0x50 , configure the serial port mode 1, the corresponding SM0 SM1 bit is 0 1, that is, the upper four bits of the SCON register are set to 0 1 0 1, the software sets the REN bit to 1 by default, and it is allowed to receive data .
  • The relevant configuration bits of the timer , timer 1, mode 2, eight-bit auto-reload mode, do not enable the interrupt of timer 1, and start the timing function.

insert image description here
但是光知道了怎么使用软件自动生成,而不知道其底层的原理,那也不行,所以下文会详细介绍该如何计算波特率以及定时器的初值。


1. Calculate the initial value of timer T1 according to the baud rate to be configured

Usually, when we communicate with the serial port, we need to keepThe baud rate of the two-machine communication is the same, otherwise there will be incorrect sending and receiving data. The following is directly above the figure, telling how to configure the initial value of the timer T1.

First of all, four points must be clarified, as an example

    1. Assume that the crystal oscillator of the development board is 12MHZ.
    1. Timer T1 selects mode 2, eight-bit auto-reload mode.
    1. Serial port selection mode 1, 10-bit asynchronous transceiver mode, and the baud rate is doubled.
    1. The configured baud rate is 4800
      well, come on! Directly above the picture, simple and easy to understand

insert image description here
The formula for calculating the baud rate of the serial port:
Calculate directly according to the figure below
to clarify two points:

  • 1. Selection of serial port mode
  • 2. Whether the baud rate is doubled, that is, SMOD is 0 OR 1

insert image description here

2. Calculate the baud rate according to the initial value of the timer

After the above reading, I believe you have a deep impression on the baud rate of serial communication.
So how do we calculate the baud rate based on the initial value of the timer?
Serial port structure diagram
insert image description here
Come on, directly on the picture
insert image description here


Summarize

The above is a summary of the calculation of the serial port baud rate and the calculation of the initial value of the timer.

Mastering the calculation of serial port baud rate will give you a deeper understanding of serial port communication

come on! Come on!

Hope this article is helpful to you.

Sorry for the hasty writing at the end, please correct me if I am wrong.

Guess you like

Origin blog.csdn.net/cyaya6/article/details/127490178