⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 用ATmega128做的mp3播放器
💻 C
字号:
#include <iom128v.h>
#include "fat.h"
#include "ata.h"
#include "vs1001.h"
#include "remote.h"
#include "main.h"
#include "generic.h"
#include <string.h>
#include <stdio.h>
#include <macros.h>

#define debug
#define debugport 1

//******************************************************************
//*	MAIN Global Variable
//******************************************************************
unsigned char text[255];

//*************************************
// void main(void)
//*************************************
void main(void)
{
init_sci();
ClrScr();
tx_char(debugport,0x07); // Bell
GotoXY(1,1);
tx_string(debugport,"MP3 Player 1.0\0");
GotoXY(1,2);
tx_string(debugport,"--------------\0");
GotoXY(1,3);
if (MCUCSR & 0x01) tx_string(debugport,"-Power ON\0");
GotoXY(1,4);
if (MCUCSR & 0x02) tx_string(debugport,"-Ext RESET\0");
GotoXY(1,5);
if (MCUCSR & 0x04) tx_string(debugport,"-Brown Out\0");
GotoXY(1,6);
if (MCUCSR & 0x08) tx_string(debugport,"-Watch Dog\0");
MCUCSR = 0x0f; // Clear flags

delay_ms(5000);

WDR();
WDTCR = 0x0f; // Watch Dog enable

ClrScr();
GotoXY(1,1);
tx_string(debugport,"MP3 Player 1.0\0");
GotoXY(1,2);
tx_string(debugport,"--------------\0");
//while(1) WDR();
VS1001Init();

GotoXY(1,4);
if (init_ata(0)) tx_string(debugport,"-ATA OK\0");
else tx_string(debugport,"-ATA FAIL\0");

GotoXY(1,5);
if (Reg_Test()) tx_string(debugport,"-MP3 OK\0");
else tx_string(debugport,"-MP3 FAIL\0");

GotoXY(1,6);
tx_string(debugport," Press a key  \0");

InitFat();

VS1001_SetVolume(50,50);
Sine_Sweep();

while(!is_char(debugport)) WDR();
rx_char(debugport);

DirBrowser(2);
}

//*************************************
// void DirBrowser(unsigned long Cluster)
//*************************************
void DirBrowser(unsigned long Cluster)
{
extern unsigned char text[255];
extern struct FileInfo File;
unsigned char ch;
unsigned int MaxFile;
static int FilePtr = 0;
static int Page = 0;
static int FilePtrSave;

ClrScr();
GotoXY(1,1);
tx_string(debugport,"-- Dir List --\0");
FilePtr = Page * SONGPAGE;
MaxFile = GetDir(Cluster,FilePtr,999,DIR,FALSE);
GotoXY(2,2);
GetDir(2,FilePtr,SONGPAGE,DIR,TRUE);
FilePtr = FilePtrSave;
GotoXY(1,(FilePtr%SONGPAGE)+2); 
while(1)
		{
		while(!is_char(debugport)) WDR();
		ch = rx_char(debugport);
		if (ch == '2')
		   {
		   FilePtr++;
		   if (FilePtr >= MaxFile) FilePtr--;
		   if ((FilePtr / SONGPAGE) != Page)
		   	  {
			  Page = FilePtr / SONGPAGE;
			  ClrScr();
			  GotoXY(1,1);
			  tx_string(debugport,"-- Dir List --\0");
			  GotoXY(2,2);
			  GetDir(Cluster,FilePtr,SONGPAGE,DIR,TRUE);
			  }
		   GotoXY(1,(FilePtr%SONGPAGE)+2);
		   }

		if (ch == '8')
		   {
		   FilePtr--;
		   if (FilePtr < 0) FilePtr = 0;
		   if ((FilePtr / SONGPAGE) != Page)
		   	  {
			  FilePtr -= (SONGPAGE - 1);
			  Page = FilePtr / SONGPAGE;
			  ClrScr();
			  GotoXY(1,1);
			  tx_string(debugport,"-- Dir List --\0");
			  GotoXY(2,2);
			  GetDir(Cluster,FilePtr,SONGPAGE,DIR,TRUE);
			  FilePtr += (SONGPAGE - 1);
			  }
		   GotoXY(1,(FilePtr%SONGPAGE)+2);
		   }
		   
		if (ch == '6')
		   {
		   FilePtrSave = FilePtr;
		   GetDir(Cluster,FilePtr,1,DIR,TRUE);
		   FileBrowser(File.FirstClust);
		   }
		}
}

//*************************************
// void FileBrowser(unsigned long Cluster)
//*************************************
void FileBrowser(unsigned long Cluster)
{
extern unsigned char text[255];
extern struct FileInfo File;
unsigned char ch;
unsigned int MaxFile;
int FilePtr,Page;

ClrScr();
GotoXY(1,1);
tx_string(debugport,"--Song List --\0");
FilePtr = 0;
Page = 0;
MaxFile = GetDir(Cluster,FilePtr,999,FILE,FALSE);
GotoXY(2,2);
GetDir(Cluster,FilePtr,SONGPAGE,FILE,TRUE);
GotoXY(2,2);
while(1)
		{
		while(!is_char(debugport)) WDR();
		ch = rx_char(debugport);
		if (ch == '2')
		   {
		   FilePtr++;
		   if (FilePtr >= MaxFile) FilePtr--;
		   if ((FilePtr / SONGPAGE) != Page)
		   	  {
			  Page = FilePtr / SONGPAGE;
			  ClrScr();
			  GotoXY(1,1);
			  tx_string(debugport,"--Song List --\0");
			  GotoXY(2,2);
			  GetDir(Cluster,FilePtr,SONGPAGE,FILE,TRUE);
			  }
		   GotoXY(1,(FilePtr%SONGPAGE)+2);
		   }

		if (ch == '8')
		   {
		   FilePtr--;
		   if (FilePtr < 0) FilePtr = 0;
		   if ((FilePtr / SONGPAGE) != Page)
		   	  {
			  FilePtr -= (SONGPAGE - 1);
			  Page = FilePtr / SONGPAGE;
			  ClrScr();
			  GotoXY(1,1);
			  tx_string(debugport,"--Song List --\0");
			  GotoXY(2,2);
			  GetDir(Cluster,FilePtr,SONGPAGE,FILE,TRUE);
			  FilePtr += (SONGPAGE - 1);
			  }
		   GotoXY(1,(FilePtr%SONGPAGE)+2);
		   }
		   
		if (ch == '6')
		   {
		   FilePtr--;
		   	   do 
		   	   {
			   FilePtr++;
			   if (FilePtr > MaxFile) break;
			   ClrScr();
			   GotoXY(1,1);
			   GetDir(Cluster,FilePtr,1,FILE,FALSE);
		   	   tx_string(debugport,"-Song Playing-\0");  
			   GotoXY(1,3); 
		   	   sprintf(&text[0],"%s\0",&File.Name[0]);
		   	   tx_string(debugport,&text[0]);	
			   GotoXY(2,5);
   			   sprintf(&text[0],"Percent = 0  \0");
   			   tx_string(debugport,&text[0]);	
			   }
		   	   while(PlayMP3());
			   
		   ClrScr();
		   GotoXY(1,1);
		   tx_string(debugport,"--Song List --\0");
		   GotoXY(2,2);
		   GetDir(Cluster,FilePtr,SONGPAGE,FILE,TRUE);
		   GotoXY(1,(FilePtr%SONGPAGE)+2);		   
		   }
		if (ch == '4')
		   {
		   DirBrowser(2);
		   }		  
		}
}

//*************************************
// int PlayMP3(void)
//
// return 0 -> Error
// return 1 -> successfull play
//*************************************
int PlayMP3(void)
{
unsigned char buffer[512];
unsigned long LBA,i,j,NextCluster,TimeOut,ByteSend,Percent;
extern struct FileInfo File;
extern struct BootRecord Boot;

VS1001Init();
ByteSend = 0;
NextCluster = File.FirstClust;
while (1)
 	  {
	  LBA = Clust2LBA(NextCluster);
	  for (i=0;i<Boot.SecPerClust;i++)
	  	  {
		  ata_read_sector_byte(0, LBA+i, 0, 512, (unsigned char*)&buffer);
		  for (j=0;j<Boot.BytesPerSec;j=j+32)
			  {
			  TimeOut = 0;
			  while (!(PORT_MP3_IN & DREQ))
			  		{
					WDR();
					if (DoTask((int)Percent) == ABORT) return 0;
					TimeOut++;
					if (TimeOut > 100000) return 0; // VS1001 is jam
					}	
			  VS1001_Stream(&buffer[j],32);
			  }
		  }
	  ByteSend += (Boot.BytesPerSec * Boot.SecPerClust);
	  Percent = (ByteSend / (File.FileSize / 100));
	  NextCluster = GetNextCluster(NextCluster);
	  if (NextCluster > CLUST_RSRVD) break;
	  }
return 1;
}

//*************************************
// void DoTask(int Percent)
//*************************************
int DoTask(int Percent)
{
extern unsigned char text[255];
static int LastPercent;

if ((Percent != LastPercent) && (Percent < 100))
   {
   LastPercent = Percent;
   GotoXY(2,5);
   sprintf(&text[0],"Percent = %d\0",Percent);
   tx_string(debugport,&text[0]);	
   }
   
if (is_char(debugport))
   {
   if (rx_char(debugport) == '5') return ABORT;
   }  
return OK;
}	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -