/**************************************************************
* Function: IIC_ReadByte
* Parameters: ack
* Return: val
*
* Read a byte from IIC bus
**************************************************************/
byte IIC_ReadByte(byte ack)
{
byte i, val;
for(i=8; i>0; i--)
{
val <<= 1;
val |= IIC_GetBit();
}
if (ack == IIC_ACK)
IIC_DATA_0();
else
IIC_DATA_1();
return val;
}
/**************************************************************
* Function: IIC_SendByte
* Parameters: tx_byte
* Return: none
*
* Write a byte to IIC bus
**************************************************************/
void IIC_SendByte(byte tx_byte)
{
byte k;
for(k=8;k>0;k--)
{
if (tx_byte & 0x80)
IIC_DATA_1();
else
IIC_DATA_0();
tx_byte <<= 1;
}
ak3=IIC_GetBit();
}
byte IIC_SendByteM(byte tx_byte)
{
byte k;
for(k=8;k>0;k--)
{
if (tx_byte & 0x80)
IIC_DATA_1();
else
IIC_DATA_0();
tx_byte <<= 1;
}
ak3=IIC_GetBit();
return(ak3);
}
/**************************************************************
* Function: SwIIC_ReadByte
* Parameters: RegAdd
* Return: none
*
* Complete data transfer - read a byte from device register
* Note: Secured routine through check of acknowledge
**************************************************************/
byte SwIIC_ReadByte(byte RegAdd)
{
byte temp;
ak1=NACK;
ak2=NACK;
ak3=NACK;
while(!(ak3==ACK))
{
IIC_START();
SwIIC_Delay(START_DELAY);
SwIIC_SendAddressWrite(ION_ADDR);
SwIIC_Delay(ADDRESS_DELAY);
if(!ak1)
{
SwIIC_SendRegister(RegAdd);
if(!ak2)
{
IIC_REPEAT_START();
SwIIC_Delay(START_DELAY);
SwIIC_SendAddressRead(ION_ADDR);
if(!ak3) temp=IIC_ReadByte(IIC_NACK);
}
}
IIC_STOP();
ak1=NACK;
ak2=NACK;
}
return(temp);
}
/**************************************************************
* Function: SwIIC_WriteByte
* Parameters: RegAdd, RegVal
* Return: none
*
* Complete data transfer - write a byte to device (ION) register
* Note: Secured routine through check of acknowledge
**************************************************************/
void SwIIC_WriteByte(byte RegAdd, byte RegVal)
{
ak1=NACK;
ak2=NACK;
ak3=NACK;
while(ak3)
{
IIC_START();
SwIIC_Delay(START_DELAY);
SwIIC_SendAddressWrite(ION_ADDR);
SwIIC_Delay(ADDRESS_DELAY);
if(!ak1)
{
SwIIC_SendRegister(RegAdd);
SwIIC_Delay(ADDRESS_DELAY);
if(!ak2)
{
IIC_SendByte(RegVal);
}
}
IIC_STOP();
ak1=NACK;
ak2=NACK;
}
}
/**************************************************************
* Function: SwIIC_MultiRead
* Parameters: RegAdd, *buff (data), cnt - length
* Return: none
*
* Complete data transfer - multiple write a bytes to device (ION) registers
* Note: Secured routine through check of acknowledge
**************************************************************/
void SwIIC_MultiReadBytes(byte RegAdd, byte *buff, byte cnt)
{
byte i;
i=0;
ak1=NACK;
ak2=NACK;
ak3=NACK;
while(!(ak3==ACK))
{
IIC_START();
SwIIC_Delay(START_DELAY);
SwIIC_SendAddressWrite(ION_ADDR);
SwIIC_Delay(ADDRESS_DELAY);
if(!ak1)
{
SwIIC_SendRegister(RegAdd);
if(!ak2)
{
IIC_REPEAT_START();
SwIIC_SendAddressRead(ION_ADDR);
SwIIC_Delay(ADDRESS_DELAY);
if(!ak3)
{
for(i=0;i<(cnt - 1);i++)
{
*buff++ = IIC_ReadByte(IIC_ACK);
}
*buff++ = IIC_ReadByte(IIC_NACK);
}
}
}
IIC_STOP();
ak1=NACK;
ak2=NACK;
}
}
/**************************************************************
* Function: SwIIC_MultiWrite
* Parameters: RegAdd, *buff (data), cnt - length
* Return: none
*
* Complete data transfer - multiple write a bytes into device (ION) registers
* Note: Secured routine through check of acknowledge
**************************************************************/
void SwIIC_MultiWriteBytes(byte RegAdd, byte *buff, byte cnt)
{
byte i,j,ak[32];
i=0;
ak1=NACK;
ak2=NACK;
ak3=NACK;
for(j=0;j<32;j++) ak[j]=NACK;
while(!(ak[cnt+2]==ACK))
{
IIC_START();
SwIIC_Delay(START_DELAY);
SwIIC_SendAddressWrite(ION_ADDR);
SwIIC_Delay(ADDRESS_DELAY);
if(!ak1)
{
SwIIC_SendRegister(RegAdd);
SwIIC_Delay(ADDRESS_DELAY);
ak[2]=ak2;
while((!ak[i+2])&&(i<cnt))
{
ak[i+3]=IIC_SendByteM(*(buff++));
i++;
}
}
IIC_STOP();
ak1=NACK;
for (j=0;j<(cnt-1);j++) ak[j+2]=NACK;
}
}
|