• 按键中断实验


    key.h

    #include "key.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

               if(GPIOE->ODR & (0x1<<10) != 0)

                   GPIOE->ODR &= (~(0x1<<10));

                else

                    GPIOE->ODR |=(0x1<<10);

                GICD->ICPENDR[3] |= (0x1<<3);

                EXTI->FPR1 |= (0x1<<9);

                break;

            case 97://key2

                GPIOB->ODR |= (0x1<<6);

                GICD->ICPENDR[3] |= (0x1<<1);

                EXTI->FPR1 |= (0x1<<7);

                break;

            case 98://key3

                GPIOE->ODR |= (0x1<<9);

                GICD->ICPENDR[3] |= (0x1<<2);

                EXTI->FPR1 |= (0x1<<8);

        }

        //清除处理完的中断号GICC_EOIR

        GICC->EOIR =irqno;

    }

    key.h

    #ifndef __KEY_H__

    #define __KEY_H__

    #include "stm32mp1xx_rcc.h"

    #include "stm32mp1xx_gpio.h"

    #include "stm32mp1xx_exti.h"

    #include "stm32mp1xx_gic.h"

    void mykey();

    void key3_init();

    void key12_init();

    #endif

    key.c

    #include "key.h"

    void mykey()

    {

        RCC->MP_AHB4ENSETR |= (0X3<<4);

        RCC->MP_AHB4ENSETR |= (0x1<<1);

        //led1灯初始

        GPIOE->MODER &= (~(0X3<<20));

        GPIOE->MODER |= (0x1<<20);

        GPIOE->OTYPER &= (~(0X1<<10));

        GPIOE->OSPEEDR &= (~(0X3<<20));

        GPIOE->PUPDR &= (~(0X3<<20));

        GPIOE->ODR |=(0x1<<10);

        //蜂鸣器

        GPIOB->MODER &= (~(0x1<<12));

        GPIOB->MODER |= (0x1<<12);

        GPIOB->OTYPER &= (~(0x1<<6));

        GPIOB->OSPEEDR |= (~(0x3<<12));

        GPIOB->PUPDR &= (~(0x3<<12));

        GPIOB->ODR |= (~(0x1<<6));

        //风扇

        GPIOE->MODER &= (~(0x1<<18));

        GPIOE->MODER |= (0x1<<18);

        GPIOE->OTYPER &= (~(0x1<<9));

        GPIOE->OSPEEDR |= (~(0x3<<18));

        GPIOE->PUPDR &= (~(0x3<<18));

        GPIOE->ODR &= (~(0x1<<9));

    }

    void key3_init()

    {

        //RCC使能GPIOF时钟

        RCC->MP_AHB4ENSETR |= (0x1<<5);

        GPIOF->MODER &= (~(0x3<<16));

        EXTI->EXTICR3 &=(~(0xFF<<0));

        EXTI->EXTICR3 |= (0x5<<0);

        EXTI->FTSR1 |= (0x1<<8);

        EXTI->C1IMR1 |= (0x1<<8);

        GICD->ISENABLER[3] |= (0X1<<2);

        GICD->IPRIORITYR[24] &= (~(0X1F<<19));

        GICD->ITARGETSR[24]  &= (~(0X3<<16));

        GICD->ITARGETSR[24] |= (0X1<<16);

    }

    void key12_init()

    {

        RCC->MP_AHB4ENSETR |= (0x1<<5);

        GPIOF->MODER &= (~(0x3<<18));

        GPIOF->MODER &= (~(0x3<<14));

        EXTI->EXTICR2 &=(~(0xFF<<24));

        EXTI->EXTICR2 |= (0x5<<24);

        EXTI->EXTICR3 &=(~(0xFF<<8));

        EXTI->EXTICR3 |= (0x5<<8);

        EXTI->FTSR1 |= (0x1<<7);

        EXTI->FTSR1 |= (0x1<<9);

        EXTI->C1IMR1 |= (0x1<<7);

        EXTI->C1IMR1 |= (0x1<<9);

        GICD->ISENABLER[3] |= (0X1<<1);

        GICD->ISENABLER[3] |= (0X1<<3);

        GICD->IPRIORITYR[24] &= (~(0X1F<<11));

        GICD->IPRIORITYR[24] &= (~(0X1F<<27));

        GICD->ITARGETSR[24]  &= (~(0X3<<8));

        GICD->ITARGETSR[24] |= (0X1<<8);

        GICD->ITARGETSR[24]  &= (~(0X3<<24));

        GICD->ITARGETSR[24] |= (0X1<<24);

        GICD->CTRL |= 0X1;

        GICC->PMR |= (0x1F<<3);

        GICC->CTRL |= 0X1;    

    }

    main.c

    #include "key.h"

    int main()

    {

        mykey();

        key12_init();

        key3_init();

        for(;;);

        return 0;

    }

  • 相关阅读:
    【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
    Docker私有仓库打开2375端口(linux)
    大数据安全 | (一)介绍
    【postgresql】角色(Role)
    xshell终端下tmux显示异常,显示多个状态栏
    【Java毕设】基于idea Java的在线考试系统(附源码+课件)
    risc-v 栈分析
    c++ 信奥编程 1129:统计数字字符个数
    Go:关于 Channel
    Python:获取域名ssl证书信息和到期时间!
  • 原文地址:https://blog.csdn.net/zero2all/article/details/133802487