微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > stm32快速学习3——简单按键中断

stm32快速学习3——简单按键中断

时间:11-17 来源:互联网 点击:
2个按键中断,控制2个LED

Main文件

#include"stm32f10x.h"

voidRCC_Configuration(void);

voidGPIO_Configuration(void);

voidEXTI_Configuration(void);//外部中断设置

voidNVIC_Configuration(void);//中断优先级别设置

unsignedcharled_bit1=0;

unsignedcharled_bit2=0;

intmain(void)

{

RCC_Configuration();

GPIO_Configuration();

EXTI_Configuration();

NVIC_Configuration();

GPIO_SetBits(GPIOA,GPIO_Pin_0);

GPIO_SetBits(GPIOA,GPIO_Pin_1);

while(1);

}

voidRCC_Configuration(void)

{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_AFIO ,ENABLE);

}

voidGPIO_Configuration(void)

{

GPIO_InitTypeDefGPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;

GPIO_Init(GPIOA,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_8;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(GPIOD,&GPIO_InitStructure);

}

voidEXTI_Configuration(void)

{

EXTI_InitTypeDefEXTI_InitStructure;

GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource3);

EXTI_InitStructure.EXTI_Line=EXTI_Line3;

EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;

EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;

EXTI_InitStructure.EXTI_LineCmd=ENABLE;

EXTI_Init(&EXTI_InitStructure);

GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource8);

EXTI_InitStructure.EXTI_Line=EXTI_Line8;

EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;

EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;

EXTI_InitStructure.EXTI_LineCmd=ENABLE;

EXTI_Init(&EXTI_InitStructure);

}

voidNVIC_Configuration(void)

{

/*

查看固件库手册,关于中断部分NVIC_IRQChannel

PA3、PA8分别设置

EXTI3_IRQChannel外部中断线3中断

EXTI9_5_IRQChannel外部中断线9-5中断

关于中断线,查看STM32 Reference Manual的9.2.5外部中断/事件线路映像

stm32使用了4个位来保存优先级别

查固件库

Table273.先占优先级和从优先级值

占先优先级->主

副优先级->次

号码越小,等级越高

判断主的级别->一个高一个低->ok

判断主的级别->两个都相同->判断次的级别->一个高一个低->ok

判断主的级别->两个都相同->判断次的级别->两个都相同->按照列表顺序运行

*/

NVIC_InitTypeDefNVIC_InitStructure;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//占先优先级、副优先级的资源分配

NVIC_InitStructure.NVIC_IRQChannel=EXTI3_IRQn;//指定中断源

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//占先优先级设定

NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;//副优先级设定

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;

NVIC_Init(&NVIC_InitStructure);

}

stm32f10x_it.c中加入

externunsignedcharled_bit1,led_bit2;

voidEXTI9_5_IRQHandler(void)

{

if(EXTI_GetITStatus(EXTI_Line8)!=RESET)

{

//添加中断处理程序

if(led_bit1)

{

GPIO_SetBits(GPIOA,GPIO_Pin_1);

led_bit1=0;

}

else

{

GPIO_ResetBits(GPIOA,GPIO_Pin_1);

led_bit1=1;

}

EXTI_ClearFlag(EXTI_Line8);

}

}

voidEXTI3_IRQHandler(void)

{

if(EXTI_GetITStatus(EXTI_Line3)!=RESET)

{

if(led_bit2)

{

GPIO_SetBits(GPIOA,GPIO_Pin_0);

led_bit2=0;

}

else

{

GPIO_ResetBits(GPIOA,GPIO_Pin_0);

led_bit2=1;

}

EXTI_ClearFlag(EXTI_Line3);

}

}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top