stm32快速學(xué)習(xí)3——簡單按鍵中斷
Main文件
本文引用地址:http://m.butianyuan.cn/article/201611/315442.htm#include"stm32f10x.h"
voidRCC_Configuration(void);
voidGPIO_Configuration(void);
voidEXTI_Configuration(void);//外部中斷設(shè)置
voidNVIC_Configuration(void);//中斷優(yōu)先級別設(shè)置
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)
{
/*
查看固件庫手冊,關(guān)于中斷部分NVIC_IRQChannel
PA3、PA8分別設(shè)置
EXTI3_IRQChannel外部中斷線3中斷
EXTI9_5_IRQChannel外部中斷線9-5中斷
關(guān)于中斷線,查看STM32 Reference Manual的9.2.5外部中斷/事件線路映像
stm32使用了4個位來保存優(yōu)先級別
查固件庫
Table273.先占優(yōu)先級和從優(yōu)先級值
占先優(yōu)先級->主
副優(yōu)先級->次
號碼越小,等級越高
判斷主的級別->一個高一個低->ok
判斷主的級別->兩個都相同->判斷次的級別->一個高一個低->ok
判斷主的級別->兩個都相同->判斷次的級別->兩個都相同->按照列表順序運行
*/
NVIC_InitTypeDefNVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//占先優(yōu)先級、副優(yōu)先級的資源分配
NVIC_InitStructure.NVIC_IRQChannel=EXTI3_IRQn;//指定中斷源
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//占先優(yōu)先級設(shè)定
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;//副優(yōu)先級設(shè)定
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);
}
}
評論