• arm day 9


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

    main.c

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

    key_it.c

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

    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. led1_init(); //led初始化
    7. spi_init(); //蜂鸣器初始化
    8. fan_init(); //风扇初始化
    9. static int flag=0;
    10. int irqno;//保存中断号
    11. irqno=GICC->IAR & 0X3FF;
    12. switch(irqno)
    13. {
    14. case 99://key1
    15. //按键1中断处理
    16. if(0==flag)
    17. {
    18. GPIOE->ODR |= (0x1<<10);
    19. flag=1;
    20. }
    21. else if(1==flag)
    22. {
    23. GPIOE->ODR &= (~(0x1<<10));
    24. flag=0;
    25. }
    26. //清除挂起中断标志位GICD_ICPENDRx
    27. GICD->ICPENDR[3] |= (0x1<<3);
    28. //清除中断触发标志位EXTI_FPR1
    29. EXTI->FPR1 |= (0x1<<9);
    30. break;
    31. case 97://key2
    32. //按键2中断处理
    33. if(0==flag)
    34. {
    35. GPIOB->ODR |= (0x1<<6);
    36. flag=1;
    37. }
    38. else if(1==flag)
    39. {
    40. GPIOB->ODR &= (~(0x1<<6));
    41. flag=0;
    42. }
    43. //清除挂起中断标志位GICD_ICPENDRx
    44. GICD->ICPENDR[3] |= (0x1<<1);
    45. //清除中断触发标志位EXTI_FPR1
    46. EXTI->FPR1 |= (0x1<<7);
    47. break;
    48. case 98://key3
    49. //按键3中断处理
    50. if(0==flag)
    51. {
    52. GPIOE->ODR |= (0x1<<9);
    53. flag=1;
    54. }
    55. else if(1==flag)
    56. {
    57. GPIOE->ODR &= (~(0x1<<9));
    58. flag=0;
    59. }
    60. //清除挂起中断标志位GICD_ICPENDRx
    61. GICD->ICPENDR[3] |= (0x1<<2);
    62. //清除中断触发标志位EXTI_FPR1
    63. EXTI->FPR1 |= (0x1<<8);
    64. break;
    65. }
    66. //清除处理完的中断号GICC_EOIR
    67. GICC->EOIR =irqno;
    68. }

    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 delay1(int ms);
    10. void led1_init();
    11. void spi_init();
    12. void fan_init();
    13. #endif

  • 相关阅读:
    docker常用命令
    Spring Boot框架的原理及应用详解(四)
    软件测试 遇到bug却无法重现怎么办?
    华为云云服务器云耀L实例评测 | 从零到一:华为云云耀云服务器L实例上手体验
    一些C语言知识
    小景的Dba之路--如何导出0记录表以及数据泵的使用
    对话永洪科技CEO何春涛:专注BI,决胜AI时代丨数据猿专访
    导航【操作系统】
    【C语言】—— 动态内存管理
    【笔记】html图片映射usemap(vue环境下、map、area、coords)
  • 原文地址:https://blog.csdn.net/A18801772899/article/details/133799468