📄 vs1001._c
字号:
#include <iom128v.h>
#include <macros.h>
#include "vs1001.h"
#include "generic.h"
#include "remote.h"
//*************************************
// unsigned char byte void WriteByteSPI(unsigned char byte)
//*************************************
unsigned char WriteByteSPI(unsigned char byte)
{
SPDR = byte;
while(!(SPSR & (1<<SPIF))); // 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)
{
while(Qte--)
{
PORT_MP3_OUT |= BSYNC; // BSYNC High
WDR();
WDR();
SPDR = *Data++;
delay_us(1);
PORT_MP3_OUT &= ~BSYNC; // BSYNC Low
while(!(SPSR & (1<<SPIF))); // Wait Queue off
}
}
//*************************************
// void VS1001Init(void)
//*************************************
void VS1001Init(unsigned char HardReset)
{
unsigned int buf[2];
unsigned char null[32];
int i;
if (HardReset == TRUE)
{
PORT_MP3_DDR = XRES | BSYNC | XCS | MOSI | SCK | SS;
SPCR = 0x51; // SPI ENABLE, MSB FIRST, MASTER,
// CPOL=0, CPHA=0, CLK/16
PORT_MP3_OUT = XRES + XCS; // XCS 1 - RESET 1 - BSYNC 0
delay_ms(5);
PORT_MP3_OUT &= ~XRES; // Reset is 0
delay_ms(5);
PORT_MP3_OUT |= XRES;; // Reset is 1
delay_ms(5);
}
for (i=0;i<32;i++) null[i] = 0;
delay_ms(5);
VS1001_Stream(&null[0],32); // 32 null's
delay_ms(200);
buf[0] = 0x0004;
VS1001_Write(0,1,buf);
delay_ms(2);
while(!(PORT_MP3_IN & DREQ)); // wait for DREQ
buf[0] = 0x0000;
VS1001_Write(0,1,buf);
delay_ms(2);
while(!(PORT_MP3_IN & DREQ)); // wait for DREQ
buf[0] = 0x9800; // Clock 12.288Mhz
VS1001_Write(3,1,buf);
VS1001_Stream(&null[0],16); // 16 null's
VS1001_SetVolume(0,0);
}
//*************************************
// void Reg_Test(void)
//*************************************
unsigned char Reg_Test()
{
unsigned int buf[20];
unsigned int j;
for (j=0;j<20;j++)
{
buf[0] = j*256+j;
VS1001_Write(3,1,buf);
buf[0] = 0xffff;
VS1001_Read(3,1,buf);
if (buf[0] != j*256+j) return 0;
}
return 1;
}
//*************************************
// void Sine_On(unsigned char Freq)
//*************************************
void Sine_On(unsigned char Freq)
{
unsigned char i,buf[4];
buf[0] = 0x53;
buf[1] = 0xef;
buf[2] = 0x6e;
buf[3] = Freq;
VS1001_Stream(&buf[0],4);
buf[0] = 0x00;
buf[1] = 0x00;
buf[2] = 0x00;
buf[3] = 0x00;
VS1001_Stream(&buf[0],4);
}
//*************************************
// void Sine_Off(void)
//*************************************
void Sine_Off(void)
{
unsigned char i,buf[4];
buf[0] = 0x45;
buf[1] = 0x78;
buf[2] = 0x69;
buf[3] = 0x74;
VS1001_Stream(&buf[0],4);
buf[0] = 0x00;
buf[1] = 0x00;
buf[2] = 0x00;
buf[3] = 0x00;
VS1001_Stream(&buf[0],4);
}
//*************************************
// void Sine_Sweep(void)
//*************************************
void Sine_Sweep(void)
{
unsigned char i;
for (i=48;i<119;i++)
{
Sine_Off();
Sine_On(i);
delay_ms(25);
}
Sine_Off();
}
//*************************************
// 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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -