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

📄 vs1003b.c

📁 MG64+VS1003+SDCARD+nokia5110 之多的MP3
💻 C
字号:
/*******************************************************************/
/*          VS1003 diriver for  Mega8 MP3 Player                   */
/*                                                                 */
/* Platform   : AVRStudio4.13 b528 + WinAVR20070122                */
/*              optimize -0s                                       */
/* Author     : bozai(Zhang Qibo)                                  */
/* E-mail     : sudazqb@163.com                                    */
/* MSN        : zhangqibo_1985@hotmail.com                         */
/* Date       : 2006-05-09                                         */
/*******************************************************************/
/*2007-11-22: add bass tremble control                             */
/*2007-10-21: add new functions,                                   */
/*2007-10-19: use new delay function                               */
/*2007-05-04: add slow start up code, and add enough delay         */
/*2007-04-21:                                                      */


#include<avr/io.h>
#include"VS1003B.h"
#include<util/delay.h>

#define uchar unsigned char
#define uint  unsigned int

void VS1003B_SPI_Low(void);//low spi clk
void VS1003B_SPI_High(void);//high spi clk
unsigned char VS1003B_WriteByte(unsigned char CH);//spi write a byte
void VS1003B_WriteCMD(unsigned char addr, unsigned int dat);//write register 
unsigned int VS1003B_ReadCMD(unsigned char addr);//read register 



/* Run at the loweset spi speed, clk should below 12.288M/6 */
void VS1003B_SPI_Low(void)
{
	SPCR =   _BV(SPE)|_BV(MSTR)|_BV(SPR1)|_BV(SPR0);
	SPSR &= ~_BV(SPI2X);
}

/* High speed, should not exceed 49.512MHz/6 */
void VS1003B_SPI_High(void)
{
	SPCR =  _BV(SPE)|_BV(MSTR);
	SPSR |= _BV(SPI2X);
}

/* write one byte to vs1003 */
unsigned char VS1003B_WriteByte(unsigned char CH)
{   
	SPDR = CH;
	while(!(SPSR & _BV(SPIF)));
	return SPDR;
}

/* read one byte from vs1003 */
unsigned char VS1003B_ReadByte()
{
    	SPDR = 0xff;
	while(!(SPSR & _BV(SPIF)));
	return SPDR;
}

/*写寄存器,参数,地址和数据   config register */
void VS1003B_WriteCMD(unsigned char addr, unsigned int dat)
{
	VS1003B_XDCS_H();
	VS1003B_XCS_L();
	VS1003B_WriteByte(0x02);
	VS1003B_WriteByte(addr);
	VS1003B_WriteByte(dat>>8);
	VS1003B_WriteByte(dat);
	VS1003B_XCS_H();
}

/* 读寄存器,参数 地址 返回内容  read register */
unsigned int VS1003B_ReadCMD(unsigned char addr)
{
	unsigned int temp;
	VS1003B_XDCS_H();
	VS1003B_XCS_L();
	VS1003B_WriteByte(0x03);
	VS1003B_WriteByte(addr);
	temp = VS1003B_ReadByte();
	temp <<= 8;
	temp += VS1003B_ReadByte();
	VS1003B_XCS_H();
	return temp;
}

/* fill 2048 zero */
void VS1003B_Fill2048Zero()
{
	unsigned char i,j;
	for(i=0;i<64;i++)
	{
		VS1003B_XDCS_L();
		while(VS1003B_NeedData()==0);
		for(j=0;j<32;j++)
		{
			VS1003B_WriteByte(0x00);
		}
		VS1003B_XDCS_H();
	}
}

/* Write 32bytes data */
void VS1003B_Write32B(unsigned char * buf)
{
	unsigned char n = 32;
	VS1003B_XDCS_L();
	while(n--)
	{
		VS1003B_WriteByte(*buf++);
	}
	VS1003B_XDCS_H();
}

/**********  Initialization, 1 means fail, 0 OK ***************/
unsigned char VS1003B_Init()
{
	unsigned char retry;
	PORT_INI();			/* Prot Initialize */

	VS1003B_XRESET_L();	/* A hardware reset */
	_delay_ms(20);
	VS1003B_XRESET_H();

	VS1003B_SPI_Low();	/* Low initialize spi clock */
	_delay_ms(20);

	retry=0;

	while(VS1003B_ReadCMD(0x03) != CLOCK_REG)	/* set PLL register */
	{
		VS1003B_WriteCMD(0x03,CLOCK_REG);
		if(retry++ >10 )return 1;
	}

	_delay_ms(20);
	
	VS1003B_WriteCMD(0x05,0x000a);
	
	retry=0;
	while(VS1003B_ReadCMD(0x0b) != 0xfefe)	/* set Volume to minimum */
	{
		VS1003B_WriteCMD(0x0b,0xfefe);
		if(retry++ >10 )return 1;
	}

	VS1003B_WriteCMD(0x05,0xac45);			/* Soft start */

	retry=0;
	while(VS1003B_ReadCMD(0x0b) != DEFAULT_VOLUME)	/* Set volume to default value */
	{
		VS1003B_WriteCMD(0x0b,DEFAULT_VOLUME);
		if(retry++ >10 )return 1;
	}

	retry=0;
	while(VS1003B_ReadCMD(0x00) != 0x0800)	/* set mode register */
	{
		VS1003B_WriteCMD(0x00,0x0800);
		if(retry++ >10 )return 1;
	}

	_delay_ms(1);

	retry=0;
	while(VS1003B_ReadCMD(0x02) != DEFAULT_BASS_TREMBLE)	/* set bass/tremble register */
	{
		VS1003B_WriteCMD(0x02,DEFAULT_BASS_TREMBLE);
		if(retry++ >10 )return 1;
	}
	_delay_ms(20);

	VS1003B_SoftReset();	/* A soft reset */

	_delay_ms(20);

	VS1003B_SPI_High();		/* High SPI clock, for internal pll has been works now */
	return 0;
}

/* VS1003B soft reset */
void VS1003B_SoftReset()
{
	VS1003B_SPI_High();
	VS1003B_WriteCMD(0x00,0x0804);	/* reset */
	_delay_ms(20);
}

/* get total decode time form last reset */
unsigned int VS1003B_ReadDecodeTime()
{
	VS1003B_SPI_High();
	return VS1003B_ReadCMD(0x04);
}

/* check if the chip need data, 1: need  0: don't */
unsigned char VS1003B_NeedData(void)
{
	if(VS1003B_DREQ_PIN & (1<<VS1003B_DREQ_BIT))return 1;
	else return 0;
}

/* Set volume */
void VS1003B_SetVolume(unsigned int vol)
{
	VS1003B_SPI_High();
	VS1003B_WriteCMD(0x0b,vol);	
}

⌨️ 快捷键说明

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