当前位置:网站首页>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
边栏推荐
- C WinForm [exit application] - practice 3
- Halcon's practice based on shape template matching [2]
- STM32 virtualization environment of QEMU
- Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
- Beijing Winter Olympics opening ceremony display equipment record 3
- Verilog -- state machine coding method
- 生产中影响滑环质量的因素
- Brief discussion on Buck buck circuit
- My-basic application 1: introduction to my-basic parser
- Naming rules for FreeRTOS
猜你喜欢
OC and OD gate circuit
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
My-basic application 2: my-basic installation and operation
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
Reasons for rapid wear of conductive slip rings
Record the visual shock of the Winter Olympics and the introduction of the screen 2
Factors affecting the quality of slip rings in production
How to select conductive slip ring
PMSM dead time compensation
How to copy formatted notepad++ text?
随机推荐
Use indent to format code
Explication de la procédure stockée pour SQL Server
Consul安装
Soem EtherCAT source code analysis I (data type definition)
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
Installation and use of libjpeg and ligpng
Verilog -- state machine coding method
Class of color image processing based on Halcon learning_ ndim_ norm. hdev
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
C language # and #
UEFI development learning 3 - create UEFI program
Talk about the function of magnetic beads in circuits
2021-10-28
Development tools -- gcc compiler usage
Imx6ull bare metal development learning 1-assembly lit LED
Introduction of air gap, etc
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
Working principle and type selection of common mode inductor
生产中影响滑环质量的因素