Program to Write Data to 24C02 EEPROM Using Arduino Mega (Part 2 of 5)

<<<< Read Previous Part (Part 1)

Arduino program to write Data to 24C02

Complete the circuit given in previous part. Arduino program to write 170 (1 0 1 0 1 0 1 0) to the 255th memory byte (total 256 memory bytes) of 24C02 is given below. Program has three parts. Device selection (If more than one 24C02 is using), Word Selection (Desired memory byte selection from 256 memory bytes) and Data writing. Detailed explanation of the program will be given in coming blogs. Now, upload the following program to your arduino board.

To change the IC number to which data to be written

Value of IC_number can be varied from 0 – 7 in the loop() function. In this case IC_number should be 0.

Screenshot from 2015-01-02 20:56:07

To change the Memory Byte to which data to be written

Value of Memory_byte_address can be varied from 0 – 255 in the loop() function. In this case Memory_byte_address should be 255 to read data from the 255th memory byte of 24C02.

Screenshot from 2015-01-02 20:56:07 (another copy)

To change the Data to be written

Value of Data can be varied from 0 – 255 in the loop() function. In this case Data should be 170 (1 0 1 0 1 0 1 0).

Screenshot from 2015-01-02 20:56:07 (copy)

int sda = 5;
int scl = 6;

int sda_read;
int delay_time=50;

// the setup function runs once when you press reset or power the board
void setup() {
  
  Serial.begin(9600);

  pinMode(scl, OUTPUT);
}

void write_to_24C02(int IC_number, int Memory_byte_address, int Data)
{
  int remainder, i;
  int position_in_binary;
  int binary_of_IC_number[] = {0, 0, 0}; 
  int binary_of_Memory_byte_address[] = {0, 0, 0, 0, 0, 0, 0, 0}; 
  int binary_of_Data[] = {0, 0, 0, 0, 0, 0, 0, 0};
  if(IC_number > 0)
  {
    position_in_binary=2;
    while(IC_number!=1)
    {
      remainder= IC_number %2;
      IC_number= IC_number / 2;
      binary_of_IC_number[position_in_binary] = remainder;
      position_in_binary--;
    }
    
    binary_of_IC_number[position_in_binary]=1;
  }
  
  if(Memory_byte_address > 0)
  {
    position_in_binary=7;
    while(Memory_byte_address!=1)
    {
      remainder= Memory_byte_address %2;
      Memory_byte_address= Memory_byte_address / 2;
      binary_of_Memory_byte_address[position_in_binary] = remainder;
      position_in_binary--;
    }
    
    binary_of_Memory_byte_address[position_in_binary]=1;
  }
  
  if(Data > 0)
  {
    position_in_binary=7;
    while(Data!=1)
    {
      remainder= Data %2;
      Data= Data / 2;
      binary_of_Data[position_in_binary] = remainder;
      position_in_binary--;
    }
    
    binary_of_Data[position_in_binary]=1;
  }
  
  for(i=0;i<3;i++)
  {
    Serial.print(binary_of_IC_number[i]);
  }
  
  pinMode(sda, OUTPUT);

  digitalWrite(sda, HIGH);  //
  delay(delay_time);        //
  digitalWrite(scl, HIGH);  //                
  delay(delay_time);        //
  digitalWrite(sda, LOW);   // Start
  delay(delay_time);        //
  digitalWrite(scl, LOW);   // 
  delay(delay_time);        //
  
  /************** DEVICE ADDRESS STARTS HERE ***************/
  
  digitalWrite(sda, HIGH);  //
  delay(delay_time);        //
  digitalWrite(scl, HIGH);  //  
  delay(delay_time);        //   1   MSB
  digitalWrite(scl, LOW);   //
  delay(delay_time);        //
  
  digitalWrite(sda, LOW);   //
  delay(delay_time);        //
  digitalWrite(scl, HIGH);  //  
  delay(delay_time);        //   2 
  digitalWrite(scl, LOW);   //
  delay(delay_time);        // 
  
  digitalWrite(sda, HIGH);  // 
  delay(delay_time);        // 
  digitalWrite(scl, HIGH);  //  
  delay(delay_time);        //   3 
  digitalWrite(scl, LOW);   //
  delay(delay_time);        //
  
  digitalWrite(sda, LOW);   //
  delay(delay_time);        //
  digitalWrite(scl, HIGH);  //  
  delay(delay_time);        //   4 
  digitalWrite(scl, LOW);   //
  delay(delay_time);        //
  
  digitalWrite(sda, binary_of_IC_number[0]);   //  
  delay(delay_time);
  digitalWrite(scl, HIGH);  //  
  delay(delay_time);        //   5 
  digitalWrite(scl, LOW);   //
  delay(delay_time);        //

  digitalWrite(sda, binary_of_IC_number[1]);   //  
  delay(delay_time);
  digitalWrite(scl, HIGH);  //  
  delay(delay_time);        //   6 
  digitalWrite(scl, LOW);   //
  delay(delay_time);        // 

  digitalWrite(sda, binary_of_IC_number[2]);   //  
  delay(delay_time);        //  
  digitalWrite(scl, HIGH);  //  
  delay(delay_time);        //   7
  digitalWrite(scl, LOW);   //
  delay(delay_time);        //

  digitalWrite(sda, LOW);   //  
  delay(delay_time);        //  
  digitalWrite(scl, HIGH);  //  
  delay(delay_time);        //   8   LSB
  digitalWrite(scl, LOW);   //
  delay(delay_time);        // 

  /************** DEVICE ADDRESS ENDS HERE ***************/

  digitalWrite(scl, HIGH);         //  
  delay(delay_time);               //
  pinMode(sda, INPUT);             //
  Serial.print(digitalRead(sda));  //   ACKNOWLEDGEMENT
  delay(delay_time);               //
  digitalWrite(scl, LOW);          //
  delay(delay_time);               //

  /************** WORD ADDRESS STARTS HERE ***************/
  
  pinMode(sda, OUTPUT);     //
 
  for(i=0;i<8;i++)
  {
    digitalWrite(sda, binary_of_Memory_byte_address[i]);   //  
    delay(delay_time);        //
    digitalWrite(scl, HIGH);  //  
    delay(delay_time);        //   1   MSB
    digitalWrite(scl, LOW);   //
    delay(delay_time);        //
  }
  
  /************** DEVICE ADDRESS ENDS HERE ***************/
  
  digitalWrite(sda, LOW);            //
  delay(delay_time);                 //
  digitalWrite(scl, HIGH);           //  
  delay(delay_time);                 //  
  pinMode(sda, INPUT);               //
  Serial.print(digitalRead(sda));    //   ACKNOWLEDGEMENT
  delay(delay_time);                 //   
  digitalWrite(scl, LOW);            //
  delay(delay_time);                 //

  /************** DATA STARTS HERE ***************/
  
  pinMode(sda, OUTPUT);
  
  for(i=0;i<8;i++)
  {
    digitalWrite(sda, binary_of_Data[i]);  //
    delay(delay_time);                     //
    digitalWrite(scl, HIGH);               //  
    delay(delay_time);                     //   1   MSB
    digitalWrite(scl, LOW);                //
    delay(delay_time);                     // 
  }
  
  /************** DATA ENDS HERE ***************/

  digitalWrite(sda, LOW);            //
  delay(delay_time);                 // 
  digitalWrite(scl, HIGH);           //  
  delay(delay_time);                 //  
  pinMode(sda, INPUT);               //
  Serial.print(digitalRead(sda));    //   ACKNOWLEDGEMENT
  delay(delay_time);                 //   
  digitalWrite(scl, LOW);            //
  delay(delay_time);                 //
  
  pinMode(sda, OUTPUT);  
  digitalWrite(sda, LOW);   //
  delay(delay_time);        //
  digitalWrite(scl, HIGH);  //  
  delay(delay_time);        //   STOP
  digitalWrite(sda, HIGH);  //   
  delay(delay_time);        //

  delay(3000);
  
  return;
}

// the loop function runs over and over again forever
void loop() {
  // Number of the 24C02 IC to which data to be written
  int IC_number = 0;    // In the range 0 - 7
  
  // Memory Byte Address of the 24C02 to which data to be written
  int Memory_byte_address = 255;   // In the range 0 - 255
  
  // Data to be written to specified memory of 24C02
  int Data = 170;       // In the range 0 - 255
  
  Serial.print("Writing ");
  Serial.print(Data);
  Serial.print(" to ");
  Serial.print(Memory_byte_address);
  Serial.print(" th Memory Byte of ");
  Serial.print(IC_number);
  Serial.print(" th 24C02  ");    
  
  // Function to write data to 24C04
  write_to_24C02(IC_number, Memory_byte_address, Data);
  
  Serial.println(" ");
}

If uploading is successful, open your serial monitor. Three zeroes will be displayed in a few seconds delay which indicates the successful writing of data.

Screenshot from 2015-01-02 22:06:57

Continued in Next Part (Part 3) >>>>

(Visited 3,104 times, 1 visits today)
paulpp644
A deep electronic enthusiast who spent most of my time for electronic experiments. I am also interested in publishing my experiments in my blog for helping other electronics enthusiasts.