基于STM32F407ZGT实现LED闪烁 蜂鸣器间隔发声 按键控制小灯的代码

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);//判断按键是否松开
		}
	}
}

  • 14
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STM32F407ZGT6上双击按键控制蜂鸣器发声,需要进行以下步骤: 1. 配置GPIO口,使其连接到按键和蜂鸣器。 2. 配置定时器,用于产生蜂鸣器的PWM波。 3. 配置中断,检测按键的状态变化。 4. 在中断处理函数中判断按键状态,如果是双击,则控制蜂鸣器发声。 下面是一个简单的示例代码: ```c #include "stm32f4xx.h" #define BEEP_PIN GPIO_Pin_8 #define BEEP_PORT GPIOA #define KEY_PIN GPIO_Pin_0 #define KEY_PORT GPIOE void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOE, ENABLE); GPIO_InitStructure.GPIO_Pin = BEEP_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(BEEP_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(BEEP_PORT, GPIO_PinSource8, GPIO_AF_TIM1); GPIO_InitStructure.GPIO_Pin = KEY_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(KEY_PORT, &GPIO_InitStructure); } void TIM_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); TIM_TimeBaseStructure.TIM_Period = 1000 - 1; TIM_TimeBaseStructure.TIM_Prescaler = 168 - 1; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_Cmd(TIM1, ENABLE); } void EXTI_Configuration(void) { EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); EXTI_InitStructure.EXTI_Line = EXTI_Line0; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } int main(void) { GPIO_Configuration(); TIM_Configuration(); EXTI_Configuration(); while (1) { } } void EXTI0_IRQHandler(void) { static uint32_t last_time = 0; uint32_t current_time = 0; if (EXTI_GetITStatus(EXTI_Line0) != RESET) { EXTI_ClearITPendingBit(EXTI_Line0); current_time = TIM_GetCounter(TIM2); if ((current_time - last_time) < 50) { TIM_SetCompare1(TIM1, 500); } else { TIM_SetCompare1(TIM1, 0); } last_time = current_time; } } ``` 该代码使用TIM1产生1000Hz的PWM波,连接到PA8口的蜂鸣器。同时,使用EXTI0检测PE0口的按键,并在中断处理函数中判断按键状态,如果是双击,则设置PWM波的占空比为50%使蜂鸣器响起。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值