
#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

#pragma used+

void delay_us(unsigned int n);
void delay_ms(unsigned int n);

#pragma used-

typedef char *va_list;

#pragma used+

char getchar(void);
void putchar(char c);
void puts(char *str);
void putsf(char flash *str);
int printf(char flash *fmtstr,...);
int sprintf(char *str, char flash *fmtstr,...);
int vprintf(char flash * fmtstr, va_list argptr);
int vsprintf(char *str, char flash * fmtstr, va_list argptr);

char *gets(char *str,unsigned int len);
int snprintf(char *str, unsigned int size, char flash *fmtstr,...);
int vsnprintf(char *str, unsigned int size, char flash * fmtstr, va_list argptr);

int scanf(char flash *fmtstr,...);
int sscanf(char *str, char flash *fmtstr,...);

#pragma used-

#pragma library stdio.lib

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);

#asm
   .equ __lcd_port=0x18 ;PORTB
#endasm

#pragma used+

void _lcd_ready(void);
void _lcd_write_data(unsigned char data);

void lcd_write_byte(unsigned char addr, unsigned char data);

unsigned char lcd_read_byte(unsigned char addr);

void lcd_gotoxy(unsigned char x, unsigned char y);

void lcd_clear(void);
void lcd_putchar(char c);

void lcd_puts(char *str);

void lcd_putsf(char flash *str);

unsigned char lcd_init(unsigned char lcd_columns);

void lcd_control (unsigned char control);

#pragma used-
#pragma library lcd.lib

char lcd_buffer[8];
unsigned int h, l;

void init(void){

lcd_init(8);   

DDRC=0x08;
PORTC=0X00;
delay_ms(1);
PORTC=0x08; 
while(PIND.2==0){}
MCUCR = 0x02;
Init_i2c();    
#asm ("sei");              
}

interrupt[2] void LCD(void) {
GICR = 0x00;  

#asm ("sei");

END =1;
i2c_index = 0;                                
i2c_ByteCount = 4;                
i2c_SlaveAddress = 0x23;                
i2c_PageAddress[0] = 0xE0;                

i2c_PageAddrIndex = 0;                        
i2c_PageAddrCount = 1;                        

i2c_Do = 0b00001000		;                        

TWCR = 1<<5|0<<4|1<<7|0<<6|1<<2|1<<0;        
while (END){}

h = i2c_Buffer[2];
l = i2c_Buffer[3];
sprintf(lcd_buffer,"%2x%2x",h,l);
if(lcd_buffer[2]==0x20) lcd_buffer[2] = 0x30;
lcd_buffer[4] = lcd_buffer[3];
lcd_buffer[3] = 0x2E; 
lcd_clear();
lcd_gotoxy(0,0); 
lcd_puts(lcd_buffer);
lcd_putsf("MHz"); 

GICR = 0x40;         
} 

void main(void){       

init();                         

i2c_index = 0;                                
i2c_ByteCount = 7;                        
END =1; 
i2c_SlaveAddress = 0x22;		

i2c_Buffer[0] = 0xE1;			
i2c_Buffer[1] = 0x80;
i2c_Buffer[2] = 0x22;
i2c_Buffer[3] = 0x60;
i2c_Buffer[4] = 0x2A;
i2c_Buffer[5] = 0x30;
i2c_Buffer[6] = 0x0A;
i2c_Do = 0b00000100		;			

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

while (END){}

delay_ms(50);

i2c_index = 0;                                
i2c_ByteCount = 6;                        
END =1; 
i2c_SlaveAddress = 0x22;		

i2c_Buffer[0] = 0x12;			
i2c_Buffer[1] = 0x00;
i2c_Buffer[2] = 0x40;
i2c_Buffer[3] = 0x00;
i2c_Buffer[4] = 0x00;
i2c_Buffer[5] = 0x30;
i2c_Do = 0b00000100		;			

TWCR = 	1<<5|0<<4|1<<7|0<<6|1<<2|1<<0;
while (END){}
delay_ms(50);

GICR = 0x40;  

while(1){   

}

} 

