Example: How to Use Page Write Feature of I2C EEPROMs


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185

    Default Example: How to Use Page Write Feature of I2C EEPROMs

    Code:
    <font color="#000080"><i>' Omer YILDIZ , Istanbul/TURKEY
    ' May-2008
    ' Sayzer @ www.picbasic.co.uk/forum
    
    ' This code is to show how to use the page write feature of I2C eeproms.
    ' Page write feature allows us to write an entire page at one shot,
    ' and takes one write cycle to complete.
    ' This code will accept 128-byte packet from RS232 port @9600bps,
    ' and write the entire packet into 24C512 external eeprom at one shot.
    ' Incoming 128-bytes packets must have at least 15mS intervals.
    
    ' Page size is different from eeprom to eeprom.
    ' For example: Page size for 24C512 is 128 bytes while 
    ' it is 32 bytes for 24LC64.
    ' That means, we can write 128-bytes into the eeprom 
    ' at once and it takes only approx 5 - 10 ms.
    ' If we had to do this one byte at a time, we would have to wait 
    ' approx. 1 second to complete.
    ' 1 second may not sound that long but, 
    ' think about 64,000 byte waiting to be written !!
    
    ' PIC18F452
    ' Set your fuse settings up here.
    </i><b>DEFINE </b></font>OSC <font color="#FF0000"><b>20
    
    </b></font><font color="#000080"><i>'Thanks to DT
    </i><b>INCLUDE </b></font><font color="#008000"><b>&quot;DT_INTS-18.bas&quot;    </b></font><font color="#000080"><i>' Base Interrupt System    
    </i><b>INCLUDE </b></font><font color="#008000"><b>&quot;ReEnterPBP-18.bas&quot; </b></font><font color="#000080"><i>' Include if using PBP interrupts
    
    'Thanks to Steve.
    </i><b>DEFINE </b></font>HSER_RCSTA <font color="#FF0000"><b>90</b></font>h       <font color="#000080"><i>' Enable serial port &amp; continuous receive  
    </i><b>DEFINE </b></font>HSER_TXSTA <font color="#FF0000"><b>24</b></font>h       <font color="#000080"><i>' Enable transmit, BRGH = 1
    </i><b>DEFINE </b></font>HSER_SPBRG <font color="#FF0000"><b>129       </b></font><font color="#000080"><i>' 9600 Baud @ 0,16%
    </i><b>DEFINE </b></font>HSER_CLROERR <font color="#FF0000"><b>1       </b></font><font color="#000080"><i>' Clear overflow automatically
    
    'Thanks to MicroChip.
    </i></font>ADCON1 = <font color="#FF0000"><b>7
    </b></font>TrisA = <font color="#FF0000"><b>%000000
    </b></font>TrisB = <font color="#FF0000"><b>%00000000
    </b></font>TrisC = <font color="#FF0000"><b>%10000000   </b></font><font color="#000080"><i>'RC7 =&gt; RS232 RX (input pin).
    </i></font>TrisD = <font color="#FF0000"><b>%00000000
    </b></font>TrisE = <font color="#FF0000"><b>%000
    
    </b></font>PortA = <font color="#FF0000"><b>0
    </b></font>PortB = <font color="#FF0000"><b>0
    </b></font>PortC = <font color="#FF0000"><b>0
    </b></font>PortD = <font color="#FF0000"><b>0
    </b></font>PortE = <font color="#FF0000"><b>0
    
    </b></font>I2C_SCL   <font color="#000080"><b>VAR </b></font>PortC.<font color="#FF0000"><b>3
    </b></font>I2C_SDA   <font color="#000080"><b>VAR </b></font>PortC.<font color="#FF0000"><b>4
    
    </b></font>AdrW      <font color="#000080"><b>VAR WORD  </b><i>' Address variable for write operation.
    </i></font>AdrR      <font color="#000080"><b>VAR WORD  </b><i>' Address variable for read operation.
    
    </i></font>Mem       <font color="#000080"><b>VAR BYTE</b></font>[<font color="#FF0000"><b>1350</b></font>] <font color="#000080"><i>' Create an array as large as possible.
    </i></font>RXFlag    <font color="#000080"><b>VAR BIT        </b><i>' Just a flag.
    
    </i></font>Writetime <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>15    </b></font><font color="#000080"><i>' This is the write cycle time for external eeprom. (mS)
    </i></font>PageSize  <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>128   </b></font><font color="#000080"><i>' Page size for 24C512 : 128 bytes; for 24LC64 : 32 bytes.
    
    
    '====This is DT's famous and useful Int routine. =========
    </i><b>ASM
    </b></font><font color="#008000">INT_LIST  macro    </font><font color="#000080"><i>; IntSource,  Label,  Type, ResetFlag?
            </i></font><font color="#008000">INT_Handler   RX_INT,  _ReadPort,   PBP,  yes
        endm
        INT_CREATE           </font><font color="#000080"><i>; Creates the interrupt processor
    </i><b>ENDASM
    
    </b></font><font color="#008000">@ INT_ENABLE  RX_INT     </font><font color="#000080"><i>; Enable RX232 RX interrupt
    
    
    </i></font>Begin:  <font color="#000080"><i>' Initial adr values are zero.
        </i></font>RXFlag  = <font color="#FF0000"><b>0          
        </b></font>AdrW    = <font color="#FF0000"><b>0        
        </b></font>AdrR    = <font color="#FF0000"><b>0
    
    </b></font>Start:
        
        <font color="#000080"><b>I2CREAD </b></font>I2C_SDA, I2C_SCL,<font color="#FF0000"><b>$A1</b></font>,AdrR, [<font color="#000080"><b>STR </b></font>Mem\PageSize]    
         <font color="#000080"><i>' Read one page from EEPROM and store into mem array. 
        </i></font>AdrR = AdrR + PageSize   <font color="#000080"><i>'First  page: 0-127
                                 'Second page: 0 +128   = 128 (128 - 255)
                                 'Third  page: 128 +128 = 256 (256 - 383)
                                     '....etc..    
        ' Do your stuff ....
        
    </i><b>GOTO </b></font>Start
    
    
    ReadPort:
        <font color="#000080"><b>HSERIN </b></font>[<font color="#000080"><b>STR </b></font>Mem\PageSize]   
        <font color="#000080"><i>' Store 128-byte (0-127) incoming packet into Mem array.
    
        </i><b>I2CWRITE </b></font>I2C_SDA, I2C_SCL,<font color="#FF0000"><b>$A0</b></font>,AdrW,[<font color="#000080"><b>STR </b></font>Mem\PageSize]  
         <font color="#000080"><i>' Starting from addresss location = Adr, 
          ' ...write 128-byte mem elements into EEPROM at one shot. 
           '...Adr is the start address of the page in line.
    
        </i><b>PAUSE </b></font>Writetime   <font color="#000080"><i>' For 24C512, 15ms is more then good enough.
                          ' For LC series, 5-6 ms should be ok.
        </i></font>AdrW = AdrW + PageSize  
        <font color="#000080"><i>' For the next page write, increment EEPROM address 
         ' ...with the size of one page.  
      
        </i></font>RXFlag = <font color="#FF0000"><b>1            </b></font><font color="#000080"><i>' Set a flag for use in main loop.
        
    </i></font><font color="#008000">@ INT_RETURN  
    
    
    
    </font>
    Attached Files Attached Files
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Thanks Sayzer,
    I will try this out.
    JS
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,802


    Did you find this post helpful? Yes | No

    Default

    Hi Sayzer.

    As the PBP manual states, the control byte must have bit 0 cleared. Is there any special reason that you have made it 1 in this command?

    I2CREAD I2C_SDA, I2C_SCL,$A1,AdrR, [STR Mem\PageSize]

    But here you still have it cleared:

    I2CWRITE I2C_SDA, I2C_SCL,$A0,AdrW,[STR Mem\PageSize]

    Regards,
    Ioannis

  4. #4
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Ioannis View Post
    Hi Sayzer.

    As the PBP manual states, the control byte must have bit 0 cleared. Is there any special reason that you have made it 1 in this command?

    I2CREAD I2C_SDA, I2C_SCL,$A1,AdrR, [STR Mem\PageSize]

    But here you still have it cleared:

    I2CWRITE I2C_SDA, I2C_SCL,$A0,AdrW,[STR Mem\PageSize]

    Regards,
    Ioannis


    Hi Ioannis,

    The way I read the data sheet of 24C512 eeprom, the write bit is 0 and the read bit is 1.
    Same as 24LC64, 24LC512 also.

    I agree that it is confusing, but it is a fact that for read operation either 0 or 1 works anyway.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,802


    Did you find this post helpful? Yes | No

    Default

    Yes, the data sheet clearly states that bit 0 defines read or write. But PBP manual on I2C commands, says that this bit must be kept at 0. May be the compiler takes care of it. Anyway, I am sure it works just fine.

    Ioannis

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    PBP handle the R/W bit for you.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. I2C Master/Slave 16F88/16F767 working code
    By DanPBP in forum Code Examples
    Replies: 2
    Last Post: - 23rd October 2012, 22:31
  2. HARDWARE I2C SAMPLE CODE question
    By Michael Wakileh in forum Code Examples
    Replies: 2
    Last Post: - 16th June 2009, 21:07
  3. EEPROM & I2C Page Write
    By Bill Legge in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 20th May 2009, 09:18
  4. Changing declared variables names on the fly
    By jessey in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 16th December 2006, 06:34
  5. error on compiling
    By parker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th June 2005, 14:31

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts