2 Replies Latest reply on Sep 2, 2014 10:51 AM by TOMMY_Z

    Read&Write 24CXXX I2C EEPROM with Galileo and Show at LCD1602

    TOMMY_Z

      This is just a small toy, can be used at Any application you want to read and write 2 address from the EEPROM, in this sample for each address will read/write 10 char.

      For example you can use it to clean/Show some PCB's Cycle Counter, or any specific EEPROM address you want to show and clean

      Just first experience to use arduino, also first experience to Galileo , The code isn't that good yet, you can make change here and there, and you can coach me to learn more new stuff.

      图像 11.png

      #include <Wire.h>
      #include <LiquidCrystal.h>
      const int ledPin =  13;      // the number of the LED pin
      const int buttonADD2 = 7;     // the number of the pushbutton pin
      const int buttonADD1 = 8;     // the number of the pushbutton pin
      // variables will change:
      int buttonADD2State = 0;         // variable for reading the pushbutton status
      int buttonADD1State = 0;         // variable for reading the pushbutton status
      // initialize the library with the numbers of the interface pins
      LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
      byte val = 0;
      void setup()
      {
        pinMode(ledPin, OUTPUT);   
        pinMode(buttonADD1, INPUT);   
        pinMode(buttonADD2, INPUT);     
         Wire.begin(); // join i2c bus as master
         Serial.begin(9600);  // start serial for output
         //lcd.print("123456789012345678901234567890123456789012"); //40char each line
         //lcd.print("34");
        testADD2();
        testADD1();
        show();
      }
      void show()
      {
         lcd.begin(16, 2);
        lcd.print("ADD1: ");
        showpin();
        lcd.print("123456789012345678901234");
        lcd.print("ADD2: ");
        showpm();
      }
      void clearADD1()
      {
        //I2C Write Timing - Start Write 7 bit address-> Write 16 bit Data address -> Data data data -> End
         
        //send IC Address
        Wire.beginTransmission(0xA0>>1); // transmit to device #50 (0xA0)//7bit throw the last bit
                                     // device address is specified in datasheet
        //Send Data Address              
        Wire.write(0x7D);   //Data Address
        Wire.write(0x17);   //Data Address
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);    
        Wire.endTransmission();     // stop transmitting
        delay(500);
      }
      void clearADD2()
      {
        //I2C Write Timing - Start Write 7 bit address-> Write 16 bit Data address -> Data data data -> End
         
        //send IC Address
        Wire.beginTransmission(0xA0>>1); // transmit to device #50 (0xA0)//7bit throw the last bit
                                     // device address is specified in datasheet
        //Send Data Address              
        Wire.write(0x7D);   //Data Address
        Wire.write(0x0C);   //Data Address
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);  
        Wire.write(0x30);    
        Wire.endTransmission();     // stop transmitting
        delay(500);
      }
      void testADD2()
      {
        //I2C Write Timing - Start Write 7 bit address-> Write 16 bit Data address -> Data data data -> End
         
        //send IC Address
        Wire.beginTransmission(0xA0>>1); // transmit to device #50 (0xA0)//7bit throw the last bit
                                     // device address is specified in datasheet
        //Send Data Address              
        Wire.write(0x7D);   //Data Address
        Wire.write(0x0C);   //Data Address
        Wire.write(0x30);  
        Wire.write(0x31);  
        Wire.write(0x32);  
        Wire.write(0x33);  
        Wire.write(0x34);  
        Wire.write(0x35);  
        Wire.write(0x36);  
        Wire.write(0x37);  
        Wire.write(0x38);  
        Wire.write(0x39);    
        Wire.endTransmission();     // stop transmitting
        delay(500);
      }
      void testADD1()
      {
        //I2C Write Timing - Start Write 7 bit address-> Write 16 bit Data address -> Data data data -> End
         
        //send IC Address
        Wire.beginTransmission(0xA0>>1); // transmit to device #50 (0xA0)//7bit throw the last bit
                                     // device address is specified in datasheet
        //Send Data Address              
        Wire.write(0x7D);   //Data Address
        Wire.write(0x17);   //Data Address
        Wire.write(0x30);  
        Wire.write(0x31);  
        Wire.write(0x32);  
        Wire.write(0x33);  
        Wire.write(0x34);  
        Wire.write(0x35);  
        Wire.write(0x36);  
        Wire.write(0x37);  
        Wire.write(0x38);  
        Wire.write(0x39);    
        Wire.endTransmission();     // stop transmitting
        delay(500);
      }
      void showpm()
      {
        //send IC Address
        Wire.beginTransmission(0xA0>>1); // transmit to device #44 (0x2c)//7bit throw the last bit
                                     // device address is specified in datasheet
        //Send Data Address
        Wire.write(0x7D);   //Data Address
        Wire.write(0x17);
         Wire.endTransmission();     // stop transmitting
         delay(500);
         //Read Data from IC
         Wire.requestFrom(0xA0>>1, 10);    // request 6 bytes from slave device #50 0xA0
         while(Wire.available())    // slave may send less than requested
         { 
           char c = Wire.read();    // receive a byte as character
           lcd.write(c);
           Serial.print(c);         // print the character
         }
         delay(500);
         
         
         
      }
      
      void showpin()
      {
        //send IC Address
        Wire.beginTransmission(0xA0>>1); // transmit to device #44 (0x2c)//7bit throw the last bit
                                     // device address is specified in datasheet
        //Send Data Address
        Wire.write(0x7D);   //Data Address
        Wire.write(0x0C);
         Wire.endTransmission();     // stop transmitting
         delay(500);
         //Read Data from IC
         Wire.requestFrom(0xA0>>1, 10);    // request 6 bytes from slave device #50 0xA0
         while(Wire.available())    // slave may send less than requested
         { 
           char c = Wire.read();    // receive a byte as character
           lcd.write(c);
           Serial.print(c);         // print the character
         }
         delay(500);
         
         
         
      }
      void showpcb()
      {
        //send IC Address
        Wire.beginTransmission(0xA0>>1); // transmit to device #44 (0x2c)//7bit throw the last bit
                                     // device address is specified in datasheet
        //Send Data Address
        Wire.write(0x00);   //Data Address
        Wire.write(0x01);
         Wire.endTransmission();     // stop transmitting
         delay(500);
         //Read Data from IC
         Wire.requestFrom(0xA0>>1, 10);    // request 6 bytes from slave device #50 0xA0
         while(Wire.available())    // slave may send less than requested
         { 
           char c = Wire.read();    // receive a byte as character
           lcd.write(c);
           Serial.print(c);         // print the character
         }
         delay(500);
         
         
         
      }
      
      void loop()
      {
          // read the state of the pushbutton value:
        buttonADD2State = digitalRead(buttonADD2);   //might need to add pullup/pulldown resistor
        buttonADD1State = digitalRead(buttonADD1);
         if (buttonADD2State == HIGH) { 
              digitalWrite(ledPin, HIGH);  
               clearADD1();
               show();
        } 
        else if (buttonADD1State == HIGH){
              digitalWrite(ledPin, HIGH);  
               clearADD2();
               show();
      
        }  
        else {}
      }
      
      
        • 1. Re: Read&Write 24CXXX I2C EEPROM with Galileo and Show at LCD1602
          DiegoV_Intel

          Hi TOMMY_Z,

           

          It looks very good, thanks for sharing with the community. I will give you some friendly suggestions about the code:

           

              1. The functions clearADD1() and clearADD2() are very similar except for one line (line 43 in clearADD1() and line 66 in clearADD2()) so you can use only one function cleanADD(ADDx) where ADDx could be ADD1=0x17 or ADD2=0x0C and add this line inside the function instead: Wire.write(ADDx) (x value is 1 or 2). ADD1 and ADD2 would be constants with their values defined at the beginning of the code.

           

              2. The functions testADD1() and testADD2() are very similar too so you can do the same like in cleanADDx functions.

           

              3. The functions showpm(), showpin() and showpcb() are also very similar. You can use only one function show(x) where x could be PM=0x7D17, PIN=0x7D0C or PCB=0001 and add this two lines inside the function instead:

           

                              Wire.write(x>>8);        // First send  8bits MSB

                              Wire.write(x);              // Then send 8bits LSB

           

              PM, PIN and PCB would be constants with their values defined at the beginning of the code.

           

          Let me know if these suggestions are helpful. Have a nice day!

           

          Regards,

          Diego.

          • 2. Re: Read&Write 24CXXX I2C EEPROM with Galileo and Show at LCD1602
            TOMMY_Z

            Thanks So much Diego, Those are all very helpful!!

            I will give it a try.