
基于stm32的mp3播放器设计与实现提供mdk完整源码.doc
23页本文将介绍一个利用STM32处理器实现简易MP3 Player的设计实例,这个综合应用实例有助于读者了解STM32、SPI接口、SD卡、TIMER、中断、FAT文件系统、USB等的应用这里提供了两种设计方案,第一种方案是简易声波播放器,仅使用STM103V100评估板,令计时器TIM4工作在PWM模式下,将wav格式的声波文件从SD卡中读出,由TIM4产生不同频率的方波通过低通滤波器和放大器送喇叭,如图1所示;第二种方案则是简易MP3播放器,还需要使用额外的解码芯片,将MP3格式的文件从SD卡读出,然后送解码芯片解码播放,如图2所示本节将先介绍SD卡、FAT16文件格式、VS1003编解码器等关键部分,然后再分别给出两种设计方案的软件设计图1 简易声波播放器方案图2 简易MP3 Player方案1 SD卡的结构及读写方法STM103V100评估板有SD连接器,其使用SPI总线与STM32处理器连接,如图3所示图3 SD连接器与STM32处理器SPI连接图SD卡(Secure Digital Memory Card)是一种为满足安全性、容量、性能和使用环境等各方面的需求而设计的一种新型存储器件,SD卡允许在两种模式下工作,即SD模式和SPI模式,本系统采用SPI模式。
本小节仅简要介绍在SPI模式下,STM32处理器如何读写SD卡,如果读者如希望详细了解SD卡,可以参考相关资料SD卡内部结构及引脚如图4所示图4 SD卡内部结构及引脚SD卡主要引脚和功能为:n CLK:时钟信号,每个时钟周期传输一个命令或数据位,频率可在0~25MHz之间变化,SD卡的总线管理器可以不受任何限制的自由产生0~25MHz的频率;n CMD:双向命令和回复线,命令是一次主机到从卡操作的开始,命令可以是从主机到单卡寻址,也可以是到所有卡;回复是对之前命令的回答,回复可以来自单卡或所有卡;n DAT0~3:数据线,数据可以从卡传向主机也可以从主机传向卡 SD卡以命令形式来控制SD卡的读写等操作可根据命令对多块或单块进行读写操作在SPI模式下其命令由6个字节构成,其中高位在前SD卡命令的格式如表1所示,其中相关参数可以查阅SD卡规范表1 SPI命令格式Byte 1/ Byte2-5/Byte 67,6,5,0/31,0/7,00,1/Command/Command Argument/CRC,1------------------------------------下面分别给出读写SD卡的两个函数:n 读取SD卡函数u8 MSD_ReadBlock(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead);/****************************************************************** Function Name : MSD_ReadBlock* Description : Reads a block of data from the MSD.* Input : - pBuffer : pointer to the buffer that receives the data read* from the MSD.* - ReadAddr : MSD's internal address to read from.* - NumByteToRead : number of bytes to read from the MSD.* Output : None* Return : The MSD Response: - MSD_RESPONSE_FAILURE: Sequence failed* - MSD_RESPONSE_NO_ERROR: Sequence succeed *****************************************************************/u8 MSD_ReadBlock(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead){ u32 i = 0; u8 rvalue = MSD_RESPONSE_FAILURE; /* MSD chip select low */ MSD_CS_LOW(); /* Send CMD17 (MSD_READ_SINGLE_BLOCK) to read one block */ MSD_SendCmd(MSD_READ_SINGLE_BLOCK, ReadAddr, 0xFF); /* Check if the MSD acknowledged the read block command: R1 response (0x00: no errors) */ if (!MSD_GetResponse(MSD_RESPONSE_NO_ERROR)) { /* Now look for the data token to signify the start of the data */ if (!MSD_GetResponse(MSD_START_DATA_SINGLE_BLOCK_READ)) { /* Read the MSD block data : read NumByteToRead data */ for (i = 0; i < NumByteToRead; i++) { /* Save the received data */ *pBuffer = MSD_ReadByte(); /* Point to the next location where the byte read will be saved */ pBuffer++; } /* Get CRC bytes (not really needed by us, but required by MSD) */ MSD_ReadByte(); MSD_ReadByte(); /* Set response value to success */ rvalue = MSD_RESPONSE_NO_ERROR; } } /* MSD chip select high */ MSD_CS_HIGH(); /* Send dummy byte: 8 Clock pulses of delay */ MSD_WriteByte(DUMMY); /* Returns the reponse */ return rvalue;}n 写读取SD卡函数u8 MSD_WriteBlock(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite) /****************************************************************** Function Name : MSD_WriteBlock* Description : Writes a block on the MSD* Input : - pBuffer : pointer to the buffer containing the data to be* written on the MSD.* - WriteAddr : address to write on.* - NumByteToWrite: number of data to write* Output : None* Return : The MSD Response: - MSD_RESPONSE_FAILURE: Sequence failed* - MSD_RESPONSE_NO_ERROR: Sequence succeed *****************************************************************/u8 MSD_WriteBlock(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite){ u32 i = 0; u8 rvalue = MSD_RESPONSE_FAILURE; /* MSD chip select low */ MSD_CS_LOW(); /* Send CMD24 (MSD_WRITE_BLOCK) to write multiple block */ MSD_SendCmd(MSD_WRITE_BLOCK, WriteAddr, 0xFF); /* Check if the MSD acknowledged the write block command: R1 response (0x00: no errors) */ if (!MSD_GetResponse(MSD_RESPONSE_NO_ERROR)) { /* Send a dummy byte */ MSD_WriteByte(DUMMY); /* Send the data token to signify the start of the data */ MSD_WriteByte(0xFE); /* Write the block data to MSD : write count data by block */ for (i = 0; i < NumByteToWrite; i++) { /* Send the pointed byte */ MSD_WriteByte(*pBuffer); /* Point to the next location where the byte read will be saved */ pBuffer++; } /* Put CRC bytes (not really needed by us, but required by MSD) */ MSD_ReadByte(); MSD_ReadByte(); /* Read data response */ if (MSD_GetDataResponse() == MSD_DATA_OK) { rvalue = MSD_RESPONSE_NO_ERROR; } } /* MSD chip select high */ MSD_CS_HIG。









![2019版 人教版 高中语文 必修 上册《第一单元》大单元整体教学设计[2020课标]](http://img.jinchutou.com/static_www/Images/s.gif)


