2023/10/12 -- ARM

按键中断

led.h:

#ifndef __LED_H__
#define __LED_H__

// typedef struct{
// 	unsigned int moder;
// 	unsigned int otyper;
// 	unsigned int ospeedr;
// 	unsigned int pupdr;
// 	unsigned int idr;
// 	unsigned int odr;
// }gpio_t;

// #define GPIOE (*(gpio_t *)0X50006000)
// #define GPIOF (*(gpio_t *)0X50007000)
// #define RCC (*(unsigned int *)0X50000A28)

#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"

void delay(int msc);

void led_init();

#endif

led.c:

#include "led.h"

void delay(int msc){
	int i,j;
	for(i = 0;i < msc;i++){
		for(j = 0;j < 3000;j++);
	}
}

void led_init(){
	// RCC |= (0X3<<4);

	// GPIOE.moder &= ~(0x33<<16);
	// GPIOE.moder |= (0x11<<16);
	// GPIOF.moder &= ~(0x3<<20);
	// GPIOF.moder |= (0x1<<20);

	// GPIOE.otyper &= ~(0x5<<8);
	// GPIOF.otyper &= ~(0x1<<10);

	// GPIOE.ospeedr &= ~(0x33<<16);
	// GPIOF.ospeedr &= ~(0x3<<20);

	// GPIOE.pupdr &= ~(0x33<<16);
	// GPIOF.pupdr &= ~(0x3<<20);

	//PE10
	RCC->MP_AHB4ENSETR |= (0X1<<4);
    GPIOE->MODER &= (~(0X3<<20));
    GPIOE->MODER |= (0X1<<20);
    GPIOE->OSPEEDR &= (~(0X1<<10));
    GPIOE->OSPEEDR &= (~(0X3<<20));
    GPIOE->PUPDR &= (~(0X3<<20));
}

beep.h:

#ifndef __BEEP_H__
#define __BEEP_H__

#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"

void beep_init();

#endif

beep.c:

#include "beep.h"

void beep_init(){
    //PB6
    RCC->MC_AHB4ENSETR |= (0X1<<1);
    GPIOB->MODER &= (~(0X2<<12));
    GPIOB->MODER |= (0X1<<12);
    GPIOB->OTYPER &= (~(0X1<<6));
    GPIOB->OSPEEDR &= (~(0X3<<12));
    GPIOB->PUPDR &= (~(0X3<<12));
}

fan.h:

#ifndef __FAN_H__
#define __FAN_H__

#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"

void fan_init();

#endif

fan.c:

#include "fan.h"

void fan_init(){
    //PE9
    RCC->MP_AHB4ENSETR |= (0X1<<4);
    GPIOE->MODER &= (~(0X3<<18));
    GPIOE->MODER |= (0X1<<18);
    GPIOE->OSPEEDR &= (~(0X1<<9));
    GPIOE->OSPEEDR &= (~(0X3<<18));
    GPIOE->PUPDR &= (~(0X3<<18));
}

do_irq.c:

#include "key_it.h"
#include "led.h"
#include "beep.h"
#include "fan.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
unsigned int j = 0;
unsigned int k = 0;
void do_irq(void)
{
	int irqno; // 保存中断号
	irqno = GICC->IAR & 0X3FF;
	switch (irqno)
	{
	case 99: // key1
		// 按键1中断处理
		if(0 == i){
			GPIOE->ODR |= (0X1<<10);
			i = 1;
		}else if(1 == i){
			GPIOE->ODR &= (~(0X1<<10));
			i = 0;
		}
		// 清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1 << 3);
		// 清除中断触发标志位EXTI_FPR1
		EXTI->FPR1 |= (0x1 << 9);
		break;
	case 97: // key2
		// 按键2中断处理
		//PB6
		if(0 == j){
			GPIOB->ODR |= (0X1<<6);
			j = 1;
		}else if(1 == j){
			GPIOB->ODR &= (~(0X1<<6));
			j = 0;
		}
		// 清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1 << 1);
		// 清除中断触发标志位EXTI_FPR1
		EXTI->FPR1 |= (0x1 << 7);
		break;
	case 98: // key3
		// 按键3中断处理
		//PE9
		if(0 == k){
			GPIOE->ODR |= (0X1<<9);
			k = 1;
		}else if(1 == k){
			GPIOE->ODR &= (~(0X1<<9));
			k = 0;
		}
		// 清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1 << 2);
		// 清除中断触发标志位EXTI_FPR1
		EXTI->FPR1 |= (0x1 << 8);
		break;
	}
	// 清除处理完的中断号GICC_EOIR
	GICC->EOIR = irqno;
}

main.c:

#include "key_it.h"
#include "led.h"
#include "beep.h"
#include "fan.h"

int main()
{
	key_it_config();
	key3_it_config();
	led_init();
	beep_init();
	fan_init();
	while(1){
		
	}
	return 0;
}

效果图:

按键中断

你可能感兴趣的:(arm开发)