stm32f407按键控制led闪烁频率和蜂鸣器声音大小
时间: 2024-01-02 10:03:42 浏览: 129
以下是基于STM32F407的按键控制LED闪烁频率和蜂鸣器声音大小的示例代码:
```c
#include "stm32f4xx.h"
#define LED_PIN GPIO_Pin_13
#define LED_PORT GPIOD
#define BUTTON_PIN GPIO_Pin_0
#define BUTTON_PORT GPIOA
#define BUZZER_PIN GPIO_Pin_8
#define BUZZER_PORT GPIOC
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t prescalerValue = 0;
uint32_t buttonState = 0;
uint32_t lastButtonState = 0;
uint32_t buttonPressed = 0;
uint32_t buttonReleased = 0;
uint32_t ledState = 0;
uint32_t buzzerState = 0;
uint32_t buzzerPeriod = 0;
uint32_t buzzerDutyCycle = 0;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED_PIN;
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_Speed_100MHz;
GPIO_Init(LED_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = BUTTON_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(BUTTON_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = BUZZER_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(BUZZER_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(BUZZER_PORT, GPIO_PinSource8, GPIO_AF_TIM3);
prescalerValue = (uint16_t)((SystemCoreClock / 2) / 1000000) - 1;
TIM_TimeBaseStructure.TIM_Period = 1000 - 1;
TIM_TimeBaseStructure.TIM_Prescaler = prescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &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(TIM2, &TIM_OCInitStructure);
TIM_Cmd(TIM2, ENABLE);
while (1)
{
buttonState = GPIO_ReadInputDataBit(BUTTON_PORT, BUTTON_PIN);
if (buttonState != lastButtonState)
{
if (buttonState == 0)
{
buttonPressed = 1;
}
else
{
buttonReleased = 1;
}
}
lastButtonState = buttonState;
if (buttonPressed)
{
ledState = !ledState;
GPIO_WriteBit(LED_PORT, LED_PIN, ledState);
buttonPressed = 0;
}
if (buttonReleased)
{
buzzerState = !buzzerState;
if (buzzerState)
{
buzzerPeriod = 1000;
buzzerDutyCycle = 500;
}
else
{
buzzerPeriod = 0;
buzzerDutyCycle = 0;
}
TIM_TimeBaseStructure.TIM_Period = buzzerPeriod - 1;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_Pulse = buzzerDutyCycle - 1;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3, ENABLE);
buttonReleased = 0;
}
}
}
```
该代码使用TIM2定时器控制LED的闪烁频率,并使用TIM3定时器控制蜂鸣器的声音大小。按下按键时,LED状态会切换,并通过蜂鸣器发出嘀嗒声。松开按键时,蜂鸣器会停止发声。
请注意,这只是一个示例代码,您需要根据您的具体应用程序进行修改。
阅读全文