标准库按键中断
Posted zoute
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了标准库按键中断相关的知识,希望对你有一定的参考价值。
由于中文注释所以插入截图作为参考
#include "stm32f10x_it.h" #include "stm32f10x_gpio.h" #include "stm32f10x.h" #include "stm32f10x_exti.h" #include "Button.h" #include "Led.h" void init_Button(){ //????PA0?D??òy?? RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//éè??ê±?ó GPIO_InitTypeDef button_structer;//?¨ò??á11ì? button_structer.GPIO_Speed = GPIO_Speed_50MHz;//éè??òy??μ??¢D??ù?è button_structer.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????ê?è? button_structer.GPIO_Pin = GPIO_Pin_0; GPIO_Init(GPIOA,&button_structer);//3?ê??ˉGPIOA PA0 //?D??′|àíoˉêy ía2??D?? /* 1????ê±?ó 2 GPIO1¤×÷·?ê? 3NVIC?D??ó??è?? (ó??è??·?×é£?×ó/?÷ó??è???3?μ) 4EXTIía2??D?????? (????£?extiê1?ü) 5?D??′|àíoˉêy*/ //NVICó??è?????? NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//ó??è??·?×é 0-4ê1?àá¢μ? 9-5 15-10 NVIC_InitTypeDef nvic; nvic.NVIC_IRQChannelCmd = ENABLE; //ê1?ü nvic.NVIC_IRQChannelPreemptionPriority = 0; //?÷ó??è?????? 0-15 nvic.NVIC_IRQChannelSubPriority = 0; //×óó??è?????? 0 nvic.NVIC_IRQChannel = EXTI0_IRQn; //?????D??o? PA0 ?a 0 NVIC_Init(&nvic); //3?ê??ˉ?D?? //ía2??D?????? RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); EXTI_InitTypeDef exti; //?D???á11ì? exti.EXTI_Line = EXTI_Line0; // í¨μà???· exti.EXTI_LineCmd = ENABLE; // ?D??ê1?ü exti.EXTI_Mode = EXTI_Mode_Interrupt; //1¤×÷?£ê?éè???a?D???£ê? exti.EXTI_Trigger = EXTI_Trigger_Falling; //???μ??′¥·¢?D?? EXTI_Init(&exti); GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);//°??D??ó?òy??á??ó?eà′ //????PC13?D??òy?? RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//éè??ê±?ó GPIO_InitTypeDef button_structer1;//?¨ò??á11ì? button_structer1.GPIO_Speed = GPIO_Speed_50MHz;//éè??òy??μ??¢D??ù?è button_structer1.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????ê?è? button_structer1.GPIO_Pin = GPIO_Pin_13; GPIO_Init(GPIOC,&button_structer1);//3?ê??ˉGPIOA PA0 } void EXTI0_IRQHandler(){//?D??′|àíoˉêy int i; if(EXTI_GetFlagStatus(EXTI_Line0) != RESET){ Led_Red_On(); for(i = 0;i<1000000;i++); Led_Red_Off(); for(i = 0;i<1000000;i++); //??3y?D??±ê???? EXTI_ClearITPendingBit(EXTI_Line0); } } void EXTI15_10_IRQHandler(){//?D??′|àíoˉêy int i; if(EXTI_GetFlagStatus(EXTI_Line0) != RESET){ Led_Red_On(); for(i = 0;i<1000000;i++); Led_Red_Off(); for(i = 0;i<1000000;i++); //??3y?D??±ê???? EXTI_ClearITPendingBit(EXTI_Line0); } }
主函数运行
#include "stm32f10x_it.h" #include "stm32f10x_gpio.h" #include "stm32f10x.h" #include "Led.h" #include "Button.h" #include "Uart.h" int main(){ init_Button();//按键初始化 while(1); return 0; }
现象
以上是关于标准库按键中断的主要内容,如果未能解决你的问题,请参考以下文章
STM32 HAL库学习系列第9篇---NVIC按键外部中断函数