• stm32mp157中断简单应用


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

    main.c

    1. #include "gpio.h"
    2. #include "key_it.h"
    3. int main()
    4. {
    5. //中断初始化
    6. key3_it_config();
    7. key_it_config();
    8. led_init();
    9. beep_init();
    10. fan_init();
    11. while(1)
    12. {
    13. //保证主程序不结束
    14. }
    15. return 0;
    16. }

    do_irq.c

    1. #include "key_it.h"
    2. extern void printf(const char *fmt, ...);
    3. unsigned int i = 0;
    4. void delay(int ms){
    5. for(int i=0; i<ms; i++){
    6. for(int j=0; j<2000; j++){}
    7. }
    8. }
    9. unsigned int flag_led = 0;
    10. unsigned int flag_beep = 0;
    11. unsigned int flag_fan = 0;
    12. void do_irq(void)
    13. {
    14. //保存中断号
    15. int irqno;
    16. printf("%d\n", irqno);
    17. irqno = GICC->IAR & 0x3FF;
    18. switch(irqno)
    19. {
    20. case 99:
    21. //按键1中断处理
    22. if(flag_led==0){
    23. GPIOE->ODR |= (0x1<<10);
    24. GPIOF->ODR |= (0x1<<10);
    25. GPIOE->ODR |= (0x1<<8);
    26. flag_led = 1;
    27. }else{
    28. GPIOE->ODR &= (~(0x1<<10));
    29. GPIOF->ODR &= (~(0x1<<10));
    30. GPIOE->ODR &= (~(0x1<<8));
    31. flag_led = 0;
    32. }
    33. //清除挂起中断标志位GICD_ICPENDRx
    34. GICD->ICPENDR[3] |= (0x1 << 3);
    35. //清除中断触发标志位EXTI_FPR1
    36. EXTI->FPR1 |= (0x1 << 9);
    37. break;
    38. case 97:
    39. //按键2中断处理
    40. if(flag_beep==0){
    41. GPIOB->ODR |= (0x1<<6);
    42. flag_beep = 1;
    43. }else{
    44. GPIOB->ODR &= (~(0x1<<6));
    45. flag_beep = 0;
    46. }
    47. //清除挂起中断标志位GICD_ICPENDRx
    48. GICD->ICPENDR[3] |= (0x1 << 1);
    49. //清除中断触发标志位EXTI_FPR1
    50. EXTI->FPR1 |= (0x1 << 7);
    51. break;
    52. case 98:
    53. //按键3中断处理
    54. if(flag_fan==0){
    55. GPIOE->ODR |= (0x1<<9);
    56. flag_fan = 1;
    57. }else{
    58. GPIOE->ODR &= (~(0x1<<9));
    59. flag_fan = 0;
    60. }
    61. //清除挂起中断标志位GICD_ICPENDRx
    62. GICD->ICPENDR[3] |= (0x1 << 2);
    63. //清除中断触发标志位EXTI_FPR1
    64. EXTI->FPR1 |= (0x1 << 8);
    65. break;
    66. }
    67. //清除处理完的中断号GICC_EOIR
    68. GICC->EOIR = irqno;
    69. }

    key_it.c

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

    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 key3_it_config();
    9. void led_init();
    10. void beep_init();
    11. void fan_init();
    12. #endif

    效果图

  • 相关阅读:
    《宝塔面板教程1》:宝塔面板安装命令及安装要求
    [PAT练级笔记] 35 Basic Level 1035 插入与归并
    做抖店最主要核心是什么?运营技术重要吗?
    Sharding-JDBC分库分表-自定义分片算法-4
    java创建本地文件并读取文件
    Linux C语言 vim编辑器 使用 sqlite3数据库 makefile 的网络编程 qq 聊天室项目
    vue--5.nextTick、局部和全局、单文件组件、单文件组件的属性、css相关技术
    最长回文子串&最长子串&第K大的数字&atoi
    【四】ElasticSearch 文档操作:增删改、高级搜索、聚合搜索,倒排索引
    2024,java开发,已经炸了吗?
  • 原文地址:https://blog.csdn.net/qq_64682865/article/details/133799334