📄 vs1003.c
字号:
/****************************************Copyright (c)**************************************************
** qsw
**
**
**
**--------------File Info-------------------------------------------------------------------------------
** File name: vs1003.h
** Last modified Date: 2007.11.21
** Last Version: V1.0
** Descriptions: vs1003驱动硬件包: 配置文件
**
**
**------------------------------------------------------------------------------------------------------
** Created by: qsw
** Created date: 2007.11.21
** Version: V1.0
** Descriptions: The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include "main.h"
uint16 VOL_BUFFER[30] =
{
0x9191,0x8c8c,0x8787,0x8282,0x7d7d,0x7878,0x7373,0x6e6e,0x6969,0x6464,
0x5f5f,0x5a5a,0x5555,0x5050,0x4b4b,0x4646,0x4141,0x3c3c,0x3737,0x3232,
0x2d2d,0x2828,0x2323,0x1e1e,0x1919,0x1414,0x0f0f,0x0a0a,0x0505,0x0000
};
/**************************************************************************************************
**Name:
**Function:
**Input:
**Output:
**************************************************************************************************/
void VS1003_Delay_Nmicrosecond(uint32 t)
{
uint32 i;
for(i=0;i<=t;i++);
}
/**************************************************************************************************
**Name: void VS1003_Power()
**Function: turn off the card's power, and turn on
**Input: NULL
**Output: NULL
**************************************************************************************************/
void VS1003_Power(uint8 state)
{
switch(state)
{
case ON:
VS1003_POWER_SET(); /* turn on power of vs1003 */
break;
case OFF:
SPI0_SCK_CLR(); /*SCK to zero */
SPI0_MISO_CLR(); /*MISO to zero */
SPI0_MOSI_CLR(); /*MOSI to zero */
SPI0_CS_CLR(); /* CS to zero */
VS1003_XRESET_CLR(); /*RST to zero */
VS1003_XDCS_CLR(); /*XDCS to zero*/
VS1003_POWER_CLR(); /*turn off power of sd card */
break;
}
}
/**************************************************************************************************
**Name: void SPI0_SrByte(INT8U byte)
**Function: send a byte by SPI0 interface
**Input: INT8U byte: the byte that will be send
**Output: NULL
**************************************************************************************************/
uint8 SPI0_SrByte(INT8U byte)
{
S0PDR = byte;
OS_ENTER_CRITICAL();
while(0 == (S0PSR & 0x80)); /* wait for SPI0F being set, that is, wait for finishing of data being send */
OS_EXIT_CRITICAL();
return(S0PDR);
}
/**************************************************************************************************
**Name: VS1003_WriteReg(uint8 addr, uint16 dat)
**Function: write reg address and data
**Input: addr dat
**Output:
**************************************************************************************************/
void VS1003_WriteReg(uint8 addr,uint16 dat)
{
VS1003_XDCS_SET(); /*write reg of the vs1003*/
SPI0_CS_CLR(); /*slect the sleve machine*/
SPI0_SrByte(VS1003_WRITE_COM);
SPI0_SrByte(addr);
SPI0_SrByte((uint8)(dat>>8));
SPI0_SrByte((uint8)(dat));
SPI0_CS_SET(); /*free the sleve machine*/
}
/**************************************************************************************************
**Name: uint16 VS1003_ReadReg(uint16 addr)
**Function: read reg address and data
**Input: addr
**Output: dat
**************************************************************************************************/
uint16 VS1003_ReadReg(uint8 addr)
{
uint16 dat = 0x0000;
uint16 datL = 0x0000;
VS1003_XDCS_SET();
SPI0_CS_CLR();
SPI0_SrByte(VS1003_READ_COM);
SPI0_SrByte(addr);
dat = SPI0_SrByte(0xff);
dat = dat<<8;
datL = SPI0_SrByte(0xff);
dat =dat|datL;
SPI0_CS_SET();
return dat;
}
/**************************************************************************************************
**Name: void VS1003_WriteDat(unsigned char dat)
**Function: write music dat
**Input: dat
**Output: NULL
**************************************************************************************************/
void VS1003_WriteDat(uint8 dat)
{
VS1003_XDCS_CLR();
SPI0_CS_CLR();
SPI0_SrByte(dat);
SPI0_CS_SET();
}
/**************************************************************************************************
**Name: void VS1003_HardWareInit()
**Function: initialize the hardware condiction that access sd card
**Input: NULL
**Output: NULL
**************************************************************************************************/
void SPI0_Init(uint8 speed)
{
VS1003_POWER_GPIO();
VS1003_POWER_OUT();
SPI0_CS_GPIO();
SPI0_CS_OUT();
VS1003_XDCS_GPIO();
VS1003_XDCS_OUT();
VS1003_XRESET_GPIO();
VS1003_XRESET_OUT();
VS1003_DREQ_GPIO();
VS1003_DREQ_IN();
VS1003_Power(ON);
PINSEL0 &= ~((0x03 << 8) + (0x03 << 10) + (0x03 << 12));
PINSEL0 |= (0x01 << 8) + (0x01 << 10) + (0x01 << 12);
S0PCCR = speed; /* 设置SPI0时钟分频值为8 Set the value of dividing frequency to 8 */
S0PCR = (0 << 3) | // CPHA = 0, 数据 在SCK 的第一个时钟沿采样
(1 << 4) | // CPOL = 1, SCK 为低有效
(1 << 5) | // MSTR = 1, SPI 处于主模式
(0 << 6) | // LSBF = 0, SPI 数据传输MSB (位7)在先
(0 << 7); // SPIE = 0, SPI 中断被禁止
}
/**************************************************************************************************
**Name: void VS1003_HardWareInit()
**Function: initialize the hardware condiction that access sd card
**Input: NULL
**Output: NULL
**************************************************************************************************/
void VS1003_HwRst(void)
{
VS1003_XRESET_CLR();
//delay /*delay tiem >1.35ms*/
VS1003_XRESET_SET();
//VS1003_SoftWareInit();
}
/**************************************************************************************************
**Name: void VS1003_HardWareInit()
**Function: initialize the hardware condiction that access sd card
**Input: NULL
**Output: NULL
**************************************************************************************************/
void VS1003_SfRst(void)
{
VS1003_WriteReg(0x00,0x0804); //写复位
while(VS1003_DREQ_STATE() != VS1003_DREQ); //等待软件复位结束
VS1003_WriteReg(VS1003_SPI_CLOCKF,0x9800); //设置时钟3倍频
VS1003_WriteReg(VS1003_SPI_AUDATA,0xB199); //采样率48k,立体声
VS1003_WriteReg(VS1003_SPI_BASS,0x0055); //设置重音
//VS1003_WriteReg(VS1003_SPI_VOL,2020); //设置音量
VS1003_WriteDat(0); //向vs1003发送四个字节
VS1003_WriteDat(0); //无效数据,启动spi
VS1003_WriteDat(0);
VS1003_WriteDat(0);
}
/**************************************************************************************************
**Name: uint8 VS1003_Init()
**Function: vs1003_Init
**Input: dat
**Output: sucess :1 FALSE:0
**************************************************************************************************/
uint8 VS1003_Init(void)
{
SPI0_Init(8);
SPI0_CS_SET();
VS1003_XDCS_SET();
VS1003_XRESET_CLR();
VS1003_Delay_Nmicrosecond(99999);
VS1003_XRESET_SET(); //使能芯片
VS1003_Delay_Nmicrosecond(99999);
while(VS1003_DREQ_STATE() != VS1003_DREQ);
VS1003_WriteReg(0x00,0x0800);
while(VS1003_DREQ_STATE() != VS1003_DREQ);
VS1003_WriteReg(0x03,0x9800);
while(VS1003_DREQ_STATE() != VS1003_DREQ);
VS1003_WriteReg(0x05,0xB199);
while(VS1003_DREQ_STATE() != VS1003_DREQ);
VS1003_WriteReg(0x02,0x0099);
while(VS1003_DREQ_STATE() != VS1003_DREQ);
VS1003_WriteReg(VS1003_SPI_VOL ,0x3232);
while(VS1003_DREQ_STATE() != VS1003_DREQ);
return 1;
}
/**************************************************************************************************
**Name:
**Function:
**Input:
**Output:
**************************************************************************************************/
void VS1003_Test(void)
{
//VS1003_HwRst();
VS1003_SfRst();
SPI0_SrByte(0xff); //发送一个字节的无效数据,启动SPI传输
SPI0_CS_SET();
VS1003_XDCS_SET();
VS1003_XRESET_SET();
//delay100ms
VS1003_WriteReg(VS1003_SPI_MODE,0x0820); //enter vs1003 test model
while (VS1003_DREQ_STATE()== 0); //wait for DREQ Highlevel
//向vs1003发送正弦测试命令:0x53 0xef 0x6e n 0x00 0x00 0x00 0x00
//其中n = 0x24, 设定vs1003所产生的正弦波的频率值,具体计算方法见vs1003的datasheet
VS1003_WriteDat(0x53);
VS1003_WriteDat(0xef);
VS1003_WriteDat(0x6e);
VS1003_WriteDat(0x24);
VS1003_WriteDat(0x00);
VS1003_WriteDat(0x00);
VS1003_WriteDat(0x00);
VS1003_WriteDat(0x00);
VS1003_Delay_Nmicrosecond(50000);
VS1003_Delay_Nmicrosecond(50000);
VS1003_Delay_Nmicrosecond(50000);
VS1003_Delay_Nmicrosecond(50000);
//程序执行到这里后应该能从耳机听到一个单一频率的声音
//退出正弦测试
VS1003_WriteDat(0x45);
VS1003_WriteDat(0x78);
VS1003_WriteDat(0x69);
VS1003_WriteDat(0x74);
VS1003_WriteDat(0x00);
VS1003_WriteDat(0x00);
VS1003_WriteDat(0x00);
VS1003_WriteDat(0x00);
VS1003_Delay_Nmicrosecond(50000);
VS1003_Delay_Nmicrosecond(50000);
VS1003_Delay_Nmicrosecond(50000);
VS1003_Delay_Nmicrosecond(50000);
VS1003_XDCS_SET();
//再次进入正弦测试并设置n值为0x44,即将正弦波的频率设置为另外的值
VS1003_WriteDat(0x53);
VS1003_WriteDat(0xef);
VS1003_WriteDat(0x6e);
VS1003_WriteDat(0x44);
VS1003_WriteDat(0x00);
VS1003_WriteDat(0x00);
VS1003_WriteDat(0x00);
VS1003_WriteDat(0x00);
VS1003_Delay_Nmicrosecond(50000);
VS1003_Delay_Nmicrosecond(50000);
VS1003_Delay_Nmicrosecond(50000);
VS1003_Delay_Nmicrosecond(50000);
//退出正弦测试
VS1003_WriteDat(0x45);
VS1003_WriteDat(0x78);
VS1003_WriteDat(0x69);
VS1003_WriteDat(0x74);
VS1003_WriteDat(0x00);
VS1003_WriteDat(0x00);
VS1003_WriteDat(0x00);
VS1003_WriteDat(0x00);
VS1003_Delay_Nmicrosecond(50000);
VS1003_Delay_Nmicrosecond(50000);
VS1003_Delay_Nmicrosecond(50000);
VS1003_Delay_Nmicrosecond(50000);
VS1003_Delay_Nmicrosecond(50000);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -