main.c
cpp
#include "led.h"
#include "uart4.h"
#include "mykey.h"
//char a;
char rx_data[20]="";
extern unsigned char led1_flag = 0;
extern unsigned char led3_flag = 0;
extern unsigned char beep_flag = 0;
void delay_ms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++);
}
}
int main()
{
uart4_config();
led_init();
key1_int_config();
key2_int_config();
key3_int_config();
while(1)
{
if(led1_flag)LED1_CTRL(1);//guan
else LED1_CTRL(0);
if(led3_flag)LED3_CTRL(1);//guan
else LED3_CTRL(0);
if(beep_flag)BEEP_CTRL(1);//guan
else BEEP_CTRL(0);
}
return 0;
}
mykey.c
cpp
#include"mykey.h"
void key1_int_config()
{
// 使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
// 设置PF9为输入
GPIOF->MODER &= (~(0X3<<18));
// 设置PF9产生EXTI9事件
EXTI->EXTICR3 &= (~(0xff<<8));
EXTI->EXTICR3 |= (0x5<<8);
// 设置下降沿检测EXTI9事件
EXTI->FTSR1 |= (0x1<<9);
// 取消EXTI9事件的屏蔽
EXTI->C1IMR1 |= (0x1<<9);
// 全局使能99号中断
GICD->ISENABLER[3] |= (0x1<<3);
// 设置99号中断的优先级
GICD->IPRIORITYR[24] &= (~(0x1f<<27));//最高优先级
// 设置99号中断的目标处理器
GICD->ITARGETSR[24] |= (0x1<<24);
// 允许组0中断被转发到GICC层
GICD->CTRL |= 0x1;
// 设置中断优先级掩码
GICC->PMR |= (0x1f<<3);
// 允许组0中断被转发给处理器
GICC->CTRL |= 0x1;
}
void key2_int_config()
{
// 使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
// 设置PF8为输入
GPIOF->MODER &= (~(0X3<<16));
// 设置PF8产生EXTI8事件
EXTI->EXTICR3 &= (~(0xff));
EXTI->EXTICR3 |= (0x5);
// 设置下降沿检测EXTI8事件
EXTI->FTSR1 |= (0x1<<8);
// 取消EXTI8事件的屏蔽
EXTI->C1IMR1 |= (0x1<<8);
// 全局使能98号中断
GICD->ISENABLER[3] |= (0x1<<2);//
// 设置98号中断的优先级
GICD->IPRIORITYR[24] &= (~(0x1f<<19));//最高优先级
// 设置98号中断的目标处理器
GICD->ITARGETSR[24] |= (0x1<<16);//
// 允许组0中断被转发到GICC层
GICD->CTRL |= 0x1;
// 设置中断优先级掩码
GICC->PMR |= (0x1f<<3);
// 允许组0中断被转发给处理器
GICC->CTRL |= 0x1;
}
void key3_int_config()
{
// 使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
// 设置PF7为输入
GPIOF->MODER &= (~(0X3<<14));
// 设置PF7产生EXTI7事件
EXTI->EXTICR3 &= (~(0xff<<24));
EXTI->EXTICR3 |= (0x5<<24);
// 设置下降沿检测EXTI7事件
EXTI->FTSR1 |= (0x1<<7);
// 取消EXTI7事件的屏蔽
EXTI->C1IMR1 |= (0x1<<7);
// 全局使能97号中断
GICD->ISENABLER[3] |= (0x1<<1);
// 设置97号中断的优先级
GICD->IPRIORITYR[24] &= (~(0x1f<<11));//最高优先级
// 设置97号中断的目标处理器
GICD->ITARGETSR[24] |= (0x1<<8);
// 允许组0中断被转发到GICC层
GICD->CTRL |= 0x1;
// 设置中断优先级掩码
GICC->PMR |= (0x1f<<3);
// 允许组0中断被转发给处理器
GICC->CTRL |= 0x1;
}
doirq.c
cpp
#include"mykey.h"
extern void printf(const char *fmt, ...);
unsigned char led1_flag;
unsigned char led3_flag;
unsigned char beep_flag;
unsigned int i = 0;
void do_irq(void)
{
//获取中断号
unsigned int irqno=(GICC->IAR&0x3ff);
switch(irqno)
{
case 99:
//中断处理逻辑
led1_flag = ~led1_flag;
printf("111");
//清除GICD的中断标志
GICD->ICPENDR[3] |= (0X1<<3);
//清除EXTI中的中断触发标志
EXTI->FPR1 |= (0x1<<9);
break;
case 98:
//中断处理逻辑
beep_flag = ~beep_flag;
printf("222");
//清除GICD的中断标志
GICD->ICPENDR[3] |= (0X1<<2);
//清除EXTI中的中断触发标志
EXTI->FPR1 |= (0x1<<8);
break;
case 97:
//中断处理逻辑
led3_flag = ~led3_flag;
printf("333");
//清除GICD的中断标志
GICD->ICPENDR[3] |= (0X1<<1);
//清除EXTI中的中断触发标志
EXTI->FPR1 |= (0x1<<7);
break;
}
//清除IAR寄存器中保存的中断号
GICC->EOIR=irqno;
}