Electromagnetic buzzer drive principle:
The buzzer sounds the principle that the current passes through the electromagnetic coil, causing the electromagnetic coil to generate a magnetic field to drive the diaphragm to sound. Therefore, a certain current is required to drive it, and the voltage output from the microcontroller I/O pin is small. The TTLK level of the MCU output cannot basically drive the buzzer, because an amplifier circuit needs to be added. Here, a triode is used as an amplifying circuit. Here is the schematic:
My J8 end is connected to the P1^5 port of the chip. When the P1^5 output is high, the triode is turned off and the buzzer does not sound. Otherwise, when the output is low, the buzzer sounds.
To drive the buzzer to sound like a singing, in fact, just make the buzzer sound different in frequency and duration. The period is equal to the reciprocal of the frequency, so the time can be known by the frequency, so it can be realized by calling the delay function or the timer. The duration of the same vocalization can also be realized by the delay function, so the buzzer is the most important thing to sing. Is to know how much time to extend!
To play music with a single-chip computer, it is only necessary to figure out two concepts, namely "tone" and "beat".
The pitch indicates the frequency at which a note sings.
The beat indicates how much time a note should sing.
There are two ways to do this:
(1) look-up table methodThe complexity of this method is that you need to find the corresponding frequency of each note (found according to the note; frequency comparison table), and then convert to the corresponding time according to the formula (take a half cycle), and then through the delay function. The final programming implementation.
The whole process is like this:
First, each tone is converted to a corresponding frequency according to the score of the Happy Birthday song.
For example: the happy birthday score on the left and the note frequency conversion table on the right.
Here to understand a little knowledge of the music, the left scores below the number is a bit of bass, no point is normal, the number is a bit high, and the bass of 5 is 4.5, the treble is 5.5, other notes are the corresponding reason.
At the top left of the score, there is “1=Fâ€, and the general score is C, which is “1=Câ€. Note that the 1234567 in the score corresponds to ABCDEFG. It is CDEFGAB! So the rules here are F,
Then it means that 2 will sing G, 3 will sing A, ... 7 will sing E, so the bass 5 here should correspond to the bass 1.5! ! ! ! It is the so-called corresponding left or right shift. If you still don't understand, look at the following:
1 The original corresponding should be C, 4 should be corresponding to F, right?
Then now the corresponding 1 becomes F, which is equivalent to 4, right?
So what is 1.5 corresponding?
4.5!
What is the corresponding 2?
5å‘—!
So good, bass 5 is 4.5, is it equal to 1.5? So the half cycle is 1803μs.
As for why it is based on the half-cycle, it is because the microcontroller is set by the loop to the port connected to the buzzer, reset to make the sound, so it is a half cycle. Because I use a passive buzzer, the active buzzer is full cycle.
Then follow the above principle, one by one, and use the delay function to achieve, because each note has a different conversion frequency, or use multiple delay functions to achieve accurate pitch frequency one by one, but this is too annoying, and the microcontroller itself Not special
Get it to sing. We shouldn't be embarrassed about them, so we will just be able to do it. So the delay function has a similar frequency in order to adapt to each tone. This is calculated by yourself, and the value of the different songs is different, so this is the problem.
Difficulties.
The next question is how long it takes to sing. The default beat of a typical song is 400ms-500ms.
In the same way, the top left of the score, it has written "3/4", which means that the quarter notes are beats, and each knot has three beats.
There is a horizontal line below each number, that is, the time of that beat is multiplied by 0.5, two are multiplied by 0.25, and three are multiplied by 0.125. . . The basic knowledge of music is not difficult for me, I am a music idiot. . So I understand that. .
Hahahahaha~
As for the conversion of the beat to the frequency, there is also a corresponding table, see below:
The same is also achieved by the delay function, of course, there will be errors.
The programming idea is quite simple, that is, the note frequency and the time to be sung are converted first and placed in two arrays. Then in the main program, through the delay to reach the corresponding frequency, sing it again, stop for a while, then sing just fine.
(2) Transcoding with tools and using a timerThis code is obviously coming from the network. Haha. Forgive me, I will not explain this. Hey, let's talk about the steps. . (I don't understand it very well. Differently, this is really versatile, so I put it on my notes.)
Initially use MUSICENCODE to convert the score into the corresponding code, and then generate a new frequency table based on the pitch and the sound zone. The so-called sound zone is the downward adjustment, the flat adjustment, and the upward adjustment. This can be known according to the corresponding code of the converted code.
Then calculate the length of the song, use while, just use it to set a song and then do it again or perform other actions.
Then take out the odd bits of the array data, which is the length of the sound. Then calculate how long a sing is based on this and assign the initial value to the timer. About the use of the timer, it will be released later.
The best way to sing a tone is to set a basic duration, because in this example, a quarter beat is about 400ms-500ms, then some 1/4 beats (100ms), and some 2/4 beats (200ms), so Set one
50ms timer, just set the parameters when calling. The benefit of doing this is convenience.
Then call the function to make it sing.
Compare the advantages and disadvantages of these two methods. The first method is too complicated to transcode, but the sound is very good and the resolution is high. The second method is universal! Different songs, you just need to use the tool to turn the code, then re-use the array to save the values, and then pass
Play (song name, key number, lifting octave, playing speed) this function call is fine, but the effect quality achieved by this method is not very good. In fact, the most important thing is that this method does not understand the music knowledge, and the first method is still
Need some knowledge to get the correct pitch frequency and length.
Fork Type Terminals,Insulated Bullet Sockets Terminals,Insulated Bullet Terminals,Type Fork Insulate Terminal
Taixing Longyi Terminals Co.,Ltd. , https://www.lycopperlugs.com