关键代码extern uint8_t keyValue;,我来为大家科普一下关于stm32两个按键控制流水灯方向?以下内容希望对你有帮助!

stm32两个按键控制流水灯方向(学习STM32编程按键中断键控流水灯)

stm32两个按键控制流水灯方向

关键代码

extern uint8_t keyValue;

void EXTIx_Init(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

EXTI_InitTypeDef EXTI_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);

SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE,EXTI_PinSource2);

SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE,EXTI_PinSource3);

SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE,EXTI_PinSource4);

SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0);

EXTI_InitStructure.EXTI_Line=EXTI_Line0;

EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;

EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;

EXTI_InitStructure.EXTI_LineCmd=ENABLE;

EXTI_Init(&EXTI_InitStructure);

EXTI_InitStructure.EXTI_Line=EXTI_Line2|EXTI_Line3|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);

NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00;

NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x01;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel=EXTI2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00;

NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x02;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel=EXTI3_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00;

NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel=EXTI4_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00;

NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x04;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

void EXTI2_IRQHandler(void)

{

delay_ms(10);

if(KEY_R==0)

{

keyValue=KEY_R_PRESS;

}

EXTI_ClearITPendingBit(EXTI_Line2);

}

void EXTI3_IRQHandler(void)

{

delay_ms(10);

if(KEY_D==0)

{

keyValue=KEY_D_PRESS;

}

EXTI_ClearITPendingBit(EXTI_Line3);

}

void EXTI4_IRQHandler(void)

{

delay_ms(10);

if(KEY_L==0)

{

keyValue=KEY_L_PRESS;

}

EXTI_ClearITPendingBit(EXTI_Line4);

}

主函数:

uint8_t keyValue=0;

LED_WorkModeTypeDef myLEDWorkMode=LED_MODE1;

int main(void)

{

delay_init(168);

LED_Init();

Key_Init();

EXTIx_Init();

while(1)

{

switch(keyValue)

{

case KEY_L_PRESS:

myLEDWorkMode=LED_MODE1;

break;

case KEY_D_PRESS:

myLEDWorkMode=LED_MODE2;

break;

case KEY_R_PRESS:

myLEDWorkMode=LED_MODE3;

break;

default:

break;

}

if(myLEDWorkMode==LED_MODE1)

{

LED_Mode1(1000);

}

else if(myLEDWorkMode==LED_MODE2)

{

LED_Mode2();

}

else if(myLEDWorkMode==LED_MODE3)

{

LED_Mode3();

}

}

}

,