Many current systems that use single-chip microcomputers to perform music are implemented using development boards combined with simulators. This method is not very complicated and is easy to implement, but it is not very convenient to debug and the cost is also high. This paper proposes a method of playing music with a single-chip microcomputer based on Proteus. It is very simple and practical, and this method is implemented based on software, so the cost is very low, the debugging is convenient, and the effect is also very good. It is suitable for single-chip microcomputer learners who love music. The design of the single-chip microcomputer system is divided into two parts: hardware design and software design.
1 Hardware Design
The hardware part is relatively simple. If you are doing experiments on a development board, you can follow the circuit connection shown in Figure 1 below.
The P2.5 port of the AT89C51 microcontroller controls an 8550 transistor, which controls the power on and off of the electromagnetic buzzer.
If Proteus software is used for simulation, the circuit will be simpler, as shown in Figure 2.
Figure 2 Schematic diagram of using a single-chip microcomputer to control the buzzer sound in the Proteus environment. [page]
Key components required: microcontroller and buzzer.
In order to facilitate software programming, we must first understand the basic principles of microcontroller singing.
What is sound? Sound is the vibration of air. We can hear different tones of sound with different vibration frequencies. The spectrum of sound ranges from tens to thousands of hertz.
Secondly, how to make the buzzer sound? There are many types of buzzers, but they can be roughly divided into two categories: active type (makes sound when DC is supplied, but the frequency is single); passive type (makes different sounds depending on the frequency of the input square wave). Here we choose a passive buzzer.
The basic principle of single-chip microcomputer singing: use the program to control a certain port of the single-chip microcomputer to output a square wave of a certain frequency to the buzzer, and the buzzer can emit a sound of a certain tone. If different delay programs are used to change the output frequency, the tone can be changed, and the single-chip microcomputer can then emit the music of "1", "2", "3", "4", "5", "6", and "7".
2 Software Design
This is achieved by changing the high and low level states of the port line at different frequencies through software delay or timer delay. If it is just to make the buzzer sound, this is enough. But what we want is singing, so there is still some work to be done.
2.1 Tone
Output square waves of different frequencies to achieve different tones such as 1, 2, 3, 4, etc. For example, to emit 200Hz audio, its period is 1/200s, that is, 5ms. In this way, when the duration of the high level or low level of P2.5 is 2.5ms, a 200HZ tone can be emitted. We can write a delay subroutine and use R3 to provide parameters. When R3=1, the delay is 20us. Then when R3 takes 2500/20=125 (7DH), a 200Hz tone can be emitted. By providing different R3 constants, different tone changes can be obtained.
The general sound generation program can be used to program music performance. Music is composed of tones with certain relationships of high and low, long and short, and strong and weak. In a piece of music, the pitch and length of each note are related to the frequency and beat (as shown in Figure 3).
Figure 3 The relationship between the pitch and duration of a note and its frequency
Figure 3 shows the piano keys of two scales (one scale has 8 notes) and the note name and frequency (Hz) of each key. The low scale is from low C (130.8Hz) to middle C (261.7Hz) and the high scale is from middle C to high C (523.3Hz). The black key is half a tone higher or lower than the white key next to it. The frequency and duration of each note that makes up the music are two important data required for the music program to sound. The frequency can be obtained from the figure, and the duration of the note can be determined according to the music speed and the number of beats of each note, which can be obtained from the music score.
2.2 Beat
Control the time of a note output, such as 1 beat, 1/4 beat. In 4/4 (4/4 time), a quarter note is a beat, 4 beats per measure, a whole note lasts 4 beats, a half note lasts 2 beats, a quarter note lasts 1 beat, and an eighth note lasts half a beat. A whole note takes 1s, so a half note lasts 0.5s (50×10ms), a quarter note lasts 0.25s (25×10ms), and an eighth note lasts 0.125s (12.5×10ms).
Knowing the relationship between pitch, frequency and time, we can define the frequency and duration of each note into two data tables according to the music score, and then write a program to retrieve the time value and frequency value in the table in turn.
The above is not enough. To play a piece of music accurately, you must accurately control the rhythm of the music, that is, the duration of a note. For example, if the rhythm of a piece of music is 94 beats per minute, then one beat is 60/94=0.64s.
We can use a timer to control the beat of music. Simply put, if a note of one beat is sung for 0.64s, we set a timer for 0.64s, and change to the next note when the time is up. However, since the maximum timing time of T0 of the microcontroller can only be 65ms under the 12MHz crystal oscillator, it is impossible to directly change the initial value of T0 to achieve different beats. How to time a longer time, we can use T0 to generate a 10ms time base, and then set an interrupt counter, and control the length of the beat time by judging the value of the interrupt counter.
For example, for a 1/4 beat note, the timing time is 0.16s, and the corresponding time constant (mid-segment counter) is 16 (i.e. 10H); for a 3 beat note, the timing time is 1.92s, and the corresponding time constant is 192 (i.e. C0H). [page]
2.3 Circuit Implementation
Because a piece of music has more than one note, we cannot "manually" provide time constants for the scale of each note. To simplify the implementation, we compile a table that groups the time constant of each note and its corresponding beat constant, and arranges all the constants in the music in order. In the program, we use table lookup instructions to find out the frequency and corresponding beat time of each note in turn, generate notes and control the rhythm, and then we can achieve the performance effect.
In addition, the end character and the body end character 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 sense of hand-plucked rhythm, between certain notes (for example two identical notes) a note with a slightly different frequency per time unit is inserted.
The program flowchart is shown in Figure 4. This program plays the folk song "Osmanthus Flowers Bloom Everywhere in August", in the key of C, with a rhythm of 94 beats/min.
Use Keil to compile the program, as shown in Figure 5. The specific method can be found in the literature [5]. After the compilation is correct, a .hex file is generated. Then double-click AT89C51 in the schematic diagram under the Proteus environment to find this .hex file, and then click the Play button. At this time, the buzzer controlled by the microcontroller can start playing music.
Figure 4 Program flow chart
Figure 5 Compiling the program in Keil environment
3 Conclusion
The method introduced above has been tested and works well. It has a certain enlightenment effect on beginners of single-chip microcomputers. Readers can also find a song by themselves, translate the music into a code table and input it into the single-chip microcomputer without changing the program. This experimental method is simple, even people who don't understand music can easily translate an unfamiliar song into code.
Previous article:Design and implementation of multi-DSP boot scheme for link port based on CPCI bus
Next article:AD program of STC12C5410AD single chip microcomputer two digital tube display
Recommended ReadingLatest update time:2024-11-16 21:59
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- Single-chip microcomputer C language programming and simulation
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Single chip microcomputer control technology (Li Shuping, Wang Yan, Zhu Yu, Zhang Xiaoyun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- "Dating Spring" + A cherry tree in full bloom
- 【Silicon Labs Development Kit Review】+ Key Program Test
- Welcome to join the open source project--Aurora
- Looking for a software and hardware embedded engineer who works from home, salary is optional
- Only by working hard can you win! The Infineon Gate Driver IC Jigsaw Puzzle Challenge has begun!
- What MCU chips are in short supply now?
- TMS320F28335——SCI serial port
- The sensor amplifier circuit has no output
- No phase difference? Two channels output simultaneously? This is a dual-channel power amplifier!
- mpy's LoRa driver library uPyLora