• 按键中断实验


    key.h

    1. 1 #ifndef __KEY_H__
    2. 2 #define __KEY_H__
    3. 3
    4. 4 #include "stm32mp1xx_gpio.h"
    5. 5 #include "stm32mp1xx_gic.h"
    6. 6 #include "stm32mp1xx_rcc.h"
    7. 7 #include "stm32mp1xx_exti.h"
    8. 8
    9. 9
    10. 10 //需要初始化GPIOF key1 = pf pin 9
    11. 11 void hal_gpio_key1_init(void);
    12. 12
    13. 13
    14. 14 //初始化exti
    15. 15 void hal_exti_key1_init(void);
    16. 16
    17. 17 //初始化gicd
    18. 18 void hal_gicd_key1_init(void);
    19. 19
    20. 20 //初始化gicc
    21. 21 void hal_gicc_key1_init(void);
    22. 22
    23. 23 #endif
    24. ~
    25. ~
    26. ~

    IRQ中断执行代码:

    1. 1 #include "key.h"
    2. 2 //#include "led.h"
    3. 3
    4. 4 extern void printf(const char *fmt, ...);
    5. 5 extern void delay_ms(int ms);
    6. 6
    7. 7 unsigned int i = 0;
    8. 8 void do_irq(void)
    9. 9 {
    10. 10 printf("key1 is pressed\n\r");
    11. 11 //获取中断号
    12. 12 i = GICC->IAR;
    13. 13
    14. 14 printf("i = %d\n\r",i);
    15. 15 i &= (0x3ff);
    16. 16 //判断中断号
    17. 17 if(99 == i)
    18. 18 {
    19. 19
    20. 20
    21. 21 GICC->EOIR &= ~(0x3ff);
    22. 22 GICC->EOIR |= ((0x3ff)&i);
    23. 23 //clear
    24. 24 //99/32 = 3---3
    25. 25 GICD->ICPENDR[3] |= (0x1 <<3);
    26. 26
    27. 27 EXTI->FPR1 |= (0x1 <<9);
    28. 28
    29. 29
    30. 30 //清除中断号以及中断挂起标志
    31. 31
    32. 32 }
    33. 33
    34. 34
    35. 35 return;
    36. 36 }

    key.c

    1. 1 #include "key.h"
    2. 2
    3. 3
    4. 4
    5. 5 //需要初始化GPIOF key1 = pf pin 9
    6. 6 void hal_gpio_key1_init(void)
    7. 7 {
    8. 8 //RCC使能GPIOF
    9. 9 RCC->MP_AHB4ENSETR |= (0X1 <<5);
    10. 10
    11. 11 //GPIOF pf9设置为input
    12. 12 GPIOF->MODER &= ~(0x3 <<(9*2));
    13. 13
    14. 14 return;
    15. 15 }
    16. 16
    17. 17
    18. 18 //初始化exti
    19. 19 void hal_exti_key1_init(void)
    20. 20 {
    21. 21 EXTI->EXTICR3 &= ~(0XFF <<8);
    22. 22 EXTI->EXTICR3 |= (0x05 <<8);
    23. 23
    24. 24 EXTI->FTSR1 |= (0X1 <<9);
    25. 25
    26. 26 EXTI->C1IMR1 |= (0X1 <<9);
    27. 27
    28. 28 /*
    29. 29 EXTI->FPR1 |= (0x1 <<9);
    30. 30 */
    31. 31 return;
    32. 32 }
    33. 33
    34. 34 //初始化gicd
    35. 35 void hal_gicd_key1_init(void)
    36. 36 {
    37. 37 GICD->CTRL |= (0X1);
    38. 38
    39. 39 //BIT 3
    40. 40 GICD->ISENABLER[3] |= (0X1<<3);
    41. 41
    42. 42 //99/4 = 24---3; 3:24~31 -->24+3~31
    43. 43 GICD->IPRIORITYR[24] &= ~(0x1f <<27);
    44. 44 GICD->IPRIORITYR[24] |= (0x01 <<27);
    45. 45
    46. 46 //99/4 = 24---3; 3*8 = 24; [25~24]
    47. 47 GICD->ITARGETSR[24] &= ~(0x3 <<24);
    48. 48 GICD->ITARGETSR[24] |= (0x1 <<24);
    49. 49
    50. 50 /*
    51. 51 //clear
    52. 52 //99/32 = 3---3
    53. 53 GICD->ICPENDR[3] |= (0x1 <<3);
    54. 54 */
    55. 55 return;
    56. 56 }
    57. 57
    58. 58 //初始化gicc
    59. 59 void hal_gicc_key1_init(void)
    60. 60 {
    61. 61 GICC->CTRL |= (0x1<<0);
    62. 62
    63. 63 GICC->PMR &= ~(0x1f <<3);
    64. 64 GICC->PMR |= (0X11 <<3);
    65. 65
    66. 66 /*
    67. 67
    68. 68 GICC->EOIR &= ~(0x3ff);
    69. 69 GICC->EOIR |= ((0x3ff)&GICC->IAR);
    70. 70
    71. 71 */
    72. 72 }
    73. 73
    74. 74
    75. 75
    76. ~

    main.c

    1. 1 //#include "led.h"
    2. 2 #include "key.h"
    3. 3
    4. 4 extern void printf(const char *fmt, ...);
    5. 5 void delay_ms(int ms)
    6. 6 {
    7. 7 int i,j;
    8. 8 for(i = 0; i < ms;i++)
    9. 9 for (j = 0; j < 1800; j++);
    10. 10 }
    11. 11
    12. 12
    13. 13 int main(void)
    14. 14 {
    15. 15 hal_gpio_key1_init();
    16. 16
    17. 17 hal_exti_key1_init();
    18. 18
    19. 19 hal_gicd_key1_init();
    20. 20
    21. 21 hal_gicc_key1_init();
    22. 22 while(1)
    23. 23 {
    24. 24 ;
    25. 25 }
    26. 26 return 0;
    27. 27 }
    28. 28 /*
    29. 29 LED1(INIT);//led1_init();
    30. 30 LED2(INIT);//led2_init();
    31. 31 LED3(INIT);//led3_init();
    32. 32 LED1(ON);//led1_on();
    33. 33 delay_ms(500);
    34. 34 LED1(OFF);//led1_off();
    35. 35 delay_ms(500);
    36. 36
    37. 37 LED2(ON);//led2_on();
    38. 38 delay_ms(500);
    39. 39 LED2(OFF);//led2_off();
    40. 40 delay_ms(500);
    41. 41
    42. 42 LED3(ON);//led3_on();
    43. 43 delay_ms(500);
    44. 44 LED3(OFF);//led3_off();
    45. 45 delay_ms(500);
    46. 46
    47. 47 LED2(ON);//led2_on();
    48. 48 delay_ms(500);
    49. 49 LED2(OFF);//led2_off();
    50. 50 delay_ms(500);
    51. 51
    52. 52
    53. 53 */
    54. ~

  • 相关阅读:
    Uniapp 报错Uncaught URIError: URI malformed
    css加载动画
    这500万你领了吗?成都市生物医药产业发展的专项政策申报奖励
    C++虚函数(定义,作用,原理,案例)
    SpringBoot扩展点EnvironmentPostProcessor
    【PAT甲级 - C++题解】1031 Hello World for U
    [go学习笔记.第十六章.TCP编程] 3.项目-海量用户即时通讯系统-redis介入,用户登录,注册
    unix网络编程(四)epoll反应堆
    Web安全技能树-资源汇总
    漏洞补丁:windwos补丁下载(MS17-010)
  • 原文地址:https://blog.csdn.net/Kill_the_joker/article/details/128124210