使用stm32f103c8芯片,创建基于HAL库的工程,编写程序,控制蜂鸣器工作,并且要求,当输入信号为高电平时蜂鸣器发出的声音和输入信号为低电平时蜂鸣器发出的声音在频率上有明显区别,给出具体的代码实现
时间: 2024-06-09 18:04:37 浏览: 49
stm32f103的蜂鸣器程序
4星 · 用户满意度95%
以下是使用STM32F103C8芯片,基于HAL库的代码实现,控制蜂鸣器工作,当输入信号为高电平时蜂鸣器发出的声音和输入信号为低电平时蜂鸣器发出的声音在频率上有明显区别的程序:
```c
#include "stm32f1xx_hal.h"
TIM_HandleTypeDef htim2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM2_Init();
while (1)
{
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET) // 当输入信号为高电平
{
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1); // 开启PWM输出
__HAL_TIM_SET_PRESCALER(&htim2, 72); // 设置预分频器,控制发声频率
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 500); // 设置占空比,控制发声音量
HAL_Delay(500); // 持续发声时间
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1); // 关闭PWM输出
}
else // 当输入信号为低电平
{
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1); // 开启PWM输出
__HAL_TIM_SET_PRESCALER(&htim2, 36); // 设置预分频器,控制发声频率
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 500); // 设置占空比,控制发声音量
HAL_Delay(500); // 持续发声时间
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1); // 关闭PWM输出
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_TIM2_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 999;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
```
在上述程序中,使用了定时器TIM2的PWM输出功能,控制蜂鸣器的发声。当输入信号为高电平时,设置了预分频器为72,占空比为50%,持续发声时间为500ms;当输入信号为低电平时,设置了预分频器为36,占空比为50%,持续发声时间为500ms。
阅读全文