#include<avr/io.h>
#include<avr/interrupt.h>
#include <string.h> 
#include <stdio.h>
#include <compat/deprecated.h>
//#define Freq 4000000
#define START 0x08
#define MT_SLA_ACK 0X18
#define MT_DATA_ACK 0X28
int i=0;
int j=0;
int k=0;


void TWI_Byte_Read()
{

    PORTD |= 0x80;
    TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);   //Send start condition
    while (!(TWCR & (1<<TWINT)));             //wait while int flag set
    if ((TWSR & 0xF8) != START)               
    {
       cbi(PORTC,1);
        //exit(0);
    }
    TWDR = 0x90;                         //load control byte in data reg
    TWCR = (1<<TWINT)|(1<<TWEN);          //send command
    while (!(TWCR & (1<<TWINT)));         //wait while int flag set
    if ((TWSR & 0xF8) != MT_SLA_ACK)
    {
        cbi(PORTC,1);
        //exit(0);
    }
    TWDR = 0xAA;                  //load command byte in data reg
    TWCR = (1<<TWINT)|(1<<TWEN);//send command
    while (!(TWCR & (1<<TWINT)));         //wait while int flag set
    if ((TWSR & 0xF8) != MT_DATA_ACK)
    {
        cbi(PORTC,1);
        //exit(0);
    }
	    

	TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);   //Send repeat start ACK ne prihodit!!!!!
    while (!(TWCR & (1<<TWINT)));             //wait while int flag set
    if ((TWSR & 0xF8) != START)               
    {
       cbi(PORTC,1);
        //exit(0);
    }
	TWDR = 0x91;                         //load control byte in data reg
    TWCR = (1<<TWINT)|(1<<TWEN);          //send command
    while (!(TWCR & (1<<TWINT)));         //wait while int flag set
    if ((TWSR & 0xF8) != MT_SLA_ACK)
    {
        cbi(PORTC,1);
        //exit(0);
    }

	
    TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);     //send STOP condition
    
    PORTD &= 0x7f;
}





int main (void)

 {                                                                                                                                                                                                                                                                                                                        
//	SP=0x400;
TWBR=10;
	DDRC=0x00;
	PORTC=0x00;
	sbi(DDRC,1);
	sbi(PORTC,1);
	sei();
	TWI_Byte_Read();
	
	

//	TCCR1B = 2;		//CS10


//	TIMSK = 4;
	
	for(;;)	{}



}



