
#pragma used+
sfrb TWBR=0;
sfrb TWSR=1;
sfrb TWAR=2;
sfrb TWDR=3;
sfrb ADCL=4;
sfrb ADCH=5;
sfrw ADCW=4;      
sfrb ADCSRA=6;
sfrb ADMUX=7;
sfrb ACSR=8;
sfrb UBRRL=9;
sfrb UCSRB=0xa;
sfrb UCSRA=0xb;
sfrb UDR=0xc;
sfrb SPCR=0xd;
sfrb SPSR=0xe;
sfrb SPDR=0xf;
sfrb PIND=0x10;
sfrb DDRD=0x11;
sfrb PORTD=0x12;
sfrb PINC=0x13;
sfrb DDRC=0x14;
sfrb PORTC=0x15;
sfrb PINB=0x16;
sfrb DDRB=0x17;
sfrb PORTB=0x18;
sfrb EECR=0x1c;
sfrb EEDR=0x1d;
sfrb EEARL=0x1e;
sfrb EEARH=0x1f;
sfrw EEAR=0x1e;   
sfrb UBRRH=0x20;
sfrb UCSRC=0X20;
sfrb WDTCR=0x21;
sfrb ASSR=0x22;
sfrb OCR2=0x23;
sfrb TCNT2=0x24;
sfrb TCCR2=0x25;
sfrb ICR1L=0x26;
sfrb ICR1H=0x27;
sfrw ICR1=0x26;   
sfrb OCR1BL=0x28;
sfrb OCR1BH=0x29;
sfrw OCR1B=0x28;  
sfrb OCR1AL=0x2a;
sfrb OCR1AH=0x2b;
sfrw OCR1A=0x2a;  
sfrb TCNT1L=0x2c;
sfrb TCNT1H=0x2d;
sfrw TCNT1=0x2c;  
sfrb TCCR1B=0x2e;
sfrb TCCR1A=0x2f;
sfrb SFIOR=0x30;
sfrb OSCCAL=0x31;
sfrb TCNT0=0x32;
sfrb TCCR0=0x33;
sfrb MCUCSR=0x34;
sfrb MCUCR=0x35;
sfrb TWCR=0x36;
sfrb SPMCR=0x37;
sfrb TIFR=0x38;
sfrb TIMSK=0x39;
sfrb GIFR=0x3a;
sfrb GICR=0x3b;
sfrb SPL=0x3d;
sfrb SPH=0x3e;
sfrb SREG=0x3f;
#pragma used-

#asm
	#ifndef __SLEEP_DEFINED__
	#define __SLEEP_DEFINED__
	.EQU __se_bit=0x80
	.EQU __sm_mask=0x70
	.EQU __sm_powerdown=0x20
	.EQU __sm_powersave=0x30
	.EQU __sm_standby=0x60
	.EQU __sm_ext_standby=0x70
	.EQU __sm_adc_noise_red=0x10
	.SET power_ctrl_reg=mcucr
	#endif
#endasm

typedef void (*IIC_F)(void);								

extern IIC_F MasterOutFunc;									
extern IIC_F ErrorOutFunc;

extern unsigned char     END;

extern unsigned char     i2c_Do;											

extern unsigned char     i2c_Buffer[8			];
extern unsigned char     i2c_index;
extern unsigned char     i2c_ByteCount;

extern unsigned char     i2c_SlaveAddress;
extern unsigned char     i2c_PageAddress[2			];

extern unsigned char     i2c_PageAddrIndex;
extern unsigned char     i2c_PageAddrCount;

extern void Init_i2c(void);

void DoNothing(void);

unsigned char     i2c_Do;								

unsigned char     i2c_Buffer[8			];			
unsigned char     i2c_index;							
unsigned char     i2c_ByteCount;						

unsigned char     i2c_SlaveAddress;						

unsigned char     i2c_PageAddress[2			];	
unsigned char     i2c_PageAddrIndex;						
unsigned char     i2c_PageAddrCount;						

IIC_F MasterOutFunc = &DoNothing;			
IIC_F ErrorOutFunc = &DoNothing;			
unsigned char     END;

interrupt[18]	 void twi(void){							

switch(TWSR & 0xF8)						
{
case 0x00:	
{
i2c_Do |= 0b00000001		;
TWCR = 0<<5|1<<4|1<<7|0			<<6|1<<2|1<<0;  	
(ErrorOutFunc)();
break;
}

case 0x08:	
{
if( (i2c_Do & 0b00001100		)== 0b00000000		)							
{
i2c_SlaveAddress |= 0x01;									
}
else															
{
i2c_SlaveAddress &= 0xFE;									
}

TWDR = i2c_SlaveAddress;													
TWCR = 0<<5|0<<4|1<<7|0			<<6|1<<2|1<<0;  	
break;
}

case 0x10:	
{
if( (i2c_Do & 0b00001100		) == 0b00001000		)						
{
i2c_SlaveAddress |= 0x01;									
}
else
{
i2c_SlaveAddress &= 0xFE;									
}

TWDR = i2c_SlaveAddress;													
TWCR = 0<<5|0<<4|1<<7|0			<<6|1<<2|1<<0;  	
break;
}

case 0x18:	
{
if( (i2c_Do & 0b00001100		) == 0b00000100		)						
{
TWDR = i2c_Buffer[i2c_index];								
i2c_index++;												
TWCR = 0<<5|0<<4|1<<7|0			<<6|1<<2|1<<0;  

}

if( (i2c_Do & 0b00001100		) == 0b00001000		)
{
TWDR = i2c_PageAddress[i2c_PageAddrIndex];					
i2c_PageAddrIndex++;										
TWCR = 0<<5|0<<4|1<<7|0			<<6|1<<2|1<<0;	
}	 
}
break;

case 0x20:	
{
i2c_Do |= 0b00010000		;															
TWCR = 0<<5|1<<4|1<<7|0			<<6|1<<2|1<<0;			

(ErrorOutFunc)(); 														
break;
}

case 0x28: 	
{	
if( (i2c_Do & 0b00001100		) == 0b00000100		)							
{
if (i2c_index == i2c_ByteCount)												
{																		
TWCR = 0<<5|1<<4|1<<7|0			<<6|1<<2|1<<0;	

(MasterOutFunc)();														

}
else
{
TWDR = i2c_Buffer[i2c_index];												
i2c_index++;
TWCR = 0<<5|0<<4|1<<7|0			<<6|1<<2|1<<0;  	
}
}

if( (i2c_Do & 0b00001100		) == 0b00001000		)						
{
if(i2c_PageAddrIndex == i2c_PageAddrCount)					
{
TWCR = 1<<5|0<<4|1<<7|0			<<6|1<<2|1<<0;		
}
else
{														
TWDR = i2c_PageAddress[i2c_PageAddrIndex];				
i2c_PageAddrIndex++;									
TWCR = 0<<5|0<<4|1<<7|0			<<6|1<<2|1<<0;		
}
}	 
}
break;

case 0x30:	
{
i2c_Do |= 0b00000010		;				

TWCR = 0<<5|1<<4|1<<7|0			<<6|1<<2|1<<0;		

(MasterOutFunc)();															

break;
}

case 0x38:	
{
i2c_Do |= 0b00100000		;			

i2c_index = 0;
i2c_PageAddrIndex = 0;

TWCR = 1<<5|0<<4|1<<7|0			<<6|1<<2|1<<0;		
break;																		
}

case 0x40: 
{
if(i2c_index+1 == i2c_ByteCount)								
{
TWCR = 0<<5|0<<4|1<<7|0<<6|1<<2|1<<0;	
}															
else
{
TWCR = 0<<5|0<<4|1<<7|1<<6|1<<2|1<<0;	
}

break;
}

case 0x48: 
{
i2c_Do |= 0b00010000		;															
TWCR = 0<<5|1<<4|1<<7|0			<<6|1<<2|1<<0;			

(ErrorOutFunc)();														
break;
}

case 0x50: 
{ 
i2c_Buffer[i2c_index] = TWDR;			
i2c_index++;

if (i2c_index+1 == i2c_ByteCount)		
{
TWCR = 0<<5|0<<4|1<<7|0<<6|1<<2|1<<0;		
}
else
{
TWCR = 0<<5|0<<4|1<<7|1<<6|1<<2|1<<0;		
}
break;
}

case 0x58:	
{
i2c_Buffer[i2c_index] = TWDR;													
TWCR = 0<<5|1<<4|1<<7|0			<<6|1<<2|1<<0;			

(MasterOutFunc)();																

break;
}

}
}

void DoNothing(void)																
{
END = 0;
}

void Init_i2c(void)							
{
PORTC				 |= 1<<5					|1<<4;			
DDRC &=~(1<<5					|1<<4);
TWBR = 0x0C;                                         
TWSR = 0x00;
}

