ARM cortex-A7核中断实验 串口显示KEY按键

注意:

        GICC 清除中断挂起标志位(清除中断号),是将值放到 GICC->EOIR 寄存器中即可。
        GICC->EOIR |= num;          

        //  GICC->EOIR &= (~(0x3ff));    清除可写位无效            
              

头文件:

#ifndef __KEY_H__
#define __KEY_H__

#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"

//KEY1-->PF9
//初始化RCC和GPIO章节
void hal_gpio_key1(void);

//初始化EXTI章节
void hal_key1_exit_it(void);

//初始化GICD章节
void hal_key1_gicd_it(void);

//初始化GICC章节
void hal_key1_gicc_it(void);
#endif

源文件:

  1 #include "key.h"
  2 
  3 //KEY1-->PF9
  4 //初始化RCC和GPIO章节
  5 void hal_gpio_key1(void)
  6 {
  7     RCC->MP_AHB4ENSETR |= (0x1<<5); // 使能GPIOF组控制器
  8     GPIOF->MODER &= (~(0x3<<18)); // key1输入模式
  9     GPIOF->MODER &= (~(0x3<<14)); // key2
 10     GPIOF->MODER &= (~(0x3<<16)); // key3
 11 }
 12 
 13 //初始化EXTI章节
 14 void hal_key1_exit_it(void)
 15 {
 16     // 外部中断选择寄存器
 17     EXTI->EXTICR3 &= (~(0xff<<8));  // key1
 18     EXTI->EXTICR3 |= (0x05<<8);
 19     EXTI->EXTICR2 &= (~(0xff<<24)); // key2
 20     EXTI->EXTICR2 |= (0x05<<24);
 21     EXTI->EXTICR3 &= (~0xff);       // key3
 22     EXTI->EXTICR3 |= 0x05;
 23 
 24     // 下降沿选择触发寄存器
 25     EXTI->FTSR1 |= (0x1<<9);
 26     EXTI->FTSR1 |= (0x1<<7);
 27     EXTI->FTSR1 |= (0x1<<8);
 28                                                             
 29     // 中断屏蔽寄存器
 30     EXTI->C1IMR1 |= (0x1<<9);
 31     EXTI->C1IMR1 |= (0x1<<7);
 32     EXTI->C1IMR1 |= (0x1<<8);
 33 
 34     // 中断挂起寄存器
 35 //  EXTI->FPR1 |= (0x1<<9);
 36 //  EXTI->FPR1 |= (0x1<<7);
 37 //  EXTI->FPR1 |= (0x1<<8);
 38 }
 39 
 40 //初始化GICD章节
 41 void hal_key1_gicd_it(void)
 42 {
 43     //GICD控制寄存器
 44     GICD->CTRL |= 0x1;
 45 
 46     //中断设置使能寄存器
 47     GICD->ISENABLER[3] |= (0x1<<3);
 48     GICD->ISENABLER[3] |= (0x1<<1);
 49     GICD->ISENABLER[3] |= (0x1<<2);
 50 
 51     //中断优先级寄存器
 52     GICD->IPRIORITYR[24] |= (0x3<<27);
 53     GICD->IPRIORITYR[24] |= (0x3<<11);
 54     GICD->IPRIORITYR[24] |= (0x3<<19);
 55 
 56     //中断目标分配寄存器
 57     GICD->ITARGETSR[24] |= (0b1<<24);
 58     GICD->ITARGETSR[24] |= (0b1<<8);
 59     GICD->ITARGETSR[24] |= (0b1<<16);
 60 
 61     //清除中断挂起标志位寄存器
 62     GICD->ICPENDR[3] |= (0x1<<3);
 63     GICD->ICPENDR[3] |= (0x1<<1);
 64     GICD->ICPENDR[3] |= (0x1<<2);
 65 }
 66 
 67 //初始化GICC章节
 68 void hal_key1_gicc_it(void)
 69 {
 70     //GICC控制寄存器
 71     GICC->CTRL |= 0x1;
 72 
 73     //GICC层设置中断优先级
 74     GICC->PMR &= (~(0x1f<<3));
 75     GICC->PMR |= (0x4<<3); // 要求大于GICD的优先级
 76 
 77 
 78 }
~                                                               

主函数:

 1 #include "key.h"
 2 
 3 extern void printf(const char *fmt, ...);
 4 void delay_ms(int ms)
 5 {
 6     int i,j;
 7     for(i = 0; i < ms;i++)
 8         for (j = 0; j < 1800; j++);
 9 }
10 
11 int main()
12 {
13     hal_gpio_key1();
14 
15     hal_key1_exit_it();
16 
17     hal_key1_gicd_it();
18 
19     hal_key1_gicc_it();
20 
21     while(1)
22     {
23 
24     }                                               
25     return 0;
26 }
                                                       
                                                       

中断函数:

 1 #include "key.h"                             
 2                                              
 3 extern void printf(const char *fmt, ...);    
 4 unsigned int i = 0;                          
 5 void do_irq(void)                            
 6 {                                            
 7     //获取中断号                             
 8     unsigned int num = 0;                    
 9     num = (GICC->IAR & 0x3ff);               
10                                              
11     //判断中断号                             
12     if(99 == num)                            
13     {                                        
14         printf("别按 KEY1 了...\n");         
15         EXTI->FPR1 |= (0x1<<9);              
16         GICD->ICPENDR[3] |= (0x1<<3);        
17     }                                        
18                                              
19     //清除中断挂起标志位,清除中断号         
20 //  GICC->EOIR &= (~(0x3ff));                
21     GICC->EOIR |= num;                       
22 }                                            
                                                

实验现象:

ARM cortex-A7核中断实验 串口显示KEY按键_第1张图片

 

你可能感兴趣的:(单片机,c语言,嵌入式硬件)