?? 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 + -