当前位置:网站首页>II Stm32f407 chip GPIO programming, register operation, library function operation and bit segment operation
II Stm32f407 chip GPIO programming, register operation, library function operation and bit segment operation
2022-07-02 11:15:00 【Heavy vehicle water】
Stm32f407 chip GPIO Programming
GPIO It's called general input and output interface , As a general input and output function , Input and output 0 and 1, In high level 1, Low level means 0. stay ARM in , The high level is 3.3v, The low level is 0v.
1. View the downloaded schematic , It should be noted that , Pins with the same schematic name are physically connected , You can search the name of silk screen in principle to find the corresponding schematic diagram of hardware .
LED Schematic diagram corresponding to hardware

STM32 Schematic diagram corresponding to the chip


see LED Schematic diagram should achieve two purposes ( Connect or not VDD3v3 Situation judgment )
(1) Make clear the working mode of the operating hardware ( Combined with the instruction manual of hardware )
The corresponding pin is low ——LED bright
The corresponding pin is high ——LED destroy
(2) The hardware to operate is connected to CPU Those pins
D1——————PF9
D2——————PF10
D3——————PE13
D4——————PE14
see CPU The chip manual (STM32F4xx Chinese reference manual and Cortex-M4 Authoritative guide ) Hardware control is achieved by operating registers . There are three register configuration methods ,GPIO Register bit operation , Library function operation , Bit segment operation , The following will be introduced separately .


———————————————————————————————————————————
———————————————————————————————————————————
GPIO Register bit operation
(1)GPIO I / O selection
Input ———— Floating space , On / Drop down input , Reuse , simulation
Output ———— push-pull / Open a leak , On / The drop-down , Reuse , simulation
The difference between push-pull and open drain : Push pull is to pull up , Opening a leak is a weak pull up , Opening the drain and raising the resistance requires external pull-up , Leakage opening also has the function of absorbing current , Can do two-way IO
(2) Output speed
low —— in —— high —— fast
(3) Locking mechanism
lock GPIO Configuration of
(4) Data register
Input data register ———— obtain GPIO Input level
Output data register Set up / Reset register ———— Set up GPIO Output level
(5) Function reuse options
Select multiplexing register
(6) Configuration register
Look up the table to determine the value of register configuration


Check the corresponding hardware register
Stm32f407 share 9 Group GPIO(A~I), Each group 16 individual (0~15),stm32 All registers are 32 Bit .
Check the information you need to get from the hardware register
1. Get the address of the hardware register
stm32 The address of the register in uses the base address + The offset
The base address is in the reference manual 2.3 Query in the table of section
such as GPIOF The base address of the register =0x40021400, If there is an offset, add the offset
The offset of each register is on the description of the register
2. The contents represented by each bit of the register
———————————————————————————————————————————
———————————————————————————————————————————
Do a simple exercise
To configure PF9 The register is General push-pull output , No up and down , High speed output
Mode register
Base address = 0x40021400
(7) Configuration mode register

Bit operation configuration , More trouble
Volatile unsigned int *pxx = (unsigned int*)0x40021400;
// take 18~19 Bit is set to 01
*pxx &=~(0x3<<18); // Bits and operations , First the 18~19 Bit is set to 00
*pxx |=~(0x1<<18); // Bitwise OR operation , then 18~19 Bit is set to 01
Configure the output type register

Configure the output speed register

Pull up and down to select register

Through the above 4 The configuration of registers is completed PF9 The initialization , General push-pull output , No up and down , Fast output , The following is through reset / The set register controls the level of the output .

Here through configuration GPIO Set up / Reset register To control GPIO Output register Output level of , To configure PF9 register , The working principle we need to understand is ( Please look carefully at the register configuration matching relationship in the above figure , This is very important ):
The first point : Set the reset register BS9 Write in bit 1, Output register ODR9 The position will be set 1, Set the reset register BS9 Write in bit 0, Output register ODR9 The position will not change ;
Second point : Set the reset register BR9 Write in bit 1, Output register ODR9 The position will be cleared 0, Set the reset register BR9 Write in bit 0, Output register ODR9 The position will not change ; This is through GPIO Set up / Reset register control GPIO Method of outputting the output level of the register .
to want to GPIO Normal work , Must be turned on GPIO Clock switch , Configure through the peripheral clock enable register
Base address :0x40023800

Write drivers
Stm32 The macro definition of register address is officially provided , You can use it directly .
The idea of writing code ( Pseudo code )
- initialization
Start GPIO The clock
take PF9 Configured as universal push-pull output , No up and down , High speed output
- flashing
While(1){
bright
Time delay
destroy
Time delay
}
——————————————————————————————————————————————————————————————————————————————————————
GPIO The use of library functions , You can find it through the source code or library function manual
(1)GPIO Library function initialization
Void GPIO_Init(GPIO_TypeDef*GPIOx ,GPIO_TypeDef*GPIO_InitStruct);
GPIOx ———— Which group GPIO
GPIO_InitStruct ————GPIO Initialize structure
Typedef struct
{
Uint32_t GPIO_Pin; // Which pins of this group are initialized
GPIOMode_TypeDef GPIO_Mode; // Mode selection
GPIOSpeed_TypeDef GPIOSpeed; // Output speed
GPIOOType_TypeDef GPIOOType; // The output mode
GPIOPuPd_TypeDef GPIO_PuPd; // Select... From the drop-down list
}GPIO_InitTypeDef;
(2)AHB1 Bus clock enable
Void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph,FunctinalStste NewState);
RCC_AHB1Periph———— Which clock RCC_AHB1Periph_GPIOF
NewState———— Can make / close ENABLE/DISABLE
(3) Set up GPIO Output value
void GPIO_SetBits(GPIO_TypeDef*GPIOx,uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_TypeDef*GPIOx,uint16_t GPIO_Pin);
Parameters :
GPIOx———— Which group of pins
GPIO_Pin———— Which pins
stay keil5 Add source code , The source file should be added to the project directory on the left of the project , The path of the header file is added to the configuration interface C/C++ Of include paths in

——————————————————————————————————————————————————————————————————————————————————————
GPIO Bit segment operation
The operation of registers is usually in binary , Relatively speaking, it is not so convenient ,stm32 Bit segment operation is provided to allow developers to access some bits in the register more conveniently and efficiently . Bit segment operation is convenient for programming , It also improves the running efficiency of the code , If register access is frequent , You can use bit segment operations .
bit_word_adddr = bit_band_base+(byte_offset*32)+(bit_number*4)
bit_word_adddr Represents the address of the word to be mapped to the target bit in the alias area
bit_band_base Represents the starting address of the alias area
byte_offset Represents the byte number in the bit segment area where the target bit is located
bit_number Bit position representing the target bit (0-7)
——————————————————————————————————————————————————————————————————————————————————————
practice
utilize GPIO Library function configuration PF9,PF10 Pin is output mode , Push pull output , High speed output , No up and down
// Function code
void led_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
//1. Turn on GPIOE GPIOF The clock of
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOF,ENABLE);
//2.GPIO initialization PF9 PF10 PE13 PE14
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;// The output mode
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;// Push pull output
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;// High speed
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;// No up and down
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;//PF9 PF10
GPIO_Init(GPIOF,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14;//PE13 PE14
GPIO_Init(GPIOE,&GPIO_InitStruct);
//3.LED Off by default
GPIO_SetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);
GPIO_SetBits(GPIOE,GPIO_Pin_13|GPIO_Pin_14);
}
边栏推荐
- 二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
- Luogu p1892 [boi2003] Gang (and search for variant anti set)
- QT学习日记8——资源文件添加
- Implement custom drawer component in quick application
- Logu p3398 hamster looks for sugar (double LCA on the tree to judge whether the two paths in the tree intersect)
- 金山云——2023届暑期实习
- 通过券商经理的开户二维码开股票账户安全吗?还是去证券公司开户安全?
- Complement (Mathematical Simulation
- static 函数中的静态变量
- [AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?
猜你喜欢

【云原生】2.5 Kubernetes 核心实战(下)

二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
![Luogu p5536 [xr-3] core city (greed + tree DP looking for the center of the tree)](/img/dc/2aa55c9b3f23c292820a56ea72fedd.png)
Luogu p5536 [xr-3] core city (greed + tree DP looking for the center of the tree)

从攻击面视角,看信创零信任方案实践

二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)

ctf 记录

二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)

PHP tea sales and shopping online store

二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)

三.芯片啟動和時鐘系統
随机推荐
V2X-Sim数据集(上海交大&纽约大学)
Openmldb meetup No.4 meeting minutes
二叉树专题--AcWing 3540. 二叉搜索树建树(实用板子 构建二叉搜索树 并输出前、中、后序遍历)
QT学习日记8——资源文件添加
Tick Data and Resampling
js中给数组添加元素的方法有哪些
二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)
【深入浅出玩转FPGA学习4----漫谈状态机设计】
Tick Data and Resampling
TIPC Getting Started6
ren域名有价值吗?值不值得投资?ren域名的应用范围有哪些?
flink二开,实现了个 batch lookup join(附源码)
SQLite modify column type
How to use ide to automatically sign and debug Hongmeng application
Special topic of binary tree -- acwing 3540 Binary search tree building (use the board to build a binary search tree and output the pre -, middle -, and post sequence traversal)
Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)
JVM garbage collector
From the perspective of attack surface, see the practice of zero trust scheme of Xinchuang
spritejs
[paid promotion] collection of frequently asked questions, recommended list FAQ