基于STM32的WAV音频解码和播放
- 格式:pdf
- 大小:1.78 MB
- 文档页数:14


单片机 wav原理
单片机是一种集成了微处理器核心、存储器和各种输入/输出设
备的微型计算机系统。
而WAV是一种音频文件格式,它是一种无损
压缩的音频格式,通常用于存储音频数据。
单片机可以通过外部的
数字模拟转换器(DAC)或者脉冲宽度调制(PWM)等方式来控制音
频输出,从而实现WAV音频文件的播放。
在单片机中实现WAV音频文件的播放,首先需要将WAV文件存
储在单片机的存储器中,比如闪存或者SD卡等。
然后,单片机需要
通过相应的程序读取WAV文件的音频数据,并将其转换为模拟信号
输出到音频输出端口。
这通常需要使用专门的音频解码器或者DAC
芯片,以确保音频数据能够以正确的格式和采样率输出。
另外,单片机需要具备足够的计算能力和存储能力来处理WAV
文件的音频数据,以及实现音频数据的解码和输出。
在实际应用中,可能还需要考虑音频数据的缓存和解码算法的优化,以确保音频播
放的流畅性和音质。
总的来说,单片机实现WAV音频文件的播放需要考虑存储、解码、输出等多个方面的问题,涉及到硬件和软件的设计与开发。
这
需要工程师综合考虑单片机的性能、外围设备的选择和音频处理算法的优化等多个方面因素,以实现稳定、高质量的音频播放功能。
基于STM32的MP3播放器设计与实现作者:周双飞黄海波简炜高云王志虎来源:《电脑知识与技术》2016年第01期摘要:基于STM32F103VET6增强型微控制器设计了一款MP3播放器,基于硬件解码实现了多页显示歌曲播放列表、换页、触摸换歌以及支持MP3、WAV等格式歌曲的播放等功能的操作。
硬件系统包括VS1003音频解码器、TDA1308功放模块、MicroSD卡以及液晶触摸屏;软件系统基于VS1003驱动、液晶触摸屏的驱动、μC/OS- 实时操作系统和FATFS文件系统等进行实现,系统实现表明具有很好的播放和界面操作效果。
关键词:STM32;MP3播放器;μC/OS-中图分类号:TP393 文献标识码:A 文章编号:1009-3044(2016)01-0260-03Design and Implementation of MP3 Player Based on STM32ZHOU Shuang-fei1, HUANG Hai-bo1, JIAN Wei1, GAO Yun2, WANG Zhi-hu2(School of Electrical & Information Engineering, Hubei University of Automotive Technology, Shiyan 442002, China; 2. Power Equipment Plant of Dongfeng Motor Group Co.,Ltd., Shiyan 442000, China)Abstract: A MP3 player was designed based on STM32F103VET6, which is an enhanced micro controller. Based on hardware decode, main functions are achieved, such as multiple pages show the songs list, paging, touch to change songs and support MP3,WAV format songs play,and so on. The hardware system included VS1003 audio decoder, TDA1308 power amplifier module, MicroSD card and LCD touch screen. The software was implemented based on VS1003 driver, LCD touch screen driver,μC/OS-II real-time operating system and FATFS file system. The implementation shows that the MP3 player has the very good play and interface operation effect.Key words: STM32;MP3 player;μC/OS-MP3播放器被广泛应用于各种消费类电子领域,如新型的车载音频设备、移动手机、网络MP3等,所以对MP3的研究还具有很高的市场价值。
基于STM32单片机的MP3播放器设计基于STM32单片机的MP3播放器设计摘要众所周知,越来越多的人对MP3的追求逐渐下降,MP3的功能并没有像酷狗一样显示歌词等那么方便。
通过对STM32F103C8T6单片机MP3播放器的研究设计,来提升整个系统的功能。
系统运行的原理是利用数据卡存储歌曲歌词在,然后通过LCD1602显示屏根据个人的选择进行播放和显示。
整个屏幕类似酷狗界面,可以进行歌曲的各种模式的设置,具有很强的画面感,为了贴近生活需求,采用触屏的模式来操作。
系统的编程主要是利用上C语言,根据自身的需求可以进行不同功能款式的设计,整个电路的设计是相对比较简单的,MP3体积小容易携带,加上能够进行触屏影像,会更受到广大消费者的喜爱。
关键词:MP3播放器;STM32F103C8T6;VS1003;SD卡MP3 player design based on STM32 micro-controllerABSTRSCTThis paper introduces the design method of MP3 player based on stm32f103c8t6 in detail. The system realizes the decoding and playing of MP3 files stored in SD card, and displays the name of playing songs, the remaining time of playing songs and the corresponding words of songs on LCD1602 display screen. Through the graphic interactive user chart on LCD1602 display screen, it can also realize the fast forward of songs Fast rewind, pause and change cycle mode can basically realize an MP3 player with touch screen function. The software is programmed with C language and modular design idea is adopted. The hardware circuit of the system is simple, and the subprogram is universal, which fully meets the design requirements.Key words: MP3 player; stm32f103c8t6; VS1003; SD card目录摘要 (3)abstract (4)目录 (5)1 绪论 (6)1.1 课题研究背景 (6)1.2 国内外研究现状 (6)1.3 课题设计内容 (6)2 系统的总体设计 (8)2.1 系统核心硬件的选择 (8)2.1.1 主控芯片的选择 (8)2.1.2 显示模块的选择 (8)2.1.3 数据存储芯片的选择 (9)2.1.4 按键模块的选择 (9)2.1.5 MP3解码方案 (9)2.1.6 MP3储存介质方案 (9)2.2 系统总体方案 (10)3 硬件介绍和设计 (11)3.1 单片机及最小系统 (11)3.2 LCD1602液晶显示模块 (14)3.3 按键电路设计 (16)3.4 MP3串口控语音模块 (17)3.5 LM386功放模块 (19)3.6 VS1003解码电路设计 (20)3.7 电源模块 (21)4 系统软件的设计 (23)4.1 系统程序工作分析 (23)4.2 主流程图设计 (24)4.3 显示子流程图设计 (25)4.4 音乐播放功能的设计 (25)4.5 详细代码结构 (27)5 系统调试 (31)5.1 硬件的焊接 (31)5.2 系统硬件调试 (32)5.3 系统软件调试 (32)5.4 附录 (33)结论 (33)参考文献 (35)致谢 (36)1 绪论1.1 课题研究背景随着科技的进步,每个家庭都拥有者许多家电产品和电子产品。
October 2011Doc ID 022390 Rev 11/15AN3997Application noteAudio playback and recording using the STM32F4DISCOVERY1 IntroductionThis application note describes the audio (wave) playback and recording application based on the STM32F4xx microcontroller and the STM32F4-DISCOVERY board.The audio data (wave) can be read from the internal Flash memory of the STM32F4xx microcontroller or on an external USB key (through USB FS core in Host mode). The recorded wave can be stored only in the external USB key.The recording process is based on ST MP45DT02 MEMS microphone hardware with a PDM audio software decoding Library (converting PDM data produced by the microphone to PCM data stored in the USB key).The document is structured as follows:● A description of the principles of the audio playback and recording firmware and how to run the firmware demonstration are provided in Section 2: Application overview .●Section 3 describes how to get started with the software and hardware.The source code of this application is provided in the "STM32F4-DISCOVERY board firmware package" (V1.1.0 and later) under the following path 'Project\Audio_playback_and_record.1.1 Reference documents●STM32F4DISCOVERY high-performance Discovery board data brief ●Getting started with software and firmware environments for theSTM32F4DISCOVERY Kit (UM1467)●PDM audio software decoding on STM32 microcontrollers (AN3998)●STM32F40x reference manual (RM0090)●STM32F405xx STM32F407xx datasheetThe above documents are available at /stm32f4-discovery.Contents AN3997Contents1Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1Reference documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Application overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.1Application description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.2Firmware driver description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.3Audio playback application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.3.1Playback from USB key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.3.2Playback from internal Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.4Audio record application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93How to use the audio playback and recording application . . . . . . . . . 123.1System requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123.2Running the application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 4Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142/15Doc ID 022390 Rev 1AN3997List of figures List of figuresFigure 1.Audio playback and record architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Figure 2.Audio playback and record modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Figure 3.Audio playback/record firmware driver flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Figure 4.Wave player procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Figure 5.Recording application procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Figure 6.Microphone connection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Figure 7.Hardware environment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Doc ID 022390 Rev 13/15Application overview AN39974/15Doc ID 022390 Rev 12 Application overview2.1 Application descriptionThe audio playback and record applications support two types of mass storage media. Theycan play audio data in the internal Flash of the microcontroller or on an external USB key, and record data only to an external USB key. This is selected by defines in the main.h file. In the workspace toolbar, select the project configuration:●MEDIA_IntFLASH ●MEDIA_USB_KEYThe firmware driver can:●Play a stored wave from an external USB key or internal Flash ●Record a wave in an external USB key ●Switch from play to recordThis application is based on a STM32F4xx device and an STM32F4-DISCOVERY board.The main features of the application are:●MEMS microphone ●Audio Codec DAC ●Headphone●USB key (if this the storage media used) ●MEMS accelerometerI2SUSB hostAN3997Application overviewDoc ID 022390 Rev 15/15The main STM32 modules used by this application are:●USB peripheral: configured in Host mode. Mass Storage Class (MSC) is implemented to transmit and receive audio data from/to USB key.●I2S peripheral: configured in Master Transmitter mode and used to transmit audio data to the external audio codec (DAC). It is also used as a Master Receiver as an input clock for the MEMS microphone.●DMA: is used to transmit data from the buffers to the I2S peripheral. This significantly reduces the CPU load.●I2C peripheral: is used to control several external devices like the audio Codec and to obtain data from this device.●SPI peripheral: used to control the MEMS accelerometer.●User button: is used to monitor the applications (playback or record).Note:This application note is based on the STM32 USB On-The-Go (OTG) host and device library. For more details about the USB Host stack and a mass storage demonstration,please refer to the “STM32F105/7, STM32F2xx and STM32F4xx USB On-The-Go host and device library” user manual (UM1021).Application overview AN39976/15Doc ID 022390 Rev 12.2 Firmware driver descriptionThis application contains the following set of source files:●main.c: contains the initialization code and starts the application depending on the selected 'MEDIA_IntFLASH' or 'MEDIA_USB_KEY' configuration.●stm32f4xx_it.c: contains the interrupt handlers for the application.●waveplayer.c: implements the functions used for playback.●waverecorder.c: implements the functions used for record.●usb_bsp.c: implements the board support package for the USB host library.●usbh_usr.c: includes the USB host library user callbacks.After each board reset,the wave player application runs from the selected mass storage media.●If the selected media is USB key, if the user button is pressed, the playback application is stopped and the application switches to recording. Each time the user button is pressed, it stops the running application and switches to executing the other one.●If the selected media is internal Flash, pressing the user button has no effect.AN3997Application overview1.If internal Flash is selected as mass storage media, pressing the user button has no effect.2.3 Audio playback applicationThe flowchart in Figure4 describes the playback application. It implements several controlfeatures like Pause/Resume, Repeat On/Repeat Off.The MEMS accelerometer is used to support Pause/Resume. When the wave player isrunning, the first click on the board stops playing and the second click on it resume playing.The Repeat On/Repeat Off feature is managed with DEFINEs in the main.h file.Doc ID 022390 Rev 17/15Application overview AN3997At any time (if USB key is selected as mass storage media), if the user button is pressed,playback is stopped and the record application is executed.2.3.1 Playback from USB keyIn this demonstration, any wave file stored on the USB Key can be opened using the FatFsfile system and transferred to the internal SRAM block by block (1024 bytes) using the DMAand the I2S interface.The voice sampling period is read from the Wave File Header. An audio DAC is connected tothe I2S interface to play the stored wave files.The name of the wave file loaded in the USB Key can be changed by modifying the"WAVE_NAME" definition in the main.h file. The wave file name must have elevencharacters.8/15Doc ID 022390 Rev 1AN3997Application overviewDoc ID 022390 Rev 19/15This application reads all wave files from the USB Key and displays only the .WAV files that have the following format:●Audio format: PCM (an uncompressed wave data format in which each value represents the amplitude of the signal at the time of sampling)●Sample rate: such 8000, 11025, 16000, 22050, 44100 Hz or 48000 Hz.●Bits per sample: 16 bits (audio sample data values are in the range [0-1024])●Number of channels: 2 (stereo)The wave from the USB Key is parsed to detect the sample rate in order to configure the I2S accordingly. When the play back begins the blue LED starts toggling.The playback is managed with double buffering. A first buffer is used to store the wave data retrieved from the USB Key, using the FatFs file system.Once this buffer is filled:●The DMA sends its content to the I2S peripheral which transfers it to the external audio codec DAC●The data from the USB key is stored in a second bufferThen these two buffers are swapped indefinitely, till end of the playback process.At any time, if the USB Key is disconnected from the DISCOVERY board, the blue LED is off, the audio DAC is stopped and red LED goes on.When the USB Key is reconnected again to the DISCOVERY board, the red LED goes off and the last running application starts again.2.3.2 Playback from internal FlashIn this demonstration, the wave file is stored in the internal Flash as a const array declared in the audio_sample.c file.After a reset, the playback application starts playing the wave stored in the internal Flash after initializing the Audio DAC.2.4 Audio record applicationThe flowchart in Figure 5 describes the recording application. It is based on a MEMS microphone. Audio record is available only when USB key is selected as mass storage media.Application overview AN3997The I2S peripheral is configured as master in order to generate the correct clock (1,024MHz) for the digital microphone. The 1,024 MHz clock is calculated from the output audiostreaming (16 KHz) and the decimation factor (64) chosen for the demo (16000 Hz x 64 =1.024 MHz). (Refer to AN3998 for PDM audio software decoding).The I2S peripheral is configured to generate an interrupt each time 16 bit samples havebeen acquired.10/15Doc ID 022390 Rev 1AN3997Application overviewThe filtering process uses the PDM audio software decoding library. This library implementsseveral filters for the 1-bit PDM high frequency signal output from a digital microphone andtransforms it into a 16-bit PCM at a proper audio frequency.The filtering process and the write into the USB key are managed with double buffering.When the PDM Library is filtering microphone data, the filtered data is stored in the USBkey.When a USB key is used for mass storage, if the USB key is disconnected, the recording isstopped and a red LED goes on. When it is connected again, the red LED goes off and therecording is started again.Doc ID 022390 Rev 111/153 How to use the audio playback and recordingapplicationrequirements3.1 SystemBefore running your application, you should establish the connection with theSTM32F4DISCOVERY board as shown in Figure7.Figure 7.Hardware environmentTo run the audio playback and record applications on your STM32F4DISCOVERY board,the minimum requirements are as follows:●Windows PC (2000, XP, Vista, 7)●'USB type A to Mini-B' cable, used to power the board (through USB connector CN1)from a host PC and to connect to the embedded ST-LINK/V2 for debugging andprogramming.●USB ‘micro A male to B female' cable, used to connect the USB key (through USBconnector CN5) as a USB Device to the STM32F4xx's Host.●Headphone with male jack connector.3.2 Running the applicationTo run the Audio playback and recording application, you should proceed as follows:12/15Doc ID 022390 Rev 11.Program the firmware upgrade application in the internal Flash memory2. Open the project (under Project\Audio_playback_and_record) with a toolchain of yourchoice3. Select "MEDIA_IntFLASH" or "MEDIA_USB_KEY" workspace depending on the massstorage media memory used.4. Compile, load it into target memory and run it.Another option is to use the embedded Bootloader or any in-system programming tool to reprogram this application easily.●Use STM32F4-Discovery_Audio_USB_V1.0.0.hex or STM32F4-Discovery_Audio_IntFLASH_V1.0.0.hex with any "in-system programming tool" such as STM32 ST-LINK Utility●Use STM32F4-Discovery_Audio_USB_V1.0.0.dfu or STM32F4-Discovery_Audio_IntFLASH_V1.0.0.dfu with "DFUse\DFUse Demonstration" toolFor more details please refer to section 4 of UM1467.If you select "MEDIA_USB_KEY" configuration, you should proceed as follows:1.Load a wave file to the root of a USB key, this file should have the format described inSection2.3.1: Playback from USB key.2. Plug the USB key into the STM32F4DISCOVERY board through 'USB micro A-Male toA-Female' cable (connector CN5)3. Then follow the description provided in Section2.2: Firmware driver descriptionDoc ID 022390 Rev 113/15Revision history AN399714/15Doc ID 022390 Rev 14 Revision historyTable 1.Document revision history DateRevision Changes27-Oct-20111Initial release.AN3997Please Read Carefully:Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any time, without notice.All ST products are sold pursuant to ST’s terms and conditions of sale.Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no liability whatsoever relating to the choice, selection or use of the ST products and services described herein.No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such third party products or services or any intellectual property contained therein.UNLESS O THERWISE SET FO RTH IN ST’S TERMS AND CO NDITIO NS O F SALE ST DISCLAIMS ANY EXPRESS O R IMPLIED WARRANTY WITH RESPECT TO THE USE AND/O R SALE O F ST PRO DUCTS INCLUDING WITHO UT LIMITATIO N IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT. UNLESS EXPRESSLY APPRO VED IN WRITING BY TWO AUTHO RIZED ST REPRESENTATIVES, ST PRO DUCTS ARE NO T RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY, DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any liability of ST.ST and the ST logo are trademarks or registered trademarks of ST in various countries.Information in this document supersedes and replaces all information previously supplied.The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.© 2011 STMicroelectronics - All rights reservedSTMicroelectronics group of companiesAustralia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan - Malaysia - Malta - Morocco - Philippines - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of AmericaDoc ID 022390 Rev 115/15。