按钮控制LED灯、蜂鸣器、风扇实验

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"

void key_it_config();
#endif

key_it.c

#include "key_it.h"

void key_it_config(){
    //RCC使能GPIOF时钟
    RCC->MP_AHB4ENSETR  |= (0x1<<5);
    //设置PF9 PF7 PF8设置为输入模式
    GPIOF->MODER &= (~(0x3F<<14));
    //设置PF9 PF7 PF8产生EXTI事件
    EXTI->EXTICR3 &= (~(0xFF<<8));
    EXTI->EXTICR3 |= (0x5<<8);
    EXTI->EXTICR3 &= (~(0xFF));
    EXTI->EXTICR3 |= (0x5);
    EXTI->EXTICR2 &= (~(0xFF<<24));
    EXTI->EXTICR2 |= (0x5<<24);
    //设置事件触发方式为下降沿触发EXTI_FTSR1
    EXTI->FTSR1 |= (0x7<<7);
    //设置EXTI事件触发不屏蔽EXTI_IMR1
    EXTI->C1IMR1 |= (0x7<<7);
    //使能中断能转发到特定的CPU接口层GICD_ISENABLERx
    GICD->ISENABLER[3] |= (0x7<<1);
    //GICD_ISENABLERx设置中断优先级
    //PF7
    GICD->IPRIORITYR[24] &= (~(0X1F<<11)); 
    //PF8
    GICD->IPRIORITYR[24] &= (~(0X1F<<19)); 
    //PF9
    GICD->IPRIORITYR[24] &= (~(0X1F<<27)); 
    //设置当前中断被转发到哪一个CPU处理GICD_ITARGETSRx
    //pf7 97中断
    GICD->ITARGETSR[24]  &= (~(0X3<<8));
    GICD->ITARGETSR[24] |= (0X1<<8);
    //pf8 98中断
    GICD->ITARGETSR[24]  &= (~(0X3<<16));
    GICD->ITARGETSR[24] |= (0X1<<16);
    //pf9 99中断
    GICD->ITARGETSR[24]  &= (~(0X3<<24));
    GICD->ITARGETSR[24] |= (0X1<<24);
    //使能组0转发中断GICD_CTLR
    GICD->CTRL |= 0X1;
    //设置中断优先级掩码GICC_PMR
    GICC->PMR |= (0x1F<<3);

    //使能CPU可以处理组0转发的中断GICC_CTLR
    GICC->CTRL |= 0X1;
}

led.h

#ifndef __LED_H__
#define __LED_H__

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

void all_led_init();
#endif

led.c

#include "led.h"

void all_led_init(){
    //RCC使能
    RCC->MP_AHB4ENSETR  |= (0x3<<4);
    //设置PE10 PF10 PE8为输出
    GPIOE->MODER &= (~(0X3<<20));
    GPIOE->MODER |= (0X1<<20);
    GPIOF->MODER &= (~(0X3<<20));
    GPIOF->MODER |= (0X1<<20);
    GPIOE->MODER &= (~(0X3<<16));
    GPIOE->MODER |= (0X1<<16);
    //设置推挽输出
    GPIOE->OTYPER &= (~(0X1<<10));
    GPIOF->OTYPER &= (~(0X1<<10));
    GPIOE->OTYPER &= (~(0X1<<8));
    //设置三个管脚低速输出
    GPIOE->OSPEEDR &= (~(0X3<<20));
    GPIOF->OSPEEDR &= (~(0X3<<20));
    GPIOE->OSPEEDR&= (~(0X3<<16));
    //设置三个管脚输出时无上拉电阻和下拉电阻    
    GPIOE->PUPDR &= (~(0X3<<20));
    GPIOF->PUPDR &= (~(0X3<<20));
    GPIOE->PUPDR &= (~(0X3<<16));
}

sp.h

#ifndef __SP_H__
#define __SP_H__

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

void sp_init();
#endif

sp.c

#include "sp.h"

void sp_init(){
    //RCC使能
    RCC->MP_AHB4ENSETR  |= (0x1<<1);
    //设置PE9为输出
    GPIOB->MODER &= (~(0X3<<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(){
    //RCC使能
    RCC->MP_AHB4ENSETR  |= (0x1<<4);
    //设置PE9为输出
    GPIOE->MODER &= (~(0X3<<18));
    GPIOE->MODER |= (0X1<<18);
    //设置推挽输出
    GPIOE->OTYPER &= (~(0X1<<9));
    //设置三个管脚低速输出
    GPIOE->OSPEEDR &= (~(0X3<<18));
    //设置三个管脚输出时无上拉电阻和下拉电阻    
    GPIOE->PUPDR &= (~(0X3<<18));
}

do_irq.c

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

main.c

#include "uart4.h"
#include "key_it.h"
#include "led.h"
#include "sp.h"
#include "fan.h"

int main()
{
	uart4_init();
	key_it_config();
	all_led_init();
	sp_init();
	fan_init();
	while(1){
	}
	return 0;

}



效果视频

按钮控制LED灯、蜂鸣器、风扇实验

你可能感兴趣的:(arm开发,嵌入式硬件)