STM32CubeMX系列教程1:GPIO
打开STM32CubeMX新建工程,选择STMF746IGT6芯片。选择外部高速晶振(HSE).
(用户代码要加在USER CODE BEGIN N和USER CODE END N之间,否则下次重新生成代码后,会被删除)
/* USER CODE BEGIN 2 */ /** * @brief Turns selected LED On. * @param Led: LED to be set on * This parameter can be one of the following values: * @arg LED1 * @arg LED2 * @arg LED3 * @arg LED4 * @retval None */ void BSP_LED_On(Led_TypeDef Led) { HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET); } /** * @brief Turns selected LED Off. * @param Led: LED to be set off * This parameter can be one of the following values: * @arg LED1 * @arg LED2 * @arg LED3 * @arg LED4 * @retval None */ void BSP_LED_Off(Led_TypeDef Led) { HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET); } /** * @brief Toggles the selected LED. * @param Led: LED to be toggled * This parameter can be one of the following values: * @arg LED1 * @arg LED2 * @arg LED3 * @arg LED4 * @retval None */ void BSP_LED_Toggle(Led_TypeDef Led) { HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]); } /** * @brief Returns the current joystick status. * @retval Code of the joystick key pressed * This code can be one of the following values: * @arg WAKEUP * @arg JOY_NONE * @arg JOY_CTR * @arg JOY_DOWN * @arg JOY_LEFT * @arg JOY_RIGHT * @arg JOY_UP */ JOYState_TypeDef BSP_JOY_GetState(void) { /* Check the pressed keys */ if(HAL_GPIO_ReadPin(WAKEUP_GPIO_Port,WAKEUP_Pin) == GPIO_PIN_SET) { return (JOYState_TypeDef) WAKEUP; } else if(HAL_GPIO_ReadPin(CTR_GPIO_Port,CTR_Pin) == GPIO_PIN_RESET) { return (JOYState_TypeDef) JOY_CTR; } else if(HAL_GPIO_ReadPin(DOWN_GPIO_Port,DOWN_Pin) == GPIO_PIN_RESET) { return (JOYState_TypeDef) JOY_DOWN; } else if(HAL_GPIO_ReadPin(LEFT_GPIO_Port,LEFT_Pin) == GPIO_PIN_RESET) { return (JOYState_TypeDef) JOY_LEFT; } else if(HAL_GPIO_ReadPin(RIGHT_GPIO_Port,RIGHT_Pin) == GPIO_PIN_RESET) { return (JOYState_TypeDef) JOY_RIGHT; } else if(HAL_GPIO_ReadPin(UP_GPIO_Port,UP_Pin) == GPIO_PIN_RESET) { return (JOYState_TypeDef) JOY_UP; } else { return (JOYState_TypeDef) JOY_NONE; } } /* USER CODE END 2 */
以上三个函数的功能分别为点亮LED,熄灭LED,LED状态翻转,读取按键状态。
在gpio.c前面添加LED1-LED4的管脚的数组变量。
/* USER CODE BEGIN 1 */ GPIO_TypeDef* GPIO_PORT[] = {LED1_GPIO_Port, LED2_GPIO_Port, LED3_GPIO_Port, LED4_GPIO_Port}; const uint16_t GPIO_PIN[] = {LED1_Pin, LED2_Pin, LED3_Pin, LED4_Pin}; /* USER CODE END 1 */
在gpio.h前面添加Led_TypeDef和JOYState_TypeDef两个枚举类型。声明刚才添加的四个函数。
/* USER CODE BEGIN Private defines */ typedef enum { LED1 = 0, LED2 = 1, LED3 = 2, LED4 = 3 }Led_TypeDef; typedef enum { JOY_NONE = 0, JOY_CTR = 1, JOY_DOWN = 2, JOY_LEFT = 3, JOY_RIGHT = 4, JOY_UP = 5, WAKEUP = 6 }JOYState_TypeDef; /* USER CODE END Private defines */ void MX_GPIO_Init(void); /* USER CODE BEGIN Prototypes */ void BSP_LED_On(Led_TypeDef Led); void BSP_LED_Off(Led_TypeDef Led); void BSP_LED_Toggle(Led_TypeDef Led); JOYState_TypeDef BSP_JOY_GetState(void); /* USER CODE END Prototypes */
在main.c文件中的main函数的while循环中添加应用代码。代码中使用switch case语句。先通过BSP_JOY_GetState()语句判断是否有按键按下,然后根据按下的按键改变LED1~LED4的状态。
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
switch(BSP_JOY_GetState())
{
case JOY_UP:
BSP_LED_On(LED1);break;
case JOY_RIGHT:
BSP_LED_On(LED2);break;
case JOY_DOWN:
BSP_LED_On(LED3);break;
case JOY_LEFT:
BSP_LED_On(LED4);break;
case JOY_CTR:
case WAKEUP:
BSP_LED_On(LED1);
BSP_LED_On(LED2);
BSP_LED_On(LED3);
BSP_LED_On(LED4);
break;
default:
BSP_LED_Off(LED1);
BSP_LED_Off(LED2);
BSP_LED_Off(LED3);
BSP_LED_Off(LED4);
break;
}
}
/* USER CODE END 3 */