• 按钮控制LED灯、蜂鸣器、风扇实验


    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. #endif

    key_it.c

    1. #include "key_it.h"
    2. void key_it_config(){
    3. //RCC使能GPIOF时钟
    4. RCC->MP_AHB4ENSETR |= (0x1<<5);
    5. //设置PF9 PF7 PF8设置为输入模式
    6. GPIOF->MODER &= (~(0x3F<<14));
    7. //设置PF9 PF7 PF8产生EXTI事件
    8. EXTI->EXTICR3 &= (~(0xFF<<8));
    9. EXTI->EXTICR3 |= (0x5<<8);
    10. EXTI->EXTICR3 &= (~(0xFF));
    11. EXTI->EXTICR3 |= (0x5);
    12. EXTI->EXTICR2 &= (~(0xFF<<24));
    13. EXTI->EXTICR2 |= (0x5<<24);
    14. //设置事件触发方式为下降沿触发EXTI_FTSR1
    15. EXTI->FTSR1 |= (0x7<<7);
    16. //设置EXTI事件触发不屏蔽EXTI_IMR1
    17. EXTI->C1IMR1 |= (0x7<<7);
    18. //使能中断能转发到特定的CPU接口层GICD_ISENABLERx
    19. GICD->ISENABLER[3] |= (0x7<<1);
    20. //GICD_ISENABLERx设置中断优先级
    21. //PF7
    22. GICD->IPRIORITYR[24] &= (~(0X1F<<11));
    23. //PF8
    24. GICD->IPRIORITYR[24] &= (~(0X1F<<19));
    25. //PF9
    26. GICD->IPRIORITYR[24] &= (~(0X1F<<27));
    27. //设置当前中断被转发到哪一个CPU处理GICD_ITARGETSRx
    28. //pf7 97中断
    29. GICD->ITARGETSR[24] &= (~(0X3<<8));
    30. GICD->ITARGETSR[24] |= (0X1<<8);
    31. //pf8 98中断
    32. GICD->ITARGETSR[24] &= (~(0X3<<16));
    33. GICD->ITARGETSR[24] |= (0X1<<16);
    34. //pf9 99中断
    35. GICD->ITARGETSR[24] &= (~(0X3<<24));
    36. GICD->ITARGETSR[24] |= (0X1<<24);
    37. //使能组0转发中断GICD_CTLR
    38. GICD->CTRL |= 0X1;
    39. //设置中断优先级掩码GICC_PMR
    40. GICC->PMR |= (0x1F<<3);
    41. //使能CPU可以处理组0转发的中断GICC_CTLR
    42. GICC->CTRL |= 0X1;
    43. }

    led.h

    1. #ifndef __LED_H__
    2. #define __LED_H__
    3. #include "stm32mp1xx_gpio.h"
    4. #include "stm32mp1xx_rcc.h"
    5. void all_led_init();
    6. #endif

    led.c

    1. #include "led.h"
    2. void all_led_init(){
    3. //RCC使能
    4. RCC->MP_AHB4ENSETR |= (0x3<<4);
    5. //设置PE10 PF10 PE8为输出
    6. GPIOE->MODER &= (~(0X3<<20));
    7. GPIOE->MODER |= (0X1<<20);
    8. GPIOF->MODER &= (~(0X3<<20));
    9. GPIOF->MODER |= (0X1<<20);
    10. GPIOE->MODER &= (~(0X3<<16));
    11. GPIOE->MODER |= (0X1<<16);
    12. //设置推挽输出
    13. GPIOE->OTYPER &= (~(0X1<<10));
    14. GPIOF->OTYPER &= (~(0X1<<10));
    15. GPIOE->OTYPER &= (~(0X1<<8));
    16. //设置三个管脚低速输出
    17. GPIOE->OSPEEDR &= (~(0X3<<20));
    18. GPIOF->OSPEEDR &= (~(0X3<<20));
    19. GPIOE->OSPEEDR&= (~(0X3<<16));
    20. //设置三个管脚输出时无上拉电阻和下拉电阻
    21. GPIOE->PUPDR &= (~(0X3<<20));
    22. GPIOF->PUPDR &= (~(0X3<<20));
    23. GPIOE->PUPDR &= (~(0X3<<16));
    24. }

    sp.h

    1. #ifndef __SP_H__
    2. #define __SP_H__
    3. #include "stm32mp1xx_gpio.h"
    4. #include "stm32mp1xx_rcc.h"
    5. void sp_init();
    6. #endif

    sp.c

    1. #include "sp.h"
    2. void sp_init(){
    3. //RCC使能
    4. RCC->MP_AHB4ENSETR |= (0x1<<1);
    5. //设置PE9为输出
    6. GPIOB->MODER &= (~(0X3<<12));
    7. GPIOB->MODER |= (0X1<<12);
    8. //设置推挽输出
    9. GPIOB->OTYPER &= (~(0X1<<6));
    10. //设置三个管脚低速输出
    11. GPIOB->OSPEEDR &= (~(0X3<<12));
    12. //设置三个管脚输出时无上拉电阻和下拉电阻
    13. GPIOB->PUPDR &= (~(0X3<<12));
    14. }

    fan.h

    1. #ifndef __FAN_H__
    2. #define __FAN_H__
    3. #include "stm32mp1xx_gpio.h"
    4. #include "stm32mp1xx_rcc.h"
    5. void fan_init();
    6. #endif

    fan.c

    1. #include "fan.h"
    2. void fan_init(){
    3. //RCC使能
    4. RCC->MP_AHB4ENSETR |= (0x1<<4);
    5. //设置PE9为输出
    6. GPIOE->MODER &= (~(0X3<<18));
    7. GPIOE->MODER |= (0X1<<18);
    8. //设置推挽输出
    9. GPIOE->OTYPER &= (~(0X1<<9));
    10. //设置三个管脚低速输出
    11. GPIOE->OSPEEDR &= (~(0X3<<18));
    12. //设置三个管脚输出时无上拉电阻和下拉电阻
    13. GPIOE->PUPDR &= (~(0X3<<18));
    14. }

    do_irq.c

    1. #include "key_it.h"
    2. #include "led.h"
    3. #include "sp.h"
    4. #include "fan.h"
    5. extern void printf(const char *fmt, ...);
    6. unsigned int i = 0;
    7. void do_irq(void)
    8. {
    9. int irqno;//保存中断号
    10. irqno=GICC->IAR & 0X3FF;
    11. switch(irqno)
    12. {
    13. case 99://key1
    14. //按键1中断处理
    15. if(GPIOE->ODR & (1<<10)){
    16. GPIOE->ODR &= (~(1<<10));
    17. }else{
    18. GPIOE->ODR |= (1<<10);
    19. }
    20. printf("KEY1 INT\n");
    21. //清除挂起中断标志位GICD_ICPENDRx
    22. GICD->ICPENDR[3] |= (0x1<<3);
    23. //清除中断触发标志位EXTI_FPR1
    24. EXTI->FPR1 |= (0x1<<9);
    25. break;
    26. case 97://key2
    27. //按键2中断处理
    28. if(GPIOB->ODR & (1<<6)){
    29. GPIOB->ODR &= (~(1<<6));
    30. }else{
    31. GPIOB->ODR |= (1<<6);
    32. }
    33. printf("KEY2 INT\n");
    34. //清除挂起中断标志位GICD_ICPENDRx
    35. GICD->ICPENDR[3] |= (0x1<<1);
    36. //清除中断触发标志位EXTI_FPR1
    37. EXTI->FPR1 |= (0x1<<7);
    38. break;
    39. case 98://key3
    40. //按键3中断处理
    41. if(GPIOE->ODR & (1<<9)){
    42. GPIOE->ODR &= (~(1<<9));
    43. }else{
    44. GPIOE->ODR |= (1<<9);
    45. }
    46. printf("KEY3 INT\n");
    47. //清除挂起中断标志位GICD_ICPENDRx
    48. GICD->ICPENDR[3] |= (0x1<<2);
    49. //清除中断触发标志位EXTI_FPR1
    50. EXTI->FPR1 |= (0x1<<8);
    51. break;
    52. }
    53. //清除处理完的中断号GICC_EOIR
    54. GICC->EOIR =irqno;
    55. }

    main.c

    1. #include "uart4.h"
    2. #include "key_it.h"
    3. #include "led.h"
    4. #include "sp.h"
    5. #include "fan.h"
    6. int main()
    7. {
    8. uart4_init();
    9. key_it_config();
    10. all_led_init();
    11. sp_init();
    12. fan_init();
    13. while(1){
    14. }
    15. return 0;
    16. }

    效果视频

    按钮控制LED灯、蜂鸣器、风扇实验

  • 相关阅读:
    React Native项目中《动画》实践应用——以“协议未勾选登录时文字左右抖动”为例
    在autodl搭建stable-diffusion-webui+sadTalker
    短视频矩阵系统搭建/源头----源码
    k8s限制pod的资源使用
    二分查找算法
    React小记(一)_基础部分
    计算机毕业设计选题推荐-在线考试系统-Java项目实战
    JVM性能——JVM调优参数列表
    Arial.tff字体,YOLO中下载。已解决
    17 Transformer 的解码器(Decoders)——我要生成一个又一个单词
  • 原文地址:https://blog.csdn.net/qq_53268516/article/details/133796915