这个工程是把四个按键中其中一个PA0配置成外部中断的形式触发
1.配置STM32CubeMx工程文件
(1).配置PA0为外部中断0(EXTI0)模式
(2).配置触发中断模式为下降沿检测
(3).使能外部中断
(4).中断优先级分组,设置四位抢断优先级,零位响应优先级
(5).生成工程文件
2.配置keil文件
(1).在之前按键工程基础上进行添加,添加.c、.h文件
(2).在资源文件里面找到gpio.c并全部复制粘贴到我们工程文件里面bsp_exti.c
(3).
a.对复制过来的内容进行删减,只保留对PA0配置的部分,并在头文件部分引入
#include "exti\bsp_exti.h"
b.在bsp_exti.h文件里面引入#include "main.h"。并声明外部中断初始化函数
c.删除原来key.c里面void KEY_Init(void)对PA0的配置
(4).去资源工程该文件最下端找到外部中断服务函数并复制到我们工程文件的相同位置
(5).根据截屏路径找到外部中断回掉函数,可以看出,此处是弱定义,复制函数名称,在主函数中重新定义
(6).在主函数中声明
#include "exti\bsp_exti.h"
(7).初始化外部中断;重新定义外部中断回掉函数,在步骤(5)的时候复制那个函数名就是此处要重新定义的函数
以下是main.c代码,工程文件放在文章开头,可以不用积分免费下载
#include "main.h"
#include "led\bsp_led.h"
#include "key\bsp_key.h"
#include "exti\bsp_exti.h"
//变量创建区
__IO uint32_t uwTick_Set_Point = 0;//控制Key_Proc的执行速度
//*按键扫描专用变量
unsigned char ucKey_Val, unKey_Down, ucKey_Up, ucKey_Old;
//子函数声明区
void SystemClock_Config(void);
void Key_Proc(void);
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin);
//主函数
int main(void)
{
//内核和时钟的初始化
HAL_Init();
SystemClock_Config();
//外设函数的初始化
LED_Init();
KEY_Init();
GPIO_Init();
while (1)
{
Key_Proc();
}
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
LED_Disp(0x00);
}
void Key_Proc(void)
{
if((uwTick - uwTick_Set_Point)<100) return;//减速函数
uwTick_Set_Point = uwTick;
ucKey_Val = Key_Scan();
unKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Old = ucKey_Val;
if(unKey_Down == 2)
{
LED_Disp(0x88);
}
if(unKey_Down == 1)
{
LED_Disp(0xff);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
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_2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief This 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 */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: 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,
ex: 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****/