Announcement

Collapse
No announcement yet.

Writing to External EEPROM

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Writing to External EEPROM

    Hello:

    I would like to write integers to specific addresses on an I2C EEPROM, can you give me some help as to how I might do this. I have written strings by using a buffer, but I need to have integers at specific addresses this time.

    Thanks for your help,

    Mark

  • #2
    Does this thread help? http://forum.4dsystems.com.au/forum/...xternal-eeprom
    Mark

    Comment


    • #3
      Yes, the example that I found was very helpful. I figured out what was happening. The 4D program was sending a string to the eeprom and the arduino sketch I was using was converting the string into ascii code but only the first byte. So I am making progress, I just need to parse the string on the arduino side and convert the string into the correct number of digits. It's a bit confusing if you are a novice to figure out what is happening looking at a serial monitor given all of the different formats a number can be represented in.

      Comment


      • #4
        I am trying to write integers to I2C eeprom addresses. The integers are chars generated from a keyboard. If I use this function:

        Code:
        func GetNumber(var str)
          var ch, n;
          n := 0 ;
          while ( str_GetC(&str, &ch))
            if ((ch > '9') || (ch < '0')) break;
            n := n * 10;
            n := n + (ch -'0');
          wend
          return n;
        endfunc
        and then try:

        Code:
        to(I2C); print(n);
        I2C_Write(EEPROM);
        as part of the eeprom example I previously found here, I get crazy results, the example takes a string and writes it to a buffer that is written to an address. I can do that, but I want to save these as integers not as a string. I want integers that can be read by an arduino into another function. Do I have to change the string over to an integer by the arduino after it reads the address ? What would be the best way to store these as integers before saving to the eeprom?

        Thanks for the help,

        Mark

        Comment


        • #5
          You are telling 4DGL to write a string to the I2C device, surely you meant to use something like
          I2C_Putn(&n,2) ;
          or even
          I2C_Write(n >> 8) ;
          I2C_Write(n) ;
          Mark

          Comment


          • #6
            yes, that's what I needed. Thank you

            Comment


            • #7
              I could use some help in reading an integer from an eeprom address. This is what I have so far, from examples found in the forum:

              Code:
              func Read_Data()
              
                var address, digit2;
                address := 0;
                I2C_Open(I2C_SLOW); // 100khz
                I2C_Idle(); //Ensure Module is Idle
                I2C_Start(); //Generate Start Condition
                I2C_Write(EEPROM); //Write Control byte (24LC256)
                I2C_Write(HIbyte(address)); //Write Hi Address
                I2C_Write(LObyte(address)); //Write Low Address
                
                I2C_Getn(address,2);
                I2C_Stop(); // finished writing, Initiate Write Cycle
                I2C_Idle(); //Wait for the bus
                
                Int := I2C_Read();
                I2C_Ack();
                digit2 := I2C_Read();
                I2C_Ack();
                I2C_Nack(); // finished reading, send Not Ack
                I2C_Stop(); // Send Stop Condition
                I2C_Close();
                
                digit2 << 8;
                Int := Int | digit2;
                print(Int);
              
              endfunc
              The plan, once I can read one address, is to read multiple addresses and copy the values to several variables in my program. Can you help me sort this out please.
              Thanks for your help,

              Mark

              Comment


              • #8
                Maybe
                digit2 <<= 8;
                Mark

                Comment


                • #9
                  Thanks, that helped me unravel this a bit. I still can't get the correct number from an address however. Here's what I have so far:

                  Code:
                  func readbyte(var address)
                  
                    I2C_Idle(); //Wait for bus Idle
                    
                    I2C_Start(); //Generate Start condition
                    I2C_Write(EEPROM); //send control byte for write
                    I2C_Idle(); //Wait for bus Idle
                    I2C_Write(HIbyte(address)); //Send High Address
                    I2C_Idle(); //Wait for bus Idle
                    I2C_Write(EEPROM);
                    I2C_Write(LObyte(address)); //Send Low Address
                    I2C_Idle(); //Wait for bus Idle
                    
                    I2C_Restart(); //Generate Restart
                    I2C_Write(EEPROM+1); //send control byte for Read
                    I2C_Idle(); //Wait for bus Idle
                    
                    r := I2C_Read(); //Read Length number of bytes to Data
                    
                    I2C_Nack(); //send Not Ack
                    I2C_Stop(); //Send Stop Condition
                    
                    return r;
                  
                  endfunc
                  
                  func Read_Data()
                    
                    var address, digit2;
                    address := 0;
                    I2C_Open(I2C_SLOW); // 100khz
                    I2C_Idle(); //Ensure Module is Idle
                    I2C_Start(); //Generate Start Condition
                    I2C_Write(EEPROM); //Write Control byte (24LC256)
                    I2C_Write(HIbyte(address)); //Write Hi Address
                    I2C_Write(LObyte(address)); //Write Low Address
                    
                    
                    I2C_Stop(); // finished writing, Initiate Write Cycle
                    I2C_Idle(); //Wait for the bus
                    
                    Int := readbyte(address);
                    I2C_Ack();
                    digit2 := readbyte(address);
                    I2C_Ack();
                    I2C_Nack(); // finished reading, send Not Ack
                    I2C_Stop(); // Send Stop Condition
                    I2C_Close();
                    
                    digit2 <<= 8;
                    Int := Int | digit2;
                  
                  endfunc
                  The problem is that when I save the integers 10,2,3 to addresses 0, 1, 2 and then try to read address zero, I get this:

                  20560 instead of 10. Can you see what I am missing? I verify what is stored on the EEPROM with a sketch that is running on an arduino. Any help would be appreciated.

                  Thank you,

                  Mark

                  Comment


                  • #10
                    Hi Mark,

                    Here is a code fragments that reads/writes data to an 24C256 EEPROM. The program below writes 10 values from an array to the EEPROM from address 0 to 9. Notice that there are I2C_Idle() commands inserted in between the I2C commands for timing purposes. I hope this helps in some way.

                    Regards,
                    Bryan

                    Code:
                    /**************************************************************************************************** *************************************************/
                    
                    #platform "uLCD-43PT"
                    #inherit "4DGL_16bitColours.fnc" 
                    
                    _data[10] :=[0xAA, 0x55, 0x33, 0xEE, 0x30, 0x31, 0x34, 0x44, 0x20, 0x34];
                    
                    #constant SPEED I2C_SLOW //100kHz
                    #constant SPEED_2 I2C_FAST
                    #constant address 0x57
                    
                    var flag:=0;
                    
                    func main()
                    
                      // eeprom checking cycle
                      var n, flag, addressWrite, value;
                      touch_Set(TOUCH_DISABLE); // need to disable 
                      pause(100);
                      
                      addressWrite := address << 1; //slave address appended with 0 R/W bit
                      I2C_Open(SPEED);
                      
                      for(n := 0, flag := 0; n <= 100 && flag != 1; n++) //test if I2C slave device is responding (up to 100 times).
                        
                        I2C_Idle();
                        I2C_Start();
                        
                        if(I2C_Write(addressWrite) == 1) //if the slave responds with an ACK
                          flag := 1; //set flag to 1 to exit the loop
                        endif
                        
                        I2C_Stop();
                      next
                    
                      // eeprom writing cycle
                      if(flag == 1)    
                        
                        for(n:=0; n<= 10; n++)     
                          
                          I2C_Idle();
                          I2C_Start();
                          I2C_Write(addressWrite);
                          I2C_Idle();
                          I2C_Write(0x00);
                          I2C_Idle();
                          I2C_Write(n);
                          I2C_Idle();
                          I2C_Write(_data[n]);
                          I2C_Idle();
                          I2C_Stop();
                          txt_MoveCursor(12,5) ;
                          print(" WRITING.....");
                          print("\n WRITING EEPROM : ", [HEX2Z] _data[n]);    
                        
                        next
                          
                        I2C_Stop();
                        I2C_Close();
                        txt_MoveCursor(13,5) ;
                        print(" DEVICE WRITING DONE... "); 
                      
                      endif
                        
                      I2C_Stop();
                      I2C_Close();
                      
                      flag := 0;
                    
                    
                      ///////////////////////////////////////////////////////////////////////////////////// 
                      /*
                      re-check again to make sure device is still connected
                      */
                      addressWrite := address << 1; //slave address appended with 0 R/W bit
                      I2C_Open(SPEED);
                      
                      for(n := 0, flag := 0; n <= 100 && flag != 1; n++) //test if I2C slave device is responding (up to 100 times).
                        I2C_Idle();
                        I2C_Start();
                        
                        if(I2C_Write(addressWrite) == 1) //if the slave responds with an ACK
                          flag := 1; //set flag to 1 to exit the loop
                        endif
                        
                        I2C_Stop();
                      next
                      
                      // read and compare cycle
                      for(n:= 10; n >= 0; n--)
                        I2C_Idle();
                        I2C_Start();
                        I2C_Write(address << 1); //slave address appended with R/W bit (set to 0)
                        I2C_Idle();
                        I2C_Write(0x00);
                        I2C_Idle();
                        I2C_Write(n);
                        I2C_Idle();
                        I2C_Stop();
                        
                        I2C_Start();
                        I2C_Write((address << 1) + 1); //slave address appended with R/W bit (set to 1)
                        value := I2C_Read();
                        I2C_Idle();
                        I2C_Stop();
                        txt_MoveCursor(16,5) ;
                        print(" READING.....");
                        print("\n READING EEPROM : ", [HEX2Z] value);
                        
                        if(value != _data[n]) 
                          print("\n ERROR READING EEPROM ADDRESS: ", n);
                        endif
                      next
                      
                      repeat
                      forever
                      
                    endfunc

                    Comment


                    • #11
                      Bryan,

                      Thanks for the help, this is just what I am looking for.

                      Regards,

                      Mark

                      Comment

                      Working...
                      X