From: http://bbs.ednchina.com/BLOG_ARTICLE_276006.HTM
Playing MP3, we have already achieved a sine wave test of vs1003, and we can hear the sound at a certain frequency from the headset. Then write a program to read the file from the MMC/SD card and send it to vs1003 for playback. Both the mmccard and vs1003 on my board are connected to the same hardware SPI interface, and different chip selection controls are used. The idea of the program is that the microcontroller reads a sector (512 bytes) audio file from the mmccard and stores it in the SRAM, and then sends it to the serial data port (SDI) of vs1003 ), loop through the above process to play the audio file. The principle is still very simple, but the compiled program cannot play the sound. The debugging information output through the serial port shows that there is data transmission, but it does not sound. After careful check, the program finds that the variable type of the referenced function is specified incorrectly (it should be unsigned
Int, written as unsigned char ). After this error is ruled out, repeat the experiment, update the program, power on and run it, initialize the mmccard and vs1003, and issue a command to start playing the file. There was a long time of illegal music from the headset. At that time, I was very excited. After all, it took too long to go from audible (sine wave) to music.
After being able to play MP3, I tried WMA again, which is no problem. The next step is to select different MP3 files for bit rate testing. Initially, the program can smoothly play 8, 16, 24, 32, 48, 64 Kbps files, and then it will be stuck up, it is estimated that the file transfer speed is not fast enough, start to optimize the program. First, adjust the SPI clock to 8 MHz, and then experiment. It feels better, but the improvement is not great. What is the problem? Looking back at the program, I used a loop in the function reading the mmccard, and read a byte 512 times each time. This completes reading a block and the function sending data to vs1003. In this way, the Write Program has a good sense of attention, but it feels that the overhead of the loop is too large (because it is a 512 loop, the variable must also use the int type ). Therefore, the program reads 8 bytes of data cyclically every time in the sector function, and only needs to be cyclically 64 times. The vs1003 function is also modified. In this way, the Board can smoothly play MP3 at 140 kbps, and the peak data transmission rate reaches kb. With this success, I continue to optimize the program and modify mmc_read () Again. Each time, the system reads 64 bytes of data cyclically and loops 8 times. In vs1003_write (), 32 bytes of data are read cyclically each time and 16 times in a loop (the data manual says that vs1003 can receive at least 32 bytes of data when the chip is idle, if there are more, you need to query the dreq pin status and then judge ). This improvement is also very exciting. The playback speed of files with a 172 K bit rate is very smooth, and the testing peak data transmission rate of files with a high bit rate is kbps. The sense of accomplishment of the optimization program is still great :)
Then I performed an operation on the SPI interrupt function, removed the statements of Guanzhong and open interrupt (because I executed two statements in the interrupt function), and tried to compress the processing time as much as possible. In addition, after the SPI sends the data, there is a waiting for the completion of the detection process, you must wait until the SPI sends the data before the next sending. After analysis, I found that this detection can be removed, because my SPI clock has reached 8 m and the CPU is working at 33 m, the Data Manual of STC macro crystal shows that most of the commands can be completed within 2-3 clock cycles, in addition, after each SPI sending, I need to move the pointer (another command). At this time, the SPI sending has already been completed. Therefore, you can directly send data again without detection. After modifying the program according to this idea, my board decoding speed has reached 272kbps. Later, I made some small modifications, and the maximum decoding speed was stable at 280 kbps. So far, except 320 k
The CBR-encoded MP3 player has no problems with other audio files (of course, in the format supported by vs1003 :)).
In the process of developing this program, I deeply felt very important. In addition, sometimes I can try it out in a bold way (removing SPI sending detection is an example ). In current mode, only data in the specified area of the mmccard can be played sequentially. Next, I want to transplant the FAT file system to the MCU so that the video can be played by file. I hope my experience will be helpful to my friends who are also using STC single-chip microcomputer and vs1003.
Stc12 + vs1003 playing MP3 Program Development History
Control vs1003 success-> play a sine wave-> play MP3 success-> Performance Improvement-> 64kbps-> large kbps-> 140kbps-> 172kbps-> 272kbps-> 280 kbps