#include "TEA5767_main.h"
#include "Delay.h"

void iic_start ( void )
{
    TEA5767_SDA = 1;
    delay_us ( 4 );
    TEA5767_SCL = 1;
    delay_us ( 4 );
    TEA5767_SDA = 0;
    delay_us ( 4 );
    TEA5767_SCL = 0;
    delay_us ( 4 );
}

void iic_stop ( void )
{
    TEA5767_SCL = 0;
    delay_us ( 4 );
    TEA5767_SDA = 0;
    delay_us ( 4 );
    TEA5767_SCL = 1;
    delay_us ( 4 );
    TEA5767_SDA = 1;
    delay_us ( 4 );
}

void iic_ack ( void )
{
    TEA5767_SDA = 0;
    delay_us ( 4 );
    TEA5767_SCL = 1;
    delay_us ( 4 );
    TEA5767_SCL = 0;
    delay_us ( 4 );
    TEA5767_SDA = 1;
    delay_us ( 4 );
}

/*void iic_NoAck()
{
 TEA5767_SDA=1;
 delay_us(4);
 TEA5767_SCL=1;
 delay_us(4);
 TEA5767_SCL=0;
 delay_us(4);
 TEA5767_SDA=0;
}*/

bool iic_testack ( void )
{
    bool ErrorBit;
    TEA5767_SDA = 1;
    delay_us ( 4 );
    TEA5767_SCL = 1;
    delay_us ( 4 );
    ErrorBit = TEA5767_SDA_VALUE;
    delay_us ( 4 );
    TEA5767_SCL = 0;
    return ErrorBit;
}

void iic_write8bit ( unsigned char input )
{
    unsigned char temp;

    for ( temp = 8; temp != 0; temp-- )
    {
        TEA5767_SDA = ( bool ) ( input & 0x80 );
        delay_us ( 4 );
        TEA5767_SCL = 1;
        delay_us ( 4 );
        TEA5767_SCL = 0;
        delay_us ( 4 );
        input = input << 1;
    }
}

unsigned char iic_read8bit ( void )
{
    unsigned char temp, rbyte = 0;

    for ( temp = 8; temp != 0; temp-- )
    {
        TEA5767_SCL = 1;
        delay_us ( 4 );
        rbyte = rbyte << 1;
        rbyte = rbyte | ( ( unsigned char ) TEA5767_SDA_VALUE );
        TEA5767_SCL = 0;
    }

    return rbyte;
}



