当前位置:网站首页>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 
边栏推荐
- Programming knowledge -- assembly knowledge
- Imx6ull bare metal development learning 1-assembly lit LED
- Soem EtherCAT source code analysis I (data type definition)
- Nb-iot technical summary
- Hardware 1 -- relationship between gain and magnification
- Consul installation
- Naming rules for FreeRTOS
- Hardware 3 -- function of voltage follower
- Bootloader implementation of PIC MCU
- Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
猜你喜欢

List of linked lists

Management and use of DokuWiki (supplementary)

Hardware 1 -- relationship between gain and magnification

Interview catalogue

Semiconductor devices (III) FET

Ble encryption details

Network port usage

UEFI development learning 5 - simple use of protocol

Wifi-802.11 negotiation rate table

生产中影响滑环质量的因素
随机推荐
Sizeof (function name) =?
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
Detailed explanation of SQL server stored procedures
Semiconductor devices (III) FET
Shell script basic syntax
Explain task scheduling based on Cortex-M3 in detail (Part 1)
UEFI development learning 6 - creation of protocol
Record the opening ceremony of Beijing Winter Olympics with display equipment
Halcon's practice based on shape template matching [1]
UEFI development learning 5 - simple use of protocol
Some tips for using source insight (solve the problem of selecting all)
Sql Server的存储过程详解
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
DCDC circuit - function of bootstrap capacitor
Imx6ull bare metal development learning 1-assembly lit LED
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
Void* C is a carrier for realizing polymorphism
Measurement fitting based on Halcon learning [i] fuse Hdev routine