微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 基于库函数控制按键 蜂鸣器 LED显示

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

时间:11-25 来源:互联网 点击:

GPIO_Init(Led2_Port,&GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin=Led3_Pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(Led3_Port,&GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin=Led4_Pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(Led4_Port,&GPIO_InitStructure);

LedAll_On;
Delay(0x6fffff);
LedAll_Off;
#endif
}

void Led_Spark(LedOrder Led_Order,u8 TurnOn_Time,LedState Led_State)
{
u8 i;
switch(Led_Order)
{
case LedAll: //选中所有LED
if(Led_State==LedOn) //点亮
{
for(i=0;i
{
LedAll_On;
Delay(0x5fffff);
LedAll_Off;
}
}
else if(Led_State==LedOff) //熄灭
{
LedAll_Off;
}
break;

case Led1: //选中LED1
if(Led_State==LedOn) //点亮
{
for(i=0;i
{
Led1_On;
Delay(0x5fffff);
Led1_Off;
}
}
else if(Led_State==LedOff) //熄灭
{
Led1_Off;
}
break;

case Led2: //选中LED2
if(Led_State==LedOn) //点亮
{
for(i=0;i
{
Led2_On;
Delay(0x5fffff);
Led2_Off;
}
}
else if(Led_State==LedOff) //熄灭
{
Led2_Off;
}
break;

case Led3: //选中LED3
if(Led_State==LedOn) //点亮
{
for(i=0;i
{
Led3_On;
Delay(0x5fffff);
Led3_Off;
}
}
else if(Led_State==LedOff) //熄灭
{
Led3_Off;
}
break;

case Led4: //选中LED4
if(Led_State==LedOn) //点亮
{
for(i=0;i
{
Led4_On;
Delay(0x5fffff);
Led4_Off;
}
}
else if(Led_State==LedOff) //熄灭
{
Led4_Off;
}
break;

default:
break;
}
}

#ifndef _LED_H
#define _LED_H

#include"stm32f10x.h"

#define Debug

#define Led1_RCC RCC_APB2Periph_GPIOF
#define Led1_Port GPIOF
#define Led1_Pin GPIO_Pin_7
#define Led1_On GPIO_ResetBits(Led1_Port,Led1_Pin)
#define Led1_Off GPIO_SetBits(Led1_Port,Led1_Pin)

#define Led2_RCC RCC_APB2Periph_GPIOF
#define Led2_Port GPIOF
#define Led2_Pin GPIO_Pin_8
#define Led2_On GPIO_ResetBits(Led2_Port,Led2_Pin)
#define Led2_Off GPIO_SetBits(Led2_Port,Led2_Pin)

#define Led3_RCC RCC_APB2Periph_GPIOF
#define Led3_Port GPIOF
#define Led3_Pin GPIO_Pin_9
#define Led3_On GPIO_ResetBits(Led3_Port,Led3_Pin)
#define Led3_Off GPIO_SetBits(Led3_Port,Led3_Pin)

#define Led4_RCC RCC_APB2Periph_GPIOF
#define Led4_Port GPIOF
#define Led4_Pin GPIO_Pin_10
#define Led4_On GPIO_ResetBits(Led4_Port,Led4_Pin)
#define Led4_Off GPIO_SetBits(Led4_Port,Led4_Pin)

#define LedAll_On GPIO_ResetBits(Led1_Port,Led1_Pin|Led2_Pin|Led3_Pin|Led4_Pin)
#define LedAll_Off GPIO_SetBits(Led1_Port,Led1_Pin|Led2_Pin|Led3_Pin|Led4_Pin)

typedef enum{LedAll=0,Led1=1,Led2=2,Led3=3,Led4=4}LedOrder;
typedef enum{LedOff=0,LedOn=!LedOff}LedState;

void Led_Init(void);

void Led_Spark(LedOrder Led_Order,u8 TurnOn_Time,LedState Led_State);//选中LED 点亮 闪烁次数

#endif

4.轮到蜂鸣器了beep.c与beep.h 代码

#include"stm32f10x.h"
#include"beep.h"


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

void Beep_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(Beep_RCC,ENABLE);//Beep使用的GPIO时钟

GPIO_InitStructure.GPIO_Pin=Beep_Pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(Beep_Port,&GPIO_InitStructure);

Beep_On;
Delay(0x5fffff);
Beep_Off;
}

void Beep_State(vu32 TurnOn_Time,BeepState Beep_State)
{
vu32 Time=0x5fffff;
switch(Beep_State)
{
case BeepOff:
Beep_Off;
break;

case BeepOn:
Beep_On;
Delay(TurnOn_Time*Time);
Beep_Off;
break;

default:
break;
}
}

#ifndef _BEEP_H
#define _BEEP_H

#include"stm32f10x.h"

#define Beep_RCC RCC_APB2Periph_GPIOF
#define Beep_Port GPIOF
#define Beep_Pin GPIO_Pin_6

#define Beep_On GPIO_ResetBits(Beep_Port,Beep_Pin)
#define Beep_Off GPIO_SetBits(Beep_Port,Beep_Pin)

typedef enum{BeepOff=0,BeepOn=!BeepOff}BeepState;

void Beep_Init(void);

void Beep_State(vu32 TurnOn_Time,BeepState Beep_State);//选中Beep发声还是无声

#endif

5.最后,不能忘记了中断服务函数,按键使用了两种方式查询与中断控制

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top