/*##################  C51_Funktionen fuer I2C-Bus  ##################

  die Zurordnug von SDA und SCL muss vorher geschehen
  z.B.  */

#include "reg_c51.h"

	sbit SDA=P1^0;//P1.0
	sbit SCL=P1^1;//P1.1
							
//-------------------------------------------------------------
void wait2(unsigned char wert)
{
	do
	{
		wert=wert-1;
	}
	while(wert!=0);
}

//-------------------------------------------------------------
void i2c_bus_start()  	//abfallende Flanke  von SDA ,während SCL auf High
{
	SDA=1;
	wait2(1);
    	SCL=1;
    	while(SCL==0);  	//warten falls ein Slave noch bremst
	wait2(1);
	SDA=0;		    	//abfallende Flanke produziert Start-Condition
    	wait2(1);
	SCL=0;
	wait2(1);
}

//-------------------------------------------------------------
void i2c_bus_stop()		//ansteigende Flanke  von SDA ,während SCL auf High
{
	SDA=0;
	wait2(1);
	SCL=1;
	while(SCL==0);  	//warten falls ein Slave noch bremst
    	wait2(1);
	SDA=1; 				//ansteigende Flanke produziert Stop-Condition
	wait2(1);
}

//--------------------------------------------------------------
void i2c_bit_write(bit bitwert)	//bitwert wird geschrieben
{
	SDA=bitwert;		//Bit-Wert nach  SDA übernehmen
	wait2(1);
	SCL=1;
    	while(SCL==0);  	//warten falls der slave ausbremst
	wait2(1);
	SCL=0;
    	wait2(1);
	SDA=0;
}

//-------------------------------------------------------------
bit i2c_bit_read()	//bitwert wird gelesen
{
	bit bitwert;
	SCL=0;
    	wait2(5);
	SDA=1;
	wait2(5);
	SCL=1;
	while(SCL==0);  	//warten falls Slave ausbremst
	wait2(5);		//warten ca 5µs auf die Veränderung von SDA 
    	bitwert=SDA;    	//Zustand von SDA der Bitvariable  übergeben
    	SCL=0;
    	wait2(5);
	SDA=0;
	wait2(5);
	return(bitwert);
	
}

//-------------------------------------------------------------
void i2c_ack_slave()	//Quittung vom Slave an den Master
{
	SDA=1;
	wait2(5);
	SCL=1;
	while(SCL==0);     	//warten falls ein Slave ausbremst
    	wait2(5);
	while(SDA==1);  	//warten bis Acknowledge vom Slave kommt
	wait2(5);
	SCL=0;
    	wait2(5);
	SDA=0;
	wait2(5);
}

//--------------------------------------------------------------
void i2c_ack_master()  //ACK-Quittung vom Master an den Slave
{
	SDA=0;
	wait2(1);
	SCL=1;
	while(SCL==0);  //warten falls ein Slave ausbremst
    	wait2(5);	//Zeit dass Slave vom Master das ACK  erfahren kann
	SCL=0;
    	wait2(1);
	SDA=0;
}

//--------------------------------------------------------------
void i2c_nack_master()    //NACK-Quittung vom Master an den Slave
{						
	SDA=1;
	wait2(1);
	SCL=1;
	while(SCL==0);     //warte falls ein Slave ausbremst
    	wait2(5);
	SCL=0;
	wait2(1);
	SDA=0;
}
     
//--------------------------------------------------------------
void i2c_byte_write (unsigned char wert)  //Das übergebene Zeichen wird auf den I2C-Bus geschrieben
{
	unsigned char i;
	bit bitwert;
	for(i=0;i<=7;i++)
	{
	bitwert=(wert&0x80)/128;	//MSB herausfiltern
	i2c_bit_write(bitwert);
	wert=wert<<1;
	}
}

//--------------------------------------------------------------
unsigned char i2c_byte_read()	// ein Byte wird vom I2C-Bus gelesen 
{
	unsigned char i,wert=0;
	for(i=0;i<=7;i++)
	{
	//wert = (i2c_bit_read()*(1<<(7-i)))+wert;
	}
	return(wert);
}
//--------------------------------------------------------------   
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
