PICAXE-18X, Use of a Programmed 24LC256 for Lookup Functions

copyright, Peter H Anderson, Baltimore, MD, May, '04


; TEST.ASM
;
; This is a test program to configure a 24LC256 EEPROM with values to illustrate;
;    Table lookup for a byte
;    Word Table Lookup
;    String Lookup
;
; This was assembled using MPLAB which generates a .hex file.  The 24LC256 was then programmed
; using an Olimex PG2 programmer using free software from http://www.ic-prog.com
;
; copyright, Peter H Anderson, Baltimore, MD, May, '04
;

     LIST    P=EEPROM8
     INCLUDE "MEMORY.INC"
     LIST    M=_24LC256

     ORG 0000H

     ; to illustrate the use of a programmed EEPROM to implement a byte lookup table

     DB 30H, 31H, 32H, 33H, 34H, 35H, 36H, 37H
     DB 38H, 39H, 41H, 42H, 43H, 44H, 45H, 46H

     ORG 0100H

     ; to illustrate a word lookup table, most significant byte first

     DB 4301/256, 4301%256	; 4301 decimal
     DB 4379/256, 4379%256
     DB 4432/256, 4432%256
     DB 4499/256, 4499%256
     DB 4552/256, 4552%256
     DB 4602/256, 4602%256

     DB 4662/256, 4662%256
     DB 4731/256, 4731%256
     DB 4791/256, 4791%256
     DB 4812/256, 4812%256
     DB 4877/256, 4877%256
     DB 4905/256, 4905%256


     ORG 0200H

     ; to illustrate string lookup


     DB "PICAX - 18X", 0
     DB "Anderson", 0
     DB "http://www.phanderson.com/picaxe/picaxe.html", 0
     DB "test0", 0
     DB "test1", 0
     DB "the quick brown fox", 0


     END


' EEPROM_1.Bas (PICAXE-18X)
'
' Illustrates reading from a 24LC256 EEPROM.  The content of the EEPROM
' has been programmed using MPLAB and a PIC programmer which will accommodate
' the 24LC256 EEPROM.
'
' Reads locations $0000 - $03ff and displays to terminal in hex format, 16
' bytes per line.
'
' PICAXE               24LC256
'
' SCL (term 10) -------- SCL (term 6)
' SDA (term 7) --------- SDA (term 5)
'
' 4.7K pullup resistors to +5 VDC on both SCL and SDA.
'
' 24LC256 terms A2, A1 and A0 strapped to GRD.  Thus, the device address is %10100000
'
' copyright, Peter H Anderson, Baltimore, MD, May, '04

Symbol MemLocation = W0
Symbol Dat = B2

Symbol Index = B3
Symbol Nibble = B4
Symbol Digit = B5


Main:

   Pause 5000	' to enable user to open the terminal window

   I2CSlave %10100000, I2CFast, I2CWord	' setup for device address $A0,
                          ' 2 byte address for each data byte

   Index = 1
   For MemLocation = $0000 to $03ff
      ReadI2C MemLocation, (Dat)


      ' Debug Dat
      Gosub DisplayHex
      Index = Index + 1
      If Index > 16 then NewLine   ' new line after every 16 bytes
      ' else
      SerTxD (" ")
Main1:
   Next

DONE:
   Goto DONE	' simply loop when complete

NewLine:
    Pause 200
    Index = 1
    SerTxD (13, 10)
    Goto Main1

DisplayHex:
    Nibble = Dat / 16    ' high nibble
    Lookup Nibble, ($30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $41, $42, $43, $44, $45, $46), Digit
                   ' 0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
    SerTxD (Digit)
    Nibble = Dat % 16	  ' low nibble
    Lookup Nibble, ($30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $41, $42, $43, $44, $45, $46), Digit
    SerTxD (Digit)
    Return


' EEPROM_2.Bas (PICAXE-18X)
'
' Illustrates the use of a 24LC256 EEPROM to implement a byte lookup table.
'
' The base address of the data is at BaseAddress ($0000 in this example).  N is added to
' this and the value at that 24LC256 location is read into Digit.  This is displayed.
'
' In this example, this technique is used as a lookup table to convert a value in the
' range of 0 - F to its ASCII character, '0' - '9' and 'A' - 'F'.  This could be done far
' more efficiently using the LookUp command, without the need for an external 24LC256.
' However note that this technique may be used for a lookup table of up to 32K bytes.
'
' Note that the 24LC256 was programmed using MPLab to develop data contained in the EEPROM.
' The 24LC256 was programmed using an Olimex PG2 programmer.
'
'
' PICAXE               24LC256
'
' SCL (term 10) -------- SCL (term 6)
' SDA (term 7) --------- SDA (term 5)
'
' 4.7K pullup resistors to +5 VDC on both SCL and SDA.
'
' 24LC256 terms A2, A1 and A0 strapped to GRD.  Thus, the device address is %10100000
'
' copyright, Peter H Anderson, Baltimore, MD, May, '04

Symbol MemLocation = W0
Symbol N = B2

Symbol Index = B3
Symbol Nibble = B4
Symbol Digit = B5

Symbol BaseAddress = $0000	' this is the beginning of the lookup table

Main:

   Pause 5000	' to enable user to open the terminal window

   I2CSlave %10100000, I2CFast, I2CWord	' setup for device address $A0,
                          ' 2 byte address for each data byte

   Index = 1

   For N = 0 to 254
      Nibble = N / 16
      MemLocation = BaseAddress + Nibble
      ReadI2C MemLocation, (Digit)
      SerTxD (Digit)

      Nibble = N % 16
      MemLocation = BaseAddress + Nibble
      ReadI2C MemLocation, (Digit)
      SerTxD (Digit)

      Index = Index + 1
      If Index > 16 then NewLine   ' new line after every 16 bytes
      ' else
      SerTxD (" ")
Main1:
   Next

DONE:
   Goto DONE	' simply loop when complete

NewLine:
    Pause 200
    Index = 1
    SerTxD (13, 10)
    Goto Main1



' EEPROM_3.Bas (PICAXE-18X)
'
' Illustrates the use of a 24LC256 EEPROM to implement word lookup table.  In this example,
' the table is simply 10 entries.  However, this might be extended to up to 16K words
' using a single 24LC256.
'
' One application is a lookup table to map an A/D value into a temperature.
'
' The base address of the data is at BaseAddress ($0100 in this example).  2*N is added to
' this and the values at the two 24LC256 location are read into H and L.  The resulting
' word is displayed.
'
' Note that this technique may be used for a lookup table of up to 16K two byte words.
'
' Note that the 24LC256 was programmed using MPLab to develop data contained in the EEPROM.
' The 24LC256 was programmed using an Olimex PG2 programmer.
'
'
' PICAXE               24LC256
'
' SCL (term 10) -------- SCL (term 6)
' SDA (term 7) --------- SDA (term 5)
'
' 4.7K pullup resistors to +5 VDC on both SCL and SDA.
'
' 24LC256 terms A2, A1 and A0 strapped to GRD.  Thus, the device address is %10100000
'
' copyright, Peter H Anderson, Baltimore, MD, May, '04

Symbol MemLocation = W0
Symbol N = B2

Symbol L = B4	' note that low byte followed by high byte
Symbol H = B5
Symbol Value = W2	' word made up of H and L

Symbol BaseAddress = $0100	' this is the beginning of the lookup table

Main:

   Pause 5000	' to enable user to open the terminal window

   I2CSlave %10100000, I2CFast, I2CWord	' setup for device address $A0,
                          ' 2 byte address for each data byte


   For N = 0 to 10
      MemLocation = 2 * N + BaseAddress
      ReadI2C MemLocation, (H, L)
      SerTxD (#Value, 13, 10)
   Next

DONE:
   Goto DONE	' simply loop when complete


' EEPROM_4.Bas (PICAXE-18X)
'
' Illustrates the use of a 24LC256 EEPROM to implement the storing of many null
' terminated strings having different lengths.
'
' The base address of the strings is at BaseAddress ($0200 in this example).
'
' Note that the 24LC256 was programmed using MPLab to develop data contained in the EEPROM.
' The 24LC256 was programmed using an Olimex PG2 programmer.
'
' The string to be fetched is identified in variable StringNum.
'
' If the StrNum is 0, the zeroth string up to the next null character is fetched and output.
' Otherwise, each character is fetched and the number of null characters are counted.  When the
' identified string is found, each character is fetched and output up to the next null character.
'
' This technique might be used to store strings for output on an LCD or storing phoneme sequences
' for speech synthesis.
'
' PICAXE               24LC256
'
' SCL (term 10) -------- SCL (term 6)
' SDA (term 7) --------- SDA (term 5)
'
' 4.7K pullup resistors to +5 VDC on both SCL and SDA.
'
' 24LC256 terms A2, A1 and A0 strapped to GRD.  Thus, the device address is %10100000
'
' copyright, Peter H Anderson, Baltimore, MD, May, '04

Symbol MemLocation = W0
Symbol N = W1

Symbol StringNum = B4
Symbol NullCount = B5
Symbol Character = B6

Symbol BaseAddress = $0200	' this is the beginning of the lookup table

Main:

   Pause 5000	' to enable user to open the terminal window

   I2CSlave %10100000, I2CFast, I2CWord	' setup for device address $A0,
                          ' 2 byte address for each data byte

   ' Display strings 0, 3 and then string 1 and 5

   StringNum = 0
   GoSub DisplayStr
   GoSub NewLine

   StringNum = 3
   GoSub DisplayStr
   GoSub NewLine

   StringNum = 1
   GoSub DisplayStr
   GoSub Newline

   StringNum = 5
   GoSub DisplayStr
   GoSub Newline


DONE:
   Goto DONE	' simply loop when complete

NewLine:
   SerTxD (13, 10)
   Return

DisplayStr:
   NullCount = 0

   If StringNum = 0 Then OutStr1	' If its the zeroth string

   ' else

   For N = 0 to $0200
      MemLocation = BaseAddress + N
      ReadI2C MemLocation, (Character)
      If Character <>0 Then DisplayStr1	' read until a null
      ' else
         NullCount = NullCount + 1
         If StringNum = NullCount Then OutStr

DisplayStr1:
   Next
   SerTxD ("String not found")
   Return

OutStr:
   N = N+1	' advance to the first character of the string
OutStr1:
   MemLocation = BaseAddress + N	' fetch characters and display and output
   ReadI2C MemLocation, (Character)
   If Character = 0 Then DisplayStr2
   ' else
   SerTxD (Character)
   GoTo OutStr

DisplayStr2:
   Return