Hey guys I have been trying to interface my atmega aTony system with an external serial atmel eeprom! its specs are:
2k - 256b x 8
Two Wire interface
datasheet
----
Im basicly trying to just write some data (hardcoded) into the eeprom then read it back threw RS232 USART, while searching for some helper code I came across this code:
void twiStart (void) { TWCR = _BV(TWINT) | _BV(TWSTA) | _BV (TWEN); } void twiStop (void) { TWCR = _BV(TWINT) | _BV(TWSTO) | _BV (TWEN); } void twiWait (void) { while( !(TWCR & _BV(TWINT))); } void twiSendDataByte (uint8_t data) { TWDR = data ; TWCR = _BV(TWINT) | _BV(TWEN); } uint8_t twiEEPROMReadByte (word adress) { twiStart(); twiWait(); twiSendDataByte (AT24C64ADRESS); twiWait(); twiSendDataByte (adress >> 8); // set eeprom adres high twiWait(); twiSendDataByte (adress & 0xff); // set eeprom adres low twiWait(); twiStart(); twiWait(); twiSendDataByte (AT24C64ADRESS+1); // LSB bit is responsible for read/write operation twiWait(); TWCR = _BV(TWINT) | _BV(TWEN); twiWait(); twiStop(); return TWDR; } void twiEEPROMWriteByte (word adress, uint8_t data) { twiStart(); twiWait(); twiSendDataByte (AT24C64ADRESS); twiWait(); twiSendDataByte (adress >> 8); // set eeprom adres high twiWait(); twiSendDataByte (adress & 0xff); // set eeprom adres low twiWait(); twiSendDataByte (data); twiWait(); twiStop(); // wait for write operation finish. it can took up to 5ms // wait for stop condition while(!(TWCR & _BV(TWSTO))); while(!(PINC & 0x20)); while(1) { twiStart(); twiWait(); twiSendDataByte (AT24C64ADRESS); twiWait(); if (TWSR == 0x18) break; } twiStop(); } void twiInit (void) { // 400khz@16MHZ CPU CLOCK // TWBR MSUT BE >= 10 TWBR = 12; TWSR = 0; /* TWSR=0x00; TWBR=0x00; TWAR=0x00; TWCR=0x04; */ PORTC |= 0x10 | 0x20; DDRC |= 0x10 | 0x20; } ......... //heres the code im trying to execute: uint8_t valueToWrite = 133; uint8_t b; twiEEPROMWriteByte (0x0, valueToWrite); b = twiEEPROMReadByte (0x0); printf_P(PSTR("\n\raTony>eeprom>value at address [0x%x] is '$i'\n\r"),valueToWrite,b); .......
but when I try to use it, my terminal/avr just freezes! heres my terminal output:
aTony> Please Enter a Command: ** User> cmd> \eeprom
aTony> Looking Up Command 'eeprom', Please Wait...
aTony> Writeing then Reading eeprom, please wait...
(now it feezes nothing else)
You can see the whole source file on my public pastebing @
http://www.ursrc.com/index.php?s...
or
http://post.ursrc.com/ph0rkeh
(not includeing my lcd and usart code, just the main file)
and
heres a simple shematic of the pins for the eeprom
http://post.ursrc.com/shematic.jpg
(like i said simple ;), where I got the source it said to ground all the pins exept SDA SCL and of course VCC)