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

📄 wav.c~

📁 以AVRMEGA32播放SD卡中的WAV音乐
💻 C~
字号:
/*

 09-4-4完成FAT的初始化,读取分区表,启动扇区
 09-4-5完成FAT表。文件查找
 09-4-6完成双缓冲播放//88.2K不卡(24M)








*/
#include <mega32.h>
#include <delay.h>  
#include "sd.h"
#include "fat.h"  
#include "type.h"


#define ClockCPU	8000000
#define Baud		2400//串口




extern u8     bf[512];
extern u8      secpclus; //每籁包函扇区数
extern u16     fr_fatsec;   //FAT表首扇区
extern u16        rodir;       // 根目录首扇区
extern u8      firstrodirclus; //根目录的首籁号
extern u32     filefirstclus;   //文件首籁号   
extern u32     filesize;//文件大小(字节)
u16   totalsongs; 
u16    flag,j,f=0;  
u8      chanal;//文件声道数 
u8      timer;
u8 bf1[1024];
///================

 void UART_Init(void)
{UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRL = (ClockCPU/Baud)/16-1;	
}


//----------------------------


//==============================
void TransmitByte( u8 data )
{
	while ( !(UCSRA & (1<<5)) );
	UDR = data;
}

//-------------------------------------------
//中断服务
//主要是重设定时器初值,放出声音,检测是否要读SD卡
//------------------------------------------
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{ 
 TCNT0=timer+2;
 OCR1AL=bf1[j];
 if(chanal==2)j++;//立体声时就要用下个数给下个声道
 OCR1BL=bf1[j];
 j++;
if(j==512){flag=1;} //要读缓冲2
if(j==1024)
        {j=0;
        flag=2;  //要读缓冲2
        }    
}

//-----------------------------------------------------
//播放函数
//-----------------------------------------------------

void PlayMusicwithKey()//播放音乐函数,一旦执行不会退出
{       
				
	uint8 i;	//循环变量			
			
	u32     *p;			
        uint32 totalsect;//文件拥有的扇区数	
	uint16 leftbytes;//剩余字节			
	uint16 songs=1;				

        
next:        
        #asm("cli")
	Search(&songs);		//找到相应的文件	

         TransmitByte(songs);   //文件首籁号 
         TransmitByte(filefirstclus>>24);   //文件首籁号   
         TransmitByte(filefirstclus>>16);   //文件首籁号 
         TransmitByte(filefirstclus>>8);   //文件首籁号 
         TransmitByte(filefirstclus);   //文件首籁号 

        
	
	
	delay_ms(1000);
	delay_ms(1000);
		
	totalsect = filesize/512; //计算扇区数		
	leftbytes = filesize%512; //计算剩余的字节数	
	i=0;
	flag=0;
	FAT_LoadPartCluster(filefirstclus,0,bf1);
	chanal=bf1[22];//读取声道数 
	p= &bf1[24]; 
	FAT_LoadPartCluster(filefirstclus,1,&bf1[512]); 
	//读取WAV文件头,取得采样率,计算定时器初值
	if(ClockCPU/(*p)>255)
	{
	 if(ClockCPU/(*p*8)>255)
	  {if(ClockCPU/(*p*64)>255)while(1);
	  else {timer=0xff-ClockCPU/(*p*64); 
	       TCCR0=0x03; 
	        }
	  }
	else {timer=0xff-ClockCPU/(*p*8);
	       TCCR0=0x02;
	       }
	}
	
	else {timer=0xff-ClockCPU/(*p); 
	       TCCR0=0x01;
	      }
	
	filefirstclus=FAT_NextCluster1(filefirstclus); 
	#asm("sei") 
	flag=0;
	j=0;
	while(1)
	{       
                if(flag==1)
                                {   
                                FAT_LoadPartCluster(filefirstclus,i,bf1);
	                       
	                        flag=0;
	                              
	                       i++;
	                                if(i==secpclus){i=0;      
                                            filefirstclus=FAT_NextCluster1(filefirstclus);
                                                    if(filefirstclus== 0x0fffffff || filefirstclus== 0x0ffffff8 )//如果无后续簇则结束
		                                                {
				                                songs++;
				                                if(songs>totalsongs)songs=1; 
				                                goto next;
		                                                }
                                                    }   
	                        }
        
	            if(flag==2) { 
	                           FAT_LoadPartCluster(filefirstclus,i,&bf1[512]);   
	                        flag=0;
	                        
	                        i++;
	                        if(i==secpclus){i=0;      
                                filefirstclus=FAT_NextCluster1(filefirstclus);
                                                if(filefirstclus== 0x0fffffff || filefirstclus== 0x0ffffff8 )//如果无后续簇则结束
		                                        {
				                songs++;
				                if(songs>totalsongs)songs=1; 
				                goto next;
		                                        }
	                                          }         
	                        } 
	                        DDRA=0x00;
	                        PORTA=0XFF;
	                        if(PINA!=0xff)
	                          { #asm("cli")
	                                  delay_ms(100);
	                                  if(PINA!=0xff)
	                                         {  f=PINA; 
	                                          while(PINA!=0xff);   
	                                          
	                                          if(f==0xfe)
	                                           {songs++;
	                                           if(songs>totalsongs)songs=1;
	                                           goto next;
	                                           }
	                                           if(f==0xfd)
	                                           {songs--;
	                                           if(songs==0)songs=1;
	                                           goto next;
	                                           }
	                                         
	                                         }
	                                         #asm("sei")
	                                 }
	                                 
        }
}

 







void main(void)
{         
	uint8 retry = 0;
        DDRA= 0xff;//初始化端口
	PORTA = 0xff;
	DDRC= 0xff;//初始化端口
	PORTC = 0xff;
	DDRD= 0xff;//初始化端口
	PORTD = 0xff; 	
	delay_ms(10);

        TCCR0=0x02;
        TCNT0=0xbc;
        OCR0=0x00;

        TCCR1A=0xA2;
        TCCR1B=0x19;
        TCNT1H=0x00;
        TCNT1L=0x00;
        ICR1H=0x0;
        ICR1L=0xff;
        OCR1AH=0x00;
        OCR1AL=30;
        OCR1BH=0x00;
        OCR1BL=30;  
        
        TIMSK=0x01;

        ACSR=0x80;

        MMC_SD_Init ();//初始化spi口 
        UART_Init();

	while(MMC_SD_Reset())//初始化SD卡
	{
		retry++;
		if(retry>20)
		{     DDRA=0XFF;
			while(1)
			{for (retry=0;retry<0xff;retry++)
				{TransmitByte(0X22); }
	
			}
		}
	} 
      delay_ms(0xff); 
	if(FAT_Init())//初始化文件系统 支持FAT16和FAT32	
	{
	      DDRD=0XFF;
		while(1)
		{
		TransmitByte(0xf2);
		}

	}  
  	
totalsongs=0;
Search(&totalsongs);//找WAV文件总数 
/*
TransmitByte(totalsongs); 
for(retry=1;retry<totalsongs;retry++) 
         {Search(&retry); 
         TransmitByte(retry);   //文件首籁号 
         TransmitByte(filefirstclus>>24);   //文件首籁号   
         TransmitByte(filefirstclus>>16);   //文件首籁号 
         TransmitByte(filefirstclus>>8);   //文件首籁号 
         TransmitByte(filefirstclus);   //文件首籁号 

         }
*/
PlayMusicwithKey();

while (1)
      {
      // Place your code here
        //TransmitByte(0xf0);
      };
}

⌨️ 快捷键说明

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