IIS是飛利浦公司定義的一種用于音頻傳輸的數字總線,LPC1768支持該總線, I2S接口為一條3線串行總線,含有1根數據線、1根時鐘線和1根字選擇信號線?;镜腎2S連接具有一個主機(其總是為主機)和一個從機。LPC1700系列Cortex—M3微控制器的I2S提供了彼此獨立的發(fā)送和接收通道,每個通道都可作為主機或從機.
使用代碼如下
void IISInit(void)
{
//打開IIS時鐘
LPC_SC->PCONP|=1<<27;
LPC_SC->PCLKSEL1&=~(0X03<<22); //時鐘選擇25M
//0.4 IIS RX CLK
LPC_PINCON->PINSEL0&=~(0x03u<<8);
LPC_PINCON->PINSEL0|=(0x01u<<8);
//0.5 IIS RX LR
LPC_PINCON->PINSEL0&=~(0x03u<<10);
LPC_PINCON->PINSEL0|=(0x01u<<10);
//0.6 IIS RX DAT
LPC_PINCON->PINSEL0&=~(0x03u<<12);
LPC_PINCON->PINSEL0|=(0x01u<<12);
//0.7 IIS TX CLK
LPC_PINCON->PINSEL0&=~(0x03u<<14);
LPC_PINCON->PINSEL0|=(0x01u<<14);
//0.8 IIS TX LR
LPC_PINCON->PINSEL0&=~(0x03u<<16);
LPC_PINCON->PINSEL0|=(0x01u<<16);
//0.9 IIS TX DAT
LPC_PINCON->PINSEL0&=~(0x03u<<18);
LPC_PINCON->PINSEL0|=(0x01u<<18);
//mclk IIS TX MCLK
LPC_PINCON->PINSEL9&=~(0x03u<<26);
LPC_PINCON->PINSEL9|=(0x01u<<26);
//FS 48K DATALENGTH 16 MONO ON MCLK 256FS
LPC_I2S->I2STXRATE=0;
LPC_I2S->I2STXRATE=(98<<8)|100; //X 98 Y 100 MCLK (25000000/2)*0.98 12250000
LPC_I2S->I2SRXRATE=0;
LPC_I2S->I2SRXRATE=(98<<8)|100; //X 98 Y 100 MCLK (25000000/2)*0.98 12250000
LPC_I2S->I2STXBITRATE=7; //BIT_RATE=MCLK/(7+1)=1531250 FS = 1531250/16/2 = 47.85
LPC_I2S->I2SRXBITRATE=7; //BIT_RATE=MCLK/(7+1)=1531250 FS = 1531250/16/2 = 47.85
LPC_I2S->I2STXMODE|=1<<3;//禁止TCLK輸出
LPC_I2S->I2SRXMODE|=1<<3;//禁止RCLK輸出
//16位數據 立體聲 使能fifo 主模式 采樣周期32 僅僅發(fā)送0
LPC_I2S->I2SDAO=(1<<0)|(0<<2)|(0<<3)|(0<<4)|(0<<5)|(15<<6)|(0<<15);
LPC_I2S->I2SDAI=((16-1)<<6)|(0<<5)|(0<<4)|(0<<3)|(0<<2)|(1<<0);
IIS_Stop();
LPC_I2S->I2SIRQ=(0<<0)|(0<<1)|(32<<8)|(32<<16); //32位fifo深度 不使能接受中斷,發(fā)送中斷
NVIC_EnableIRQ(I2S_IRQn);
FillBuffer(Buf_L,1000,Buf_R,1000);
}
void IISStart()
{
LPC_I2S->I2SDAO&=(~(1<<4));//復位停止訪問fifo使能并開始發(fā)送
LPC_I2S->I2SDAO&=(~(1<<3));
LPC_I2S->I2SDAO&=(~(1<<15));
}
void IISStartTransData(void)
{
IISStart();
LPC_I2S->I2SIRQ|=LPC_I2S->I2SIRQ|(1<<1);
}
void I2S_IRQHandler(void)
{
u32 state=LPC_I2S->I2SSTATE;
if(state&0x1)
{
if(((state>>16)&0x1f)==0)
{
s16 datl=Buf_L[current_l++];
s16 datr=Buf_R[current_r++];
LPC_I2S->I2STXFIFO=(datr<<16)|(datl&0xFFFF);
if(current_l>=len_l)current_l=0;
if(current_r>=len_r)current_r=0;
}
}
}