• PicŪ Basic


  • 24LC256 I2C eeprom memory

    The 24LC256 I2C eeprom memory is well known. I wrote this library for a new project and I would to share it to make life easier for PDS users. This device has a page write capability of up to 64 bytes of data. It is capable of both random and sequential reads up to the 256K boundary. Functional address lines allow up to eight devices on the same I2C bus, for up to 2 Mbit address space.
    Write/read data in a single memory location is easy but nothing efficient. It is interesting to know that for all the memories, a page of 8 bytes will never overlap 2 pages of 64 or 128 bytes.

    Number of pages for the 24LC256.
    32768 Bytes/ 8 = 4096 pages of 8 bytes
    32768 Bytes/ 64 = 512 pages of 64 bytes

    Number of pages for the 24LC512.
    65536 Bytes/ 8 = 8192 pages of 8 bytes
    65536 Bytes/ 128 = 512 pages of 128 bytes

    Commands:

    From Les's idea.
    M24LC256_Present(NumberOfDevice(Byte), ACKReturn(Byte))
    If ACKReturn = 0 then the memory is present.

    Write/Read one Byte only.
    WriteByte_E2P(NumberOfDevice(0-7), Address(Word), Data(Byte), ReturnNACK(Byte))

    Var1 = ReadByte_E2P(NumberOfDevice(0-7), Address(Word), ReturnNACK(Byte))
    The "ReturnNACK" is not mandatory.

    Write/Read a page of 8 Bytes. (array 8 bytes)
    WritePage_E2P8(NumberOfDevice(0 to 7), ArrayName(Output), NumberPage(Word), ReturnNACK(Byte))
    The particularity of this command is all the data that are selected from an array.

    ReadPage_E2P8(NumberOfDevice(0 to 7), ArrayName(Input), NumberPage(Word), ReturnNACK(Byte))
    The particularity of this command is that all the data is restored to an array.
    The "ReturnNACK" is not mandatory.

    Write/Read a page of 64 Bytes. (array 64 bytes)
    WritePage_E2P64(NumberOfDevice(0 to 7), ArrayName(Output), NumberPage(Word), ReturnNACK(Byte))

    ReadPage_E2P64(NumberOfDevice(0 to 7), ArrayName(Input), NumberPage(Word), ReturnNACK(Byte))
    The "ReturnNACK" is not mandatory.

    For the 24LC512 memory.
    Write/Read a page of 128 Bytes. (array 128 bytes)
    WritePage_E2P128(NumberOfDevice(0 to 7), ArrayName(Output), NumberPage(Word), ReturnNACK(Byte))

    ReadPage_E2P128(NumberOfDevice(0 to 7), ArrayName(Input), NumberPage(Word), ReturnNACK(Byte))
    The "ReturnNACK" is not mandatory.

    Why is it simpler and faster to use an array?
    We can see this with an example: DS1307 Clock Calendar chip.

    The followed bytes must be saved to the eeprom memory. One 8 bytes page could be represented (that is perfect).
    Code:
    Dim RTC_Second    As Byte    ' Byte 0, Second (0..59)        
    Dim RTC_Minute    As Byte    ' Byte 1, Minute (0..59)
    Dim RTC_Hour      As Byte    ' Byte 2, Hour    (0..11 or 0..23)
    Dim RTC_DayOfWeek As Byte    ' Byte 3, Day of the week (1..7)
    Dim RTC_Day       As Byte    ' Byte 4, Date  (0..31)
    Dim RTC_Month     As Byte    ' Byte 5, Month (1..12)
    Dim RTC_Year      As Byte    ' Byte 6, Year  (0..99)
    The Control Byte is not used    ' Byte 7
    But is it difficult to pass these bytes to the EEPROM? Not at all if we change these variables like this.
    Code:
    Dim DS1307_Array[8] As Byte
    Dim RTC_Second    As DS1307_Array#0    ' Byte 0, Second (0..59)        
    Dim RTC_Minute    As DS1307_Array#1    ' Byte 1, Minute (0..59)
    Dim RTC_Hour      As DS1307_Array#2    ' Byte 2, Hour    (0..11 or 0..23)
    Dim RTC_DayOfWeek As DS1307_Array#3    ' Byte 3, Day of the week (1..7)
    Dim RTC_Day       As DS1307_Array#4    ' Byte 4, Date  (0..31)
    Dim RTC_Month     As DS1307_Array#5    ' Byte 5, Month (1..12)
    Dim RTC_Year      As DS1307_Array#6    ' Byte 6, Year  (0..99)
    The Control Byte is not saved           ' Byte 7, Free for saving a Temperature of another data.
    Result: Nothing to do... to pass all data to the eeprom memory.

    Write a Page to the 24LC256 eeprom memory.
    Save the Temperature value to the DS1307_Array#7 Byte. (an example)
    DS1307_Array#7 = Temperature

    WritePage_E2P8(0, DS1307_Array, 0, ReturnNACK)
    8 bytes has been written to the eeprom in page 0, memory number 0 (A0, A1, A2) with Time/Date & Temperature.
    The "ReturnNACK" is not mandatory.

    Read a Page from the 24LC256 eeprom memory.
    ReadPage_E2P8(0, DS1307_Array, 0, ReturnNACK)

    Restore the temperature from the memory.
    Temperature = DS1307_Array#7
    The "ReturnNACK" is not mandatory

    The DS1307_Array is reloaded from the eeprom memory, nothing to transform, it is ready to print.

    Particularity of the DS1307:
    If the data chosen is all decimal then you can modify the format of the DS1307 compressing the data table and as a result get 2 bytes available for data storage. But this is an other story. (Coming soon)

    The code is prepared to drive the 24LC512 memory.
    $define _24LC512_
    Include "24LC256_H.inc"

    The code is protected againts interrupts if needed. But using the Hardware I2C is very sure. If you really need then:
    $define _EnableGIE_Save_
    Include "24LC256_H.inc"

    For the Software I2C.
    $define _EnableGIE_Save_
    Include "24LC256_S.inc"

    $define _EnableGIE_Save_
    Include "24LC256_S2.inc"

    As usual I give a "24LC256_H.inc" file for Hardware I2C (using HBusOut/HBusIn commands) and also a "24LC256_S.inc" file (using BusOut/BusIn commands) for the sotware I2C. If this command is too fast (for the Amicus18 board) you can use the "24LC256_S2.inc" file (using I2COut/I2Cin commands) without ACK control.

    Conclusion:
    This new library can work easily with 32KB or 64KB eeprom memories. Only data bytes are passed with the help of arrays.
    Alberto
    Update 15/03/2016 Version 1.2
    Changed the name of the _HRSOutVersionFile_ to _HRSOutVersionFile_inc_
    Added configuration of the I2C bus pins to digital for the PIC18F2xF22 series.
    Added some minor changes for compatibility with others libraries.

    Download the code from the Files Server.
  • Recent Activity

    xldaedalus-432

    Mysterious PORTB problem

    Thread Starter: xldaedalus

    I'm using Proton+ to develop firmware for a product with switches. The MCU is an 18F26K22. Most of the switches reside on PORTB. I am NOT using a...

    xldaedalus Yesterday, 23:49 Go to last post
    tumbleweed-20435

    Watchdog timer

    Thread Starter: joesaliba

    I have a code that basically looks for four input and four outputs, depends on various timing and input conditions. I use interrupt and some delays...

    tumbleweed Yesterday, 19:54 Go to last post