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


    key.h

    1. #ifndef __KEY__H__
    2. #define __KEY__H__
    3. #include"stm32mp1xx_gpio.h"
    4. #include"stm32mp1xx_rcc.h"
    5. #include"stm32mp1xx_gic.h"
    6. #include"stm32mp1xx_exti.h"
    7. void key_config();
    8. void all_led_init();
    9. void fan_init();
    10. void sp_init();
    11. #endif

    key.c

     

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

    do_irq.c

    1. #include"key.h"
    2. extern void printf(const char *fmt, ...);
    3. unsigned int i = 0;
    4. void do_irq(void)
    5. {
    6. //获取终端号
    7. int GICC_num=GICC->IAR&0x3ff;
    8. //判断终端号
    9. switch (GICC_num)
    10. {
    11. case 99: //key1
    12. printf("key1\n");
    13. GPIOE->ODR ^= (0X1<<10);
    14. GPIOF->ODR ^= (0X1<<10);
    15. GPIOE->ODR ^= (0X1<<8);
    16. //清除EXTI
    17. EXTI->FPR1|=(0x1<<9); //key1
    18. //清除终端挂起标志位,清除终端号
    19. GICD->ICPENDR[3]|=(0x1<<3); //key1
    20. break;
    21. case 97: //key2
    22. printf("key2\n");
    23. GPIOB->ODR ^= (0X1<<6);
    24. //清除EXTI
    25. EXTI->FPR1|=(0x1<<7); //key2
    26. //清除终端挂起标志位,清除终端号
    27. GICD->ICPENDR[1]|=(0x1<<1); //key2
    28. break;
    29. case 98:
    30. printf("key3\n");
    31. GPIOE->ODR ^= (0X1<<9);
    32. //清除EXTI
    33. EXTI->FPR1|=(0x1<<8); //key3
    34. //清除终端挂起标志位,清除终端号
    35. GICD->ICPENDR[2]|=(0x1<<2); //key3
    36. break;
    37. }
    38. //清除GICC
    39. GICC->EOIR=GICC_num;
    40. }

    main.c

     

    1. #include "uart.h"
    2. #include "key.h"
    3. int main()
    4. {
    5. key_config();
    6. all_led_init();
    7. fan_init();
    8. sp_init();
    9. while(1)
    10. {
    11. }
    12. return 0;
    13. }

  • 相关阅读:
    git实战
    怎样从零开始训练一个AI车手?
    Spark系列之Spark的数据倾斜
    5. hdfs的界面详解
    深度学习(PyTorch)——DataLoader的使用方法
    弘辽科技:抖音,正在抛弃张同学
    【计算机毕业设计】基于SpringBoot+Vue的小区物业管理系统的设计与实现
    数字化转型的同群效应数据集(2007-2021年)
    设计模式-14责任链模式(责任链设计模式)详解
    集合框架的线程安全问题ArrayList、HashSet、HashMap
  • 原文地址:https://blog.csdn.net/wdc857/article/details/133798375