stm32中断

exit.h
#ifndef __EXTI_H
#define __EXTI_H



#include "stm32f4xx.h"



void Exti_PA0_Init(void);



#endif

exit.c
#include "exti.h"

/*********************************
引脚说明

KEY0 连接PA0(EXTIO),选择下降沿触发


**********************************/
void Exti_PA0_Init(void)
{

    EXTI_InitTypeDef   EXTI_InitStructure;
    GPIO_InitTypeDef   GPIO_InitStructure;
    NVIC_InitTypeDef   NVIC_InitStructure;
    
    //使能SYSCFG及GPIOA时钟
    /* Enable GPIOA clock */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    /* Enable SYSCFG clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

    /* Configure PA0 pin as input floating */
    
    GPIO_InitStructure.GPIO_Pin       = GPIO_Pin_0;        //引脚0
    GPIO_InitStructure.GPIO_Mode   = GPIO_Mode_IN;        //输出模式
    GPIO_InitStructure.GPIO_PuPd   = GPIO_PuPd_UP;         //上拉
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* Connect EXTI Line0 to PA0 pin */
    //PA0--EXTI0
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);

    /* Configure EXTI Line0 */
    EXTI_InitStructure.EXTI_Line     = EXTI_Line0;            //中断线0
    EXTI_InitStructure.EXTI_Mode     = EXTI_Mode_Interrupt;     //中断模式
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;  //下降沿触发
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;                //中断线使能
    EXTI_Init(&EXTI_InitStructure);

    /* Enable and set EXTI Line0 Interrupt to the lowest priority */
    NVIC_InitStructure.NVIC_IRQChannel                      = EXTI0_IRQn;   //中断通道  中断在stm32f4xx.h中查找
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;      //抢占优先级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority          = 0x01;    //响应优先级
    NVIC_InitStructure.NVIC_IRQChannelCmd                  = ENABLE;  //通道使能
    NVIC_Init(&NVIC_InitStructure);
    

}
/*************************************************
1.STM32的中断服务函数在startup_stm32f40_41xxx.s中查找
如果在查找不到对应的函数,说明它是普通函数。
2.中断服务函数模型:void 中断服务函数名(void)  
3.中断服务函数不需要调用,满足条件CPU自动去执行


**************************************************/

void EXTI0_IRQHandler(void)
{
    //判断中断标志位是否置1
    if(EXTI_GetITStatus(EXTI_Line0) == SET)
    {
        //非法地址写值  程序卡死(跑飞)
        *((volatile unsigned int *)(0xC0000000)) = 0x66;
        
        //清空标志位
        EXTI_ClearITPendingBit(EXTI_Line0);    
    }
    
}

 

posted @ 2023-11-22 16:48  败人两字非傲即惰  阅读(7)  评论(0编辑  收藏  举报