LED闪烁 蜂鸣器间隔发声
eld.h
#ifndef __LED_H
#define __LED_H
void LED_Init(void);
#endif
led.c
#include "led.h"
#include "stm32f4xx.h"
//代码步骤
/*
1.看原理图确认要操作的引脚以及相关逻辑
2.GPIO初始化
1.申明结构体 GPIO_InitTypeDef
2.时钟使能 RCC_AHBlPeriphClockCmd
3.配置结构体
4.调用初始化函数进行初始化
3.写逻辑代码
*/
/**
* @brief LED初始化函数
* @note 初始化两个LED灯,引脚分别为PF9和PF10
* @param void
* @retval None
*/
void LED_Init(void)//初始化
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//时钟使能,通电还是断电
//开始配置F端口下的9号跟10号引脚。
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_10; //PF9和PF10引脚
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_OUT; //输出模式
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL; //不进行上下拉
GPIO_InitStructure.GPIO_Speed=GPIO_High_Speed; //引脚响应速度
GPIO_Init(GPIOF, &GPIO_InitStructure);
}
beep.h
#ifndef __BEEP_H
#define __BEEP_H
void BEEP_Init(void);
#endif
beep.c
#include "beep.h"
#include "stm32f4xx.h"
/*typedef struct
{
uint32_t GPIO_Pin;
GPIOMode_TypeDef GPIO_Mode;
GPIOSpeed_TypeDef GPIO_Speed;
GPIOOType_TypeDef GPIO_OType;
GPIOPuPd_TypeDef GPIO_PuPd;
}GPIO_InitTypeDef;*/
void BUZZER_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//时钟使能,通电还是断电
//开始配置F端口下的8号引脚。
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8; //PF8引脚
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_OUT; //输出模式
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL; //不进行上下拉
GPIO_InitStructure.GPIO_Speed=GPIO_High_Speed; //引脚响应速度
GPIO_Init(GPIOF, &GPIO_InitStructure);
}
main.c
#include "stm32f4xx.h"
#include "led.h"
#include "beep.h"
#include "key.h"
/**
* @brief Delay延时函
* @note 通过对传入的参数递减,实现CPU延时的功能
* @param nCount:需要递减的数
* @retval None
*/
void Delay2()//延时函数
{
uint32_t i;
for(i = 0; i < 10000000; i++);
}
int main(void)
{
LED_Init();
//初始化
while(1)
{
BEEP_Init();
//小灯闪烁
GPIO_SetBits(GPIOF,GPIO_Pin_9); //set 置位1 灯灭 写高电平
GPIO_SetBits(GPIOF,GPIO_Pin_10);
Delay2();
GPIO_ResetBits(GPIOF,GPIO_Pin_9); //reset 复位0 灯亮 写低电平
GPIO_ResetBits(GPIOF,GPIO_Pin_10);
Delay2();
//间隔发声
GPIO_SetBits(GPIOF,GPIO_Pin_8);//GPIOF_ODR |= 1<<8;
Delay2();
GPIO_ResetBits(GPIOF,GPIO_Pin_8);//GPIOF_ODR &= ~(1<<8);
Delay2();
}
}
按键控制小灯亮灭
**key.h**
#ifndef __KEY_H
#define __KEY_H
void KEY_Init(void);
#endif
**key.c**
#include "stm32f4xx.h"
#include "key.h"
void KEY_Init()
{
//初始化
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOA,ENABLE);
//PA0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽模式
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//不需要上拉电阻,所以是nopull
GPIO_Init(GPIOA,&GPIO_InitStructure);//PE2 PE3 PE4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽模式
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//不需要上拉电阻,所以是nopull
GPIO_Init(GPIOE,&GPIO_InitStructure);
}
**main.c**
#include "stm32f4xx.h"
#include "led.h"
#include "beep.h"
#include "key.h"
#define KEY0 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)
#define KEY1 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)
#define KEY2 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)
int main()
{
while(1)
{
BEEP_Init();
KEY_Init();
//KEY控制DS0
if(KEY0 == 0) //按键KEY0按下
{
GPIO_WriteBit(GPIOF,GPIO_Pin_9,Bit_RESET);//DS0亮
}
else
{
GPIO_WriteBit(GPIOF,GPIO_Pin_9,Bit_SET);//DS0灭
}
if(KEY1 == 0) //按键KEY1按下
{
GPIO_WriteBit(GPIOF,GPIO_Pin_10,Bit_RESET);//DS1亮
}
else
{
GPIO_WriteBit(GPIOF,GPIO_Pin_10,Bit_SET);//DS1灭
}
if((KEY0+KEY1+KEY2) <= 1)//两个或两个以上按键按下时 <= 1
{
GPIO_WriteBit(GPIOF,GPIO_Pin_8,Bit_SET);//BEEP响
}
else
{
GPIO_WriteBit(GPIOF,GPIO_Pin_8,Bit_RESET);//BEEP不响
}
}
}
利用延时消除抖动
void main()
{
while(1)
{
if(KEY == 0)//判断KEY的值是否等于0
{
delay(5);//延时5ms
if(KEY == 0)
{
LED = ~LED;//LED状态取反
}
while(!KEY);//判断按键是否松开
}
}
}