Interfacing with A 24LC256 EEPROM (I2C)


' Program E24_256_1.BAS (BASICX)
'
' Illustrates interface with 24LC256 EEPROM using I2C interface
'
' Writes data to EEPROM and then reads and displays on serial interface.
' Illustrates Random Write and Random Read.
'
' Uses Modules I2C.bas and SerialPort.bas.
'
' Note that the SDA_PIN and SCL_PIN are defined in I2C.bas
'
' SDA_PIN (term 3 on BASICX) --------------SDA (term 5 on 24LC256)
' SCL_PIN (term 2 on BASICX) --------------SCL (term 6 on 24LC256)
'
' May be used with 24LC32, LC64, LC128 or LC256.
'
' Device	Max Address	Capacity
' 24LC32	&H0fff		4096 bytes
' 24LC64	&H1fff		8192
' 24LC128	&H3fff		16384
' 24LC256	&H7fff		32768
'
' Straping of terminals A2, A1 and A0 determines the device address.
'
' copyright David Seebold, Baltimore, MD, Sept, '99

Public Sub Main()
        
   Dim mem_adr as New UnsignedInteger
   Dim dev_adr As Byte
   
   Dim dat as Byte
   Dim I As Byte

   Call OpenSerialPort(2, 9600)
   dev_adr = 0		' A2, A1, A0 strapped to 0, 0, 0

   Do	' Continual loop
   ' write five values to EEPROM
      mem_adr = &H06FF	' EEPROM address to begin writing
      For I = 0 To 4 
         dat = &Hff - I	' make up some values 
         
         Call write_to_eeprom(dev_adr, mem_adr, dat)
         mem_adr = mem_adr + 1	' next EEPROM address
      Next
	
   ' now read them and display     
      mem_adr = &H06FF	' EEPROM address to begin reading 
      For I = 0 To 4 
         
         Call read_from_eeprom(dev_adr, mem_adr, dat)

         Call PutB(dat)		' display the values
         Call NewLine()
		
	 mem_adr = mem_adr + 1
      Next

      Call Sleep (0.5)	' pause to admire
  Loop

End Sub

'-----------
'-----------

Public Sub write_to_eeprom(ByVal dev_adr As Byte, _
			      ByVal mem_adr As UnsignedInteger, _
			      ByVal dat as Byte)

   Dim y as Byte, mem_adr_hi as Byte, mem_adr_lo as Byte	

   Call SplitInt(mem_adr, mem_adr_hi, mem_adr_lo) 
                ' separate mem_adr into high and low bytes
   Call I2C_start()
   Call I2C_out_byte((&Ha0) + (dev_adr * 2))
   Call I2C_nack()
   Call I2C_out_byte(mem_adr_hi)
   Call I2C_nack()
   Call I2C_out_byte(mem_adr_lo)
   Call I2C_nack()
   Call I2C_out_byte(dat)
   Call I2C_nack()
   Call I2C_stop()
   Call Sleep(0.025)	' allow time for programming

End Sub

'----------

Public Sub read_from_eeprom(ByVal dev_adr As Byte, _
			    ByVal mem_adr As UnsignedInteger, _
			    ByRef dat as byte)

   Dim y as Byte, mem_adr_hi as Byte, mem_adr_lo as Byte	
	
   Call SplitInt(mem_adr, mem_adr_hi, mem_adr_lo) 
                ' separate mem_adr into high and low bytes
       
   Call I2C_start()
   Call I2C_out_byte((&Ha0 ) + (dev_adr * 2))
   Call I2C_nack()
   Call I2C_out_byte(mem_adr_hi)
   Call I2C_nack()
   Call I2C_out_byte(mem_adr_lo)
   Call I2C_nack()
				' no intermediate stop
   Call I2C_start()
   Call I2C_out_byte(&Ha1 + (dev_adr * 2))
   Call I2C_nack()
   Call I2C_in_byte(dat) 	' note, no ACK
	
   Call I2C_stop()

End Sub

'-----------

Public Sub SplitInt(ByVal a as UnsignedInteger, _
                    ByRef hi as Byte, _
                    ByRef lo as Byte)

' separate an integer into high and low bytes

   Dim val as New UnsignedInteger
   Dim RAM_Ptr as Integer

   val = a	' Note that as variable a (unsigned long) is passed by ref, it is
		' write protected
   RAM_Ptr = MemAddress(val)
   lo = RAMpeek(RAM_Ptr)		' lo byte followed by high byte
   hi = RAMpeek(RAM_Ptr+1)

End Sub