?? iic.c
字號:
break;
default:
break;
}
}
//===================================================================
// SMDK2410 IIC configuration
// GPE15=IICSDA, GPE14=IICSCL
// "Non-Interrupt" mode for IIC block
//===================================================================
//*********************[ Test_Iic2 ]*********************************
void Test_Iic2(void)
{
unsigned int i,j,save_E,save_PE;
static U8 data[256];
SetClockDivider(1, 1);
SetSysFclk(DFT_FCLK_VAL);
Port_Init();
Uart_Select(0);
Uart_Init(0, UART_BAUD);
Uart_Printf("IIC Test(Polling) using AT24C02\n");
//保存寄存器rGPECON和rGPEUP
save_E = rGPECON;
save_PE = rGPEUP;
//設置GPE15->IICSDA和GPE14->IICSCL
rGPEUP |= 0xc000; //Pull-up disable
rGPECON &= ~0xf0000000;
rGPECON |= 0xa0000000; //GPE15:IICSDA , GPE14:IICSCL
//Enable ACK, Prescaler IICCLK=PCLK/16, Enable interrupt, Transmit clock value Tx clock=IICCLK/16
rIICCON = (1<<7) | (0<<6) | (1<<5) | (0xf); //0xaf
//IIC clock = PCLK/(512*1)
//rIICCON = (1<<7) | (1<<6) | (1<<5) | (0); //0xe0
rIICADD = 0x10; //2410 slave address = [7:1]
rIICSTAT = 0x10; //IIC bus data output enable(Rx/Tx)
Uart_Printf("Write test data into AT24C02\n");
//寫入一個page的數據,page的大小是256byte,
//page 的起始地址是0xa0,寫入的數據是:0、1、2、...255。0xa0是AT24C02的地址。
for(i=0;i<256;i++)
_Wr24C080(0xa0,(U8)i,i);
//初始化data數組的值為0。
for(i=0;i<256;i++)
data[i] = 0;
Uart_Printf("Read test data from AT24C02\n");
//讀24C02的0xa0地址中數據到data數組中。
for(i=0;i<256;i++)
_Rd24C080(0xa0,(U8)i,&(data[i]));
//輸出data數組接收數據的值
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
Uart_Printf("%2x ",data[i*8+j]);
Uart_Printf("\n");
}
Uart_Printf("OK! Write data is same to Read data!\n");
rGPEUP = save_PE;
rGPECON = save_E;
}
//**************[ _Wr24C080 ]*****************************************
void _Wr24C080(U32 slvAddr,U32 addr,U8 data)
{
_iicMode = WRDATA;
_iicPt = 0;
_iicData[0] = (U8)addr;
_iicData[1] = data;
_iicDataCount = 2;
//8-bit data shift register for IIC-bus Tx/Rx operation.
rIICDS = slvAddr; //0xa0
//Master Tx mode, Start(Write), IIC-bus data output enable
//Bus arbitration sucessful, Address as slave status flag Cleared,
//Address zero status flag cleared, Last received bit is 0
rIICSTAT = 0xf0;
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(_iicDataCount!=-1)
Run_IicPoll();
_iicMode = POLLACK;
while(1)
{
rIICDS = slvAddr;
_iicStatus = 0x100; //To check if _iicStatus is changed
rIICSTAT = 0xf0; //Master Tx, Start, Output Enable, Sucessful, Cleared, Cleared, 0
rIICCON = 0xe0;//0xaf; //Resumes IIC operation. //hzh
while(_iicStatus==0x100)
Run_IicPoll();
if(!(_iicStatus & 0x1))
break; //When ACK is received
}
rIICSTAT = 0xd0; //Master Tx condition, Stop(Write), Output Enable
rIICCON = 0xe0;//0xaf; //Resumes IIC operation. //hzh
Delay(1); //Wait until stop condtion is in effect.
//Write is completed.
}
//************************[ _Rd24C080 ]********************************
void _Rd24C080(U32 slvAddr,U32 addr,U8 *data)
{
_iicMode = SETRDADDR;
_iicPt = 0;
_iicData[0] = (U8)addr;
_iicDataCount = 1;
rIICDS = slvAddr;
rIICSTAT = 0xf0; //MasTx,Start
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(_iicDataCount!=-1)
Run_IicPoll();
_iicMode = RDDATA;
_iicPt = 0;
_iicDataCount = 1;
rIICDS = slvAddr;
rIICSTAT = 0xb0; //Master Rx,Start
rIICCON = 0xe0;//0xaf; //Resumes IIC operation. //hzh
while(_iicDataCount!=-1)
Run_IicPoll();
*data = _iicData[1];
}
//**********************[ Run_IicPoll ]*********************************
void Run_IicPoll(void)
{
if(rIICCON & 0x10) //Tx/Rx Interrupt Enable
IicPoll();
}
//**********************[IicPoll ]**************************************
void IicPoll(void)
{
U32 iicSt,i;
iicSt = rIICSTAT;
if(iicSt & 0x8){} //When bus arbitration is failed.
if(iicSt & 0x4){} //When a slave address is matched with IICADD
if(iicSt & 0x2){} //When a slave address is 0000000b
if(iicSt & 0x1){} //When ACK isn't received
switch(_iicMode)
{
case POLLACK:
_iicStatus = iicSt;
break;
case RDDATA:
if((_iicDataCount--)==0)
{
_iicData[_iicPt++] = rIICDS;
rIICSTAT = 0x90; //Stop MasRx condition
rIICCON = 0xe0;//0xaf; //Resumes IIC operation. //hzh
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 = 0x60;//0x2f; //Resumes IIC operation with NOACK. //hzh
else
rIICCON = 0xe0;//0xaf; //Resumes IIC operation with ACK //hzh
break;
case WRDATA:
if((_iicDataCount--)==0)
{
rIICSTAT = 0xd0; //stop MasTx condition
rIICCON = 0xe0;//0xaf; //resumes IIC operation. //hzh
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.
for(i=0;i<10;i++); //for setup time until rising edge of IICSCL
rIICCON = 0xe0;//0xaf; //resumes IIC operation. //hzh
break;
case SETRDADDR:
// Uart_Printf("IicPoll() SETRADDR [S%d]",_iicDataCount);
if((_iicDataCount--)==0)
{
break; //IIC operation is stopped because of IICCON[4]
}
rIICDS = _iicData[_iicPt++];
for(i=0;i<10;i++); //for setup time until rising edge of IICSCL
rIICCON = 0xe0;//0xaf; //resumes IIC operation. //hzh
break;
default:
break;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -