News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Positron - Writing and Reading a 24C1024 I2C EEPROM

Started by top204, Jul 30, 2022, 08:12 AM

Previous topic - Next topic

top204

With a 24C1024 EEPROM, its address range goes above 16-bits, and standard I2C protocol cannot send an address greater than 65535 (16-bits). So it uses bit-1 of the slave address, commonly named the P0 bit. See the Device Addressing section in the various datasheets for the 24C1024 devices.

If the P0  bit is clear, the address range is 0 to 65535, and if set, the address range is 65536 to 131071. So this method still works with a standard I2C protocol that cannot send more than 16-bits for the address. The program listing below shows two simple procedures that will access a 24C1024 EEPROM and display what it is writing and reading on a serial terminal:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Write and read a 24C1024 1M-bit I2C EEPROM
' Written for the Positron8 BASIC compiler by Les Johnson
'
    Device = 18F26K22                       ' Tell the compiler what device to compile for
    Declare Xtal = 16                       ' Tell the compiler what frequency the device is operating at
'
' Setup USART1
'
    Declare Hserial1_Baud = 9600            ' Setup USART1 to operate at 9600 Baud
    Declare HRSOut1_Pin = PORTC.6           ' Tell the compiler what TX pin is used for USART1
'
' Setup the pins used by Busin and Busout
'
    Declare SDA_Pin = PORTC.4               ' The pin used for Busout/Busin SDA
    Declare SCL_Pin = PORTC.3               ' The pin used for Busout/Busin SCL
'
' Create some variables
'
    Dim bLoop    As Byte                    ' Used as a loop counter
    Dim lAddress As Long                    ' Holds the 24-bit address to read and write from/to the EEPROM
    Dim bByteIn  As Byte                    ' Holds the byte read from the EEPROM

'----------------------------------------------------------------------
' The main program starts here
' Write and read a 24C1024 1M-bit I2C EEPROM
' Transmits the bytes read on a serial terminal at 9600 Baud
'
Main:
'
' Write bytes to the 24C1024 EEPROM
'
    HRSOutLn "Writing"
    lAddress = $010000                      ' Set the start address for writing (above 16-bits)

    For bLoop = 0 To 9                      ' Create a loop to write data to the EEPROM
        AT1024_Write8(lAddress, bLoop)      ' Write a byte value to the EEPROM
        HRSOutLn IHex6 lAddress, ":",       ' \
                 Dec bLoop                  ' / Transmit the byte written to a serial terminal
        Inc lAddress                        ' Increment the address
    Next                                    ' Close the loop
'
' Read bytes from the 24C1024 EEPROM
'
    HRSOutLn "Reading"
    lAddress = $010000                      ' Set the start address for reading (above 16-bits)

    For bLoop = 0 To 9                      ' Create a loop to read data from the EEPROM
        bByteIn = AT1024_Read8(lAddress)    ' Read a byte from the EEPROM
        HRSOutLn IHex6 lAddress, ":",       ' \
                 Dec bByteIn                ' / Transmit the byte read to a serial terminal
        Inc lAddress                        ' Increment the address
    Next                                    ' Close the loop
    Stop

'----------------------------------------------------------------------
' Read a byte from a 24C1024 EEPROM
' Input     : pAddress holds the 24-bit address to read
' Output    : Returns the byte read from the EEPROM
' Notes     : Bit-1 of the slave address is set if the address is over 16-bits
'
Proc AT1024_Read8(pAddress As Long), Byte
    Dim bSlave As Byte                  ' Holds the I2C slave address
    bSlave = %10100000                  ' Setup the slave as an EEPROM Write command
    If pAddress.Byte2 > 0 Then          ' Is the address over 65535?
        bSlave.1 = 1                    ' Yes. So set bit-1 of the slave address
    EndIf

    BStart                              ' Send a Start condition
    BusOut bSlave                       ' Setup the slave as an EEPROM Write
    BusOut pAddress.Byte1               ' Send the high byte of the address
    BusOut pAddress.Byte0               ' Send the low byte of the address
    BReStart                            ' Send a Restart condition
    bSlave.0 = 1                        ' Set the slave address for a read operation
    BusOut bSlave                       ' Send the slave address to the EEPROM
    Result = BusIn                      ' Read the value from the EEPROM
    BusNack                             ' Send a Not-Acknowledge condition
    BStop                               ' Send a Stop condition
EndProc

'----------------------------------------------------------------------
' Write a byte to a 24C1024 EEPROM
' Input     : pAddress holds the 24-bit address to write
'           : pValue holds the 8-bit value to write to the EEPROM
' Output    : None
' Notes     : Bit-1 of the slave address is set if the address is over 16-bits
'
Proc AT1024_Write8(pAddress As Long, pValue As Byte)
    BStart                              ' Send a Start condition
    If pAddress.Byte2 > 0 Then          ' Is the address over 65535?
        BusOut %10100010                ' Yes. So set bit-1 of the slave address
    Else                                ' Otherwise... The address is below or on 16-bit
        BusOut %10100000                ' So... Clear bit-1 of the slave address
    EndIf
    BusOut pAddress.Byte1               ' Send the high byte of the address
    BusOut pAddress.Byte0               ' Send the low byte of the address
    BusOut pValue                       ' Send the value to the EEPROM
    BStop                               ' Send a Stop condition
    DelayMS 5                           ' Wait for the data to be entered into EEPROM matrix
EndProc

The above program is very simple, and only writes single bytes, but this can be advanced to write/read other value sizes and also write/read in a paged fashion. See how bit-1 of the I2C slave address is manipulated based upon the address value?

Below is a screenshot of the above program working within a simulator:

24C1024 EEPROM.jpg 

The source code files for the above program, and the Proteus project files, are attached below.