按键控制灯,蜂鸣器,风扇

mai.c

#include "uart.h"
#include "key_it.h"
int main()
{
	led_init();
	key_it_config();
	key3_it_config();
	while(1)
	{
		
	}
	return 0;
}

src/key_it.c

#include"key_it.h"
void key3_it_config()
{
	RCC->MP_AHB4ENSETR |= (0x1<<5);		
	GPIOF->MODER &= (~(0x3<<16));
	EXTI->EXTICR2 &= (~(0xFF));
	EXTI->EXTICR3 |= (0x5);
	EXTI->FTSR1 |= (0X1<<8);
	EXTI->C1IMR1 |= (0X1<<8);
	GICD->ISENABLER[3] |= (0x1<<2);
	GICD->IPRIORITYR[24] &= (~(0x1E<<19));
	GICD->ITARGETSR[24] &= (~(0x3<<16));
	GICD->ITARGETSR[24] |= (0x1<<16);
	
}
void key_it_config()
{
	//RCC使能GPIO时钟
	RCC->MP_AHB4ENSETR |= (0x1<<5);		
	//设置PF9 PF7 PF8 GPION输入
	//PF9
	GPIOF->MODER &= (~(0x3<<18));
	//PF7
	GPIOF->MODER &= (~(0x3<<14));	
	//PF8
	
	//设置PF9 PF7 PF8产生EXTI事件 EXTI_EXTICRx
	//PF7
	EXTI->EXTICR2 &= (~(0xFF<<24));
	EXTI->EXTICR2 |= (0x5<<24);
	//PF9
	EXTI->EXTICR3 &= (~(0xFF)<<8);
	EXTI->EXTICR3 |= (0x5<<8);
	
	//设置事件触发方式为下降沿触发 EXTI_FTSR1
	//PF7
	EXTI->FTSR1 |= (0X1<<7);
	//PF8
	
	//PF9
	EXTI->FTSR1 |= (0X1<<9);
	
	//设置EXTI事件触发不屏蔽 EXTI_IMR1
	//PF7
	EXTI->C1IMR1 |= (0X1<<7);
	//PF8
	
	//PF9
	EXTI->C1IMR1 |= (0X1<<9);
	
	//使能中断能转发到特定的CPU接口层GICD_ISEABLERx
	//PF7
	GICD->ISENABLER[3] |= (0x1<<1);
	//PF8
	//PF9
	GICD->ISENABLER[3] |= (0x1<<3);
	//GICD_ISEABLERx设置中断优先级
	//PF7
	GICD->IPRIORITYR[24] &= (~(0x1F<<11));
	//PF8
	//PF9
	GICD->IPRIORITYR[24] &= (~(0x1F<<27));
	//设置当前中断被转发到哪一个CPU处理GICD_ITARGETSRx
	//PF7 97中断
	GICD->ITARGETSR[24] &= (~(0x3<<8));
	GICD->ITARGETSR[24] |= (0x1<<8);
	//PF8 98中断
	//PF9 99中断
	GICD->ITARGETSR[24] &= (~(0x3<<24));
	GICD->ITARGETSR[24] |= (0x1<<24);
	//使能组0转发中断GICD_CTLR
	GICD->CTRL |= 0x1;
	//设置中断优先级掩码GICC_CTLR
	GICC->PMR |= (0x1F<<3);						
	//使能CPU可以处理组0转发的中断GICC_CTLR
	GICC->CTRL |= 0x1;
}

void led_init()
{
	//RCC使能
	(*(unsigned int *)0x50000A28) |= (0x1<<4);

	//设置模式为输出
	//PE1o
	PE.moder &= (~(0x3<<20));
	PE.moder  |= (0x1<<20);
	//PE9风扇为输出
	
	PE.moder &= (~(0x3<<18));
	PE.moder |= (0x1<<18);
	//pB6
	PB.moder &= (~(0x3<<12));
	PB.moder |= (0x1<<12);

	//推挽输出
	//PE10
	PE.moder &= (~(0x1<<10));
	//PE9风扇
	PE.moder &= (~(0x1<<9));
	//PB6
	PB.moder &= (~(0x1<<6));

	//低速输出
	//PE10
	PE.moder &= (~(0x1<<10));
	//PE9风扇
	PE.moder &= (~(0x1<<9));
	//PB6
	PB.moder &= (~(0x1<<6));
	
	//无上拉电阻和下拉电阻
	//PE10
	PE.moder &= (~(0x1<<10));
	//PE9风扇
	PE.moder &= (~(0x1<<9));
	//PB6
	PB.moder &= (~(0x1<<6));
	
}

include/key_it.h

#ifndef __KEY_IT_H__
#define __KEY_IT_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
#define PE (*(gpio_a *)0x50006000)
#define PB (*(gpio_a *)0x50003000)
void key_it_config();
void key3_it_config();
typedef struct{
	unsigned int moder;
	unsigned int otyper;
	unsigned int ospeedr;
	unsigned int puper;
	unsigned int idr;
	unsigned int odr;
}gpio_a;
void led_init();
#endif

src/do_irq.c

#include "key_it.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void) 
{
	//保存中断号
	int irqno;
	irqno=GICC->IAR & 0x3FF;
	printf("%d\n",irqno);
	switch(irqno)
	{
		case 99:
			//key1
			//按键1中断处理
			PE.odr ^=(0x1<<10);
			printf("KEY1 INT\n");
			//清除挂起中断标志位GICD_ICPENDRx
			GICD->ICPENDR[3] |= (0x1<<3);
			//清除中断触发标志位EXTI_FPR1
			EXTI->FPR1 |= (0x1<<9);
			//清除处理完的中断号GICC_EOIR
			GICC->EOIR = irqno;
		
			break;
		case 97:
			//key2
			//按键2中断处理
			PB.odr ^=(0x1<<6);
			printf("KEY2 INT\n");
			//清除挂起中断标志位GICD_ICPENDRx
			GICD->ICPENDR[3] |= (0x1<<1);
			//清除中断触发标志位EXTI_FPR1
			EXTI->FPR1 |= (0x1<<7);
			//清除处理完的中断号GICC_EOIR
			GICC->EOIR = irqno;

			break;
		case 98:
			//key3
			//按键1中断处理
			PE.odr ^=(0x1<<9);
			printf("KEY3 INT\n");
			//清除挂起中断标志位GICD_ICPENDRx
			GICD->ICPENDR[3] |= (0x1<<2);
			//清除中断触发标志位EXTI_FPR1
			EXTI->FPR1 |= (0x1<<8);
			//清除处理完的中断号GICC_EOIR
			GICC->EOIR = irqno;
			break;
	}
}

你可能感兴趣的:(服务器,前端,运维)