?? vs1003.c
字號:
//文件名:vs1003.c
//功能:vs1003 的底層驅動程序,主控芯片為LPC2103;
// 其他的微處理器(帶SPI接口的)只需稍加修改即可適用;
// 對于不帶硬SPI接口的微處理器可以用IO進行SPI的時序模擬
// 更多代碼敬請訪問www.icdev.com.cn
#include "vs1003.h"
#include "timer.h"
extern long volatile timeval; //用于延時的全局變量
/**********************************************************/
/* 函數名稱 : MSPI_Init */
/* 函數功能 : 初始化SPI接口,設置為主機。 */
/* 參數 : 無 */
/* 返回值 : 無 */
/*--------------------------------------------------------*/
void MSPI_Init(void)
{
PINSEL0 = (PINSEL0 & 0xFFFF00FF) | 0x00005500; //選擇 SPI
S0SPCCR = 0x08; // SPI 時鐘設置
S0SPCR = (0 << 3) | // CPHA = 0,
(0 << 4) | // CPOL = 0,
(1 << 5) | // MSTR = 1,
(0 << 6) | // LSBF = 0,
(0 << 7); // SPIE = 0,
}
/**********************************************************/
/* 函數名稱 : InitPortVS1003 */
/* 函數功能 : MCU與vs1003接口的初始化 */
/* 參數 : 無 */
/* 返回值 : 無 */
/*--------------------------------------------------------*/
void InitPortVS1003(void)
{
MSPI_Init();//SPI口的初始化
IODIR &= 0xfffeffff; //其他接口線的設置,其中dreq 為輸入口
IODIR |= MP3_XRESET | MP3_XCS | MP3_XDCS;//xRESET,xCS,xDS均為輸出口
IOSET |= MP3_XRESET | MP3_XCS | MP3_XDCS;//xRESET,xCS,xDS默認輸出高電平
}
/**********************************************************/
/* 函數名稱 : SPIPutChar */
/* 函數功能 : 通過SPI發送一個字節的數據 */
/* 參數 : 待發送的字節數據 */
/* 返回值 : 無 */
/*--------------------------------------------------------*/
void SPIPutChar(unsigned char c)
{
S0SPDR = c;
while((S0SPSR & 0x80) == 0); //等待SPI將數據發送完畢
}
/**********************************************************/
/* 函數名稱 : Mp3SoftReset */
/* 函數功能 : vs1003軟件復位 */
/* 參數 : 無 */
/* 返回值 : 無 */
/*--------------------------------------------------------*/
void Mp3SoftReset(void)
{
Mp3WriteRegister (SPI_MODE, 0x08, 0x04); //軟件復位
wait(1); //延時1ms
while (IOPIN & MP3_DREQ == 0); //等待軟件復位結束
Mp3WriteRegister(SPI_CLOCKF, 0x98, 0x00);//設置vs1003的時鐘,3倍頻
Mp3WriteRegister (SPI_AUDATA, 0xBB, 0x81); //采樣率48k,立體聲
Mp3WriteRegister(SPI_BASS, 0x00, 0x55);//設置重音
Mp3SetVolume(20,20);//設置音量
wait(1); //延時1ms
//向vs1003發送4個字節無效數據,用以啟動SPI發送
Mp3SelectData();
SPIPutChar(0);
SPIPutChar(0);
SPIPutChar(0);
SPIPutChar(0);
Mp3DeselectData();
}
/**********************************************************/
/* 函數名稱 : Mp3Reset */
/* 函數功能 : vs1003硬件復位 */
/* 參數 : 無 */
/* 返回值 : 無 */
/*--------------------------------------------------------*/
void Mp3Reset(void)
{
Mp3PutInReset();//xReset = 0 復位vs1003
wait(100);//延時100ms
// SPIPutCharWithoutWaiting(0xff);//發送一個字節的無效數據,啟動SPI傳輸
Mp3DeselectControl(); //xCS = 1
Mp3DeselectData(); //xDCS = 1
Mp3ReleaseFromReset(); //xRESET = 1
wait(100); //延時100ms
while (IOPIN & MP3_DREQ == 0);//等待DREQ為高
wait(100); //延時100ms
Mp3SetVolume(10,10);
Mp3SoftReset();//vs1003軟復位
}
/***********************************************************/
/* 函數名稱 : VsSineTest */
/* 函數功能 : vs1003正弦測試,將該函數放在while循環中, */
/* 如果能持續聽到一高一低的聲音,證明測試通過 */
/* 參數 : 無 */
/* 返回值 : 無 */
/*---------------------------------------------------------*/
void VsSineTest(void)
{
Mp3PutInReset(); //xReset = 0 復位vs1003
wait(100); //延時100ms
SPIPutChar(0xff);//發送一個字節的無效數據,啟動SPI傳輸
Mp3DeselectControl();
Mp3DeselectData();
Mp3ReleaseFromReset();
wait(100);
Mp3WriteRegister(SPI_MODE,0x08,0x20);//進入vs1003的測試模式
while (IOPIN & MP3_DREQ == 0); //等待DREQ為高
Mp3SelectData(); //xDCS = 1,選擇vs1003的數據接口
//向vs1003發送正弦測試命令:0x53 0xef 0x6e n 0x00 0x00 0x00 0x00
//其中n = 0x24, 設定vs1003所產生的正弦波的頻率值,具體計算方法見vs1003的datasheet
SPIPutChar(0x53);
SPIPutChar(0xef);
SPIPutChar(0x6e);
SPIPutChar(0x24);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
wait(500);
Mp3DeselectData();//程序執行到這里后應該能從耳機聽到一個單一頻率的聲音
//退出正弦測試
Mp3SelectData();
SPIPutChar(0x45);
SPIPutChar(0x78);
SPIPutChar(0x69);
SPIPutChar(0x74);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
wait(500);
Mp3DeselectData();
//再次進入正弦測試并設置n值為0x44,即將正弦波的頻率設置為另外的值
Mp3SelectData();
SPIPutChar(0x53);
SPIPutChar(0xef);
SPIPutChar(0x6e);
SPIPutChar(0x44);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
wait(500);
Mp3DeselectData();
//退出正弦測試
Mp3SelectData();
SPIPutChar(0x45);
SPIPutChar(0x78);
SPIPutChar(0x69);
SPIPutChar(0x74);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
SPIPutChar(0x00);
wait(500);
Mp3DeselectData();
}
/*************************************************************/
/* 函數名稱 : Mp3WriteRegister */
/* 函數功能 : 寫vs1003寄存器 */
/* 參數 : 寄存器地址,待寫數據的高8位,待寫數據的低8位 */
/* 返回值 : 無 */
/*-----------------------------------------------------------*/
void Mp3WriteRegister(unsigned char addressbyte, unsigned char highbyte, unsigned char lowbyte)
{
Mp3DeselectData();
Mp3SelectControl();//XCS = 0
SPIPutChar(VS_WRITE_COMMAND); //發送寫寄存器命令
SPIPutChar(addressbyte); //發送寄存器的地址
SPIPutChar(highbyte); //發送待寫數據的高8位
SPIPutChar(lowbyte); //發送待寫數據的低8位
Mp3DeselectControl();
}
/*************************************************************/
/* 函數名稱 : Mp3ReadRegister */
/* 函數功能 : 寫vs1003寄存器 */
/* 參數 : 寄存器地址 */
/* 返回值 : vs1003的16位寄存器的值 */
/*-----------------------------------------------------------*/
unsigned int Mp3ReadRegister(unsigned char addressbyte)
{
unsigned int resultvalue = 0;
Mp3DeselectData();
Mp3SelectControl();//XCS = 0
SPIPutChar(VS_READ_COMMAND); //發送讀寄存器命令
SPIPutChar((addressbyte)); //發送寄存器的地址
SPIPutChar(0xff); //發送讀時鐘
resultvalue = (SPI_RESULT_BYTE) << 8;//讀取高8位數據
SPIPutChar(0xff); //發送讀時鐘
resultvalue |= (SPI_RESULT_BYTE); //讀取低8位數據
Mp3DeselectControl();
return resultvalue; //返回16位寄存器的值
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -