80 lines
3.1 KiB
C
80 lines
3.1 KiB
C
|
#include "buzzer.h"
|
||
|
|
||
|
uint32_t Frequency = 0;
|
||
|
uint32_t CLOCK = 10500000;
|
||
|
int i;
|
||
|
|
||
|
int pwm = MIN_BUZZER_PWM;
|
||
|
int psc = 0;
|
||
|
|
||
|
unsigned int notes[] = {
|
||
|
G3,C4,D4s,D4,C4,D4s,C4,D4,C4,G3s,A3s,G3,
|
||
|
G3,C4,D4s,D4,C4,D4s,C4,D4,C4,G3,F3s,F3,
|
||
|
F3,G3s,B3,D4, F3,G3s,B3,C4,
|
||
|
C4,D4s,A4s,G4s,G4,A4s,G4s,G4s,G4,G4,B3,C4,
|
||
|
C5,C5,B4,A4s, A4s,D5,C5,G4s,G4,
|
||
|
G4,A4s,G4,F4, F4,G4s,G4,F4,G4
|
||
|
};
|
||
|
unsigned int duration[] = {
|
||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5,
|
||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5,
|
||
|
1, 1, 1, 5, 1, 1, 1, 4,
|
||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5,
|
||
|
1, 1, 1, 3, 1, 2, 1, 1, 4,
|
||
|
1, 1, 1, 3, 1, 2, 1, 1, 4
|
||
|
};
|
||
|
|
||
|
void buzzer_on(uint16_t note) {
|
||
|
Frequency = CLOCK / note; // 计算定时器的重载值
|
||
|
|
||
|
htim4.Init.Prescaler = 0; // 预分频器设置为0
|
||
|
htim4.Init.Period = Frequency; // 设置定时器的重载值
|
||
|
|
||
|
// 设置PWM的占空比为50%
|
||
|
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3, Frequency / 2);
|
||
|
|
||
|
// 启动PWM
|
||
|
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
|
||
|
}
|
||
|
|
||
|
void noTone(void){
|
||
|
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3, 0);
|
||
|
HAL_Delay(30);
|
||
|
}
|
||
|
|
||
|
|
||
|
void playSong(void){
|
||
|
for (i = 0; i < sizeof(notes)/sizeof(notes[0]); i++) {
|
||
|
Frequency = CLOCK / notes[i]; // 计算定时器的重载值
|
||
|
htim4.Instance->ARR = Frequency; // 设置定时器的重载值
|
||
|
__HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3, Frequency / 2);// 设置PWM的占空比为50%
|
||
|
HAL_Delay(400*duration[i]);
|
||
|
noTone();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void set_buzzer_frequency(uint32_t freq){
|
||
|
__HAL_TIM_SetAutoreload(&htim4, freq);
|
||
|
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, freq/2);
|
||
|
}
|
||
|
uint32_t music[3][205]={{9, 10, 9, 6, 9, 0, 6, 10, 9, 10, 12, 10, 9, 10, 9, 6, 8, 0, 10, 9, 12, 10}, {5, 9, 8, 3, 0, 8, 9, 10, 9, 8, 9, 5, 9, 8, 3, 0, 8, 9, 10, 9, 8, 9, 5, 9, 8, 3, 0, 8, 9, 10, 9, 8, 9, 5, 9, 8, 3, 0, 8, 10, 12, 13, 12, 0, 8, 9, 9, 8, 9, 10, 0, 10, 12, 13, 14, 13, 12, 10, 9, 9, 8, 9, 9, 9, 8, 0, 8, 10, 12, 13, 12, 0, 8, 9, 9, 8, 10, 0, 9, 10, 12, 13, 15, 16, 17, 16, 15, 12, 13, 15, 17, 17, 17, 16, 0, 12, 13, 15, 17, 17, 17, 16, 0}};
|
||
|
uint32_t lst[3][205]={{ 1, 1, 1, 1, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 2, 2, 1, 1, 2}, {2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 6, 6, 3, 1, 2, 2, 2, 2, 4, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 1, 1, 1, 6, 6, 3, 1, 2, 2, 2, 2, 4, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 4, 1, 1, 2, 2, 2, 2, 2, 8}};
|
||
|
uint32_t tones[]={21000000, 262, 294, 330, 349, 392, 440, 494, 523, 587, 659, 698, 784, 880, 988, 1046, 1174, 1318, 1396, 1568, 1760, 1976};
|
||
|
uint32_t lenth[]={22, 103};
|
||
|
|
||
|
uint32_t frequency = 21000000;
|
||
|
uint32_t nw = 0, time_base = 125;
|
||
|
uint32_t song = 1;
|
||
|
|
||
|
void see_you_again(void)
|
||
|
{
|
||
|
set_buzzer_frequency(frequency/tones[music[song][nw]]);
|
||
|
/* USER CODE BEGIN 3 */
|
||
|
HAL_Delay(time_base*lst[song][nw]-time_base*1/4);
|
||
|
set_buzzer_frequency(frequency/tones[0]);
|
||
|
HAL_Delay(time_base*1/64);
|
||
|
nw = (nw + 1) % lenth[song];
|
||
|
|
||
|
}
|