从STM32开始的RoboMaster生活:进阶篇 I [GPIO]
本文已经同步发布于作者部署的私人博客为了更好的排版和观看体验
可以移步到 从STM32开始的RoboMaster生活:进阶篇 I
## 从STM32开始的RoboMaster生活:进阶篇 I
## 0.0 一点点声明
- 本文以及以后的所有属于RoboMaster主题STM32系列的文章,均以**RoboMaster开发板A型**为说明和测试目标,其芯片代号为**STM32F427IIHx**,如果您的芯片代号不同,则不能直接使用本教程代码和引脚配置,请酌情修改!
## 1.0 什么是GPIO?
- General Purpose Input Output通用输入/输出端口
> GPIO接脚可以供使用者由程控自由使用,PIN脚依现实考量可作为通用输入 ( GPI ) 或通用输出 ( GPO ) 或通用输入与输出 ( GPIO ) 。在嵌入式系统中,经常需要控制许多结构简单的外部设备或者电路,这些设备有的需要通过CPU控制,有的需要CPU提供输入信号。对设备的控制,使用传统的串口或者并口就显得比较复杂,所以,在嵌入式微控制器上通常提供了一种“通用可编程I/O端口”,也就是GPIO。一个GPIO端口至少需要两个寄存器,一个做控制用的“通用IO端口控制寄存器”,还有一个是存放数据的“通用I/O端口数据寄存器”。数据寄存器的每一位是和GPIO的硬件引脚对应的,而数据的传递方向是通过控制寄存器设置的,通过控制寄存器可以设置每一位引脚的数据流向。
## 2.0 GPIO在哪里?
1. 在开发板A型中,提供了18个用户自定义GPIO
!(https://i.loli.net/2020/03/11/3HRykqAVfwa2ng5.jpg)
上图中,26即为GPIO,位于开发板A型的中间位置
!(https://i.loli.net/2020/03/11/2CW5rYOqiARvo9d.png)
这是GPIO的具体引脚图,在配置芯片引脚时请务必核实引脚号是否正确
2. RoboMaster同样也非常体贴地预先将一些GPIO接到了用户自定义LED上,在2.1图中的位置为10和18,10的LED×8皆为绿色,而18的LED×2则是一绿一红,在用户自定义按键附近。只能设置为GPIO_Output模式。
!(https://i.loli.net/2020/03/11/fgDRBQVn6vHyx9J.png)
3. 用户自定义按键(白色按键,黑色按键是STM32重启按键)。可以设置为GPIO_Input模式,或者GPIO_EXTI2模式(用于Interrupt 中断,现在不用理解这个,下个教程会讲)
!(https://i.loli.net/2020/03/11/I89YxqeuBpKRQLT.png)
## 3.0 如何使用GPIO?
### 3.1 读取GPIO
```c
GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
```
- 参数
- GPIOx:想要读取的GPIO所属的外设
- GPIO_Pin:想要读取的GPIO的引脚
- 返回值
- GPIO_PinState:当I/O为低 ( 0 ) 时,返回GPIO_PIN_RESET;当I/O为高 ( 1 ) 时,返回GPIO_PIN_SET
### 3.2 写入GPIO
```c
void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
```
- 参数
- GPIOx:想要写入的GPIO所属的外设
- GPIO_Pin:想要写入的GPIO的引脚
- PinState:想要写入的GPIO状态【GPIO_PIN_RESET / GPIO_PIN_SET】
### 3.3 反转GPIO
```c
void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
```
- 参数
- GPIOx:想要反转的GPIO所属的外设
- GPIO_Pin:想要反转的GPIO的引脚
> 如果GPIO状态为GPIO_PIN_RESET,则改为GPIO_PIN_SET;
> 如果GPIO状态为GPIO_PIN_SET,则改为GPIO_PIN_RESET。
### 3.4 锁死GPIO
```c
HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
```
- 参数
- GPIOx:想要锁死的GPIO所属的外设
- GPIO_Pin:想要锁死的GPIO的引脚
> 锁死的是该GPIO的配置,而不是状态。任何尝试修改其配置的操作都会失败,除非重置GPIO。
### 3.5 重置GPIO
```c
void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
```
- 参数
- GPIOx:想要重置的GPIO所属的外设
- GPIO_Pin:想要重置的GPIO的引脚
> 重置GPIO到默认的配置(Input Floating模式)。用于当我们不再使用某个外设时,节约电量,避免额外的消耗。
## 4.0 练习项目
### 4.1 项目简介
- **按键版流水灯**:按下白色按键后,依次让自带的LED灯按顺序亮起,或者依次熄灭
### 4.2 芯片配置
- 芯片视角
!(https://i.loli.net/2020/03/11/cbXkZP7GRwnOVfF.png)
- GPIO配置列表
!(https://i.loli.net/2020/03/11/NUafpOxlEuqTJCS.png)
### 4.3 项目代码
- **一点有必要的说明**:
- 主要看`int main(void)`函数中的代码,其余部分没看懂不用担心,不是本篇章的重点
- 我只放了`main.h`和`main.c`,完整的工程文件可以在[这里](https://github.com/AlchemicRonin/-STM32-RoboMaster-)找到!
- 完整的工程目录
!(https://i.loli.net/2020/03/11/HefBMFyXkY64dpO.png)
- `Inc/main.h`
```c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.h
* @brief : Header for main.c file.
* This file contains the common defines of the application.
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
/* USER CODE END ET */
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
/* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */
/* Private defines -----------------------------------------------------------*/
#define LD8_Pin GPIO_PIN_8
#define LD8_GPIO_Port GPIOG
#define LD7_Pin GPIO_PIN_7
#define LD7_GPIO_Port GPIOG
#define LD6_Pin GPIO_PIN_6
#define LD6_GPIO_Port GPIOG
#define LD5_Pin GPIO_PIN_5
#define LD5_GPIO_Port GPIOG
#define LD4_Pin GPIO_PIN_4
#define LD4_GPIO_Port GPIOG
#define LD3_Pin GPIO_PIN_3
#define LD3_GPIO_Port GPIOG
#define LD2_Pin GPIO_PIN_2
#define LD2_GPIO_Port GPIOG
#define Button_Pin GPIO_PIN_2
#define Button_GPIO_Port GPIOB
#define LD1_Pin GPIO_PIN_1
#define LD1_GPIO_Port GPIOG
#define LD_RED_Pin GPIO_PIN_11
#define LD_RED_GPIO_Port GPIOE
#define LD_GREEN_Pin GPIO_PIN_14
#define LD_GREEN_GPIO_Port GPIOF
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
```
- `Src/main.c`
```c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @briefThe application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
if(HAL_GPIO_ReadPin(Button_GPIO_Port,Button_Pin) == GPIO_PIN_SET){
HAL_Delay(500);
HAL_GPIO_TogglePin(LD_RED_GPIO_Port,LD_RED_Pin);
HAL_Delay(100);
HAL_GPIO_TogglePin(LD_GREEN_GPIO_Port,LD_GREEN_Pin);
HAL_Delay(100);
HAL_GPIO_TogglePin(LD1_GPIO_Port,LD1_Pin);
HAL_Delay(100);
HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin);
HAL_Delay(100);
HAL_GPIO_TogglePin(LD3_GPIO_Port,LD3_Pin);
HAL_Delay(100);
HAL_GPIO_TogglePin(LD4_GPIO_Port,LD4_Pin);
HAL_Delay(100);
HAL_GPIO_TogglePin(LD5_GPIO_Port,LD5_Pin);
HAL_Delay(100);
HAL_GPIO_TogglePin(LD6_GPIO_Port,LD6_Pin);
HAL_Delay(100);
HAL_GPIO_TogglePin(LD7_GPIO_Port,LD7_Pin);
HAL_Delay(100);
HAL_GPIO_TogglePin(LD8_GPIO_Port,LD8_Pin);
}
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOG, LD8_Pin|LD7_Pin|LD6_Pin|LD5_Pin
|LD4_Pin|LD3_Pin|LD2_Pin|LD1_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LD_RED_GPIO_Port, LD_RED_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LD_GREEN_GPIO_Port, LD_GREEN_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : LD8_Pin LD7_Pin LD6_Pin LD5_Pin
LD4_Pin LD3_Pin LD2_Pin LD1_Pin */
GPIO_InitStruct.Pin = LD8_Pin|LD7_Pin|LD6_Pin|LD5_Pin
|LD4_Pin|LD3_Pin|LD2_Pin|LD1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
/*Configure GPIO pin : Button_Pin */
GPIO_InitStruct.Pin = Button_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(Button_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : LD_RED_Pin */
GPIO_InitStruct.Pin = LD_RED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LD_RED_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : LD_GREEN_Pin */
GPIO_InitStruct.Pin = LD_GREEN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LD_GREEN_GPIO_Port, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @briefThis function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#ifdefUSE_FULL_ASSERT
/**
* @briefReports the name of the source file and the source line number
* where the assert_param error has occurred.
* @paramfile: pointer to the source file name
* @paramline: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
```
### 4.4 效果展示
https://www.bilibili.com/video/av95347547/
本文已经同步发布于作者部署的私人博客
为了更好的排版和观看体验
可以移步到 从STM32开始的RoboMaster生活:进阶篇 I
如果诸位看官喜欢的话,想让这个系列继续下去的话,就请在下方留言吧~
你成功地把我拉进了坑 帅猫 发表于 2020-3-12 21:44
你成功地把我拉进了坑
哼哼,只要拉了一个人进坑,那也是胜利了(硬核文写了也没什么人看)~{:5_131:} 炼金浪人 发表于 2020-3-12 21:59
哼哼,只要拉了一个人进坑,那也是胜利了(硬核文写了也没什么人看)~ ...
你一时半会也拉不了我入坑,我还在慢慢玩arduino,玩熟了再说。 不错的板子
页:
[1]