This article introduces a solution to restore sound using PWM, which can solve this contradiction well. For example, in security alarm applications, the system usually already includes a microcontroller (used to handle human-computer interaction and system control, etc.). When an alarm is issued, it can be a "BB" or "Dangdang" buzzer; of course, a better approach is to issue a clear voice.
The basic principle of using PWM to generate sound is to use the audio sampling data stored in the Flash or the sound data generated by a certain algorithm to control the duty cycle of each PWM waveform; then, through a low-pass filter, the sound can be separated from the PWM pulse wave and drive the speaker to produce sound.
1 Extracting sound sampling data from WAV files
Generally speaking, sound data can be extracted from WAV files. Standard WAV format sound files contain sound sampling data and file headers. The file header describes some information about the following sound data, such as the number of channels, sampling frequency, sampling bits, and data length.
The number of channels refers to the number of sound sampling paths, such as mono, stereo, etc. The sampling frequency refers to the number of times the sound is sampled per second. The higher the sampling frequency, the closer the restored sound is to the original sound, as shown in Table 1. To accurately restore a waveform of a certain frequency, the minimum sampling rate should be twice that of the waveform.
The sampling bit refers to the sampling accuracy of each sampling. The higher the sampling bit, the smaller the quantization noise of the restored sound, and the closer the waveform is to the original waveform. The file header definition of WAV file:
When extracting sound data, please pay attention to the relationship between sampling frequency, sampling bit number, storage capacity and storage time, as listed in Table 2. Generally, a sampling frequency of 11.025 kHz and a sampling bit number of 8 bits can obtain clear speech and good music sound, and occupy less storage space.
By understanding and analyzing the format of WAV files, the sound sampling data in the file can be read out and converted into an array structure in C language format so that it can be compiled and downloaded to the chip together with other programs. For example:
2 Generate PWM waveforms
To restore the sound, the minimum requirement is that the HCSl2 series microcontroller has a PWM module. Another detail of chip selection is that there must be enough Flash storage capacity to store the sound sampling data. MC9S12DP256 has a 16-bit PWM module. It can generate PWM waveforms with 16-bit resolution accuracy, which means that nr can use 16-bit sampling data to generate PWM. MC9S12DP256 also has 256 KB of Flash, which can store 23 78 s of eight-bit 11.025 kHz sampling data.
Steps to generate PWM waveforms: ① Set the timer. Generate a timer interrupt: If the sampling rate is 11.025 kHz, set the timer interrupt frequency to 11.025 kHz. ② Initialize the PWM module and generate a 11.025kHz PWM waveform. ③ Wait for the timer interrupt, get the sampling data in the interrupt handler, and set the PWM duty cycle register to determine whether the sound is played. If completed, turn off the timer interrupt and stop the PWM output.
3 Low-pass filtering
After the PWM output, it must be filtered through a low-pass filter to restore the sound that can be recognized by the human ear. The type and parameters of the low-pass filter depend on the sampling frequency of the sound and the price budget. The simplest one is the RC filter. The advantage of this filter is that it only requires two components. The other is an active filter, which has a good filtering effect but is relatively complex.
The selection of the filter cutoff frequency is very important for audio output. It is recommended to set it at half the sampling frequency. To obtain a better filter cutoff frequency, it is necessary to further analyze the sampling data to find the maximum frequency with valid sound data. Of course, in general applications, there is no such strict requirement. Even in some occasions, even if the components with unclear parameters are used, good sound output can be obtained. At this time, the RC filter will be the best choice.
It is recommended to use the filter shown in Figure 1, with a cutoff frequency of 5.5 kHz (11.025 kHz/2). In this design, two operational amplifiers are used, which are enough to drive a small speaker.
Figure 2 also shows a simple and direct connection method: using only a 22μF capacitor for low-pass filtering to drive a 5 kHz, 8Ω small speaker.
Conclusion
The PWM function of the MC9S12DP256 microcontroller can be used to add voice functions to the application of the microcontroller. The storage method for sound sampling data is direct and original sampling data. In specific practical applications, in order to store sound data for a longer time, the sound data can also be compressed. For specific algorithms, please refer to relevant materials.
Previous article:Design and simulation of electronic password lock based on STC89C52 single chip microcomputer based on C language
Next article:Design of a Micro Thermal Printer Based on Freescale Microcontroller
Recommended ReadingLatest update time:2024-11-16 23:50
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
- Protection processing circuit in IoT system and equivalent circuit of HF RFID reader
- EVAL-M3-TS6-665PN development board circuit structure and features
- Ask about the SYN480R circuit
- TI C6748 chip PRU part assembly software package sprc940.zip
- The new issue of "Analog Dialogue" is online, free download!
- After adding shutdown and low power consumption, stm32 will run away after working for 8-9 hours
- [Silicon Labs Development Kit Review] +6-axis inertial sensor ICM-20648
- Welcome our new moderator, Azuma Simeng~~
- Thanks to all the staff of Electronic Engineering Forum
- TI DSP--Memory space allocation and designation