STM32H7S78-DK板卡上面有丰富的外设接口,板载了4个LED小灯与一个用户按键。
其中LD1连接到PO1引脚,LD2连接到PO5引脚。
用户按键连接到PC13引脚。
下面开始在CubeMx工具内进行配置小灯引脚为GPIO输入,按键引脚为外部中断模式,上升沿触发。
点击Generate Code自动生成代码。
static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* USER CODE BEGIN MX_GPIO_Init_1 */ /* USER CODE END MX_GPIO_Init_1 */ /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOM_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOO_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOO, LD1_GREEN_Pin|LD2_ORANGE_Pin, GPIO_PIN_RESET); /*Configure GPIO pin : USER_BUTTON_Pin */ GPIO_InitStruct.Pin = USER_BUTTON_Pin; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(USER_BUTTON_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pins : LD1_GREEN_Pin LD2_ORANGE_Pin */ GPIO_InitStruct.Pin = LD1_GREEN_Pin|LD2_ORANGE_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOO, &GPIO_InitStruct); /* EXTI interrupt init*/ HAL_NVIC_SetPriority(USER_BUTTON_EXTI_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USER_BUTTON_EXTI_IRQn); /* USER CODE BEGIN MX_GPIO_Init_2 */ /* USER CODE END MX_GPIO_Init_2 */ }
在中断处理函数中添加应用代码。每次按键按下,绿色小灯LD1进行翻转。
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { /* Prevent unused argument(s) compilation warning */ UNUSED(GPIO_Pin); /* NOTE: This function should not be modified, when the callback is needed, the HAL_GPIO_EXTI_Callback could be implemented in the user file */ HAL_GPIO_TogglePin(LD1_GREEN_GPIO_Port, LD1_GREEN_Pin); }