当前位置:网站首页>STM32 introductory tutorial lesson 2
STM32 introductory tutorial lesson 2
2022-07-27 02:17:00 【Ruomu·】
List of articles
STM32 Introductory tutorial lesson 2 ------ Introduce GPIO
Catalog
Two .GPIO Introduction to the mode of ( Four inputs and four outputs )
1. Floating input _IN_FLOATING:
7. Open drain multiplexed output _AF_OD
8. Push pull multiplex output _AF_PP
1. First step , Use RCC Turn on GPIO The clock :
2. The second step , Use GPIO_Init Function initialization GPIO:
3. The third step , Use output and input functions to control GPIO mouth , With a little LED For example :
Preface
In the previous article, we learned how to build a stm32 Of keil engineering , Let's talk about it in detail GPIO
One 、GPIO What is it? ?
1. Brief introduction GPIO
Let's first introduce GPIO,GPIO(General Input Output) It is a general input and output port , It can be configured into eight input and output modes , Pin level :0—3.3V, Some pins tolerate 5V.
The output mode —— It can control the output high and low levels of the port , To drive LED, Control the buzzer , Analog communication protocol output timing, etc ;
The input mode —— It can read the high and low level or voltage of the port , Used to read key input , External module level signal input ,AOC Voltage acquisition , Analog communication protocol, receiving data, etc .
2.GPIO The basic structure

Two .GPIO Introduction to the mode of ( Four inputs and four outputs )
1. Floating input _IN_FLOATING:
When GPIO When using floating input mode ,STM32 The pin state of is uncertain ,I/O The level signal of the port directly enters the input data register . here STM32 The resulting level state depends entirely on GPIO External level status , So in the GPIO The external pin is suspended ( Without signal input ) when , Reading the level state of the port is an uncertain value .
Usually used for IIC、USART.
2. Analog input _AIN 
It's easy to understand , The most common occasion is ADC Analog input , Unlike other input modes, only 0 and 1, Analog input mode can read very small changes in values ,I/O Analog signal of the port ( Voltage signal , Not the level signal ) Direct analog input to on-chip peripheral modules , such as ADC Module etc. . Analog signals are generally :3.3v 5v 9v.
3 With pull-up input _IPU

External interrupt mode with rising edge trigger detection , Compared with the floating input mode described earlier , Just on the data channel , A pull-up resistor is connected , according to STM32 The data book for , The pull-up resistance is between 30K~50K ohm . Again ,CPU At any time “ Input data register ” On the other end of , Read through the internal data bus I/O The state of the level change of the port .
4 . With drop-down input _IPD

Pull down input mode ,I/O The level signal of the port directly enters the input data register . But in I/O The port is suspended ( No signal input ) Under the circumstances , The level of the input terminal remains low ; And in I/O When the port input is high , The input level is also high .
5. Open drain output _OUT_OD

Open drain output mode ( Pull up resistance +N-MOS tube ), Set... By setting bit / Clear the value of the register or output data register , Via N-MOS tube , Finally output to I/O port .
Can output 0 and 1, It is suitable for occasions with mismatched levels , To get a high level, you need to pull up the resistor .
6. Push pull output _OUT_PP

In push-pull output mode (P-MOS tube +N-MOS tube ), Set... By setting bit / Clear the value of the register or output data register , Via P-MOS Tube and N-MOS tube , Finally output to I/O port .
Can output high and low level 0 and 1, Applicable to two-way IO Use .
7. Open drain multiplexed output _AF_OD

Open drain multiplexing output mode , It is very similar to the open drain output mode . Just the source of the high and low levels of the output , Not let CPU Write directly to the output data register , Instead, it is determined by using the multiplexing function output of the on-chip peripheral module .
8. Push pull multiplex output _AF_PP

Push pull multiplexing output mode , It is very similar to the push-pull output mode . Just the source of the high and low levels of the output , Not let CPU Write directly to the output data register , Instead, it is determined by using the multiplexing function output of the on-chip peripheral module .
In one sentence, distinguish between open drain and push-pull output :
Both high and low levels of push-pull output have driving ability
Open drain output high level has no driving ability , Low level has driving ability

3、 ... and 、GPIO How to use
1. First step , Use RCC Turn on GPIO The clock :
Code up :
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC,ENABLE); Start RCC The clock
The detailed steps : stay libraries in --stm32f10x_rcc.h in :

Find the definition of the following library function :

It's about GPIO Clock open library function definition , Which one to choose depends on your project needs
With APB2 For example , Go to function definition :
Go straight to the code :
/**
* @brief Enables or disables the High Speed APB (APB2) peripheral clock.
* @param RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
* This parameter can be any combination of the following values:
* @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,
* RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,
* RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,
* RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,
* RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,
* RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,
* RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11
* @param NewState: new state of the specified peripheral clock.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
RCC->APB2ENR |= RCC_APB2Periph;
}
else
{
RCC->APB2ENR &= ~RCC_APB2Periph;
}
}
According to the I/O Determine the first function parameter , then ENABLE Is the second parameter ;
The clock starts successfully .
2. The second step , Use GPIO_Init Function initialization GPIO:
Code up :
GPIO_Init(GPIOA,&GPIO_InitStructure);The detailed steps : go to GPIO_Init Function definition :
/**
* @brief Initializes the GPIOx peripheral according to the specified
* parameters in the GPIO_InitStruct.
* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
* @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that
* contains the configuration information for the specified GPIO peripheral.
* @retval None
*/
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
uint32_t tmpreg = 0x00, pinmask = 0x00;
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
According to the I/O Determine the first function parameter , With GPIOA For example , So the first parameter is GPIOA, According to the function definition , The second parameter needs to define the structure ;
Code up :
GPIO_InitTypeDef GPIO_InitStructure;
Defining structure , If you make a mistake 268, You can put this line in RCC Above the clock , Put it in RCC The following words need to be in the compiler magic wand -C/C++ Change it to C99
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;// Push pull output
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
After the first line declares the structure , Three contents of the structure are introduced below , The following describes the method in detail :
First, first GPIO_Mode
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;// Push pull output
go to GPIO_Mode Definition :
GPIOMode_TypeDef GPIO_Mode;
/*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIOMode_TypeDef*/Continue to GPIOMode_TypeDef:

It's an enumeration :
/**
* @brief Configuration Mode enumeration
*/
typedef enum
{ GPIO_Mode_AIN = 0x0,
GPIO_Mode_IN_FLOATING = 0x04,
GPIO_Mode_IPD = 0x28,
GPIO_Mode_IPU = 0x48,
GPIO_Mode_Out_OD = 0x14,
GPIO_Mode_Out_PP = 0x10,
GPIO_Mode_AF_OD = 0x1C,
GPIO_Mode_AF_PP = 0x18
}GPIOMode_TypeDef;
Select according to the needs of the project , Take push-pull output as an example : choice GPIO_Mode_Out_PP, therefore ,Mode Get it done .
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;// Push pull output In the same way :GPIO_Pin and GPIO_Speed;
It should be noted that , turn GPIO_Pin Select member, Here's the picture :

After all is written , Structure definition completed
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Push pull output
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;
thus ,GPIO_Init Write well :
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Push pull output
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;
GPIO_Init(GPIOA,&GPIO_InitStructure);
// Up to the top ,GPIO Initialization complete
3. The third step , Use output and input functions to control GPIO mouth , With a little LED For example :
On the first code :
GPIO_SetBits(GPIOC,GPIO_Pin_13);// take PC13 Set the port to high level ( The light goes out )
GPIO_ResetBits(GPIOC,GPIO_Pin_13);// take PC13 Set the port to low level ( Light on )
Go to function definition and write parameters according to project requirements , I won't explain it in detail , The next issue will explain control in detail LED The operation of
thus ,GPIO Introduction complete .
边栏推荐
- Golang - sync包的使用 (WaitGroup, Once, Mutex, RWMutex, Cond, Pool, Map)
- C语言——赋值运算符、复合的赋值运算符、自增自减运算符、逗号运算符、条件运算符、goto语句、注释
- The gradient descent method and Newton method are used to calculate the open radical
- Flink1.13.6 detailed deployment method
- Golang bufio Reader 源码详解
- Unity Huatuo hot update environment installation and sample project
- 关于编程的自我介绍和规划
- [explain C language in detail] takes you to play with functions
- OSPF protocol overview and basic concepts
- 2022最新抖音直播监控(二)直播间流媒体下载
猜你喜欢

Text to image intensive reading of paper gr-gan: gradually refine text to image generation

【volatile原理】volatile原理
npm报错, Error: EPERM: operation not permitted, mkdir
![[MySQL] MySQL startup and shutdown commands and some error reports to solve problems](/img/23/b4c90604eba796692fbe049d2679d1.png)
[MySQL] MySQL startup and shutdown commands and some error reports to solve problems
![[explain C language in detail] takes you to play with the choice (Branch) structure](/img/ca/7ee9f62a2478785c97684c7a0cc749.png)
[explain C language in detail] takes you to play with the choice (Branch) structure
![[volatile principle] volatile principle](/img/d4/adb4b43aaccecd506065ce838c569e.png)
[volatile principle] volatile principle

Static routing default routing VLAN experiment

静态综合实验(静态路由、环回接口、缺省路由、空接口、浮动静态的综合练习)

第五讲—按键控制LED

广域网技术实验
随机推荐
MySQL课程1.简单命令行--简单记录 欢迎补充纠错
【volatile原理】volatile原理
C language implementation of the small game [sanziqi] Notes detailed logic clear, come and have a look!!
Text to image paper intensive reading ssa-gan: text to image generation with semantic spatial aware Gan
【数据库课程设计】SQLServer数据库课程设计(学生宿舍管理),课设报告+源码+数据库关系图
Solution: various error reports and pit stepping and pit avoidance records encountered in the alchemist cultivation plan pytoch+deeplearning (I)
第四讲—讲解GPIO_Write函数以及相关例程
7.13 蔚来提前批笔试
HCIA基础知识(1)
Experiment of OSPF in mGRE environment
[explain C language in detail] takes you to play with the choice (Branch) structure
SQL anti injection regular expression
Solution to high collapse
HCIA静态路由基础模拟实验
7.8 锐捷网络笔试
Unity Huatuo example project source code analysis and inspiration
WAN technology experiment
2022 latest live broadcast monitoring 24-hour monitoring (III) analysis of barrage in live broadcast room
2022最新抖音直播监控(二)直播间流媒体下载
HCIA(网络初级综合实验练习)

