2024/5/22 ARMday7

按键控制LED灯亮和灭

do_irq.c

#include "key_it.h"
//#include "led.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void) 
{
    //获取中断号
    unsigned int irqno=(GICC->IAR & (0x3FF));
    switch (irqno)
    {
    case 99:
        //处理中断
        GPIOE->ODR ^= (0x1<<8);
        //清除中断队列
        GICD->ICPENDR[3] |= (0x1<<3);
        //清除中断挂起标志
        EXTI->FPR1 |= (0x1<<9);
        break;
    case 98:
        //处理中断
        GPIOE->ODR ^= (0x1<<10);
        //清除中断队列
        GICD->ICPENDR[3] |= (0x1<<2);
        //清除中断挂起标志
        EXTI->FPR1 |= (0x1<<8);
        break;
    case 97:
        //处理中断
        GPIOF->ODR ^= (0x1<<10);
        //清除中断队列
        GICD->ICPENDR[3] |= (0x1<<1);
        //清除中断挂起标志
        EXTI->FPR1 |= (0x1<<7);
        break;
    default:
        break;
    }
    //清除中断号
    GICC->EOIR = irqno;
}


key_it.c

#include "key_it.h"
 
//按键1初始化
void key1_init(){
    //使能GPIOF的RCC时钟
    RCC->MC_AHB4ENSETR |= (0x1<<5);
 
    //配置PF9为输入模式
    GPIOF->MODER &= (~(0x3<<18));
 
    //配置中断选择器EXTI9为PF
    EXTI->EXTICR3 &= (~(0xFF<<8));
    EXTI->EXTICR3 |= (0x05<<8);
 
    //配置为下降沿触发
    EXTI->FTSR1 |= (0x1<<9);
 
    //使能中断,关闭中断屏蔽
    EXTI->C1IMR1 |= (0x1<<9);
 
    //设置中断使能
    GICD->ISENABLER[3] |= (0x1<<3);
 
    //设置中断优先级
    GICD->IPRIORITYR[24] &= (~(0x1F<<27));
 
    //设置中断处理cpu
    GICD->ITARGETSR[24] |= (0x1<<24);
 
    //使能中断,中断默认在组0
    GICD->CTRL |=(0x1);
 
    //设置使能
    GICC->CTRL |= (0x1);
 
    //设置优先级掩码
    GICC->PMR |= (0x1f<<3);
 
}
 
//按键2初始化
void key2_init(){
   //配置PF7为输入模式
    GPIOF->MODER &= (~(0x3<<14));
 
    //配置中断选择器EXTI7为PF
    EXTI->EXTICR2 &= (~(0xFF<<24));
    EXTI->EXTICR2 |= (0x05<<24);
 
    //配置为下降沿触发
    EXTI->FTSR1 |= (0x1<<7);
 
    //使能中断,关闭中断屏蔽
    EXTI->C1IMR1 |= (0x1<<7);
 
    //设置中断使能
    GICD->ISENABLER[3] |= (0x1<<1);
 
    //设置中断优先级
    GICD->IPRIORITYR[24] &= (~(0x1F<<11));
 
    //设置中断处理cpu
    GICD->ITARGETSR[24] |= (0x1<<8);
    
    //使能中断,中断默认在组0
    GICD->CTRL |=(0x1);
 
    //设置使能
    GICC->CTRL |= (0x1);
 
    //设置优先级掩码
    GICC->PMR |= (0x1f<<3);
}
 
//按键3初始化
void key3_init(){
 //配置PF8为输入模式
    GPIOF->MODER &= (~(0x3<<16));
 
    //配置中断选择器EXTI8为PF
    EXTI->EXTICR3 &= (~0xFF);
    EXTI->EXTICR3 |= 0x05;
 
    //配置为下降沿触发
    EXTI->FTSR1 |= (0x1<<8);
 
    //使能中断,关闭中断屏蔽
    EXTI->C1IMR1 |= (0x1<<8);
 
    //设置中断使能
    GICD->ISENABLER[3] |= (0x1<<2);
 
    //设置中断优先级
    GICD->IPRIORITYR[24] &= (~(0x1F<<19));
 
    //设置中断处理cpu
    GICD->ITARGETSR[24] |= (0x1<<16);
    
    //使能中断,中断默认在组0
    GICD->CTRL |=(0x1);
 
    //设置使能
    GICC->CTRL |= (0x1);
 
    //设置优先级掩码
    GICC->PMR |= (0x1f<<3);
}

led.h

#ifndef __LED_H__
#define __LED_H__
 
/*//gpio寄存器
typedef struct{
    volatile unsigned int MODER;
    volatile unsigned int OTYPER;
    volatile unsigned int OSPEEDR;
    volatile unsigned int PUPDR;
    volatile unsigned int IDR;
    volatile unsigned int ODR;
    volatile unsigned int BSRR;
}gpio_t;*/
 
//GPIOE和GPIOF寄存器宏定义
//#define GPIOE (*(gpio_t *)0x50006000)
//#define GPIOF (*(gpio_t *)0x50007000)
 
//led灯1初始化
void led1_init();
//led灯2初始化
void led2_init();
//led灯3初始化
void led3_init();
 
//led灯1控制
void led1_ctl(int flag);
//led灯2控制
void led2_ctl(int flag);
//led灯3控制
void led3_ctl(int flag);
 
#endif

main.c

#include "gpio.h"
#include "led.h"
#include "uart4.h"
#include "key_it.h"

void delay(){
	for(int i=0;i<500;i++){
		for(int j=0;j<2000;j++){
 
		}
	}
}
extern void printf(const char *fmt, ...);
int main()
{
	//使能PE、PF时钟
	*((unsigned int *)0x50000A28) |= (0x3<<4);	
	led1_init();
	led2_init();
	led3_init();
	key1_init();
	key2_init();
	key3_init();
	while (1)
	{
		printf("do_main\n");
		delay(1000);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值