stm32单⽚机的按键中断控制流⽔灯最简程序
#include"stm32f10x_lib.h"
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStructure);
void NVIC_Configuration(void);
void EXTI_Configuration(void);
void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct);
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct);
void Delay(vu32 nCount);
int main(void)
{
#ifdef DEBUG
debug();
#endif
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8; //PB的5678接LED2345 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
NVIC_Configuration();
EXTI_Configuration();
while(1)
{
}
}
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
#endif
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel; //EXTI4通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //启动
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel; //通道EXTI5-9
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //启动
NVIC_Init(&NVIC_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure; //EXTI初始化结构定义
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource4); //PA4567作为EXTI中断线4567的中断源输⼊EXTI_InitStructure.EXTI_Line = EXTI_Line4;
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_PinSource5); EXTI_InitStructure.EXTI_Line = EXTI_Line5;
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_PinSource6); EXTI_InitStructure.EXTI_Line = EXTI_Line6;
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_PinSource7); EXTI_InitStructure.EXTI_Line = EXTI_L
ine7;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
debug灯void Delay(vu32 nCount)
{
for(;nCount!=0;nCount--);
}
中断服务程序如下:
库⾥⾯要填写的程序如下(也就是中断服务函数)
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line5) == SET)
{
EXTI_ClearITPendingBit(EXTI_Line5); //清中断
EXTI_ClearFlag( EXTI_Line5); //清中断标志
GPIO_SetBits(GPIOB,GPIO_Pin_6);
Delay(2000000); //延时
GPIO_ResetBits(GPIOB,GPIO_Pin_6);//点亮LED3
}
if(EXTI_GetITStatus(EXTI_Line6) == SET)
{
EXTI_ClearITPendingBit(EXTI_Line6);
EXTI_ClearFlag( EXTI_Line6);
GPIO_SetBits(GPIOB,GPIO_Pin_7);
Delay(2000000);
GPIO_ResetBits(GPIOB,GPIO_Pin_7);
}
if(EXTI_GetITStatus(EXTI_Line7) == SET)
{
EXTI_ClearITPendingBit(EXTI_Line7);
EXTI_ClearFlag( EXTI_Line7);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
Delay(2000000);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
}
}
void EXTI4_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line4) == SET) {
EXTI_ClearITPendingBit(EXTI_Line4); EXTI_ClearFlag( EXTI_Line4);
GPIO_SetBits(GPIOB,GPIO_Pin_8); Delay(2000000);
GPIO_ResetBits(GPIOB,GPIO_Pin_8);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。