Troubles communicating with a 24AA02 EEPROM chip - Page 1
Forum section: electronics / beginners

author: nicknml
author_rank: Contributor
timestamp: May 25, 2014, 01:23:27 pm
content: I'm using an MSP430g2553 with Energia. I've been trying to write a value to the first byte on my 24AA02 I2C eeprom but for some reason I can't seem to get it to write a value to the first byte, It just keeps returning 255. I've tried using the wire library as opposed to bit banging with no luck (although I have never tried the library before so I could have been doing something wrong.) I've run similar code to communicate with a DS1621 and it worked fine.Either I am doing something incredibly stupid or the eeprom chip is picky over something. Yes, I did make sure that the write protect pin is pulled to ground.Datasheet:http://ww1.microchip.com/downloads/en/DeviceDoc/21709c.pdfCode:define sda 9 //data line#define sck 10 //clock lineint value=0;void setup(){pinMode(sck,OUTPUT);pinMode(sda,OUTPUT);digitalWrite(sck,HIGH);digitalWrite(sda,HIGH);Serial.begin(9600);delay(3000);writedata1();}void loop(){delay(500);readdata();}void debug(){boolean status=0;pinMode(sda,INPUT);status=digitalRead(sda);if(status==0){Serial.println("okay");}else{Serial.println("error");}pinMode(sda,OUTPUT);}void writedata1(){start();digitalWrite(sda,HIGH);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,HIGH);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW); //writeclk();debug();clk();//ignoring ACK//this part will be redone addressdigitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();debug();clk();//ignore ack//valuedigitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,HIGH);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();debug();clk();//ignore ackstop();}void readdata(){start();digitalWrite(sda,HIGH);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,HIGH);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW); //writeclk();//debug();clk();//ignoring ACKdigitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();//debug();clk();//ignore ack//stop(); //teststart();digitalWrite(sda,HIGH);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,HIGH);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,HIGH); //readclk();//debug();clk();//ignoring ACKpinMode(sda,INPUT);boolean raw=0;raw=digitalRead(sda);//clk();if(raw==1){value=value+128;}raw=digitalRead(sda);clk();if(raw==1){value=value+64;}raw=digitalRead(sda);clk();if(raw==1){value=value+32;}raw=digitalRead(sda);clk();if(raw==1){value=value+16;}raw=digitalRead(sda);clk();if(raw==1){value=value+8;}raw=digitalRead(sda);clk();if(raw==1){value=value+4;}raw=digitalRead(sda);clk();if(raw==1){value=value+2;}raw=digitalRead(sda);clk();if(raw==1){value=value+1;}pinMode(sda,OUTPUT);clk();stop();Serial.println(value);value=0;}void start(){pinMode(sda,OUTPUT);digitalWrite(sda,LOW);//delay(1);digitalWrite(sck,LOW);}void stop(){pinMode(sda,OUTPUT);digitalWrite(sck,HIGH);//delay(1);digitalWrite(sda,HIGH);}void clk(){digitalWrite(sck,HIGH);//delay(5);digitalWrite(sck,LOW);//delay(5);}

author: nicknml
author_rank: Contributor
timestamp: May 25, 2014, 03:12:35 pm
content: I've played a bit more with the Wire library functions and did get it to work. After some additional testing it looks like my bit bang sketch is actually successfully writing, so now it looks like the issue lies with my read function somehow.

author: nicknml
author_rank: Contributor
timestamp: May 25, 2014, 05:04:21 pm
content: Fixed! In the read function apparently I had to place pinMode(sda,INPUT) after the first clock cycle of issuing the control byte read command. instead of after the second cycle. This isn't an issue with the DS1621 but for some reason it made a huge difference.Code:#define sda 9 //data line#define sck 10 //clock lineint data=0;void setup(){pinMode(sck,OUTPUT);pinMode(sda,OUTPUT);digitalWrite(sck,HIGH);digitalWrite(sda,HIGH);Serial.begin(9600);delay(3000);//writedata1();}void loop(){delay(500);readdata1();}void debug(){boolean status=0;pinMode(sda,INPUT);status=digitalRead(sda);if(status==0){Serial.println("okay");}else{Serial.println("error");}pinMode(sda,OUTPUT);}void writedata1(){start();digitalWrite(sda,HIGH);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,HIGH);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW); //writeclk();clk();//ignoring ACK//this part will be redone addressdigitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();clk();//ignore ack//datadigitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,HIGH);clk();digitalWrite(sda,LOW);clk();clk();//ignore ackstop();//delay(10);}void readdata1(){start();digitalWrite(sda,HIGH);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,HIGH);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW); //writeclk();debug();clk();//ignoring ACKdigitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();clk();//ignore ackstop(); //teststart();digitalWrite(sda,HIGH);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,HIGH);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,LOW);clk();digitalWrite(sda,HIGH); //readclk();debug();//####WORKING LOCATION######pinMode(sda,INPUT);clk();//ignoring ACK//pinMode(sda,INPUT); //original locationboolean raw=0;raw=digitalRead(sda);//clk();if(raw==1){data=data+128;}clk();raw=digitalRead(sda);if(raw==1){data=data+64;}clk();raw=digitalRead(sda);if(raw==1){data=data+32;}clk();raw=digitalRead(sda);if(raw==1){data=data+16;}clk();raw=digitalRead(sda);if(raw==1){data=data+8;}clk();raw=digitalRead(sda);if(raw==1){data=data+4;}clk();raw=digitalRead(sda);if(raw==1){data=data+2;}clk();raw=digitalRead(sda);if(raw==1){data=data+1;}//pinMode(sda,OUTPUT);clk();pinMode(sda,OUTPUT);//digitalWrite(sda,LOW);//clk();stop();Serial.println(data);data=0;}void start(){pinMode(sda,OUTPUT);digitalWrite(sda,LOW);digitalWrite(sck,LOW);}void stop(){pinMode(sda,OUTPUT);digitalWrite(sck,HIGH);digitalWrite(sda,HIGH);}void clk(){digitalWrite(sck,HIGH);digitalWrite(sck,LOW);}