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.
Previous article:STM32 register access
Next article:STM32 register lighting introduction
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- RAQ #223: How to measure and determine soft-start timing without a soft-start equation?
- RAQ #223: How to measure and determine soft-start timing without a soft-start equation?
- GigaDevice's full range of automotive-grade SPI NOR Flash GD25/55 wins ISO 26262 ASIL D functional safety certification
- GigaDevice's full range of automotive-grade SPI NOR Flash GD25/55 wins ISO 26262 ASIL D functional safety certification
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- STMicroelectronics IO-Link Actuator Board Brings Turnkey Reference Design to Industrial Monitoring and Equipment Manufacturers
- Melexis uses coreless technology to reduce the size of current sensing devices
- 【McQueen Trial】McQueen's car goes through the maze
- Collection: Impedance matching and the basic principles of the Smith chart
- PyBoard W5500 firmware compilation issue
- Causes of thermal resistor soft breakdown and troubleshooting of thermal resistor soft breakdown
- "Playing with the board" + lighting up the SPI touch screen of Xintao
- Ultrasonic flaw detection system using high-speed and high-resolution signal acquisition card
- 2020-7-5-Abdominal breathing training trainer
- ST MEMS Device Resource Library - Hardware Design Guide
- &FF0000 and &880000 are both red. What is the difference between these two reds?
- Internet radio design based on RSIC-V RVB2601 (3)