当前位置:   article > 正文

STM32学习和实践笔记(19):按键控制实验(用STM32的外部中断的方式实现)

STM32学习和实践笔记(19):按键控制实验(用STM32的外部中断的方式实现)

花了两小时,对照着视频的讲解,终于成功做完这个实验~~

代码如下:

main.c

  1. #include "system.h"
  2. #include "led.h"
  3. #include "SysTick.h"
  4. #include "beep.h"
  5. #include "DigitalTube.h"
  6. #include "key.h"
  7. #include "exti.h"
  8. int main()
  9. {
  10. u8 i=0;
  11. SysTick_Init(72);
  12. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//中断优先级分组
  13. LED_Init();
  14. // BEEP_Init();
  15. // DigitalTube_Init();
  16. key_Init();
  17. My_Exti_Init();
  18. while(1)
  19. {
  20. i++;
  21. if(i%20 ==0)
  22. {
  23. led1=!led1;//LED1闪
  24. }
  25. delay_ms(10);
  26. }
  27. }

exti.c

  1. #include "exti.h"
  2. #include "SysTick.h"
  3. #include "key.h"
  4. #include "led.h"
  5. void My_Exti_Init(void) //因为Exti_Init函数名已在库函数中使用了,所以这里取这个名称
  6. {
  7. NVIC_InitTypeDef NVIC_InitStructure;
  8. EXTI_InitTypeDef EXTI_InitStructure;
  9. //(1)使能IO口时钟,配置IO口模式为输入,这一步在key_Init()已做了,所以这里不需要
  10. //(2)开启 AFIO 时钟,设置 IO 口与中断线的映射关系
  11. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//开启 AFIO 时钟
  12. GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);//配置PA0为EXT0的输入线
  13. GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource2);//配置PE2为EXT2的输入线
  14. GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource3);//配置PE3为EXT3的输入线
  15. GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource4);//配置PE4为EXT4的输入线
  16. //(3)配置中断分组(NVIC),中断分组NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);放在main里了,
  17. // 并对中断分组初始化以及使能中断
  18. NVIC_InitStructure.NVIC_IRQChannel= EXTI0_IRQn;
  19. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =2;
  20. NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;
  21. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  22. NVIC_Init(&NVIC_InitStructure);
  23. NVIC_InitStructure.NVIC_IRQChannel= EXTI2_IRQn;
  24. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =2;
  25. NVIC_InitStructure.NVIC_IRQChannelSubPriority =2;
  26. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  27. NVIC_Init(&NVIC_InitStructure);
  28. NVIC_InitStructure.NVIC_IRQChannel= EXTI3_IRQn;
  29. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =2;
  30. NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;
  31. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  32. NVIC_Init(&NVIC_InitStructure);
  33. NVIC_InitStructure.NVIC_IRQChannel= EXTI4_IRQn;
  34. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =2;
  35. NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
  36. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  37. NVIC_Init(&NVIC_InitStructure);
  38. //(4)初始化EXTI
  39. //配置EXTI_Line0的模式、触发方式并打开
  40. EXTI_InitStructure.EXTI_Line = EXTI_Line0;
  41. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  42. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  43. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  44. EXTI_Init(&EXTI_InitStructure);
  45. //配置EXTI_Line2,3,4的模式、触发方式并打开
  46. EXTI_InitStructure.EXTI_Line = EXTI_Line2|EXTI_Line3|EXTI_Line4;
  47. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  48. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  49. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  50. EXTI_Init(&EXTI_InitStructure);
  51. }
  52. //以下是中断服务程序
  53. void EXTI0_IRQHandler(void)
  54. {
  55. if(EXTI_GetITStatus(EXTI_Line0)==1) //判断是否有按键按下而产生外部中断
  56. {
  57. delay_ms(10);//消除抖动
  58. if(K_UP ==1)
  59. {
  60. led2 = ~led2;
  61. }
  62. }
  63. EXTI_ClearITPendingBit(EXTI_Line0);
  64. }
  65. void EXTI2_IRQHandler(void)
  66. {
  67. if(EXTI_GetITStatus(EXTI_Line2)==1) //判断是否有按键按下而产生外部中断
  68. {
  69. delay_ms(10);//消除抖动
  70. if(K_LEFT ==0)
  71. {
  72. led3 = ~led3;
  73. }
  74. }
  75. EXTI_ClearITPendingBit(EXTI_Line2);
  76. }
  77. void EXTI3_IRQHandler(void)
  78. {
  79. if(EXTI_GetITStatus(EXTI_Line3)==1) //判断是否有按键按下而产生外部中断
  80. {
  81. delay_ms(10);//消除抖动
  82. if(K_DOWN ==0)
  83. {
  84. led4 = ~led4;
  85. }
  86. }
  87. EXTI_ClearITPendingBit(EXTI_Line3);
  88. }
  89. void EXTI4_IRQHandler(void)
  90. {
  91. if(EXTI_GetITStatus(EXTI_Line4)==1) //判断是否有按键按下而产生外部中断
  92. {
  93. delay_ms(10);//消除抖动
  94. if(K_RIGHT ==0)
  95. {
  96. led5 = ~led5;
  97. }
  98. }
  99. EXTI_ClearITPendingBit(EXTI_Line4);
  100. }

exti.h

  1. #ifndef _exti_H
  2. #define _exti_H
  3. #include "system.h"
  4. void My_Exti_Init(void) ;
  5. #endif

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/479280
推荐阅读
相关标签
  

闽ICP备14008679号