📄 mp3_player.c
字号:
#include "AT89C51SND1_REG.H"
#include "MP3_PLAYER.H"
#include "FILE_SYS.H"
#include "MCU_UART.H"
void MP3FsInit(unsigned char NDIV, unsigned int RDIV, unsigned char MPCD, unsigned char AUCD)
{
PLLNDIV = 0;
PLLRDIV = 0;
MP3CLK = 0;
AUDCLK = 0;
//PLLclk=(OSCclk*(RDIV+1))/(int)(NDIV+1)
PLLNDIV = 0x7f&NDIV; //set NDIV
PLLCON |= (0x3&RDIV) << 6; //set RDIV
PLLRDIV = (0x3ff&RDIV) >> 2;
//MP3CLK=PLLCLK/(int)(MPCD+1)
MP3CLK |= MPCD;
//AUDCLK=PLLCLK/(int)(AUCD+1)
AUDCLK |= AUCD;
}
void PllInit(void)
{
CKCON |= X2;
PLLCON &= (~PLLRES); //Enable PLL
PLLCON |= PLLEN;
}
void MP3Init()
{
MP3VOR = 0x0a;
MP3VOL = 0x0a;
MP3BAS = 0x10;
MP3MED = 0x10;
MP3TRE = 0x10;
MP3CON &= (~MSKREQ); //Clear to allow the
//MPREQ flag to generate a MP3 interrupt.
MP3CON |= MPEN; //enable the MP3 decoder.
}
void AudioInit()
{
AUDCON0 = 0x77; //0111 0110
AUDCON1 &= (~0xB0); //SRC=0,MSREQ=0,MUDRN=0
AUDCON1 |= 0x01;
AUDCON1 |= 0x40;
}
void MP3_Init(void)
{
PllInit();
MP3Init();
AudioInit();
}
void PlayMP3()
{
unsigned int i,j,total_size,readbyte;
unsigned char data MP3_Framehead[4];
i = 0;
readbyte = ReadFile(); //Read 512 Bytes at first
/* when the first 3 bytes are 49 44 33 the next can be 03,this means ID3 V2.3 */
if (PageBuf[0] == 0x49)
{
if ((PageBuf[1] == 0x44) && (PageBuf[2] == 0x33))
{
total_size = (PageBuf[6] & 0x7F) * 0x200000 + (PageBuf[7] & 0x7F) * 0x4000 + (PageBuf[8] & 0x7F) * 0x80 + (PageBuf[9] & 0x7F);
while (total_size > 512)
{
ReadFile();
total_size -=512;
}
i = total_size;
printu("Skip ID3V2\n");
if (PageBuf[i] != 0xFF) //if not 0xFF,there is not the Frame head, but extend label
{i += 10;
printu("Skip Extend Label\n");
}
}
}
else printu("No ID3V2\n");
printuf16x("Frame Header Offset in Page is %x\n",i);
if ((PageBuf[i] == 0xFF) && (PageBuf[i + 1] & 0xF0 == 0xF0))//get MP3 information from FF FX XX XX 4bytes,
{
MP3_Framehead[0] = PageBuf[i];
MP3_Framehead[1] = PageBuf[i + 1];
MP3_Framehead[2] = PageBuf[i + 2];
MP3_Framehead[3] = PageBuf[i + 3];
printu("MP3 Frame Header is ");
for(j=0;j<4;j++)
printuf("%x ",MP3_Framehead[j]);
printu("\n");
}
if (MP3_Framehead[1] & 0x08)
{
switch ((MP3_Framehead[2] & 0x0C) >> 2)
{
case 0x00 : MP3FsInit(24, 126, 3, 5);
printu("Bit Rate is 44.1 KHz\n");break; //Fs=44.1kHz
case 0x01 : MP3FsInit(124, 575, 3, 4);
printu("Bit Rate is 48 KHz\n");break; //Fs=48kHz
default : break;
}
}
else
{
switch ((MP3_Framehead[2] & 0x0C) >> 2)
{
case 0x00 : MP3FsInit(24, 126, 3, 11);
printu("Bit Rate is 22.05 KHz\n");break; //Fs=22.05kHz
case 0x01 : MP3FsInit(124, 575, 3, 9);
printu("Bit Rate is 24 KHz\n");break; //Fs=24kHz
default : break;
}
}
for (j=i; j<readbyte; j++) //将已经读到的Page写入MP3解码器
{
while (!(MP3STA1 & MPBREQ));
MP3DAT = PageBuf[j];
}
while (1)
{
readbyte = ReadFile(); //继续读Page并送入MP3解码器
for (i=0; i<readbyte; i++)
{
while (!(MP3STA1 & MPBREQ));
MP3DAT = PageBuf[i];
}
if (readbyte < 512)
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -