📄 iic.c
字号:
#include "inc\44b0x.h"
#include "DataType.h"
#include "main.h"
#include "iic.h"
#define IICBUFSIZE 150
U8 _iicData[IICBUFSIZE];
volatile int _iicDataCount;
int _iicPt;
void vIicInit(void)
{
rPCONF |= 0xa; //PF0:IICSCL, PF1:IICSDA
rPUPF |= 0x3; //pull-up disable
//pISR_IIC = (unsigned)IicInt;
//rINTMSK &= ~(BIT_IIC);
rIICCON = (1<<7)|(0<<6)|(1<<5)|(0xe);
//Enable interrupt, IICCLK=MCLK/16, Enable ACK
//48Mhz/16/(14+1)=200kHz
rIICADD=0x10; // S3C44B0X slave address
rIICSTAT=0x10;
}
uchar ucWr24C512(U32 slvAddr,U32 addr,U8 p_ucLen,U8 *data)
{
U8 i;
U32 j;
_iicPt=0;
_iicData[0]=(U8)(addr/256);
_iicData[1]=(U8)(addr%256);
copydata(&_iicData[2], data, p_ucLen);
_iicDataCount = p_ucLen+2;
rIICDS=slvAddr;//0xa0
rIICSTAT=0xf0; //MasTx,Start
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(1)
{
j = 0;
while(0 == (rIICCON&0x10))
{
Feed_WatchDog();
j++;
if (10000 <= j) // time out
{
return FAIL;
}
}
if((_iicDataCount--)==0)
{
rIICSTAT=0xd0; //stop MasTx condition
rIICCON=0xae; //resumes IIC operation.
for (j=0; j<1200; j++) // 12ms, wait until write in eeprom finish
{
vDelay10us();
}
break;
}
rIICDS=_iicData[_iicPt++]; //_iicData[0] has dummy.
for(i=0;i<10;i++); //for setup time until rising edge of IICSCL
rIICCON=0xae; //resumes IIC operation.
}
return SUCCESS;
//write is completed.
}
uchar ucRd24C512(U32 slvAddr,U32 addr,U8 p_ucLen,U8 *data)
{
U8 i;
U32 j;
_iicPt=0;
_iicData[0]=(U8)(addr/256);
_iicData[1]=(U8)(addr%256);
_iicDataCount=2;
rIICDS=slvAddr;
rIICSTAT=0xf0; //MasTx,Start
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(1)
{
j = 0;
while(0 == (rIICCON&0x10))
{
Feed_WatchDog();
j++;
if (10000 <= j) // time out
{
return FAIL;
}
}
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=0xae; //resumes IIC operation.
}
_iicPt=0;
_iicDataCount=p_ucLen;
rIICDS=slvAddr;
rIICSTAT=0xb0; //MasRx,Start
rIICCON=0xae; //resumes IIC operation.
while(1)
{
j = 0;
while(0 == (rIICCON&0x10))
{
Feed_WatchDog();
j++;
if (10000 <= j) // time out
{
return FAIL;
}
}
if((_iicDataCount--)==0)
{
_iicData[_iicPt++]=rIICDS;
rIICSTAT=0x90; //stop MasRx condition
rIICCON=0xae; //resumes IIC operation
for (j=0; j<200; j++) // 2ms, wait until stop condtion is in effect.
{
vDelay10us();
}
break;
}
_iicData[_iicPt++]=rIICDS;
//The last data has to be read with no ack.
if((_iicDataCount)==0)
rIICCON=0x2e; //resumes IIC operation with NOACK.
else
rIICCON=0xae; //resumes IIC operation with ACK
}
copydata(data, &_iicData[1], p_ucLen);
return SUCCESS;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -