当前位置:网站首页>STM32 single chip microcomputer - bit band operation
STM32 single chip microcomputer - bit band operation
2022-07-05 08:15:00 【chen_ bx】
STM32 Single chip microcomputer --- Bit band operation
One 、 Bit band operation
One 、 Bit band operation
1. significance
Think back to writing 51 Code
P0 = 0x10; // take P0 Port set to 0x10
P1_0=1; // take P1 port 0 Pin No. 1 is set to high level
a = P2_2; // obtain P2 port 2 The level of pin No
According to the above method , We can find that fast positioning can modify the level of a pin and obtain the status of the pin
2. reason
GPIO_SetBits、GPIO_ResetBits operation IO The performance of the mouth has not reached the extreme , Because these functions require on-site protection and on-site recovery , It takes time , Not one step in place , Using bit band operation does not have the above trouble , Simple and fast !
Example 1:
GPIO_SetBits(GPIOF,GPIO_Pin_9);
It is amended as follows
PFout(9)=1;
Example 2:
GPIO_ResetBits(GPIOF,GPIO_Pin_9);
It is amended as follows
PFout(9)=0;
Because it is used to set the level or read the level of the pin , Library function efficiency is not high , It's hard to cope with high-performance occasions , The following code , To modify the level of a pin, at least 3 Line code , It also does not include the implicit calling function and the return process of the function .
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->BSRRL = GPIO_Pin;
}



3. The formula
About IO The access address corresponding to the pin , You can refer to the following formula
The bits of the register are aliased = 0x42000000 + ( The address of the register -0x40000000)*32 + Pin number *4
Two 、 Register address and alias address translation skills
1. Determine the starting address of a port , Such as port F The starting address of access is GPIOF_BASE
#define GPIOF ((GPIO_TypeDef *) GPIOF_BASE)
typedef struct
{
__IO uint32_t MODER; /*!< GPIO port mode register, Address offset: 0x00 */
__IO uint32_t OTYPER; /*!< GPIO port output type register, Address offset: 0x04 */
__IO uint32_t OSPEEDR; /*!< GPIO port output speed register, Address offset: 0x08 */
__IO uint32_t PUPDR; /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */
__IO uint32_t IDR; /*!< GPIO port input data register, Address offset: 0x10 */
__IO uint32_t ODR; /*!< GPIO port output data register, Address offset: 0x14 */
__IO uint16_t BSRRL; /*!< GPIO port bit set/reset low register, Address offset: 0x18 */
__IO uint16_t BSRRH; /*!< GPIO port bit set/reset high register, Address offset: 0x1A */
__IO uint32_t LCKR; /*!< GPIO port configuration lock register, Address offset: 0x1C */
__IO uint32_t AFR[2]; /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */
} GPIO_TypeDef;
2. Calculate the offset value according to the register address to be accessed , Such as calculation
GPIOF Of ODR Register address = GPIOF_BASE+0x14;
3. Convert according to the following formula
The bits of the register are aliased = 0x42000000 + ( The address of the register -0x40000000)*8*4 + Pin number *4
The detailed schematic diagram is shown below :
4. Set up PF9 The pin level code is as follows
uint32_t *PF9_BitBand = (uint32_t *)(0x42000000 + (GPIOF_BASE + 0x14 - 0x40000000)*32 + 9*4);
Better solution method :
uint32_t *PF9_BitBand = (uint32_t *)(0x42000000 + ((uint32_t)&GPIOF->ODR - 0x40000000)*32 + 9*4);
3、 ... and 、 Bit band operation LED Lamp example
#include "stdio.h"
#include "stm32f10x.h"
#define PAout(n) *((uint32_t *)(0x42000000 + ((uint32_t)&GPIOA->ODR - 0x40000000)*32 + n*4))
//#define PAout(x) *((uint32_t *)(0x42000000 + (((uint32_t)&GPIOA->ODR) - 0x40000000)*32 + x*4))
void delay(int len) // The time delay function
{
volatile unsigned int i = 6*1000*len;
while(i--);
}
int main(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // Enable port A The clock of
/* LED I/O To configure */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //1 and 3 Pin
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // General push-pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; // 2MHz
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Close all after configuration LED */
GPIO_SetBits(GPIOA,GPIO_Pin_1);
while(1)
{
PAout(3) = 0;
delay(200);
PAout(3) = 1;
delay(200);
}
}
proteus Simulation 
边栏推荐
- PMSM dead time compensation
- LED display equipment records of the opening ceremony of the Beijing Winter Olympics
- [tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)
- Let's briefly talk about the chips commonly used in mobile phones - OVP chips
- Slist of linked list
- UEFI development learning 3 - create UEFI program
- Soem EtherCAT source code analysis I (data type definition)
- Screen record of the opening ceremony of the Beijing winter olympics 2
- Adaptive filter
- Define in and define out
猜你喜欢
![[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc](/img/9e/2524cbb9b90135c54669ba8d5338b7.jpg)
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc

Why is 1900 not a leap year

Circleq of linked list

FIO测试硬盘性能参数和实例详细总结(附源码)

Some thoughts on extracting perspectives from ealfa and Ebeta

Network communication process
![Measurement fitting based on Halcon learning [i] fuse Hdev routine](/img/91/34c92065e797c87d6ce5ea13903993.jpg)
Measurement fitting based on Halcon learning [i] fuse Hdev routine

Carrier period, electrical speed, carrier period variation
![Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine](/img/55/0f05291755dc1c3c03db8e991a1ba1.jpg)
Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine

Programming knowledge -- assembly knowledge
随机推荐
Record the opening ceremony of Beijing Winter Olympics with display equipment
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
Take you to understand the working principle of lithium battery protection board
Charge pump boost principle - this article will give you a simple understanding
Talk about the circuit use of TVs tube
Development tools -- gcc compiler usage
Live555 push RTSP audio and video stream summary (I) cross compilation
H264 (I) i/p/b frame gop/idr/ and other parameters
Brief discussion on Buck buck circuit
Factors affecting the quality of slip rings in production
Classic application of MOS transistor circuit design (2) - switch circuit design
Tailq of linked list
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
Halcon's practice based on shape template matching [1]
UEFI development learning series
Installation and use of libjpeg and ligpng
Gradle复合构建
Void* C is a carrier for realizing polymorphism
Relationship between line voltage and phase voltage, line current and phase current
Measurement fitting based on Halcon learning [i] fuse Hdev routine