您的位置 首页 主动

stm32快速学习3——简略按键中止

2个按键中断,控制2个LEDMain文件#includestm32f10x.hvoidRCC_Configuration(void);voidGPIO_Configuration(void);void

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);

}

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ziliao/zhudong/268259.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部