#include <i2c.h>
#include "rw.h"
#include <delay.h> 


void i2c_transmit(unsigned char dev, unsigned char reg1, unsigned char reg0, unsigned char val)
{
        /*unsigned char SDA0 = 0x00; 
        unsigned char SDA1 = 0x00;
        unsigned char SDA2 = 0x00;
        unsigned char SDA3 = 0x00;
        unsigned char SDA4 = 0x00;
        unsigned char SDA5 = 0x00; 
        unsigned char SDA6 = 0x00;

        SDA0 = PINC & 0x30;*/
	i2c_start();
	//SDA1 = PINC & 0x30;
	i2c_w(dev); 
	//SDA2 = PINC & 0x30;
	i2c_write(reg1);
	//SDA3 = PINC & 0x30;
	i2c_write(reg0);
        //SDA4 = PINC & 0x30;
	i2c_write(val); 
	//SDA5 = PINC & 0x30;
	i2c_stop();  
	//SDA6 = PINC & 0x30; 
	delay_ms(10);
	/*printf("SDA0: %x SCL0: %x \r\n SDA1: %x SCL1: %x \r\n SDA2: %x SCL2: %x \r\n SDA3: %x SCL3: %x \r\n SDA4: %x SCL4: %x \r\n SDA5: %x SCL5: %x \r\n SDA6: %x SCL6: %x \r\n",
	       ((SDA0 & 0x10) ? 1 : 0),((SDA0 & 0x20) ? 1 : 0),((SDA1 & 0x10) ? 1 : 0),((SDA1 & 0x20) ? 1 : 0),(SDA2 & 0x10),(SDA2 & 0x20),
	       ((SDA3 & 0x10) ? 1 : 0),((SDA3 & 0x20) ? 1 : 0),((SDA4 & 0x10) ? 1 : 0),((SDA4 & 0x20) ? 1 : 0),((SDA5 & 0x10)? 1 : 0),((SDA5 & 0x20)? 1 : 0),
	       ((SDA6 & 0x10) ? 1 : 0),((SDA6 & 0x20) ? 1 : 0));*/ 
	printf ("reg1: %x ; reg0 %x ; val %x \r\n", reg1, reg0, val);
	
	
}   

unsigned char i2c_receive(unsigned char dev, unsigned char reg1, unsigned char reg0)
{    
	unsigned char data; 
	unsigned char SDA0 = 0x00;
	unsigned char SDA1 = 0x00;
        unsigned char SDA2 = 0x00;
        unsigned char SDA3 = 0x00;
        unsigned char SDA4 = 0x00;
        unsigned char SDA5 = 0x00; 
        unsigned char SDA6 = 0x00;  
	SDA0 = PINC & 0x30;
	i2c_start();
	SDA1 = PINC & 0x30;
	i2c_write(dev);
	SDA2 = PINC & 0x30;
	i2c_write(reg1); 
	SDA3 = PINC & 0x30; 
	i2c_write(reg0);  
	SDA4 = PINC & 0x30;
	i2c_start();    
	SDA5 = PINC & 0x30;
	i2c_write(dev | 1);    // 
	data = i2c_read(0);     // 0 = kein ACK abfragen   
	SDA6 = PINC & 0x30;
	i2c_stop();   
	 
	/*printf("SDA0: %x SCL0: %x \r\n SDA1: %x SCL1: %x \r\n SDA2: %x SCL2: %x \r\n SDA3: %x SCL3: %x \r\n SDA4: %x SCL4: %x \r\n SDA5: %x SCL5: %x \r\n SDA6: %x SCL6: %x \r\n",
	       ((SDA0 & 0x10) ? 1 : 0),((SDA0 & 0x20) ? 1 : 0),((SDA1 & 0x10) ? 1 : 0),((SDA1 & 0x20) ? 1 : 0),(SDA2 & 0x10),(SDA2 & 0x20),
	       ((SDA3 & 0x10) ? 1 : 0),((SDA3 & 0x20) ? 1 : 0),((SDA4 & 0x10) ? 1 : 0),((SDA4 & 0x20) ? 1 : 0),((SDA5 & 0x10)? 1 : 0),((SDA5 & 0x20)? 1 : 0),
	       ((SDA6 & 0x10) ? 1 : 0),((SDA6 & 0x20) ? 1 : 0));*/
	printf ("data: %x reg1: %x reg0: %x\r\n",data,reg1,reg0);
	return data;
}