STM32F1外部中断实例

使用外部中断的基本步骤如下:
1. 设置好相应的时钟;
2. 设置相应的中断;
3. IO口初始化;
4. 把相应的IO口设置为中断线路(要在设置外部中断之前)并初始化;
5. 在选择的中断通道的响应函数中中断函数。

1、开启时钟

void RCC_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE)|
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
}

设置相应的时钟所需要的RCC函数在stm32f10x_rcc.c中,所以要在工程中添加此文件。

2、设置相应中断

void NVIC_Config()
{
    /*设置相应中断的优先级*/
    NVIC_InitTypeDefNVIC_InitStructure;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 

    /*EXTI2*//*配置NVIC_InitTypeDef结构体参数*/
    NVIC_InitStructure.NVIC_IRQChannel= EXTI2_IRQn;     
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 0; 
    NVIC_InitStructure.NVIC_IRQChannelSubPriority= 0;        
    NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;                                   
    NVIC_Init(&NVIC_InitStructure);

    /*EXTI3*/
    NVIC_InitStructure.NVIC_IRQChannel=EXTI3_IRQn;                 
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 1;                    
    NVIC_InitStructure.NVIC_IRQChannelSubPriority= 1;  
    NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;                                   
    NVIC_Init(&NVIC_InitStructure);

    /*EXTI5*/
    NVIC_InitStructure.NVIC_IRQChannel= EXTI9_5_IRQN;  
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 2;    
    NVIC_InitStructure.NVIC_IRQChannelSubPriority= 2;      
    NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;      
    NVIC_Init(&NVIC_InitStructure);                                      

}

3、IO口初始化

void GPIO_Config(void)
{
    GPIO_InitTypeDef   GPIO_InitStructure;

    /*PE2--OUT_PP*/
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;                                             
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;           
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;                  
    GPIO_Init(GPIOE,&GPIO_InitStructure);
    GPIO_ResetBits(GPIOE,GPIO_Pin_2);                              

    /*PC2.PE3.PE5--IN_FLOATING*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_5; 
    GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IN_FLOATING; 
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;          
    GPIO_Init(GPIOC,&GPIO_InitStructure);                                 

    /*PD3.PD6--OUT_PP*/  
    GPIO_InitStructure.GPIO_Pin= GPIO_Pin_3 |GPIO_Pin_6;                  
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;           
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;                  
    GPIO_Init(GPIOD,&GPIO_InitStructure);

    /*PB5--OUT_PP*/
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;                                         
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;          
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;                  
    GPIO_Init(GPIOB,&GPIO_InitStructure);        
}

4、 把相应的IO口设置为中断线路

void EXTI_cfg()
{
    EXTI_InitType   DefEXTI_InitStructure;

    /*清空中断标志*/   
    EXTI_ClearITPendingBit(EXTI_Line2);
    EXTI_ClearITPendingBit(EXTI_Line3);
    EXTI_ClearITPendingBit(EXTI_Line5);

    /*设置中断线*/    
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource2);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource3);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource5);

    EXTI_InitStructure.EXTI_Line= EXTI_Line2 | EXTI_Line3 | EXTI_Line5; 
    EXTI_InitStructure.EXTI_Mode= EXTI_Mode_Interrupt;    
    EXTI_InitStructure.EXTI_Trigger= EXTI_Trigger_Rising_Falling;   
    EXTI_InitStructure.EXTI_LineCmd=ENABLE;         
    EXTI_Init(&EXTI_InitStructure);                                      
}

5、中断服务程序

voidEXTI2_IRQHandler(void)
{
    GPIO_SetBits(GPIOD,GPIO_Pin_6);//点亮LED灯
    EXTI_ClearITPendingBit(EXTI_Line2); //清空中断标志位
}

voidEXTI3_IRQHandler(void)
{
    GPIO_SetBits(GPIOD,GPIO_Pin_3);
    EXTI_ClearITPendingBit(EXTI_Line3);
}

voidEXTI9_5_IRQHandler(void)
{
    GPIO_SetBits(GPIOB,GPIO_Pin_5);
    EXTI_ClearITPendingBit(EXTI_Line5);
}

中断服务程序要写在stm32f10x_it.c中。
这部分内容为对上一篇的补充。不足之处欢迎指教。

你可能感兴趣的:(stm32,实例,函数,f1,STM32F1单片机)