tft每日頭條

 > 科技

 > stm32兩個按鍵控制流水燈方向

stm32兩個按鍵控制流水燈方向

科技 更新时间:2025-01-07 23:25:41

stm32兩個按鍵控制流水燈方向?關鍵代碼extern uint8_t keyValue;,我來為大家科普一下關于stm32兩個按鍵控制流水燈方向?以下内容希望對你有幫助!

stm32兩個按鍵控制流水燈方向(學習STM32編程按鍵中斷鍵控流水燈)1

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

}

}

}

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关科技资讯推荐

热门科技资讯推荐

网友关注

Copyright 2023-2025 - www.tftnews.com All Rights Reserved