Aruba's full mobile network accelerates Wanda's O2O experience

STM32 library function operation brings a lot of convenience to design developers. Developers do not need to fully understand the internal registers and hardware mechanisms of STM32. As long as they have C language foundation, they can complete the development of single-chip microcomputer, shorten the development cycle and reduce development. Difficult, so much loved by engineers.

The library function-based development model is similar to the API (Application Programming Interface)-based software development. The programmer can develop the application by calling the API function without accessing the source code or understanding the details of the internal working mechanism. Reduce programming tasks. STM32's function library-based development model is the same, so for engineers with experience in microcontroller development, learning STM32 is easy to get started.

Although you can not consider the internal details of the library function, and do not consider how to implement the configuration of the hardware registers, but the in-depth understanding of the library function is very beneficial to improve the programming ability. Let's take the system tick clock as an example to explain its workflow.

The tick clock is a 24-bit timer inside the STM32 that is relatively simple to operate and has fewer configuration registers. The general workflow is like this. The timer must first have a clock source. After the clock source is configured, set the timing time, then the timer starts. When the timing time expires, the flag is set, and the initial value of the reload timer is used. You can use the query flag bit and interrupt mode to make corresponding responses. Let's see how the program implements the delay function.

/ / Initialize the configuration function

Void Delay_Init()


RCC_ClocksTypeDef RCC_ClocksStatus;

RCC_GetClocksFreq(&RCC_ClocksStatus);//Get the clock frequency

SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);//The clock source is configured as the system main clock frequency /8

SysTick_ITConfig(DISABLE);//Do not enable interrupt, use query mode

Delay_fac_us = RCC_ClocksStatus.HCLK_Frequency / 8000000;// 1us timing initial value


/ / Realize the delay function of the delay Nus

Void Delay_us(u32 Nus)


SysTick_SetReload(delay_fac_us * Nus);//Load initial value

SysTick_CounterCmd(SysTick_Counter_Clear);//Counter cleared

SysTick_CounterCmd(SysTick_Counter_Enable);//Counter starts counting



Status = SysTick_GetFlagStatus(SysTick_FLAG_COUNT);

}while (Status != SET);//Continuously query the flag bit. When the initial value is loaded and the counter is equal, the flag bit is set.

SysTick_CounterCmd(SysTick_Counter_Disable);//Close counter

SysTick_CounterCmd(SysTick_Counter_Clear);//Clearing counter


/ / Realize the flashing lights





Delay_us(500000); / / delay 500ms



Let's take a look at how the library function implements the corresponding register configuration.

Void SysTick_ITConfig(FunctionalState NewState)


/* Check the parameters */


If (NewState != DISABLE)






SysTick->CTRL &= CTRL_TICKINT_Reset;



The function of this function is to configure the register to open/close the interrupt. FunctionalState is a custom data type, which is an enumerated type, typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;

An enumerated type is a primitive data type rather than a constructed type. It is used to declare a set of named constants. The values ​​of the variables are listed one by one. The value of the variable is only limited to the range of values ​​listed, so when a variable When there are several possible values, you can define it as an enumerated type.


The purpose of this sentence is to determine whether the value of the parameter NewState is correct. If the parameter is found to be in error, it will call the function assert_failed() to report the error to the programmer.

Void assert_failed(uint8_t* file, uint32_t line)


While (1)



SysTick->CTRL |= CTRL_TICKINT_Set; This sentence is the statement used to configure the register, SysTick is a system-defined structure as follows, SysTick->CTRL is the control register of the tick clock.

Typedef struct


__IO uint32_t CTRL; /*!< Offset: 0x000 (R/W) SysTick Control and Status Register */

__IO uint32_t LOAD; /*!< Offset: 0x004 (R/W) SysTick Reload Value Register */

__IO uint32_t VAL; /*!< Offset: 0x008 (R/W) SysTick Current Value Register */

__I uint32_t CALIB; /*!< Offset: 0x00C (R/ ) SysTick Calibration Register */

} SysTick_Type; //Declare a structure of type SysTick_Type.

#define SysTick ((SysTick_Type *) SysTick_BASE ) /*!< SysTick configuration struct */

#define SysTick_BASE (SCS_BASE + 0x0010UL) /*!< SysTick Base Address */

#define SCS_BASE (0xE000E000UL) /*!< System Control Space Base Address */

Define a structure instance SysTick of type SysTick_Type, which is basically an address, which is the actual address 0xE000E000UL+0x0010UL allocated to the tick clock inside the STM32 chip.

CTRL_TICKINT_Set is a macro definition defined as follows


#define CTRL_TICKINT_Set ((u32)0x00000002)

#define CTRL_TICKINT_Reset ((u32)0xFFFFFFFD)

At this point, SysTick->CTRL |= CTRL_TICKINT_Set; the meaning of this sentence is very clear, is to assign a value of 0x00000002 to the address 0xE000E000+0x0010 +0x000, corresponding to the second position of the CTRL register of the tick clock 1. the meaning of.

The above is to use the way of query, let's talk about the interrupt trigger. Simply call the following function to complete the interrupt settings.

SysTick_Config(uint32_t ticks); The specific implementation is as follows:

__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)


If ((ticks - 1) > SysTick_LOAD_RELOAD_Msk) return (1);

SysTick->LOAD = ticks - 1;

NVIC_SetPriority (SysTick_IRQn, (1"__NVIC_PRIO_BITS) - 1);

SysTick->VAL = 0;

SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |



Return (0);


The parameter of the function is ticks, which is the count value to be loaded into the register SysTick->LOAD. If the system clock is 72M, the ticks is assigned to SystemFrequency/10000, which means that the count reaches 720 clock cycles to generate an interrupt, and one clock cycle time. It is (1/72)us, so 720x (1/72)=10us, which realizes the function of timing 10us.

NVIC_SetPriority (SysTick_IRQn, (1"__NVIC_PRIO_BITS) - 1); Set the priority for the SysTick interrupt. Clear the value of the register SysTick->VAL to 0. Then enable the interrupt, enable the SysTick timer, and select the clock source as the AHB clock. When the timing time expires, enter the interrupt function

Void SysTick_Handler(void)


/ / The specific function implementation is written by the user.


Through the operation of such a simple timer, we can initially understand the use of the STM32 library function. In fact, developers do not need to go into the internal processing of the library function, just need to understand the library function that has been packaged, call Yes, so the development cycle can be greatly reduced, the development efficiency can be improved, and more functions are left to the reader to research and develop.

0 times
Window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdMini": "2", "bdMiniList": false, "bdPic": "", "bdStyle": " 0", "bdSize": "24" }, "share": {}, "image": { "viewList": ["qzone", "tsina", "tqq", "renren", "weixin"], "viewText": "Share to:", "viewSize": "16" }, "selectShare": { "bdContainerClass": null, "bdSelectMiniList": ["qzone", "tsina", "tqq", "renren" , "weixin"] } }; with (document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.' + ~(-new Date() / 36e5)];

Shopping Mall Escalator

Smart Elevators,Shopping Escalator,Shopping Lift Elevator,Shopping Mall Escalator