新聞中心

        EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32 基于庫(kù)函數(shù)控制按鍵 蜂鳴器 LED顯示

        STM32 基于庫(kù)函數(shù)控制按鍵 蜂鳴器 LED顯示

        作者: 時(shí)間:2016-11-25 來(lái)源:網(wǎng)絡(luò) 收藏
        這里就涵蓋了STM32的IO控制了,按鍵是輸入,蜂鳴器與LED控制是輸出。

        首先來(lái)一張工程的結(jié)構(gòu)圖:

        本文引用地址:http://www.104case.com/article/201611/321297.htm

        代碼解說(shuō)就不用了吧,IO口這玩意也沒(méi)有什么好說(shuō)的。直接上代碼:

        首先是main.c部分

        #include"stm32f10x.h"
        #include"key.h"
        #include"beep.h"
        #include"led.h"

        KeyNumber Key_Number=KeyNone;


        int main(void)
        {
        Led_Init(); //LED初始化

        Beep_Init(); //蜂鳴器初始化

        Key_Init();

        #ifdef Key_Polling //采用查詢(xún)的方式
        //Key_Init(); //按鍵GPIO初始化
        while(1)
        {
        switch(Get_Key_Number())
        {
        case Key1:
        Led_Spark(Led1,1,LedOn);
        Beep_State(1,BeepOn);
        break;
        case Key2:
        Led_Spark(Led2,1,LedOn);
        Beep_State(1,BeepOn);
        break;
        case Key3:
        Led_Spark(Led3,1,LedOn);
        Beep_State(1,BeepOn);
        break;
        case Key4:
        Led_Spark(Led4,1,LedOn);
        Beep_State(1,BeepOn);
        break;
        default:
        LedAll_Off;
        Beep_Off;
        break;
        }
        }
        #endif

        #ifdef Key_Interrupt //采用中斷的方式
        //Key_Init();
        Key_EXTI();
        Key_NVIC();
        while(1)
        {
        #if 0 //這部分現(xiàn)在這里導(dǎo)致按鍵時(shí)好時(shí)壞 原因不明
        switch(Key_Number)
        {
        case Key1:
        Led_Spark(Led1,1,LedOn);
        Beep_State(1,BeepOn);
        Key_Number=KeyNone;
        break;
        case Key2:
        Led_Spark(Led2,1,LedOn);
        Beep_State(1,BeepOn);
        Key_Number=KeyNone;
        break;
        case Key3:
        Led_Spark(Led3,1,LedOn);
        Beep_State(1,BeepOn);
        Key_Number=KeyNone;
        break;
        case Key4:
        Led_Spark(Led4,1,LedOn);
        Beep_State(1,BeepOn);
        Key_Number=KeyNone;
        break;
        default:
        LedAll_Off;
        Beep_Off;
        Key_Number=KeyNone;
        break;
        }
        #endif
        }
        #endif
        }

        2.接下載是按鍵key.c 與key.h


        #include"stm32f10x.h"
        #include"key.h"


        #ifdef Key_Polling

        static void Delay(vu32 Time)
        {
        for(;Time!=0;Time--);
        }


        void Key_Init(void)
        {
        GPIO_InitTypeDef GPIO_InitStructure;


        RCC_APB2PeriphClockCmd(Key1_RCC,ENABLE);

        GPIO_InitStructure.GPIO_Pin=Key1_Pin;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;

        GPIO_Init(Key1_Port,&GPIO_InitStructure);


        RCC_APB2PeriphClockCmd(Key2_RCC,ENABLE);
        GPIO_InitStructure.GPIO_Pin=Key2_Pin;

        GPIO_Init(Key2_Port,&GPIO_InitStructure);


        RCC_APB2PeriphClockCmd(Key3_RCC,ENABLE);
        GPIO_InitStructure.GPIO_Pin=Key3_Pin;

        GPIO_Init(Key3_Port,&GPIO_InitStructure);


        RCC_APB2PeriphClockCmd(Key4_RCC,ENABLE);
        GPIO_InitStructure.GPIO_Pin=Key4_Pin;

        GPIO_Init(Key4_Port,&GPIO_InitStructure);
        }



        KeyNumber Get_Key_Number(void)
        {
        KeyNumber Key_Number;
        Key_Number=KeyNone;

        if(!GPIO_ReadInputDataBit(Key1_Port,Key1_Pin))
        {
        Delay(0xffff);//延時(shí)防抖
        if(!GPIO_ReadInputDataBit(Key1_Port,Key1_Pin))
        Key_Number=Key1;
        }

        if(!GPIO_ReadInputDataBit(Key2_Port,Key2_Pin))
        {
        Delay(0xffff);
        if(!GPIO_ReadInputDataBit(Key2_Port,Key2_Pin))
        Key_Number=Key2;
        }

        if(!GPIO_ReadInputDataBit(Key3_Port,Key3_Pin))
        {
        Delay(0xffff);
        if(!GPIO_ReadInputDataBit(Key3_Port,Key3_Pin))
        Key_Number=Key3;
        }

        if(!GPIO_ReadInputDataBit(Key4_Port,Key4_Pin))
        {
        Delay(0xffff);
        if(!GPIO_ReadInputDataBit(Key4_Port,Key4_Pin))
        Key_Number=Key4;
        }

        return Key_Number;
        }
        #endif

        #ifdef Key_Interrupt

        void Key_Init(void)
        {
        GPIO_InitTypeDef GPIO_InitStructure;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);


        RCC_APB2PeriphClockCmd(Key1_RCC,ENABLE);

        GPIO_InitStructure.GPIO_Pin=Key1_Pin;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;

        GPIO_Init(Key1_Port,&GPIO_InitStructure);


        RCC_APB2PeriphClockCmd(Key2_RCC,ENABLE);
        GPIO_InitStructure.GPIO_Pin=Key2_Pin;

        GPIO_Init(Key2_Port,&GPIO_InitStructure);


        RCC_APB2PeriphClockCmd(Key3_RCC,ENABLE);
        GPIO_InitStructure.GPIO_Pin=Key3_Pin;

        GPIO_Init(Key3_Port,&GPIO_InitStructure);


        RCC_APB2PeriphClockCmd(Key4_RCC,ENABLE);
        GPIO_InitStructure.GPIO_Pin=Key4_Pin;

        GPIO_Init(Key4_Port,&GPIO_InitStructure);
        }


        void Key_EXTI(void)
        {
        EXTI_InitTypeDef EXTI_InitStructure;


        GPIO_EXTILineConfig(Key1_EXTI_PortSource,Key1_EXTI_PinSource); //配置GPIO為外部中斷事件

        EXTI_InitStructure.EXTI_Line =Key1_EXTI_Line; //中斷線
        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(Key2_EXTI_PortSource,Key2_EXTI_PinSource);

        EXTI_InitStructure.EXTI_Line =Key2_EXTI_Line;

        EXTI_Init(&EXTI_InitStructure);


        GPIO_EXTILineConfig(Key3_EXTI_PortSource,Key3_EXTI_PinSource);

        EXTI_InitStructure.EXTI_Line =Key3_EXTI_Line;

        EXTI_Init(&EXTI_InitStructure);


        GPIO_EXTILineConfig(Key4_EXTI_PortSource,Key4_EXTI_PinSource);

        EXTI_InitStructure.EXTI_Line =Key4_EXTI_Line;

        EXTI_Init(&EXTI_InitStructure);
        }



        void Key_NVIC(void)
        {
        NVIC_InitTypeDef NVIC_InitStructure;

        NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0000); //NVIC起始地址在FLASH非外部SRAM

        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //優(yōu)先級(jí)分組方式


        NVIC_InitStructure.NVIC_IRQChannel =Key1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
        NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;

        NVIC_Init(&NVIC_InitStructure);


        NVIC_InitStructure.NVIC_IRQChannel =Key2_IRQn;
        //NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;

        NVIC_Init(&NVIC_InitStructure);


        NVIC_InitStructure.NVIC_IRQChannel =Key3_IRQn;
        //NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;

        NVIC_Init(&NVIC_InitStructure);


        NVIC_InitStructure.NVIC_IRQChannel =Key4_IRQn;
        //NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;

        NVIC_Init(&NVIC_InitStructure);
        }
        #endif


        #ifndef _KEY_H
        #define _KEY_H

        #include"stm32f10x.h"



        #define Key_Interrupt

        #define Key1_RCC RCC_APB2Periph_GPIOA
        #define Key1_Port GPIOA
        #define Key1_Pin GPIO_Pin_0
        #define Key1_EXTI_Line EXTI_Line0
        #define Key1_EXTI_PortSource GPIO_PortSourceGPIOA
        #define Key1_EXTI_PinSource GPIO_PinSource0
        #define Key1_IRQn EXTI0_IRQn


        #define Key2_RCC RCC_APB2Periph_GPIOC
        #define Key2_Port GPIOC
        #define Key2_Pin GPIO_Pin_13
        #define Key2_EXTI_Line EXTI_Line13
        #define Key2_EXTI_PortSource GPIO_PortSourceGPIOC
        #define Key2_EXTI_PinSource GPIO_PinSource13
        #define Key2_IRQn EXTI15_10_IRQn


        #define Key3_RCC RCC_APB2Periph_GPIOA
        #define Key3_Port GPIOA
        #define Key3_Pin GPIO_Pin_1
        #define Key3_EXTI_Line EXTI_Line1
        #define Key3_EXTI_PortSource GPIO_PortSourceGPIOA
        #define Key3_EXTI_PinSource GPIO_PinSource1
        #define Key3_IRQn EXTI1_IRQn


        #define Key4_RCC RCC_APB2Periph_GPIOC
        #define Key4_Port GPIOC
        #define Key4_Pin GPIO_Pin_3
        #define Key4_EXTI_Line EXTI_Line3
        #define Key4_EXTI_PortSource GPIO_PortSourceGPIOC
        #define Key4_EXTI_PinSource GPIO_PinSource3
        #define Key4_IRQn EXTI3_IRQn

        typedef enum{KeyNone=0,Key1=1,Key2=2,Key3=3,Key4=4}KeyNumber;

        #ifdef Key_Polling
        void Key_Init(void);
        KeyNumber Get_Key_Number(void);
        #endif
        #ifdef Key_Interrupt
        void Key_Init(void);
        void Key_EXTI(void);
        void Key_NVIC(void);
        #endif

        #endif

        3.上led.c與led.h代碼

        #include"stm32f10x.h"
        #include"led.h"



        static void Delay(vu32 Time)
        {
        for(;Time!=0;Time--);
        }


        上一頁(yè) 1 2 下一頁(yè)

        評(píng)論


        技術(shù)專(zhuān)區(qū)

        關(guān)閉
        主站蜘蛛池模板: 莎车县| 米脂县| 五河县| 府谷县| 桃江县| 长治市| 广东省| 宁晋县| 武夷山市| 镇坪县| 柳河县| 寿宁县| 永新县| 池州市| 衡东县| 绥滨县| 阿尔山市| 内丘县| 腾冲县| 张家界市| 都昌县| 南皮县| 波密县| 凤翔县| 临朐县| 化德县| 儋州市| 通榆县| 合江县| 平乡县| 来凤县| 侯马市| 义马市| 高陵县| 凤城市| 葫芦岛市| 龙山县| 自治县| 策勒县| 巢湖市| 新乡县|