×
单片机 > 单片机程序设计 > 详情

STM32F401利用CubeMX实现按键中断翻转LED

发布时间:2020-06-01 发布时间:
|

硬件平台:NucleoF401RE


软件环境:CubeMX+Kiel5


硬件说明:参考Nucleo板子原理图,用户按键链接IO为PC13,LED链接IO为PA5;



如上搞清楚资源链接关系,就可以利用CubeMX进行初始化代码生成配置,比较简单。需要注意的是在中断嵌套部分需要设置外部中断10-15LINES;


利用CubeMX工具生产代码,下面对部分代码进行说明。


CubeMX生成初始化代码配置后,用户字需要添加自己业务需要的功能即可。


中断函数全部在stm2f4xx_it.c当中,如下所示:


/******************************************************************************/

/* STM32F4xx Peripheral Interrupt Handlers                                    */

/* Add here the Interrupt Handlers for the used peripherals.                  */

/* For the available peripheral interrupt handler names,                      */

/* please refer to the startup file (startup_stm32f4xx.s).                    */

/******************************************************************************/

 

/**

* @brief This function handles EXTI line[15:10] interrupts.

*/

void EXTI15_10_IRQHandler(void)

{

  /* USER CODE BEGIN EXTI15_10_IRQn 0 */

 

  /* USER CODE END EXTI15_10_IRQn 0 */

  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);

  /* USER CODE BEGIN EXTI15_10_IRQn 1 */

 

  /* USER CODE END EXTI15_10_IRQn 1 */

}

如上可以看出,中断触发后会调用HAL_GPIO_EXIT_IRQHandler()函数,其定义原型位于stm32f4xx_hal_gpio.c文件当中。


/**

  * @brief  This function handles EXTI interrupt request.

  * @param  GPIO_Pin Specifies the pins connected EXTI line

  * @retval None

  */

void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)

{

  /* EXTI line interrupt detected */

  if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)

  {

    __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);

    HAL_GPIO_EXTI_Callback(GPIO_Pin);

  }

}

 

/**

  * @brief  EXTI line detection callbacks.

  * @param  GPIO_Pin Specifies the pins connected EXTI line

  * @retval None

  */

__weak 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

   */

}

如上回调函数,定义了UNUSED(GPIO_PIN)虚拟函数,注释中说明了如果需要实现回调功能,需要用户自己在User文件当中定义实现,如下在main.c中重新定义回调函数:


/* USER CODE BEGIN 4 */

void HAL_GPIO_EXTI_Callback (uint16_t GPIO_Pin)

{

    if(GPIO_Pin == GPIO_PIN_13)

        HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

}

/* USER CODE END 4 */

中断发生时,调用回调函数需要判断是哪个IO引起的,分别对应地去实现自定义的功能。



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
8051单片机的函数发生器的设计