Using PWM to add voice capabilities to microcontroller applications

Publisher:幸福之星Latest update time:2011-07-09 Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The HCSl2 series microcontroller does not include a D/A conversion module. To expand its voice function, the usual practice is to add a D/A conversion chip. Although this can achieve very good sound quality (depending on the performance of the D/A conversion chip), the use of an additional D/A conversion chip will add extra cost to the design in applications that do not require high sound quality, and also make the circuit design relatively complex.
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.

Keywords:MCU Reference address:Using PWM to add voice capabilities to microcontroller applications

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

AVR MCU Learning Activities - Learn WinAVR
I won't go into details about GCC (Note: GCC and WINAVR in this article are the same concept). I believe that friends who are learning AVR microcontrollers have heard of this compiler software. There are many AVR development software. The following is a comparison of several compiler software that I searched on the Int
[Microcontroller]
AVR MCU Learning Activities - Learn WinAVR
Two-way learning fan remote control switch with natural wind MCU source program + circuit
The circuit diagram is as follows:   Production instructions: This circuit uses a 15W104 microcontroller to control the fan via an infrared remote controller. When using it for the first time, first press the keys to learn the codes. How to learn the code: Find an idle infrared remote control, select two eas
[Microcontroller]
Two-way learning fan remote control switch with natural wind MCU source program + circuit
MCU pin flip test
The IO port rate of STM32 can reach 50MHz, but when the IO port is used as a read/write signal to read and write data, the flip rate is not that fast. When using STM32 to read digital camera data and write it into TFT, the data transmission rate is involved, and the IO port needs to be flipped quickly. So, I did thi
[Microcontroller]
GD32 MCU won the Embedded Award 2020 Best Hardware Product Award
GigaDevice's GD32 MCU participated in this year's first top international exhibition - Embedded World 2020 in Nuremberg, Germany. The GD32VF103 series RISC-V core MCU stood out in the hardware field nominations and won the championship, winning the only Best Hardware Product Award of the year! GigaDevice GD32 MCU is
[Embedded]
GD32 MCU won the Embedded Award 2020 Best Hardware Product Award
N76E003AT20 MCU serial port 0 debugging
If a key 1 is pressed, the serial port 0 of N76E003 will send 1 to the serial port debugging assistant, but there is always a problem. Which great god can help me? The code is as follows: #include "n76e003.h" #include "stdio.h" #define uint32 unsigned int //typedef unsigned int uint32; void delay_us(int n) { int m; fo
[Microcontroller]
51 MCU (assembly language) to achieve decimal to hexadecimal conversion
mission details DEC0 DEC1 DEC2 store "decimal" numbers eg: 03 35 96 HEX0 HEX1 stores hexadecimal numbers, eg: 83 3C Mission Principle The essence is BCD code to binary The so-called decimal number "15" corresponds to the hexadecimal number 0x15H, and the decimal number of 0x15 is 21, and the binary number is 0001 01
[Microcontroller]
51 MCU (assembly language) to achieve decimal to hexadecimal conversion
MCU LCD Operation
LCD operation: 1602 LCD can only display ASCI. Today, I will drive 1602 LCD. LCD operation is simpler than digital tube.  Basic operation sequence of LCD: 1.1 Read state: Input: RS = L, RW = H, E = H Output D0~D7 1.2 Write instruction: Input: RS = L, RW = L, D0~D7 instruction code, E = high pulse, output: none; RS is
[Microcontroller]
MCU LCD Operation
Design of Dual-computer Communication Based on Single-chip Microcomputer
1. Experimental purpose 1. Master the principle and programming method of dual-machine communication. 2. Learn how to use 8251A 2. Experimental content 1. Experimental schematic diagram: 2. Experimental content: As shown in the figure, TXC and RXC are the sending clock and receiv
[Microcontroller]
Design of Dual-computer Communication Based on Single-chip Microcomputer
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号