设置按键中断,按键1按下,LED亮,再按一次,灭按键2按下,蜂鸣器响。再按一次,不响按键3按下,风扇转,再按一次,风扇停

src/key_it.c

cs 复制代码
#include"key_it.h"
//GPIO初始化
void all_led_init()
{
    //RCC使能
    RCC->MP_AHB4ENSETR  |= (0X1<<4);
    //设置PE10 PF10 PE8为输出
    GPIOE->MODER &= (~(0X3<<20));
    GPIOE->MODER |= (0X1<<20);
    //设置推挽输出
    GPIOE->OTYPER &= (~(0X1<<10)); 
    //设置三个管脚低速输出
    GPIOE->OSPEEDR &= (~(0X3<<20));
    //设置三个管脚输出时无上拉电阻和下拉电阻    
    GPIOE->PUPDR &= (~(0X3<<20));
}
void all_spi_init()
{
    //RCC使能
    RCC->MP_AHB4ENSETR  |= (0X3<<1);
    //设置PE10 PF10 PE8为输出
    GPIOB->MODER &= (~(0X3<<12));
    GPIOB->MODER |= (0X1<<12);
    //设置推挽输出
    GPIOB->OTYPER &= (~(0X1<<6)); 
    //设置三个管脚低速输出
    GPIOB->OSPEEDR &= (~(0X3<<12));
    //设置三个管脚输出时无上拉电阻和下拉电阻    
    GPIOB->PUPDR &= (~(0X3<<12));
}
void all_fan_init()
{
    //RCC使能
    RCC->MP_AHB4ENSETR  |= (0X3<<4);
    //设置PE10 PF10 PE8为输出
    GPIOE->MODER &= (~(0X3<<18));
    GPIOE->MODER |= (0X1<<18);
    //设置推挽输出
    GPIOE->OTYPER &= (~(0X1<<9)); 
    //设置三个管脚低速输出
    GPIOE->OSPEEDR &= (~(0X3<<18));
    //设置三个管脚输出时无上拉电阻和下拉电阻    
    GPIOE->PUPDR &= (~(0X3<<18));
}
//按键2的配置
void key2_it_config()
{
    //RCC使能GPIOF时钟
    RCC->MP_AHB4ENSETR |= (0x1<<5);
    //pf7
    GPIOF->MODER &= (~(0x3<<14));
    //pf7
    EXTI->EXTICR2 &=(~(0xFF<<24));
    EXTI->EXTICR2 |= (0x5<<24);
    //pf7
    EXTI->FTSR1 |= (0x1<<7);
    //PF7
    EXTI->C1IMR1 |= (0x1<<7);
    //PF7 97号中断
    GICD->ISENABLER[3] |= (0X1<<1); 
    //PF7
    GICD->IPRIORITYR[24] &= (~(0X1F<<11)); 
    //pf7 97中断
    GICD->ITARGETSR[24]  &= (~(0X3<<8));
    GICD->ITARGETSR[24] |= (0X1<<8);
}
//按键1 按键3以及GICC层的配置
void key_it_config()
{
    //RCC使能GPIOF时钟
    RCC->MP_AHB4ENSETR |= (0x1<<5);
    //设置PF9 PF7 PF8GPIO输入
    //PF9
    GPIOF->MODER &= (~(0x3<<18));
    //PF8
    GPIOF->MODER &= (~(0x3<<16));
    //设置PF9 PF7 PF8产生EXTI事件 EXTI_EXTICRx
    //pf8
    EXTI->EXTICR3 &=(~(0xFF<<0));
    EXTI->EXTICR3 |= (0x5<<0);
    //pf9
    EXTI->EXTICR3 &=(~(0xFF<<8));
    EXTI->EXTICR3 |= (0x5<<8);
    //设置事件触发方式为下降沿触发EXTI_FTSR1
    //pf8
    EXTI->FTSR1 |= (0x1<<8);
    //pf9
    EXTI->FTSR1 |= (0x1<<9);
    //设置EXTI事件触发不屏蔽EXTI_IMR1
    //PF8
    EXTI->C1IMR1 |= (0x1<<8);
    //PF9
    EXTI->C1IMR1 |= (0x1<<9);
    //使能中断能转发到特定的CPU接口层GICD_ISENABLERx
    //PF8 98号中断
    GICD->ISENABLER[3] |= (0X1<<2); 
    //PF9 99号中断
    GICD->ISENABLER[3] |= (0X1<<3); 
    //GICD_ISENABLERx设置中断优先级
    //PF8
    GICD->IPRIORITYR[24] &= (~(0X1F<<19)); 
    //PF9
    GICD->IPRIORITYR[24] &= (~(0X1F<<27)); 
    //设置当前中断被转发到哪一个CPU处理GICD_ITARGETSRx
    //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;

    
}
     

src/do_irq.c

cs 复制代码
    #include "key_it.h"
    extern void printf(const char *fmt, ...);
    unsigned int i = 0;
    void do_irq(void) 
    {
        static int flag=0;
        static int flag1=0;
        static int flag2=0;
        int irqno;//保存中断号
            irqno=GICC->IAR & 0X3FF;
            switch(irqno)
            {
                case 99://key1
                    //按键1中断处理
                    //printf("KEY1 INT\n");
                        if(0==flag)
                        {
                            //LED1亮
                           GPIOE->ODR |= (0X1<<10);
                            flag=1;
                        }
                        else if(1==flag)
                        {
                            //LED1灭
                             GPIOE->ODR &= (~(0X1<<10));
                            flag=0;
                        }
            //清除挂起中断标志位GICD_ICPENDRx
                GICD->ICPENDR[3] |= (0x1<<3);
            //清除中断触发标志位EXTI_FPR1
                EXTI->FPR1 |= (0x1<<9);
                break;
            case 97://key2
                //按键2中断处理
                //printf("KEY2 INT\n");
                        if(0==flag1)
                        {
                            //蜂鸣器响
                           GPIOB->ODR |= (0X1<<6);
                            flag1=1;
                        }
                        else if(1==flag1)
                        {
                            //蜂鸣器不响
                             GPIOB->ODR &= (~(0X1<<6));
                            flag1=0;
                        }
        //清除挂起中断标志位GICD_ICPENDRx
        GICD->ICPENDR[3] |= (0x1<<1);
        //清除中断触发标志位EXTI_FPR1
        EXTI->FPR1 |= (0x1<<7);
                break;
            case 98://key3
                //按键3中断处理
               // printf("KEY3 INT\n");
                        if(0==flag2)
                        {
                            //风扇转
                           GPIOE->ODR |= (0X1<<9);
                            flag2=1;
                        }
                        else if(1==flag2)
                        {
                            //风扇停
                             GPIOE->ODR &= (~(0X1<<9));
                            flag2=0;
                        }
        //清除挂起中断标志位GICD_ICPENDRx
        GICD->ICPENDR[3] |= (0x1<<2);
        //清除中断触发标志位EXTI_FPR1
        EXTI->FPR1 |= (0x1<<8);
                break;
        }
        //清除处理完的中断号GICC_EOIR
        GICC->EOIR =irqno;
    }

include/key_it.h

cs 复制代码
#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();
void key2_it_config();
void all_led_init();
void all_spi_init();
void all_fan_init();

#endif

main.c

cs 复制代码
#include "uart.h"

#include "key_it.h"



int main()

{


    uart4_init();//串口初始化

    all_led_init();

    //中断初始化

    key_it_config();

	key2_it_config();

    all_spi_init();

    all_fan_init();

    while(1)

    {

        //保证主程序不结束

    }  

	return 0;


}
相关推荐
scan19 小时前
单片机串口接收状态机STM32
stm32·单片机·串口·51·串口接收
Qingniu019 小时前
【青牛科技】应用方案 | RTC实时时钟芯片D8563和D1302
科技·单片机·嵌入式硬件·实时音视频·安防·工控·储能
深圳市青牛科技实业有限公司11 小时前
【青牛科技】应用方案|D2587A高压大电流DC-DC
人工智能·科技·单片机·嵌入式硬件·机器人·安防监控
Mr.谢尔比12 小时前
电赛入门之软件stm32keil+cubemx
stm32·单片机·嵌入式硬件·mcu·信息与通信·信号处理
LightningJie12 小时前
STM32中ARR(自动重装寄存器)为什么要减1
stm32·单片机·嵌入式硬件
西瓜籽@13 小时前
STM32——毕设基于单片机的多功能节能窗控制系统
stm32·单片机·课程设计
远翔调光芯片^1382879887215 小时前
远翔升压恒流芯片FP7209X与FP7209M什么区别?做以下应用市场摄影补光灯、便携灯、智能家居(调光)市场、太阳能、车灯、洗墙灯、舞台灯必看!
科技·单片机·智能家居·能源
极客小张16 小时前
基于STM32的智能充电桩:集成RTOS、MQTT与SQLite的先进管理系统设计思路
stm32·单片机·嵌入式硬件·mqtt·sqlite·毕业设计·智能充电桩
m0_7393128719 小时前
【STM32】项目实战——OV7725/OV2604摄像头颜色识别检测(开源)
stm32·单片机·嵌入式硬件
嵌入式小章19 小时前
基于STM32的实时时钟(RTC)教学
stm32·嵌入式硬件·实时音视频