Besides driving the motor, PWM can also set different frequencies to simulate the notes output of different scales, the actual test found that the sound is quite beautiful, but the common active buzzer can not (Red Cattle Development Board from the belt), the need for specialized procurement of passive buzzer, the difference between the two is the active buzzer power on the ring, But the passive buzzer needs to enter a certain frequency signal to be audible.
We use Timer3 as the output source of PWM, we count fixed to 36, duty ratio is fixed to 1/2, through the frequency factor to set the corresponding PWM output frequency.
The underlying code is as follows, because the official no PWM driver template, the following functions are defined by me.
//采用Timer3 36M PB0输出
CPU_TIMER_Initialize(timer,36,PSC,Music_ISR,(void *)timer);
CPU_TIMER_SetCCR(timer,2,param0==0 ? 0:18);
UINT32 m_Count= param0*param1/1000;
CPU_TIMER_SetCCM(timer,2,6); //PWM1模式
CPU_TIMER_PWM_Start(timer,2);
CPU_TIMER_Start(timer);
Note frequency corresponds to the table below, according to which we can output the notes of different scales through PWM:
Notes |
Frequency/hz |
Semi-periodic/US |
Notes |
Frequency/hz |
Semi-periodic/US |
Notes |
Frequency/hz |
Semi-periodic/US |
Low |
Sound |
Area |
In |
Sound |
Area |
High |
Sound |
Area |
1 |
262 |
1908 |
1 |
523 |
0956 |
1 |
1046 |
0478 |
A |
277 |
1805 |
A |
554 |
0903 |
A |
1109 |
0451 |
2 |
294 |
1700 |
2 |
578 |
0842 |
2 |
1175 |
0426 |
2# |
311 |
1608 |
2# |
622 |
0804 |
2# |
1245 |
0402 |
3 |
330 |
1516 |
3 |
659 |
0759 |
3 |
1318 |
0372 |
4 |
349 |
1433 |
4 |
698 |
0716 |
4 |
1397 |
0358 |
4# |
370 |
1350 |
4# |
740 |
0676 |
4# |
1480 |
0338 |
5 |
392 |
1276 |
5 |
784 |
0638 |
5 |
1568 |
0319 |
5# |
415 |
1205 |
5# |
831 |
0602 |
5# |
1661 |
0292 |
6 |
440 |
1136 |
6 |
880 |
0568 |
6 |
1760 |
0284 |
6# |
466 |
1072 |
6# |
932 |
0536 |
6# |
1865 |
0268 |
7 |
494 |
1012 |
7 |
988 |
0506 |
7 |
1976 |
0253 |
"#" means a flat, used to rise or fall half a sound, multiplied by two to raise the sound of a eight-degree scale, halved to a eight degree.
Taking into account the IO driver, so add a NPN transistor as the magnification output (the actual test, the effect is not obvious, and direct drive difference is not small), the schematic diagram is as follows: