• 设置按键中断,按键1按下,LED亮,再按一次,灭按键2按下,蜂鸣器响。再按一次,不响按键3按下,风扇转,再按一次,风扇停


    src/key_it.c

    1. #include"key_it.h"
    2. //GPIO初始化
    3. void all_led_init()
    4. {
    5. //RCC使能
    6. RCC->MP_AHB4ENSETR |= (0X1<<4);
    7. //设置PE10 PF10 PE8为输出
    8. GPIOE->MODER &= (~(0X3<<20));
    9. GPIOE->MODER |= (0X1<<20);
    10. //设置推挽输出
    11. GPIOE->OTYPER &= (~(0X1<<10));
    12. //设置三个管脚低速输出
    13. GPIOE->OSPEEDR &= (~(0X3<<20));
    14. //设置三个管脚输出时无上拉电阻和下拉电阻
    15. GPIOE->PUPDR &= (~(0X3<<20));
    16. }
    17. void all_spi_init()
    18. {
    19. //RCC使能
    20. RCC->MP_AHB4ENSETR |= (0X3<<1);
    21. //设置PE10 PF10 PE8为输出
    22. GPIOB->MODER &= (~(0X3<<12));
    23. GPIOB->MODER |= (0X1<<12);
    24. //设置推挽输出
    25. GPIOB->OTYPER &= (~(0X1<<6));
    26. //设置三个管脚低速输出
    27. GPIOB->OSPEEDR &= (~(0X3<<12));
    28. //设置三个管脚输出时无上拉电阻和下拉电阻
    29. GPIOB->PUPDR &= (~(0X3<<12));
    30. }
    31. void all_fan_init()
    32. {
    33. //RCC使能
    34. RCC->MP_AHB4ENSETR |= (0X3<<4);
    35. //设置PE10 PF10 PE8为输出
    36. GPIOE->MODER &= (~(0X3<<18));
    37. GPIOE->MODER |= (0X1<<18);
    38. //设置推挽输出
    39. GPIOE->OTYPER &= (~(0X1<<9));
    40. //设置三个管脚低速输出
    41. GPIOE->OSPEEDR &= (~(0X3<<18));
    42. //设置三个管脚输出时无上拉电阻和下拉电阻
    43. GPIOE->PUPDR &= (~(0X3<<18));
    44. }
    45. //按键2的配置
    46. void key2_it_config()
    47. {
    48. //RCC使能GPIOF时钟
    49. RCC->MP_AHB4ENSETR |= (0x1<<5);
    50. //pf7
    51. GPIOF->MODER &= (~(0x3<<14));
    52. //pf7
    53. EXTI->EXTICR2 &=(~(0xFF<<24));
    54. EXTI->EXTICR2 |= (0x5<<24);
    55. //pf7
    56. EXTI->FTSR1 |= (0x1<<7);
    57. //PF7
    58. EXTI->C1IMR1 |= (0x1<<7);
    59. //PF7 97号中断
    60. GICD->ISENABLER[3] |= (0X1<<1);
    61. //PF7
    62. GICD->IPRIORITYR[24] &= (~(0X1F<<11));
    63. //pf7 97中断
    64. GICD->ITARGETSR[24] &= (~(0X3<<8));
    65. GICD->ITARGETSR[24] |= (0X1<<8);
    66. }
    67. //按键1 按键3以及GICC层的配置
    68. void key_it_config()
    69. {
    70. //RCC使能GPIOF时钟
    71. RCC->MP_AHB4ENSETR |= (0x1<<5);
    72. //设置PF9 PF7 PF8GPIO输入
    73. //PF9
    74. GPIOF->MODER &= (~(0x3<<18));
    75. //PF8
    76. GPIOF->MODER &= (~(0x3<<16));
    77. //设置PF9 PF7 PF8产生EXTI事件 EXTI_EXTICRx
    78. //pf8
    79. EXTI->EXTICR3 &=(~(0xFF<<0));
    80. EXTI->EXTICR3 |= (0x5<<0);
    81. //pf9
    82. EXTI->EXTICR3 &=(~(0xFF<<8));
    83. EXTI->EXTICR3 |= (0x5<<8);
    84. //设置事件触发方式为下降沿触发EXTI_FTSR1
    85. //pf8
    86. EXTI->FTSR1 |= (0x1<<8);
    87. //pf9
    88. EXTI->FTSR1 |= (0x1<<9);
    89. //设置EXTI事件触发不屏蔽EXTI_IMR1
    90. //PF8
    91. EXTI->C1IMR1 |= (0x1<<8);
    92. //PF9
    93. EXTI->C1IMR1 |= (0x1<<9);
    94. //使能中断能转发到特定的CPU接口层GICD_ISENABLERx
    95. //PF8 98号中断
    96. GICD->ISENABLER[3] |= (0X1<<2);
    97. //PF9 99号中断
    98. GICD->ISENABLER[3] |= (0X1<<3);
    99. //GICD_ISENABLERx设置中断优先级
    100. //PF8
    101. GICD->IPRIORITYR[24] &= (~(0X1F<<19));
    102. //PF9
    103. GICD->IPRIORITYR[24] &= (~(0X1F<<27));
    104. //设置当前中断被转发到哪一个CPU处理GICD_ITARGETSRx
    105. //pf8 98中断
    106. GICD->ITARGETSR[24] &= (~(0X3<<16));
    107. GICD->ITARGETSR[24] |= (0X1<<16);
    108. //pf9 99中断
    109. GICD->ITARGETSR[24] &= (~(0X3<<24));
    110. GICD->ITARGETSR[24] |= (0X1<<24);
    111. //使能组0转发中断GICD_CTLR
    112. GICD->CTRL |= 0X1;
    113. //设置中断优先级掩码GICC_PMR
    114. GICC->PMR |= (0x1F<<3);
    115. //使能CPU可以处理组0转发的中断GICC_CTLR
    116. GICC->CTRL |= 0X1;
    117. }

    src/do_irq.c

    1. #include "key_it.h"
    2. extern void printf(const char *fmt, ...);
    3. unsigned int i = 0;
    4. void do_irq(void)
    5. {
    6. static int flag=0;
    7. static int flag1=0;
    8. static int flag2=0;
    9. int irqno;//保存中断号
    10. irqno=GICC->IAR & 0X3FF;
    11. switch(irqno)
    12. {
    13. case 99://key1
    14. //按键1中断处理
    15. //printf("KEY1 INT\n");
    16. if(0==flag)
    17. {
    18. //LED1亮
    19. GPIOE->ODR |= (0X1<<10);
    20. flag=1;
    21. }
    22. else if(1==flag)
    23. {
    24. //LED1灭
    25. GPIOE->ODR &= (~(0X1<<10));
    26. flag=0;
    27. }
    28. //清除挂起中断标志位GICD_ICPENDRx
    29. GICD->ICPENDR[3] |= (0x1<<3);
    30. //清除中断触发标志位EXTI_FPR1
    31. EXTI->FPR1 |= (0x1<<9);
    32. break;
    33. case 97://key2
    34. //按键2中断处理
    35. //printf("KEY2 INT\n");
    36. if(0==flag1)
    37. {
    38. //蜂鸣器响
    39. GPIOB->ODR |= (0X1<<6);
    40. flag1=1;
    41. }
    42. else if(1==flag1)
    43. {
    44. //蜂鸣器不响
    45. GPIOB->ODR &= (~(0X1<<6));
    46. flag1=0;
    47. }
    48. //清除挂起中断标志位GICD_ICPENDRx
    49. GICD->ICPENDR[3] |= (0x1<<1);
    50. //清除中断触发标志位EXTI_FPR1
    51. EXTI->FPR1 |= (0x1<<7);
    52. break;
    53. case 98://key3
    54. //按键3中断处理
    55. // printf("KEY3 INT\n");
    56. if(0==flag2)
    57. {
    58. //风扇转
    59. GPIOE->ODR |= (0X1<<9);
    60. flag2=1;
    61. }
    62. else if(1==flag2)
    63. {
    64. //风扇停
    65. GPIOE->ODR &= (~(0X1<<9));
    66. flag2=0;
    67. }
    68. //清除挂起中断标志位GICD_ICPENDRx
    69. GICD->ICPENDR[3] |= (0x1<<2);
    70. //清除中断触发标志位EXTI_FPR1
    71. EXTI->FPR1 |= (0x1<<8);
    72. break;
    73. }
    74. //清除处理完的中断号GICC_EOIR
    75. GICC->EOIR =irqno;
    76. }

    include/key_it.h

    1. #ifndef __KEY_IT_H__
    2. #define __KEY_IT_H__
    3. #include "stm32mp1xx_rcc.h"
    4. #include "stm32mp1xx_gpio.h"
    5. #include "stm32mp1xx_exti.h"
    6. #include "stm32mp1xx_gic.h"
    7. void key_it_config();
    8. void key2_it_config();
    9. void all_led_init();
    10. void all_spi_init();
    11. void all_fan_init();
    12. #endif

    main.c

    1. #include "uart.h"
    2. #include "key_it.h"
    3. int main()
    4. {
    5. uart4_init();//串口初始化
    6. all_led_init();
    7. //中断初始化
    8. key_it_config();
    9. key2_it_config();
    10. all_spi_init();
    11. all_fan_init();
    12. while(1)
    13. {
    14. //保证主程序不结束
    15. }
    16. return 0;
    17. }

  • 相关阅读:
    css 实现textarea标签高度自适应
    <Linux复习>基本指令及重要热键
    Github霸榜,2022最新一线大厂Java八股文合集PDF版震撼开源
    材料数据库设计问题
    【微信小程序 | 实战开发】ES5、ES6概述和新特性介绍
    Pytorch基础:Tensor的reshape方法
    (续)SSM整合之spring笔记(AOP AOP概念及相关术语)(P099)
    weblogic/CVE-2018-2894文件上传漏洞复现
    面试阿里P6,过关斩将直通2面,结果3面找了个架构师来吊打我?
    WRFDA资料同化实践技术应用
  • 原文地址:https://blog.csdn.net/yjh666jhy/article/details/133797963