×
嵌入式 > 嵌入式开发 > 详情

STM32 基于库函数控制按键 蜂鸣器 LED显示

发布时间:2020-08-24 发布时间:
|
这里就涵盖了STM32的IO控制了,按键是输入,蜂鸣器与LED控制是输出。

首先来一张工程的结构图:

代码解说就不用了吧,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 //这部分现在这里导致按键时好时坏 原因不明
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); //优先级分组方式


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



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
寻找半导体产业未来10年的驱动力