stm32快速学习3——简单按键中断
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);
}
}
stm32按键中 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)