新聞中心

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

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

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

首先來一張工程的結(jié)構圖:

本文引用地址:http://m.butianyuan.cn/article/201611/321297.htm

代碼解說就不用了吧,IO口這玩意也沒有什么好說的。直接上代碼:

首先是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 //采用查詢的方式
//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)在這里導致按鍵時好時壞 原因不明
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);//延時防抖
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)先級分組方式


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


上一頁 1 2 下一頁

評論


技術專區(qū)

關閉