您的位置 首页 设计

STM32 根据库函数操控按键 蜂鸣器 LED显现

这里就涵盖了STM32的IO控制了,按键是输入,蜂鸣器与LED控制是输出。首先来一张工程的结构图:代码解说就不用了吧,IO口这玩意也没

这儿就涵盖了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_NV%&&&&&%(void);
#endif

#endif

3.上led.c与led.h代码

#include”stm32f10x.h”
#include”led.h”

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

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ziliao/sheji/259140.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部