?? vs1003.c
字號(hào):
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
#include "vs1003.h"
/* Const define -------------------------------------------------------------*/
#define RXNE 0x01
#define TXE 0x02
#define BSY 0x80
/*******************************************************************************
* Function Name : Delay
* Description : Delay
* Input : nTime--delay time
* Output : None
* Return : None
*******************************************************************************/
void Delay(u32 nTime)
{
unsigned int i;
unsigned long j;
for(i = nTime;i > 0;i--)
for(j = 1000;j > 0;j--);
}
/*******************************************************************************
* Function Name : SPIPutChar
* Description : Send one byte by SPI2
* Input : outb--the byte to be sended
* Output : None
* Return : None
*******************************************************************************/
unsigned char SPIPutChar(unsigned char outb)
{
/* Write and Read a byte on SPI interface. */
unsigned char inb;
/* Wait if TXE cleared, Tx FIFO is full. */
while ((SPI2->SR & TXE) == 0);
SPI2->DR = outb;
/* Wait if RNE cleared, Rx FIFO is empty. */
while ((SPI2->SR & RXNE) == 0);
inb = SPI2->DR;
return (inb);
}
/*******************************************************************************
* Function Name : SPIGetChar
* Description : Read a byte from the SPI.
* Input : None.
* Output : None
* Return : The received byte.
*******************************************************************************/
u8 SPIGetChar(void)
{
u8 Data = 0;
/* Wait until the transmit buffer is empty */
while (SPI_GetFlagStatus(SPI2, SPI_FLAG_TXE) == RESET);
/* Send the byte */
SPI_SendData(SPI2, 0xFF);
/* Wait until a data is received */
while (SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE) == RESET);
/* Get the received data */
Data = SPI_ReceiveData(SPI2);
/* Return the shifted data */
return Data;
}
/*******************************************************************************
* Function Name : Mp3SoftReset
* Description : Soft reset the VS1003 chip
* Input : None.
* Output : None
* Return : None
*******************************************************************************/
void Mp3SoftReset(void)
{
Mp3WriteRegister (SPI_MODE, 0x08, 0x04); // 軟件復(fù)位
Delay(1);
while (GPIO_ReadInputData(GPIOA) & MP3_DREQ == 0); // 等待軟件復(fù)位結(jié)束
Mp3WriteRegister(SPI_CLOCKF, 0x98, 0x00); // 設(shè)置vs1003的時(shí)鐘,3倍頻
Mp3WriteRegister (SPI_AUDATA, 0xBB, 0x81); // 采樣率48k,立體聲
Mp3WriteRegister(SPI_BASS, 0x00, 0x55); // 設(shè)置重音
Mp3SetVolume(0,0); // 設(shè)置音量
Delay(1);
//向vs1003發(fā)送4個(gè)字節(jié)無效數(shù)據(jù),用以啟動(dòng)SPI發(fā)送
Mp3SelectData();
SPIPutChar(0);
SPIPutChar(0);
SPIPutChar(0);
SPIPutChar(0);
Mp3DeselectData();
}
/*******************************************************************************
* Function Name : Mp3Reset
* Description : Reset the VS1003 chip
* Input : None.
* Output : None
* Return : None
*******************************************************************************/
void Mp3Reset(void)
{
Mp3PutInReset(); //xReset = 0 復(fù)位vs1003
Delay(100);
SPIPutChar(0xff); //發(fā)送一個(gè)字節(jié)的無效數(shù)據(jù),啟動(dòng)SPI傳輸
Mp3DeselectControl(); //xCS = 1
Mp3DeselectData(); //xDCS = 1
Mp3ReleaseFromReset(); //xRESET = 1
Delay(100); //延時(shí)100ms
while (GPIO_ReadInputData(GPIOA) & MP3_DREQ == 0); //等待DREQ為高
Delay(100);
Mp3SoftReset(); //vs1003軟復(fù)位
}
/*******************************************************************************
* Function Name : VsSineTest
* Description : VS1003 sine test
* Input : None.
* Output : None
* Return : None
*******************************************************************************/
void VsSineTest(void)
{
Mp3PutInReset(); //xReset = 0 復(fù)位vs1003
Mp3Reset();
Delay(1000);//wait(100); //延時(shí)100ms
SPIPutChar(0xff);//發(fā)送一個(gè)字節(jié)的無效數(shù)據(jù),啟動(dòng)SPI傳輸
Mp3DeselectControl();
Mp3DeselectData();
Mp3ReleaseFromReset();
Mp3Reset();
Delay(500);//wait(100);
Mp3SetVolume(50,50);//設(shè)置音量
Mp3WriteRegister(SPI_MODE,0x08,0x20);//進(jìn)入vs1003的測(cè)試模式
Delay(500);
while (GPIO_ReadInputData(GPIOA) & MP3_DREQ == 0); //等待DREQ為高
Mp3SelectData(); //xDCS = 1,選擇vs1003的數(shù)據(jù)接口
//向vs1003發(fā)送正弦測(cè)試命令:0x53 0xef 0x6e n 0x00 0x00 0x00 0x00
//其中n = 0x24, 設(shè)定vs1003所產(chǎn)生的正弦波的頻率值,具體計(jì)算方法見vs1003的datasheet
SPIPutChar(0x53);
SPIPutChar(0xef);
SPIPutChar(0x6e);
SPIPutChar(0x24);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
Delay(1000);
Mp3DeselectData();//程序執(zhí)行到這里后應(yīng)該能從耳機(jī)聽到一個(gè)單一頻率的聲音
//退出正弦測(cè)試
Mp3SelectData();
SPIPutChar(0x45);
SPIPutChar(0x78);
SPIPutChar(0x69);
SPIPutChar(0x74);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
Delay(1000);
Mp3DeselectData();
//再次進(jìn)入正弦測(cè)試并設(shè)置n值為0x44,即將正弦波的頻率設(shè)置為另外的值
Mp3SelectData();
SPIPutChar(0x53);
SPIPutChar(0xef);
SPIPutChar(0x6e);
SPIPutChar(0x44);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
Delay(1000);
Mp3DeselectData();
//退出正弦測(cè)試
Mp3SelectData();
SPIPutChar(0x45);
SPIPutChar(0x78);
SPIPutChar(0x69);
SPIPutChar(0x74);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
Delay(1000);
Mp3DeselectData();
}
/*******************************************************************************
* Function Name : VsRamTest
* Description : Test VS1003's register,if received value is 0x807F,then
indicate the VS1003 is OK.
* Input : None.
* Output : None
* Return : None
*******************************************************************************/
void VsRamTest(void)
{
u16 regvalue ;
regvalue = 0;
Mp3PutInReset();
Delay(100);
SPIPutChar(0xff); //發(fā)送一個(gè)字節(jié)的無效數(shù)據(jù),啟動(dòng)SPI傳輸
Mp3DeselectControl();
Mp3DeselectData();
Mp3ReleaseFromReset();
Delay(100);
Mp3WriteRegister(SPI_MODE,0x08,0x20); // 進(jìn)入vs1003的測(cè)試模式
while (GPIO_ReadInputData(GPIOA) & MP3_DREQ == 0); // 等待DREQ為高
Mp3SelectData(); // xDCS = 1,選擇vs1003的數(shù)據(jù)接口
SPIPutChar(0x4d);
SPIPutChar(0xea);
SPIPutChar(0x6d);
SPIPutChar(0x54);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
Delay(500);
Mp3DeselectData();
regvalue = Mp3ReadRegister(SPI_HDAT0); // 如果得到的值為0x807F,則表明完好。
}
/*******************************************************************************
* Function Name : Mp3WriteRegister
* Description : Write VS1003 register
* Input : addressbyte--the vs1003 register address
highbyte--the hight 8 bits
lowbyte--the low 8 bits
* Output : None
* Return : None
*******************************************************************************/
void Mp3WriteRegister(unsigned char addressbyte, unsigned char highbyte, unsigned char lowbyte)
{
Mp3DeselectData();
Mp3SelectControl(); //XCS = 0
SPIPutChar(VS_WRITE_COMMAND); //發(fā)送寫寄存器命令
SPIPutChar(addressbyte); //發(fā)送寄存器的地址
SPIPutChar(highbyte); //發(fā)送待寫數(shù)據(jù)的高8位
SPIPutChar(lowbyte); //發(fā)送待寫數(shù)據(jù)的低8位
Mp3DeselectControl();
}
/*******************************************************************************
* Function Name : Mp3ReadRegister
* Description : Read VS1003 register
* Input : addressbyte--the vs1003 register address
* Output : None
* Return : The register value
*******************************************************************************/
u16 Mp3ReadRegister(unsigned char addressbyte)
{
u16 resultvalue = 0;
Mp3DeselectData();
Mp3SelectControl(); //XCS = 0
SPIPutChar(VS_READ_COMMAND); //發(fā)送讀寄存器命令
SPIPutChar(addressbyte); //發(fā)送寄存器的地址
resultvalue = SPIGetChar() << 8;//讀取高8位數(shù)據(jù)
resultvalue |= SPIGetChar(); //讀取低8位數(shù)據(jù)
Mp3DeselectControl();
return resultvalue; //返回16位寄存器的值
}
/*******************************************************************************
* Function Name : VS1003_Config
* Description : Configures the GPIO ports and SPI2
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void VS1003_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* PA.1->XCS
PA.0->XRESET
PA.2->XDCS,all low lever avalible
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* PB.3->DREQ */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Diable all pins */
GPIOA->ODR = 0xFF;
/* Configure SPI2 pins: SCK, MISO and MOSI ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* SPI2 Config --------------------------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
/* Enable SPI2 */
SPI_Cmd(SPI2, ENABLE);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -