龙空技术网

学习STM32编程——按键中断键控流水灯

华26104121 152

前言:

现在我们对“c语言花样流水灯”可能比较关心,小伙伴们都需要知道一些“c语言花样流水灯”的相关资讯。那么小编同时在网络上搜集了一些有关“c语言花样流水灯””的相关资讯,希望我们能喜欢,同学们快快来了解一下吧!

关键代码

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

}

}

}

标签: #c语言花样流水灯