|
晶体使用24.576;SDINEW模式.
VS1011.C
unsigned char WriteByteSPI(unsigned char byte)
{
SPDR = byte;
while(!(SPSR & (1<<7))); // Wait Queue off
return SPDR;
}
//*************************************
// void VS1001_Read(unsigned char Address,int Count,unsigned int *pData)
//*************************************
void VS1001_Read(unsigned char Address,int Count,unsigned int *pData)
{
PORT_MP3_OUT &= ~XCS; // XCS Low
WriteByteSPI(VS1001_READ);
WriteByteSPI(Address);
while(Count--)
{
*pData = WriteByteSPI(0) << 8;
*pData++ |= WriteByteSPI(0);
}
PORT_MP3_OUT |= XCS; // XCS High
delay_us(5);
}
//*************************************
// void VS1001_Write(unsigned char Address,int Count,unsigned int *pData)
//*************************************
void VS1001_Write(unsigned char Address,int Count,unsigned int *pData)
{
PORT_MP3_OUT &= ~XCS; // XCS Low
WriteByteSPI(VS1001_WRITE);
WriteByteSPI(Address);
while(Count--)
{
WriteByteSPI((unsigned char) ((*pData) >> 8));
WriteByteSPI((unsigned char) *pData);
}
PORT_MP3_OUT |= XCS; // XCS High
delay_us(5);
}
//*************************************
// void VS1001_Stream(unsigned char Data,unsigned char Qte)
//*************************************
void VS1001_Stream(unsigned char *Data,unsigned char Qte)
{
PORT_MP3_OUT &= ~BSYNC; // BSYNC Low
while((Qte --) > 0)
WriteByteSPI(*Data++);
PORT_MP3_OUT |= BSYNC; // BSYNC High
}
//*************************************
// void VS1001Init(void)
//*************************************
void VS1001Init(unsigned char HardReset)
{
unsigned int buf[2];
unsigned char bufc[4];
if (HardReset == TRUE)
{
PORT_MP3_DDR = XRES | BSYNC | XCS | MOSI | SCK | SS;
SPCR = 0x50; // SPI ENABLE, MSB FIRST, MASTER,
// CPOL=0, CPHA=0, CLK/16
PORT_MP3_OUT = XRES + XCS + BSYNC; // XCS 1 - RESET 1 - BSYNC 1
delay_ms(5);
PORT_MP3_OUT &= ~XRES; // Reset is 0
delay_ms(5);
PORT_MP3_OUT |= XRES;; // Reset is 1
delay_ms(5);
}
while(!(PORT_DREQ_IN & DREQ)); // wait for DREQ
buf[0] = 0x0804;
VS1001_Write(0,1,buf);
delay_ms(2);
while(!(PORT_DREQ_IN & DREQ)); // wait for DREQ
buf[0] = 0xac45;
VS1001_Write(5,1,buf); // 44kHz
while(!(PORT_DREQ_IN & DREQ)); // wait for DREQ
VS1001_SetVolume(0,0);
bufc[0] = 0x00;
bufc[1] = 0x00;
bufc[2] = 0x00;
bufc[3] = 0x00;
while(!(PORT_DREQ_IN & DREQ)); // wait for DREQ
VS1001_Stream(&bufc[0],4);
}
//*************************************
// void Working_Test(void)
//*************************************
void Working_Test(void)
{
unsigned int i;
for(i=0;i<10;i++)
{
while(!(PORT_DREQ_IN & DREQ)); // wait for DREQ
VS1001_SetVolume(0,0);
delay_ms(500);
while(!(PORT_DREQ_IN & DREQ)); // wait for DREQ
VS1001_SetVolume(255,255);
delay_ms(500);
}
}
//*************************************
// void Reg_Test(void)
//*************************************
unsigned char Reg_Test()
{
unsigned int buf;
unsigned int j;
for (j=0;j<20;j++)
{
buf = j*256+j;
while(!(PORT_DREQ_IN & DREQ)); // wait for DREQ
VS1001_Write(3,1,&buf);
buf = 0xffff;
VS1001_Read(3,1,&buf);
if (buf != j*256+j) return 0;
}
return 1;
}
//*************************************
// void Sine_On(unsigned char Freq)
//*************************************
void Sine_On(unsigned int freq)
{
unsigned char buf[4];
buf[0] = 0x53;
buf[1] = 0xef;
buf[2] = 0x6e;
buf[3] = ((freq * 128) / 44100) & 0x1f;
while(!(PORT_DREQ_IN & DREQ)); // wait for DREQ
VS1001_Stream(&buf[0],4);
buf[0] = 0x00;
buf[1] = 0x00;
buf[2] = 0x00;
buf[3] = 0x00;
while(!(PORT_DREQ_IN & DREQ)); // wait for DREQ
VS1001_Stream(&buf[0],4);
}
//*************************************
// void Sine_Off(void)
//*************************************
void Sine_Off(void)
{
unsigned char buf[4];
buf[0] = 0x45;
buf[1] = 0x78;
buf[2] = 0x69;
buf[3] = 0x74;
while(!(PORT_DREQ_IN & DREQ)); // wait for DREQ
VS1001_Stream(&buf[0],4);
//buf[0] = 0x00;
//buf[1] = 0x00;
//buf[2] = 0x00;
//buf[3] = 0x00;
//while(!(PORT_DREQ_IN & DREQ)); // wait for DREQ
//VS1001_Stream(&buf[0],4);
}
//*************************************
// void Sine_Sweep(void)
//*************************************
void Sine_Sweep(void)
{
unsigned int buf[2];
unsigned int i;
while(!(PORT_DREQ_IN & DREQ)); // wait for DREQ
buf[0] = 0x0820;
VS1001_Write(0,1,buf);
delay_ms(2);
while(!(PORT_DREQ_IN & DREQ)); // wait for DREQ
VS1001_SetVolume(0,0);
for (i=0;i<20000;i+=1000)
{
Sine_Off();
Sine_On(i);
delay_ms(300);
}
Sine_Off();
VS1001Init(1);
}
//*************************************
// void VS1001_SetVolume(unsigned char Left,unsigned char Right)
//*************************************
void VS1001_SetVolume(unsigned char Left,unsigned char Right)
{
unsigned int buf[2];
buf[0] = (((unsigned int)Left) << 8) | ((unsigned int)Right);
VS1001_Write(11,1,buf);
}
主程序节选,MP3文件放在FLASH中.
void PlayMP3(void)
{
unsigned char i,temp[32];
unsigned long j=0;
while(j < 0x2e1f)
{
for(i=0;i<32;i++)
{
temp = __load_program_memory(&mp3[j++]);
//USART_Putchar(temp);
}
while(!(PORT_DREQ_IN & DREQ)); // wait for DREQ
VS1001_Stream(temp,32);
}
}
//*************************************
// void main(void)
//*************************************
void main(void)
{
int i;
MCUCR = 0x80;
DDRF = 0x03;
PORTF = 0xff;
USART_Init();
while(1)
{
USART_Putstr("VS1011 initing....");
VS1001Init(HARD);
USART_Putstr("ok
");
PORTF &= ~0x02;
USART_Putstr("VS1011 working testing....");
Working_Test();
PORTF |= 0x02;
USART_Putstr("ok
");
USART_Putstr("VS1011 reg testing....");
if(Reg_Test())
USART_Putstr("ok
");
else
USART_Putstr("fail
");
VS1001_SetVolume(100,100);
USART_Putstr("VS1011 sine sweeping....");
for(i=0;i<2;i++)
Sine_Sweep();
USART_Putstr("ok
");
USART_Putstr("VS1011 mp3 playing....");
//VS1001Init(0);
PlayMP3();
USART_Putstr("ok
");
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!
|