随着技术的进步,MP3播放器已成为现代消费者首选的随身听产品,它以小巧玲珑的体积,精美的外形,低廉的价格以及其强大的功能深得消费者的厚爱。
MP3播放器的主要作用是解压压缩的MP3数字音频,再现MP3音乐。AT89C51SND1是ATMEL公司专门针对开发MP3而设计的,其主要特点是内部集成了MPEG2解码器和USB通信接口,内含64KB的内部程序存储器,支持在系统编辑ISP功能,通过USB或者串行口对芯片进行编程操作,内部数据存储器为2056B。
以下为C语言程序
#include "AT89C51SND1C.H"
#include "Initial.h"
#include "file_system.h"
#include "IDE.H"
#include "keyboard.h"
#include "hardware.h"
#include "MP3.H"
#include "LCD.H"
unsigned char status; //程序运行状态
extern unsigned char mp3_file_exist;
void DAC_start(unsigned char clk) //DAC开始工作
{
set_DAC_CLK(clk); //根据MP3文件,设置相应的DAC时钟频率
DAC_interface_en();
DAC_data_request_en();
}
unsigned char xdata function,vol,bas,med,tre; //保存音量等设置
code unsigned char MPEGVer[2][7]={"MPEG-2","MPEG-1"}; //版本显示信息
code unsigned char Fs[8]={22,44,24,48,16,32,22,44}; //采样率
code unsigned char DACFs[8]={33,16,30,15,46,22,33,16}; //256Fs
//code unsigned char DACFs[8]={22,10,20,9,30,15,0,0}; //384Fs
/////////////////////////// MP3 play /////////////////////////////////////////
void play(void)
{
unsigned long int LBA;
unsigned int i;
unsigned char sectors_count;
signed long int file_length;
unsigned char count;
unsigned char MPEG_inf; //用来保存MP3版本和采样率
/*
bit0=0 vertion 2
bit0=1 bertion 1
*/
LCD_go_home();
prints(" ");
LCD_line_feed();
prints(" ");
file_length=file.length;
LBA=file.next_cluster_number-(dbr[driver_number].root_cluster_num);
LBA=LBA*(dbr[driver_number].sectors_per_cluster)+DATA_start_sector[driver_number];
MP3_CRC_disen();
sectors_count = dbr[driver_number].sectors_per_cluster;
read_IDE_sector(LBA);
if(buffer[0]!=0xFF){pause();MP3_decoder_disen();DAC_interface_disen();get_next_file();key=0;return;}
if((buffer[1]&0x06)!=0x02){pause();MP3_decoder_disen();DAC_interface_disen();key=0;LCD_line_feed();prints("MP3 层错误!! ");while(!key);get_next_file();return;}
//////////////////////// 从MP3侦头中获取采样率和压缩版本信息 //////////////
MPEG_inf=buffer[2]&0x0C; //采样率
MPEG_inf>>=1;
if((buffer[1]&0x18)==0x18)MPEG_inf=MPEG_inf | 0x01; //版本1
if((buffer[1]&0x18)==0x10)MPEG_inf=MPEG_inf & (~0x01); //版本2
///////////////////////////////////////////////////////////////////////////
//////////////////////// 填充1K数据 ////////////////////////////////////
for(i=0;i<512;i++)MP3DAT=buffer[i];
LBA++;
sectors_count--;
if(file_length>512)file_length-=512;
else {pause();MP3_decoder_disen();DAC_interface_disen();get_next_file();key=0;return;}
if(sectors_count==0){LBA=get_next_sector();sectors_count=dbr[driver_number].sectors_per_cluster;}
read_IDE_sector(LBA);
for(i=0;i<512;i++)MP3DAT=buffer[i];
LBA++;
sectors_count--;
if(file_length>512)file_length-=512;
else {pause();MP3_decoder_disen();DAC_interface_disen();get_next_file();key=0;return;}
if(sectors_count==0){LBA=get_next_sector();sectors_count=dbr[driver_number].sectors_per_cluster;}
MP3_decoder_en(); //启动解码器
DAC_start(DACFs[MPEG_inf]); //初始化DAC接口
byte_offset=512;
key=KEY_FUN;
function=FUN_MAX;
while(1)
{
if(key) //如果有键按下
{
switch (key)
{
case KEY_FUN: //功能键按下
{
function++;
if(function>=FUN_MAX)function=FUN_SEL;
switch (function)
{
case FUN_SEL:LCD_go_home();for(count=0;count<14;count++)write_LCD_data(((unsigned char *)dir_name)[count]);LCD_line_feed();prints("当前曲号: ");printf(play_count);LED1=1;LED2=1;LED3=1;break;
case FUN_VOL:LCD_line_feed();prints("音量: ");printf(vol);prints(" ");LED1=0;LED2=1;LED3=0;break;
case FUN_BAS:LCD_line_feed();prints("低音: ");printf(bas);prints(" ");LED1=1;LED2=1;LED3=0;break;
case FUN_MED:LCD_line_feed();prints("中音: ");printf(med);prints(" ");LED1=1;LED2=0;LED3=1;break;
case FUN_TRE:LCD_line_feed();prints("高音: ");printf(tre);prints(" ");LED1=0;LED2=1;LED3=1;break;
case FUN_BOOT:LCD_line_feed();prints("重低音: ");if(MP3CON&0x40)prints("开");else prints("关");prints(" ");LED1=0;LED2=0;LED3=0;break;
}
key=0;break;
}
case KEY_UP: //上翻键按下
{
switch (function)
{
case FUN_SEL:{MP3_decoder_disen();DAC_interface_disen();get_pre_file();LCD_go_home();for(count=0;count<14;count++)write_LCD_data(((unsigned char *)dir_name)[count]);LCD_line_feed();prints("当前曲号: ");printf(play_count);key=0;return;}
case FUN_VOL:{if(vol<31)vol++;set_vol(vol);set_vor(vol);LCD_line_feed();prints("音量: ");printf(vol);prints(" ");break;}
case FUN_BAS:{if(bas<31)bas++;set_bas(bas);LCD_line_feed();prints("低音: ");printf(bas);prints(" ");break;}
case FUN_MED:{if(med<31)med++;set_med(med);LCD_line_feed();prints("中音: ");printf(med);prints(" ");break;}
case FUN_TRE:{if(tre<31)tre++;set_tre(tre);LCD_line_feed();prints("高音: ");printf(tre);prints(" ");break;}
case FUN_BOOT:{MP3_bass_boost();LCD_line_feed();prints("重低音: ");prints("开");prints(" ");break;}
}
key=0;break;
}
case KEY_DOWN: //下翻键按下
{
switch (function)
{
case FUN_SEL:{MP3_decoder_disen();DAC_interface_disen();get_next_file();LCD_go_home();for(count=0;count<14;count++)write_LCD_data(((unsigned char *)dir_name)[count]);LCD_line_feed();prints("当前曲号: ");printf(play_count);key=0;return;}
case FUN_VOL:{if(vol>0)vol--;set_vol(vol);set_vor(vol);LCD_line_feed();prints("音量: ");printf(vol);prints(" ");break;}
case FUN_BAS:{if(bas>0)bas--;set_bas(bas);LCD_line_feed();prints("低音: ");printf(bas);prints(" ");break;}
case FUN_MED:{if(med>0)med--;set_med(med);LCD_line_feed();prints("中音: ");printf(med);prints(" ");break;}
case FUN_TRE:{if(tre>0)tre--;set_tre(tre);LCD_line_feed();prints("高音: ");printf(tre);prints(" ");break;}
case FUN_BOOT:{MP3_no_bass_boost();LCD_line_feed();prints("重低音: ");prints("关");prints(" ");break;}
}
key=0;break;
}
case KEY_OK:
{
MP3_decoder_disen();
DAC_interface_disen();
status=STOP;
for(i=(folder_length/2);i<(7+folder_length/2);i++)dir_name[i]=0x2020;
LCD_line_feed();
prints("停止 ");
key=0;
return;
}
}
}
while(MPFREQ)
{
if((((unsigned char *)&byte_offset)[0])&0xFE)
{
read_IDE_sector(LBA);
LBA++;
byte_offset=0;
sectors_count--;
file_length-=512;
while(MPFREQ)
{
MP3DAT=buffer[byte_offset];
byte_offset++;
if((((unsigned char *)&byte_offset)[0])&0xFE)break;
}
if(file_length<=0) {pause();MP3_decoder_disen();DAC_interface_disen();get_next_file();key=0;return;}
if(sectors_count==0){LBA=get_next_sector();sectors_count=(dbr[driver_number].sectors_per_cluster);}
}
if(MPFREQ)
{
MP3DAT=buffer[byte_offset];
byte_offset++;
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////// |