Some good programming ideas, methods and good programs in the STM32 library

Publisher:CreativeMindLatest update time:2018-08-26 Keywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The following are excerpts from the STM32 library file, and these methods can be used in future programming.

The following are the steps to convert an IO port into a structure pointer:

       First, define GPIO, convert it into a structure, then define an object of the structure, and assign the base address of the port to it to operate the IO port. Then assign values ​​to the variables in the structure, that is, assign values ​​to the corresponding registers of the corresponding IO port.

1. Definition of GPIO

Define variables in the order of registers (32 bits are defined because the size of the register is 32 bits) (arrange in the order of registers because the variables in the structure are arranged in the order of definition, so it corresponds to the arrangement of registers in the IO port):

 

2. Determine the address of the IO port

The above are the starting addresses of each peripheral, and the following are the first addresses of each GPIO (GPIO is mounted on APB2)

(So ​​the following is written as the base address of APB2 plus the offset address of each port)

         

The first address of each GPIO is as follows, so the above writing method is obtained

3. Get a pointer variable pointing to the structure with the address of the IO port

The following converts GPIO into a pointer (the method is to force the corresponding address to be converted to a different type) (GPIOA is a pointer)


How to better configure registers

STM32 converts the configuration of each register function into macro definitions, which makes it clear at a glance.

 

GPIO working mode settings (already defined, just use its name)

In order to achieve the effect of self-explanatory, the binary form of each configuration function of each register is converted into a symbolic constant.

In order to facilitate the configuration of each IO port, the corresponding functions are combined to form a structure. After completing the assignment of this structure, the corresponding function is called to write these values ​​into the corresponding registers to complete the configuration of the IO port.   

This structure is the function configuration structure of the IO port, which can realize the configuration of the speed and working mode of a certain pin of the IO port.

(The configuration method can use the macro constants defined above)


Good programming

Enumeration can limit the values ​​of some objects to a certain range, reduce errors, and be more intuitive.

Pay attention to the following writing method, which can make the numbers more intuitive and easy to understand. For example, if 1 is shifted left by 31 bits, the 31st bit will be 1.


For example, this method of moving left and right is very convenient and clear:

(*volatile unsigned long)0x40010C00 =(2<<20) | (0<<22); // For simplicity, ignore other bits

*(volatile unsigned long *)0x40010C0C =1<<5;

(You can also use the above method to convert 5,20 into clearer characters using macro definitions)

Some abbreviations

RCC_APB2ENR: RCC is the clock register, APB2 is peripheral 2, and ENR can be understood as enable

GPIOB_CRL:GPIO Bcontrol control register
GPIOB_ODR:GPIO (general purpose input output) Boutput data register output data register

The classic way to handle bits:

 

The register configuration uses read, modify, and write methods




The STM32 library has many good ideas. You can summarize them when studying, think more, and learn more, so that you can gradually improve.


Keywords:STM32 Reference address:Some good programming ideas, methods and good programs in the STM32 library

Previous article:STM32 register access
Next article:STM32 register lighting introduction

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号