Using a single-chip microcomputer (or single-board computer) to play music is probably one of the issues that radio enthusiasts are interested in. This article starts with the basic experiment of single-chip microcomputers, discusses the design principles of music programs, and gives specific examples for reference. 1. Basic pronunciation experiment of single chip microcomputer We know that the frequency spectrum of sound ranges from about tens to thousands of hertz. If we can use a program to control the "high" or low level of a certain line of a single machine, a rectangular wave of a certain frequency can be generated on the line, and a speaker can emit a sound of a certain frequency. If we use a delay program to control the duration of the "high" and "low" levels, we can change the output frequency and thus change the tone. For example, to generate a 200HZ audio signal, connect the speaker as shown in Figure 1 (if it is a temporary experiment, the speaker can also be directly connected to the P1 port line). The experimental procedure is: Among them, subroutine DEL is a delay subroutine. When R3 is 1, the delay time is about 20us. R3 stores the delay constant. For 200HZ audio, its period is 1/200 second, that is, 5ms. In this way, when the duration of the high level or low level of P1.4 is 2.5ms, that is, when the time constant of R3 is 2500/20=125(7DH), a 200HZ tone can be emitted. Type the above program into the learning machine, and continuously modify the constant of R3 to feel the change of the tone. In the music, each note corresponds to a certain frequency. Table 1 gives the frequencies of each note in the key of C and their corresponding time constants. Readers can input the hexadecimal code into R3 according to the constants provided in Table 1, and practice repeatedly to experience. Notes can be played according to Table 1. This is not enough. To play a piece of music accurately, the rhythm of the music, that is, the duration of a note, must be accurately controlled. We can use timer T0 to control the beat of the note. By inputting different initial values, we can generate different timing times. For example, the rhythm of a song is 94 beats per minute, that is, one beat is 0.64 seconds. The corresponding relationship between other beats and time is shown in Table 2. However, since the maximum timing time of T0 can only be 131 milliseconds, it is impossible to directly change the initial value of T0 to achieve different beats. We can use T0 to generate a 10 millisecond time base, and then set an interrupt counter to control the length of the beat time by judging the value of the interrupt counter. Table 2 also gives the time constants corresponding to various beats. For example, for a 1/4 beat note, the timing time is 0.16 seconds, and the corresponding time constant is 16 (ie 10H); for a 3-beat note, the timing time is 1.92 seconds, and the corresponding time constant is 192 (ie C0H). We take the time constant of each note and its corresponding beat constant as a group, arrange all the constants in the music into a table in order, and then use the table lookup program to take them out one by one, generate notes and control the rhythm, so that the performance effect can be achieved. In addition, the end mark and body mark can be represented by the codes 00H and FFH respectively. If the table lookup result is 00H, it means the end of the song; if the table lookup result is FFH, a corresponding pause effect is produced. In order to create a hand-plucked rhythmic feel, between certain notes (e.g. two identical notes) a note with a slightly different frequency per time unit is inserted. The program flowchart is shown in Figure 2. The following is a program sequence sheet, which can be played directly on the TD-III learning machine. For other different models of learning machines, just change the address accordingly. This program plays the folk song "Osmanthus flowers bloom everywhere in August", in the key of C, with a rhythm of 94 beats per minute. Readers can also find a song by themselves, translate the music into a code table according to the constants given in Tables 1 and 2, and input it into the machine without changing the program. This experimental method is simple. Even for people who don't understand music, it is easy to translate an unfamiliar song into code. Learning to sing a song along with the performance of the machine is endless fun. Program listing (omitted, please refer to the source program description). |