the code reference to "ZVC192-IIC-HUMIDITY-CW106"(master mode/Non-interrupt mode )
//**********************************************************************************************
void Init_IIC(void);
void IIC_Start(void);
void IIC_Stop(void);
void IIC_RepeatStart(void);
void IIC_Delay(void);
void IIC_CycleWrite(byte bout);
byte IIC_CycleRead(byte ack);
void IIC_SendBlock(uint8_t address,uint8_t *WrPointer,uint8_t WrNumber,uint8_t mode);
void IIC_RecvBlock(uint8_t address,uint8_t *rdpointer,uint8_t rdNumber);
//**********************************************************************************************
// Private memory declarations
//**********************************************************************************************
static byte error;
static word timeout;
//==============================================================================
// Init_IIC module
//==============================================================================
void Init_IIC(void)
{
IIC0IBFD = 0x80; //Frequency divider register
//see attached IIC calculator in excel file
IIC0IBAD = 0x00; //Slave address of this module;
IIC0IBCR = 0x80; //Enable I2C module, No interrupts;
}
//*********************************************************
// Initiate IIC Start Condition
//*********************************************************
void IIC_Start(void)
{
IIC0IBCR_MS_SL = 1;
timeout = 0;
while ((!IIC0IBSR_IBB) && (timeout<1000))
timeout++;
if (timeout >= 1000)
error |= 0x01;
} //*** Wait until BUSY=1
//*********************************************************
// Initiate IIC Stop Condition
//*********************************************************
void IIC_Stop(void)
{
IIC0IBCR_MS_SL = 0;
timeout = 0;
while ( (IIC0IBSR_IBB) && (timeout<1000))
timeout++;
if (timeout >= 1000)
error |= 0x02;
} //*** Wait until BUSY=0
//*********************************************************
// Initiate IIC Repeat Start Condition
//*********************************************************
void IIC_RepeatStart(void)
{
IIC0IBCR_RSTA = 1;
timeout = 0;
while ((!IIC0IBSR_IBB) && (timeout<1000))
timeout++;
if (timeout >= 1000)
error |= 0x04;
} //*** Wait until BUSY=1
//*********************************************************
// IIC Delay
//*********************************************************
void IIC_Delay(void)
{
byte IICd;
for (IICd=0; IICd<100; IICd++){};
}
//*********************************************************
// IIC Cycle Write
//*********************************************************
void IIC_CycleWrite(byte bout)
{
timeout = 0;
while ((!IIC0IBSR_TCF) && (timeout<1000))
timeout++;
if (timeout >= 1000)
error |= 0x08;
IIC0IBDR = bout;
timeout = 0;
while ((!IIC0IBSR_IBIF) && (timeout<1000))
timeout++;
if (timeout >= 1000)
error |= 0x10;
IIC0IBSR_IBIF = 1;
if (IIC0IBSR_RXAK)
error |= 0x20;
}
//*********************************************************
// IIC Cycle Read
//*********************************************************
byte IIC_CycleRead(byte ack)
{
byte bread;
timeout = 0;
while ((!IIC0IBSR_TCF) && (timeout<1000))
timeout++;
if (timeout >= 1000)
error|=0x08;
IIC0IBCR_TX_RX = 0;
IIC0IBCR_TXAK = ack;
bread = IIC0IBDR;
timeout = 0;
while ((!IIC0IBSR_IBIF) && (timeout<1000))
timeout++;
if (timeout >= 1000)
error |= 0x10;
IIC0IBSR_IBIF=1;
return bread;
}
///////////////////////////////////////////////////////////////////////
void IIC_SendBlock(uint8_t address,uint8_t *WrPointer,uint8_t WrNumber,uint8_t mode)
{
uint8_t i;
IIC0IBCR_TX_RX = 1;
IIC_Start();
IIC_CycleWrite(address&0xfe);
for(i=0;i<WrNumber;i++)
{
IIC_CycleWrite(*WrPointer);
WrPointer++;
}
if(mode==0)IIC_Stop();
}
///////////////////////////////////////////////////////////////////////////////////
void IIC_RecvBlock(uint8_t address,uint8_t *rdpointer,uint8_t rdNumber)
{
uint8_t i;
uint8_t send_ack=0;
IIC0IBCR_TX_RX = 1;
IIC_RepeatStart();
IIC_CycleWrite(address|0x01);
for(i=0;i<rdNumber;i++)
{
if(i==(rdNumber-1))send_ack=1;
else send_ack=0;
*rdpointer=IIC_CycleRead(send_ack);
rdpointer++;
}
IIC_Stop();
}