十 ARM9(2440)的IIC-理論知識及程序?qū)嵗?/h1>
//*************************[ Wr24C080 ]****************************
void Wr24C080(U32 slvAddr,U32 addr,U8 data)//首地址 內(nèi)部地址 數(shù)據(jù)
{
_iicMode = WRDATA;//模式標(biāo)志記為WRDAT
_iicPt = 0;//指針記為0
_iicData[0] = (U8)addr;//內(nèi)部地址
_iicData[1] = data;// 要寫的數(shù)據(jù)
_iicDataCount = 2;
rIICDS = slvAddr; //0xa0設(shè)備地址
rIICSTAT = 0xf0; //MasTx,Start 啟動發(fā)送
//Clearing the pending bit isnt needed because the pending bit has been cleared.
while(_iicDataCount!=-1);//未發(fā)送完在此等待本文引用地址:http://m.butianyuan.cn/article/201611/322175.htm _iicMode = POLLACK;//發(fā)送完后下面等待應(yīng)答信號ACK
while(1)
{
rIICDS = slvAddr;
_iicStatus = 0x100;
rIICSTAT = 0xf0; //MasTx,Start啟動
rIICCON = 0xaf; //Resumes IIC operation. 恢復(fù)IIC總線
while(_iicStatus==0x100);//未接收到ACk在此等待
if(!(_iicStatus&0x1))
break; //When ACK is received ACK收到后跳出循環(huán)
}
rIICSTAT = 0xd0; //Stop MasTx condition 停止信號
rIICCON = 0xaf; //Resumes IIC operation.
Delay(1); //Wait until stop condtion is in effect.恢復(fù)
//Write is completed.
}
//**********************[ Rd24C080 ] ***********************************
void Rd24C080(U32 slvAddr,U32 addr,U8 *data)//首地址 內(nèi)部地址 讀取的數(shù)據(jù)存入的地址
{
_iicMode = SETRDADDR;//模式設(shè)為SETRDADDR
_iicPt = 0;
_iicData[0] = (U8)addr;//_iicData[0]存內(nèi)部地址
_iicDataCount = 1;//計數(shù)值
rIICDS = slvAddr;//首地址0xa0
rIICSTAT = 0xf0; //MasTx,Start 啟動
//Clearing the pending bit isnt needed because the pending bit has been cleared.
while(_iicDataCount!=-1);//未讀取完成
_iicMode = RDDATA;//完成后進入讀取數(shù)據(jù)模式
_iicPt = 0;
_iicDataCount = 1;
rIICDS = slvAddr;
rIICSTAT = 0xb0; //MasRx,Start設(shè)置為主接收模式
rIICCON = 0xaf; //Resumes IIC operation.恢復(fù)IIC總線操作
while(_iicDataCount!=-1);//讀取未完成
*data = _iicData[1];//1
}
//-------------------------------------------------------------------------
void __irq IicInt(void)
{
U32 iicSt,i;
rSRCPND = BIT_IIC; //Clear pending bit
rINTPND = BIT_IIC;
iicSt = rIICSTAT;//控制狀態(tài)寄存器
if(iicSt & 0x8){} //When bus arbitration is failed.總線仲裁失敗執(zhí)行空操作
if(iicSt & 0x4){} //When a slave address is matched with IICADD從地址匹配執(zhí)行空操作
if(iicSt & 0x2){} //When a slave address is 0000000b
if(iicSt & 0x1){} //When ACK isnt received 未收到應(yīng)答信號執(zhí)行空操作
switch(_iicMode)
{
case POLLACK:
_iicStatus = iicSt;//控制狀態(tài)寄存器的值賦給_iicStatus
break;
case RDDATA:
if((_iicDataCount--)==0)
{
_iicData[_iicPt++] = rIICDS;
rIICSTAT = 0x90; //Stop MasRx condition
rIICCON = 0xaf; //Resumes IIC operation.
Delay(1); //Wait until stop condtion is in effect.
//Too long time...
//The pending bit will not be set after issuing stop condition.
break;
}
_iicData[_iicPt++] = rIICDS; //The last data has to be read with no ack.
if((_iicDataCount)==0)
rIICCON = 0x2f; //Resumes IIC operation with NOACK. 第一次不產(chǎn)生應(yīng)答信號進行第二次讀取并將第二次讀取的
///////////////數(shù)據(jù)存入數(shù)組中(第一次和第二次讀取的數(shù)據(jù)實際一樣)
else
rIICCON = 0xaf; //Resumes IIC operation with ACK
break;
case WRDATA:
if((_iicDataCount--)==0)//首地址 ,內(nèi)部地址和數(shù)據(jù)都發(fā)送完畢后
{
rIICSTAT = 0xd0; //Stop MasTx condition 產(chǎn)生停止信號
rIICCON = 0xaf; //Resumes IIC operation.恢復(fù)IIC總線
Delay(1); //Wait until stop condtion is in effect.
//The pending bit will not be set after issuing stop condition.
break;
}
rIICDS = _iicData[_iicPt++]; //_iicData[0] has dummy.第一次中斷后將內(nèi)部地址addr的值存入IICDS 第二次存入要寫入的數(shù)據(jù)
for(i=0;i<10;i++); //for setup time until rising edge of IICSCL//延時
rIICCON = 0xaf; //resumes IIC operation.恢復(fù)IIC總線
break;
case SETRDADDR:
// Uart_Printf("[ S%d ]",_iicDataCount);
if((_iicDataCount--)==0)
break;//第二次中斷跳出 //IIC operation is stopped because of IICCON[4]
rIICDS = _iicData[_iicPt++];//第一次中斷將內(nèi)部地址addr存入移位寄存器
for(i=0;i<10;i++); //For setup time until rising edge of IICSCL
rIICCON = 0xaf; //Resumes IIC operation.
break;
default:
break;
}
}
void Delay(int x)
{
int k, j;
while(x)
{
for (k=0;k<=0xff;k++)
for(j=0;j<=0xff;j++);
x--;
}
}
//*************************[ Wr24C080 ]****************************
void Wr24C080(U32 slvAddr,U32 addr,U8 data)//首地址
{
}
//**********************[ Rd24C080 ] ***********************************
void Rd24C080(U32 slvAddr,U32 addr,U8 *data)//首地址 內(nèi)部地址 讀取的數(shù)據(jù)存入的地址
{
}
//-------------------------------------------------------------------------
void __irq IicInt(void)
{
//
}
void Delay(int x)
{
}
評論