Home > other >  A serial port baud rate registers configuration method
A serial port baud rate registers configuration method

Time:11-12

recently doing a small project, transplant the enjoy wealth of routines to their own development board, found a serial port to print the code, then I found that the vibration is the crystal of his own board (HSE) in different frequency and routines, and, believe a lot of new to a serial port or didn't see the calculation method of baud rate system's brothers had come across this problem, it's simple to share your own experience, (please bosses not said some directions)

My side in the use of the chip is the AT32F403A astra force, with 103 is said to be fully compliant, the M4 kernel, the frequency is higher, the cost is low, is 103, has been used, frequency zone is the kernel of what we have been using 103 also enough, estimated cost boss main consideration or just want to replace the chip, just tough for those of us who work, (escape ~ ~
So here are based on the interpretation of AT32F403A or STM32F103 to do AT this part on the serial port to ST do compatibility strong,

First of all, we have to know from where, the serial clock is AT32F403 USART1 is provided from APB1, default is 120 MHZ; And sometimes we don't know how the APB1 clock frequency is, as suggested by MCO output to check how much is the real system clock (SYSCLK), and based on the frequency division of RCC to calculate the APB1 clock frequency,

Say a few words here, in fact, most of the time the baud rate of serial port configuration and the final output do not match the baud rate, large probability is caused by the system clock does not match,

In a word, the first step is to confirm the serial clock frequency. We write it as: USART_CLK

Then, we only need USARTDIV see below this value namely, do not go into,


Finally, usually calculated USARTDIV this value is a decimal, so a little bit of a problem is that we need this value can be divided into integer and fraction are saved in to the serial port register,

That we are going to take, for example, assume that we known conditions, USART_CLK=120 MHz, want to a serial port baud rate to 115200, then the calculation method is as follows:
/16 USARTDIV=120000000/115200=65.1041666666
We take two decimal places to calculate that 65.10,
The write USARTDIV integer values for: 65; Small value of 2 (~ 0.1 * 16), converted to hexadecimal: 0 x412, finally will write a serial port baud rate 0 x412 register,
Note: the above results using the approximate calculation of the rounding

So when we know the frequency and when we want to configure the baud rate, according to the above calculation method into the baud rate of serial port register,

summarize: determine the serial clock frequency itself, according to the baud rate to calculate to configure register values, register values can be divided into integer and decimal, integer directly fill in the, decimal need multiplied by 16 and then fill in the serial port baud rate registers,
  • Related