You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
815 B
Markdown

2 years ago
# 无源蜂鸣器
- 初始化外部晶振配置SYSCLK与Timer Clock均为72MHz
- 配置PB9为TIM4_CH4
- 配置TIM4
- 勾选Internal Clock
- Channel4配置为PWM Generation CH4
- Prescaler配置为72-1
- Counter Period配置为500-1此时PWM频率为2kHz
- 可选因为蜂鸣器比较吵可以配置一下KEY1PB12按下KEY1时蜂鸣器才响
- 可选再配置KEY2PB13需Pull-Up按下KEY2可以发出不同的音调
- 改变频率通过配置ARR寄存器实现htim4.Instance->ARR = 500
- 注意驱动蜂鸣器的占空比应该保持在20%左右,过高的占空比将导致声音嘶哑
> 每次改变PWM频率都配置一次占空比
>
> __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_4, htim4.Instance->ARR/5);