当前位置:网站首页>Bit segment operation of STM32, LED lighting based on stm32f103xxx multiple methods
Bit segment operation of STM32, LED lighting based on stm32f103xxx multiple methods
2022-06-21 09:38:00 【ah_ yl】
First, let me talk about the steps of bit segment operation :
( direct cp The original text of the manual )
The following mapping formula shows how each word in the alias area corresponds to the corresponding bit in the bit band area :
bit_word_addr = bit_band_base + (byte_offset×32) + (bit_number×4)
among :
bit_word_addr Is the address of the word in the alias memory area , It maps to a target bit .
bit_band_base Is the starting address of the alias area .
byte_offset Is the sequence number in the bit segment of the byte containing the target bit
bit_number Is the location of the target bit (0-31)
Example :
The following example shows how to map in the alias area SRAM The address is 0x20000300 Bits in bytes of 2:
0x22006008 = 0x22000000 + (0x300×32) + (2×4).
Yes 0x22006008 Address write operation and pair SRAM Middle address 0x20000300 Bits of bytes 2 Carry out reading - Change - Writes have the same effect
We need to know that bit segment operations also apply to mapped addresses of peripherals
The starting address of the peripheral is 0x40000000;
We use stm32f103RC6 Chip as an example :
I bought this board pd2 Pin connected led The lamp , Therefore, some key information to know includes the following
//GPIOD Mapping address of
#define GPIOD ((GPIO_TypeDef *)GPIOD_BASE) //0x4001 1400
//GPIO The register of is defined as follows , among __IO Namely volatile keyword , about GPIOD for , We will directly GPIOD The address of the contained register is marked with , Convenient view
typedef struct
{
__IO uint32_t CRL; //0x40011400
__IO uint32_t CRH; //0x40011404
__IO uint32_t IDR; //0x40011408
__IO uint32_t ODR; //0x4001140c
__IO uint32_t BSRR; //0x40011410
__IO uint32_t BRR; //0x40011414
__IO uint32_t LCKR; //0x40011418
} GPIO_TypeDef;
How to light led The lamp ?
We can GPIOD Of ODR Register or BSSR Register operation ;
1、 Yes ODR The register of bit2(0~31) Write 0, be PD2 Output low level , Yes ODR The register of bit2(0~31) Write 1, be PD2 Output high level
2、 Yes BSSR Of bit2(0~31) Write 1,PD2 Output high level , Yes BSSR Of bit(2+16)(0~31) Write 1,PD2 Output low level , //16 Representative pin is 0~15 ,16 One pin . Yes BSSR Register write 0 , Without any action
So we can use the following methods to control LED The light goes on and off .
1、 Control with bit segment operation ODR perhaps BSSR register
2、 Write data in the direct peripheral address register
So we can follow the following method
① The first is right BSSR Perform bit segment operation , Let's look at the previous one BSSR The address for 0x40011410, Then the bit segment operation address ( Yes PD2 Pin , So it is bit2) by 0x4200 0000 + 0x11410*32 + 2*4 Pin output high level
Pin needs to output low level Then the bit segment operation is ( Yes BSSR The register of 18bit To operate ) The address is 0x4200 0000 + 11412*32 +18*4
② Yes BSSR The mapped address of the register writes data directly BSSR The address for 0x40011410, Then we are bit2 Write 1 , Operation for *((volatile uint32_t *)0x40011410) = 0x04; to PD2 Output high level
perhaps bit18 Write 1, *((volatile uint32_t *)0x40011410) = 0x040000; to PD2 Output low level
③ Of course we can do it directly GPIOD Of BSSR Register operation GPIOD->BSRR = 4;// Output high level
GPIOD->BSRR = (uint32_t)4<<16; // Output low level It's totally ok Of
④ We are right. ODR Registers perform bit segment operations ODR The mapping address of is 0x4001140c , Bit segment operation The address is 0x4200 0000 + 0x1140c*32 + 2*4
⑤ Direct pair ODR Register to write data
⑥ call HAL library
The code is as follows , Only for reference
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
int main()
{
//....
//.. some GPIO initialization
while(1)
{
HAL_Delay(1000);
switch(way)
{
case 1:
//TODO:1 Yes BSSR Registers perform bit segment operations
BIT_ADDR(0x40011410,2) = 1; // The macro definition expands to *((volatile unsigned long*)0x42228208) = 1;
HAL_Delay(1000);
BIT_ADDR(0x40011410,18) = 1;
break;
case 2:
//TODO:2 Yes BSSR The mapped address of the register is written directly
*((volatile uint32_t *)0x40011410) = 0x04;
HAL_Delay(1000);
*((volatile uint32_t *)0x40011410) = 0x040000;
break;
case 3:
//TODO:3 Yes BSSR Register operation
GPIOD->BSRR = 4;
HAL_Delay(1000);
GPIOD->BSRR = (uint32_t)4<<16;
break;
case 4:
//TODO:4 Yes ODR Registers perform bit segment operations
BIT_ADDR((GPIOD_BASE+12),2) = 1;
HAL_Delay(1000);
BIT_ADDR((GPIOD_BASE+12),2) = 0;
break;
case 5:
//TODO:5 Yes ODR The mapped address of the register is written directly
*((volatile uint32_t *)0x4001140c) = 0x04;
HAL_Delay(1000);
*((volatile uint32_t *)0x4001140c) = 0x00;
break;
case 6:
//TODO:6 call HAL Library function
HAL_GPIO_WritePin(GPIOD, LED1_Pin, 0);
HAL_Delay(1000);
HAL_GPIO_WritePin(GPIOD, LED1_Pin, 1);
break;
default:break;
}
}
}

Function is led flashing . With IDE yes STM32CUBE ide It's really easy to use !!!()
边栏推荐
- Definition of annotations and annotation compiler
- Binary search (integer binary)
- The @transactional in JUnit disappears. Can @rollback test the flag of rollback?
- Alibaba cloud OSS uploading and intelligent image recognition garbage recognition
- Underlying principle of Concurrency: thread, resource sharing, volatile keyword
- Request and response must know
- 燎原之势 阿里云数据库“百城聚力”助中小企业数智化转型
- stm32mp1 Cortex M4开发篇11:扩展板蜂鸣器控制
- Zhihu wanzan: what kind of programmers are still wanted by the company after the age of 35? Breaking the "middle age crisis" of programmers
- R language sets na Rm=true parameter, deleting missing values in calculation and analysis to obtain valid calculation results (excluding missing values from analyses)
猜你喜欢

Zhihu wanzan: what kind of programmers are still wanted by the company after the age of 35? Breaking the "middle age crisis" of programmers

还在直接用localStorage么?全网最细:本地存储二次封装(含加密、解密、过期处理)

Observation on the salary data of the post-90s: poor, counselled and serious

ADO. Net - invalid size for size property, 0 - ado NET - The Size property has an invalid size of 0

It is said that this year gold three silver four has become gold one silver two.
![[practice] stm32mp157 development tutorial FreeRTOS system 3: FreeRTOS counting semaphore](/img/b1/e4b944877fecc079a772b81c55bfc8.jpg)
[practice] stm32mp157 development tutorial FreeRTOS system 3: FreeRTOS counting semaphore

Request and response must know
![[actual combat] STM32 FreeRTOS migration series tutorial 7: FreeRTOS event flag group](/img/1c/10add042271c11cd129ddfce66f719.jpg)
[actual combat] STM32 FreeRTOS migration series tutorial 7: FreeRTOS event flag group

Unity中的地平面简介

The spring recruitment is also terrible. Ali asked at the beginning of the interview: how to design a high concurrency system? I just split
随机推荐
【实战】STM32 FreeRTOS移植系列教程1:FreeRTOS 二值信号量使用
Are you still using localstorage directly? The thinnest in the whole network: secondary encapsulation of local storage (including encryption, decryption and expiration processing)
Stm32mp1 cortex M4 Development Chapter 11: expansion board buzzer control
[practice] stm32mp157 development tutorial FreeRTOS system 3: FreeRTOS counting semaphore
【实战】STM32MP157开发教程之FreeRTOS系统篇6:FreeRTOS 列表和列表项
R language uses as The date function converts a single character variable to date data and specifies the format of the data format conversion
Solve the problem of error when typescript object gets value
Telecommuting Market Research Report
Float floating layout clear floating
Get the data in the configuration file properties
Concurrency - condition variable
嵌入式远程岗位、兼职、接单、众包平台
How to generate QR code
Clipboard learning records and pit encountered
如何将MindSpore模型转ONNX格式并使用OnnxRuntime推理---开发测试篇
Appareils pris en charge par Arcore
Alibaba P6 employees came to a small company for an interview and asked for an annual salary increase of 500000 yuan. How dare you speak
Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"
Summary of Web automated testing
JS resource disaster recovery