当前位置:网站首页>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
边栏推荐
- OC and OD gate circuit
- Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
- More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
- IEEE access personal contribution experience record
- Sql Server的存储过程详解
- Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
- [untitled] record the visual shock of the Winter Olympics and the introduction of the display screen
- Step motor generates S-curve upper computer
- C WinForm [realize the previous and next selection pictures] - practice 7
- Drive LED -- GPIO control
猜你喜欢
导电滑环磨损快的原因
Reasons for rapid wear of conductive slip rings
UEFI development learning 5 - simple use of protocol
Design a clock frequency division circuit that can be switched arbitrarily
Hardware 3 -- function of voltage follower
Summary of SIM card circuit knowledge
Installation and use of libjpeg and ligpng
C WinForm [change the position of the form after running] - Practical Exercise 4
Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
随机推荐
[paper reading] the latest transfer ability in deep learning: a survey in 2022
Slist of linked list
Basic embedded concepts
The firmware of the connected j-link does not support the following memory access
Baiwen 7-day smart home learning experience of Internet of things
Network port usage
Zero length array in GNU C
[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...)
List of linked lists
Soem EtherCAT source code analysis II (list of known configuration information)
Sql Server的存储过程详解
Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
Problem solving: interpreter error: no file or directory
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
Programming knowledge -- basis of C language
UEFI development learning 3 - create UEFI program
OC and OD gate circuit
Record the visual shock of the Winter Olympics and the introduction of the screen 2
Relationship between line voltage and phase voltage, line current and phase current