C51 COMPILER V7.09   AT24C256                                                              06/22/2006 17:37:20 PAGE 1   


C51 COMPILER V7.09, COMPILATION OF MODULE AT24C256
OBJECT MODULE PLACED IN at24c256.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE at24c256.c OPTIMIZE(SIZE) BROWSE DEBUG OBJECTEXTEND

line level    source

   1          /*
   2          **********************************************************************************************************
             -*********
   3          *                                                               SERIAL EEPROM DRIVER (ATMEL AT24C256 or compatible)
   4          * Description:  IIC-based serial eeprom driver (ATMEL AT24C256 or compatible)
   5          * File:                 at24c256.c
   6          * Software:             Keil C (C51) compiler, Eval version 7.09 under uVision2 IDE v2.40
   7          * Programmer:   John Leung, TechToys Co. (www.TechToys.com.hk)
   8          * Hardware:             AT89S52-gLCD-STK1
   9          * Date:                 14th May 2006
  10          * Version:              1.0
  11          **********************************************************************************************************
             -*********
  12          */
  13          
  14          #include <reg52.h>
  15          #include "iic.h"
  16          #include "at24c256.h"
  17          
  18          /*
  19          **********************************************************************************************************
             -*********
  20          *                                                                                       BYTE WRITE TO AT24C256 EEPROM
  21          * Function:      This function writes a single byte to AT24C256 at the address 'dataptr'
  22          * Argument:     'eeprom_addr'           EEPROM address, 0xA0 if pins A0-A2 tighted to GND
  23          *                       'dataptr'                       address to write
  24          *                       'c'                                     data to write
  25          * Return:       'EEPROM_BUS_ERR'        if any one of those three i2c_SendByte() calls returns an 'noACK'
  26          *                       'EEPROM_BUS_OK'         if no error
  27          *
  28          * Note:         Need a min 5ms write time (consult data sheet on this)
  29          **********************************************************************************************************
             -*********
  30          */
  31          uchar eeprom_WrByte(uchar eeprom_addr, uint dataptr, uchar c)
  32          {
  33   1              uchar dataptrh, dataptrl;
  34   1              
  35   1              dataptrh = (uchar) (dataptr>>8);
  36   1              dataptrl = (uchar) (dataptr);
  37   1      
  38   1              if(i2c_SeqWr(eeprom_addr, dataptrh, dataptrl, &c, 1)==noACK) return (EEPROM_BUS_ERR);
  39   1      
  40   1              return (EEPROM_BUS_OK); 
  41   1      }
  42          
  43          /*
  44          **********************************************************************************************************
             -*********
  45          *                                                               PAGE WRITE A STRING/ARRAY TO AT24C256 EEPROM
  46          *
  47          * Function:     Simply an extension to eeprom_WrByte() above. A string OR and array written to EEPROM.
  48          *                       Length of the string\array should be shorter than EEPROM_PAGE_SIZE; 64 in case of AT24C256.     
  49          *                       A page write is initiated the same as a byte write, but the microcontroller does not send
  50          *                       a stop condition after the first data word is clocked in. Instead, after the EEPROM
C51 COMPILER V7.09   AT24C256                                                              06/22/2006 17:37:20 PAGE 2   

  51          *                       acknowledges receipt of the first data word, the microcontroller can transmit up to 63
  52          *                       more data words.        
  53          *                       The data word address lower 6 bits are internally incremented following the receipt of each
  54          *                       data word. The higher data word address bits are not incremented, retaining the memory page
  55          *                       row location. When the word address, internally generated, reaches the page boundary, the
  56          *                       following byte is placed at the beginning of the same page. If more than 64 data words are
  57          *                       transmitted to the EEPROM, the data word address will ˇ°roll overˇ± and previous data will be
  58          *                       overwritten.
  59          * Argument:     'eeprom_addr'           EEPROM address, being 0xA0 if pins A0-A2 tighted to GND
  60          *                       'dataptr'                       address to write
  61          *                       '*s'                            pointer to the string/array to write
  62          *                       'length'                        length of the string/array
  63          * Return:       'EEPROM_BUS_ERR'        if any one of those i2c_SendByte() calls returns an 'noACK' 
  64                                                                          -OR- length > EEPROM_PAGE_SIZE
  65          *                       'EEPROM_BUS_OK'         if no error
  66          * Note:         Example :
  67          *                                       opcode[5] = {0x12,0x34,0x45,0x56,0x34};
  68          *                                       eeprom_WrPage(0xA0, 0x0000, &s[0], 5);
  69          *
  70          *                       Make a delay of 10ms after a PAGE WRITE!
  71          **********************************************************************************************************
             -*********
  72          */
  73          uchar eeprom_WrPage(uchar eeprom_addr, uint dataptr, uchar *s, uchar length)
  74          {
  75   1              uchar dataptrh, dataptrl;
  76   1      
  77   1              dataptrh = (uchar) (dataptr>>8);
  78   1              dataptrl = (uchar) (dataptr);
  79   1      
  80   1              if (length>EEPROM_PAGE_SIZE) return (EEPROM_BUS_ERR);
  81   1              
  82   1              if (i2c_SeqWr(eeprom_addr, dataptrh, dataptrl, &s[0], length)==noACK) return (EEPROM_BUS_ERR);
  83   1      
  84   1              return (EEPROM_BUS_OK);
  85   1      }
  86          
  87          
  88          /*
  89          **********************************************************************************************************
             -*********
  90          *                                                                                       RANDOM READ FROM AT24C256 EEPROM
  91          *
  92          * Function:     Read a single byte from AT24C256 at the address 'dataptr' and place the result in &s. 
  93          *                       A random read requires a "dummy" byte write sequence to load in the data word address.
  94          *                       Once the device address and data address are clocked in and acknowledged by the EEPROM,
  95          *                       the mcu must generate another start condition. All these encapsulated in i2c_SeqRd().   
  96          * Argument:     'eeprom_addr'           EEPROM address, being 0xA0 if pins A0-A2 tighted to GND
  97          *                       'dataptr'                       address to read from eeprom
  98          *                       '*s'                            pointer to string/array holding the byte read
  99          * Return:       'EEPROM_BUS_ERR'        if any one of those three i2c_SendByte() calls returns an 'noACK'
 100          *                       'EEPROM_BUS_OK'         if no error
 101          * Note: 
 102          **********************************************************************************************************
             -*********
 103          */
 104          uchar eeprom_RdRandomByte(uchar eeprom_addr, uint dataptr, uchar *s)
 105          {
 106   1              uchar dataptrh, dataptrl;
 107   1      
 108   1              dataptrh = (uchar) (dataptr>>8);
 109   1              dataptrl = (uchar) (dataptr);
C51 COMPILER V7.09   AT24C256                                                              06/22/2006 17:37:20 PAGE 3   

 110   1      
 111   1              if(i2c_SeqRd(eeprom_addr, dataptrh, dataptrl, &s[0], 1)==noACK) return (EEPROM_BUS_ERR);
 112   1              
 113   1              return (EEPROM_BUS_OK);
 114   1      }
 115          
 116          /*
 117          **********************************************************************************************************
             -*********
 118          *                                                                               PAGE READ AN ARRAY FROM AT24C256 EEPROM
 119          *
 120          * Function:     This function performs sequential read (page read) by a random address
 121          * Argument:     'eeprom_addr'           EEPROM address, being 0xA0 if pins A0-A2 tighted to GND
 122          *                       'dataptr'                       address to read from eeprom
 123          *                       '*s'                            pointer to the string/array holding the bytes read
 124          *                       'length'                        length of the string/array
 125          * Return:       'EEPROM_BUS_ERR'        if i2c_SeqRd() returns noACK    -OR-    length > EEPROM_PAGE_SIZE
 126          *                       'EEPROM_BUS_OK'         if no error
 127          * Note:
 128          **********************************************************************************************************
             -*********
 129          */
 130          uchar eeprom_RdPage(uchar eeprom_addr, uint dataptr, uchar *s, uchar length)
 131          {
 132   1              uchar dataptrh, dataptrl;
 133   1      
 134   1              dataptrh = (uchar) (dataptr>>8);
 135   1              dataptrl = (uchar) (dataptr);
 136   1      
 137   1              if (length>EEPROM_PAGE_SIZE) return (EEPROM_BUS_ERR);
 138   1      
 139   1              if(i2c_SeqRd(eeprom_addr, dataptrh, dataptrl, &s[0], length)==noACK) return (EEPROM_BUS_ERR);
 140   1      
 141   1              return (EEPROM_BUS_OK);
 142   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    182    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----      19
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)
