STM32 system operating frequency and CAN working baud rate setting based on library function version

Publisher:楼高峰Latest update time:2017-10-01 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. System operating frequency setting

    The register version and the library version of the STM32 system operating frequency setting are quite different. The library version of the system operating frequency is set by the SystemInit() function in system_stm32f10x.c, and other configurations are mainly in the stm32f10x_rcc.c file. For the system clock, by default, it is determined in the SetSysClock() function of the SystemInit function, and the setting is set through macro definition.

static void SetSysClock(void)

{

#ifdef SYSCLK_FREQ_HSE

  SetSysClockToHSE();

#elif defined SYSCLK_FREQ_24MHz

  SetSysClockTo24();

#elif defined SYSCLK_FREQ_36MHz

  SetSysClockTo36();

#elif defined SYSCLK_FREQ_48MHz

  SetSysClockTo48();

#elif defined SYSCLK_FREQ_56MHz

  SetSysClockTo56(); 

#elif defined SYSCLK_FREQ_72MHz

  SetSysClockTo72();

#endif

}

The higher the operating frequency, the higher the system power consumption. You do not need to update the delay_ms() function after changing the operating frequency, but you should pay special attention to the problem of setting the CAN baud rate. Changing the operating frequency will change the CAN baud rate nonlinearly.

 

2. CAN baud rate setting

         Here, it is specially noted that the optional working frequencies in the firmware library are: 24MHz, 36MHz, 48MHz, 56MHz, 72MHz, which are set by macro definition. When set to 24MHz, 48MHz, the frequency used for calculation is 48MHz; when set to 36MHz, 72MHz, the frequency used for calculation is 72MHz; when set to 56MHz, the baud rate is calculated using 56MHz. For example, in the routine given by the battleship STM32, the working frequency is 72M and the default baud rate is 450kps (36000/[(7+8+1)*5]=450Kbps). When we change the working frequency to 36M, the baud rate is still 450kps. If the working frequency is changed to 24M, the baud rate becomes 24000/[(7+8+1)*5]=300Kbps. Why is this change, the principle is not clear yet.

    The figure also gives the calculation formula of CAN baud rate. We only need to know the settings of BS1 and BS2, and the clock frequency of APB1 (usually 36Mhz, that is, the working frequency of APB1 under 72M working frequency), and we can easily calculate the baud rate. For example, if TS1=6, TS2=7 and BRP=4 are set, under the condition that the APB1 frequency is 36Mhz, the baud rate of CAN communication can be obtained = 36000/[(7+8+1)*5]=450Kbps. Set the function to CAN_Mode_Init(CAN_SJW_1tq,CAN_BS2_8tq,CAN_BS1_7tq,5,CAN_Mode_Normal);//Normal mode

Optional parameters are:

#define CAN_SJW_1tq                 ((uint8_t)0x00)  /*!< 1 time quantum */

#define CAN_SJW_2tq                 ((uint8_t)0x01)  /*!< 2 time quantum */

#define CAN_SJW_3tq                 ((uint8_t)0x02)  /*!< 3 time quantum */

#define CAN_SJW_4tq                 ((uint8_t)0x03)  /*!< 4 time quantum */

 

#define CAN_BS1_1tq                 ((uint8_t)0x00)  /*!< 1 time quantum */

#define CAN_BS1_2tq                 ((uint8_t)0x01)  /*!< 2 time quantum */

#define CAN_BS1_3tq                 ((uint8_t)0x02)  /*!< 3 time quantum */

#define CAN_BS1_4tq                 ((uint8_t)0x03)  /*!< 4 time quantum */

#define CAN_BS1_5tq                 ((uint8_t)0x04)  /*!< 5 time quantum */

#define CAN_BS1_6tq                 ((uint8_t)0x05)  /*!< 6 time quantum */

#define CAN_BS1_7tq                 ((uint8_t)0x06)  /*!< 7 time quantum */

#define CAN_BS1_8tq                 ((uint8_t)0x07)  /*!< 8 time quantum */

#define CAN_BS1_9tq                 ((uint8_t)0x08)  /*!< 9 time quantum */

#define CAN_BS1_10tq                ((uint8_t)0x09)  /*!< 10 time quantum */

#define CAN_BS1_11tq                ((uint8_t)0x0A)  /*!< 11 time quantum */

#define CAN_BS1_12tq                ((uint8_t)0x0B)  /*!< 12 time quantum */

#define CAN_BS1_13tq                ((uint8_t)0x0C)  /*!< 13 time quantum */

#define CAN_BS1_14tq                ((uint8_t)0x0D)  /*!< 14 time quantum */

#define CAN_BS1_15tq                ((uint8_t)0x0E)  /*!< 15 time quantum */

#define CAN_BS1_16tq                ((uint8_t)0x0F)  /*!< 16 time quantum */

 

#define CAN_BS2_1tq                 ((uint8_t)0x00)  /*!< 1 time quantum */

#define CAN_BS2_2tq                 ((uint8_t)0x01)  /*!< 2 time quantum */

#define CAN_BS2_3tq                 ((uint8_t)0x02)  /*!< 3 time quantum */

#define CAN_BS2_4tq                 ((uint8_t)0x03)  /*!< 4 time quantum */

#define CAN_BS2_5tq                 ((uint8_t)0x04)  /*!< 5 time quantum */

#define CAN_BS2_6tq                 ((uint8_t)0x05)  /*!< 6 time quantum */

#define CAN_BS2_7tq                 ((uint8_t)0x06)  /*!< 7 time quantum */

#define CAN_BS2_8tq                 ((uint8_t)0x07)  /*!< 8 time quantum */   

    In actual engineering applications, the design of SJW, BS1, BS2, and BRP is involved. The meanings of the four can be found in the definition in the figure below. In the CAN initialization function:

CAN_Mode_Init(CAN_SJW_1tq,CAN_BS2_8tq,CAN_BS1_7tq,5,CAN_Mode_Normal); //Normal mode

The "5" in the STM32 firmware library function is the direct frequency division coefficient, and does not require +1, which is different from the figure below. In order to achieve reliable long-distance transmission, it is necessary to consider the reasonable combination of four parameters. The theoretically allowable transmission delay is determined by the position of the sampling point, so the selection of the sampling point position within a bit period is very important. The sampling point at the back will allow a larger transmission delay error t. , so that the system can transmit farther; on the contrary, selecting a sampling point at the front will allow a larger clock tolerance. Selecting a crystal oscillator with a small clock tolerance can make the sampling point selection position later. The CAN sampling point in STM32 is between BS1 and BS2, so setting BS1 and BS2 to a larger value can achieve maximum reliability.

 


Keywords:STM32 Reference address:STM32 system operating frequency and CAN working baud rate setting based on library function version

Previous article:CAN communication network based on STM32---an ID configuration method
Next article:The process of porting from STM32F030C8 to GD32F130C8

Recommended ReadingLatest update time:2024-11-15 02:30

STM32 SysTick system clock super simple timer SysTick
/** * @brief Configures the SysTick. 系统时钟配置函数   main.c  * @param None * @retval None */ void SysTick_Configuration(void) { /* Setup SysTick Timer for 100 msec interrupts */ if (SysTick_Config((SystemCoreClock) / 10))     //     1/10s=100ms {     /* Capture error */      while (1); }    NVIC_SetPriority(Sy
[Microcontroller]
A preliminary study on how to process multiple sets of data when STM32 SPI2 interrupts
1. Description         The SPI communication between the two STM32 boards uses SPI2. The host sends multiple sets of data continuously (the data format is half word, i.e. 16 bits), and the slave uses SPI2 interrupt mode to receive multiple sets of data sent by the host. I tried two methods for slave interrupt acce
[Microcontroller]
STM32 study notes GPIO configuration issues
1. GPIO configuration (1) GPIO_Mode_AIN analog input  (2) GPIO_Mode_IN_FLOATING floating input (3) GPIO_Mode_IPD pull-down input  (4) GPIO_Mode_IPU pull-up input  (5) GPIO_Mode_Out_OD open-drain output (6) GPIO_Mode_Out_PP push-pull output (7) GPIO_Mode_AF_OD multiplexed open-drain output  (8) GPIO_Mode_AF_PP multiple
[Microcontroller]
STM32 compilation configuration related errors
This is usually caused by a wrong USE_STDPERIPH_DRIVER. This is usually caused by a spelling error of STM32F10X_HD; The correct way to write it should be ALL CAPS!!! CAPS!!! CAPS!!! CAPS!!!
[Microcontroller]
STM32 compilation configuration related errors
STM32 FSMC configuration SRAM
   The FSMC of STM32 is really convenient. It can be expanded to include SRAM/NOR/NAND/PC Card/TFT, etc. It is very easy to operate. For more information about FSMC, please refer to the STM32 Reference Manual, which is very detailed.    Engineering structure:    user_sram.c //function of code:FSMC SRAM Driver SRAM
[Microcontroller]
STM32 FSMC configuration SRAM
Things to note when using an external clock with stm32
When using an external clock, modify the default configuration in stm32f4xx_hal_conf.h according to the actual size of the external crystal oscillator used: #if !defined  (HSE_VALUE)    #define HSE_VALUE    ((uint32_t)24000000U) #endif
[Microcontroller]
STM32 ADC single conversion DMA read
 The DMA reading method is very suitable for high-frequency ADC sampling signals.      The DMA reading method of ADC is actually similar to the interrupt reading method in the previous article, and the initialization code is even more similar. The initialization code is as follows: static void ADC_GPIO_Config(void) {
[Microcontroller]
How to convert STM32 JTAG port into normal IO
When debugging the STM32 MCU, if the port is not enough, you can close the JTAG port and use only the SW debugging mode. In this way, you can use its other functions. Specific method: Just set Debug to SW debugging mode before initialization, and then call the following two sentences when the program is initialized:
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号