the CODE is below.
I use eeprom routine beciase it is also working on I2C.
I can read any register. but can not write.
volatile unsigned char pir='a',pir1='a';
void eeprom_write(unsigned char adrr , unsigned char val )
{
//init_start
TWCR=164;
while (!(TWCR & (1<<TWINT)));
//SLAVE write command
TWDR=0x42;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
// adress write command
TWDR=adrr;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
// data write command
TWDR=val;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
//stop
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}
unsigned char eeprom_read(unsigned char adrr)
{
TWCR=164; //start
while (!(TWCR & (1<<TWINT)));
//SLAVE write command
TWDR=0x42;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
TWDR=adrr;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
TWCR=164;
while (!(TWCR & (1<<TWINT)));
//SLAVE adress read command
TWDR=0x43;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
TWCR = (1<<TWINT) | (1<<TWEN) ;//| (1<<TWEA);
//TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
pir=TWDR;
//stop
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
return pir;
}
void main()
{
TWBR=16;
TWSR=(0<<TWPS1) | (0<< TWPS0); //aprox 153 KHZ
USART1_Init( MYUBRR);
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
_delay_ms(1000);
eeprom_read(0x0A);
send1(((pir & 0xF0)>>4)+0x30);
send1((pir & 0x0F)+0x30);
eeprom_read(0x0B);
send1(((pir & 0xF0)>>4)+0x30);
send1((pir & 0x0F)+0x30);
eeprom_read(0x1C);
send1(((pir & 0xF0)>>4)+0x30);
send1((pir & 0x0F)+0x30);
eeprom_read(0x1D);
send1((pir ));
//read 0x51 reg
eeprom_read(0x51);
send1((pir ));
//write 0x51 reg
eeprom_write(0x51,0x81);
_delay_ms(100);
//read again 0x51
eeprom_read(0x51);
send1((pir ));
}
---------------------------------------
Posted through
http://www.EmbeddedRelated.com