Shop OBEX P1 Docs P2 Docs Learn Events
how to read and write to eeprom 24LC32A? — Parallax Forums

how to read and write to eeprom 24LC32A?

eagletalontimeagletalontim Posts: 1,399
edited 2009-01-10 20:47 in General Discussion
I just got my eeprom chips in today from Parallax and I cannot find any documentation on them on how to hook them up or to read/write to them [noparse]:([/noparse] I need help getting started please [noparse]:)[/noparse]

Comments

  • JonnyMacJonnyMac Posts: 9,214
    edited 2009-01-09 01:44
    You're kidding, right? Somehow you are able to use the Internet to post questions here but not locate information about the 24LC32?... that's funny. There's this little company out here in California called Google -- you might might want to look into them. tongue.gif

    I use EEPROMs in a lot of projects; the framework I've attached works fine with the 24LC32.

    [noparse][[/noparse]Edit] Added a bit of demo code to the framework -- just to make it absolutely clear.

    Post Edited (JonnyMac) : 1/9/2009 1:59:30 AM GMT
  • JonnyMacJonnyMac Posts: 9,214
    edited 2009-01-09 03:54
    All kidding aside, you really should spend a few minutes learning to use Google.
  • Dave HeinDave Hein Posts: 6,347
    edited 2009-01-09 16:18
    eagletalontim,

    I think your request is reasonable for this forum.· Look at the sticky threads at the top of the SX forum.· The thread labeled "Best SX Threads ..." has lots of valuable information posted by others.· There are several posts related to using EEPROMs.· The sticky thread is located at http://forums.parallax.com/showthread.php?p=642725·.

    I did some work with EEPROMs a couple of years ago.· I spent a lot of time searching for information on the internet using AltaVista (I prefer it over Google).· I downloaded the data sheet for the EEPROM and studied it.· I couldn't get my code to work until someone on the forum pointed out that there was sample code in the SX tutorial book for an I2C interface.· I hadn't made the connection that a serial EEPROM device uses the I2C protocol.· I got my code working within hours after getting that valuable information.

    Good luck on your project.· Please continue to post any questions you might have.

    Dave
  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-01-10 00:41
    thanks for the links [noparse]:)[/noparse] I am finally able to write to the eeprom and can read back information that is stored using the program sent above. A few things I still have a slight misunderstanding of is erasing the data on a certain address. I tried putting PUT_EE eeAddr, "" and it did not erase what was put there. Also....If I store a word variable say "1234" to address 0, what would be the next storage address?

    I know I will probably be told to Google it, but 90% of the time, my searches come up with things that are not exactly what I am looking for and they are not coded in SX/B. I am still learning the SX/B language so I have to ask questions to help me get farther.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2009-01-10 00:51
    There is definitely an I2C example in SX/B Help.

    The address following Address 0?· It's Address 1.

    An address can only hold a Byte, 8 bits.· A·Word is two Bytes, 16 bits.· So, you have to break that into two Bytes (highbyte, lowbyte) and store each in a separate Address.

    Post Edit -- If you want to "erase" the contents of an address, then write $FF to it (or 00.)

    Post Edited (PJ Allen) : 1/10/2009 12:57:30 AM GMT
  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-01-10 14:06
    There must be something wrong with my PUT_EE sub. I cannot get the print sub it to display a simple number like 12 unless I add quotations around it. I am wanting to save a byte variable first, then be able to pull it from the eeprom and run it through some math function and then display it on the LCD. The reason I made the print sub is so that when I have another chip talking to the display chip, I can send 3 parameters to it and it will know what line and character to put the text on. It just makes it easier for me that way.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2009-01-10 14:36
    "I cannot get the print sub it to display a simple number like 12 unless I add quotations around it."

    The Serial LCD understands everything you send it to be ASCII data; sometimes it's character·code (letters, numerals, etc.), sometimes it's control code (line_feed, carriage return, etc.)

    SEROUT'ing·a·decimal 12 (which is really a $0C), the SX knocks out a $0C, a control code for Form Feed.· SEROUT'ing·a "12" makes the SX knock out the character codes for the numerals inside the quote_marks, a $31 and then a $32 for a numeral 1 and a numeral 2.
    ·
  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-01-10 14:50
    ok, that makes a little more sense [noparse]:)[/noparse] I am still a little confused on how the PUT_EE sub works.

    Here is the code :

    SUB PUT_EE
      tmpW1 = __WPARAM12
      tmpB1 = __PARAM3
    
      I2C_START
      I2C_OUT SlaveWr                               ' send slave ID
      I2C_OUT tmpW1_MSB                             ' send address, high byte
      I2C_OUT tmpW1_LSB                             ' send address, low byte
      I2C_OUT tmpB1                                 ' send data byte
      I2C_STOP                                      ' finish
    
      '{$IFNDEF NOEEWAIT}
      DO                                            ' let write cycle finish
        I2C_START
        I2C_OUT SlaveWr
      LOOP UNTIL ackNak = Ack
      '{$ENDIF}
    
      ENDSUB
    



    Now, I have tried to put PUT_EE eeAddr, 12 and it prints on the LCD "<<". If I put PUT_EE, eeAddr, "12" it puts "11" on the LCD. I am guessing it is something wrong with my print sub then? I have been searching the help files and the forum and still cannot quite grasp what is going on.

    Here is my updated "print" sub :

    print:
      IF __PARAMCNT = 3 THEN
        text = __PARAM1                            ' get string offset
        temp3 = __PARAM2
        temp4 = __PARAM3
        tmpB1 = 2
      ELSE
        text = __WPARAM12                            ' get string offset
        temp3 = __PARAM3
        temp4 = __PARAM4
        tmpB1 = 3
      ENDIF
    
    
      LCD_OUT LcdLine1
      IF temp3 = 1 THEN
        LCD_OUT LcdLine1
      ELSEIF temp3 = 2 THEN
        LCD_OUT LcdLine2
      ENDIF
      FOR temp5 = 1 TO temp4
        LCD_OUT LcdRt
      NEXT
      LCD_OUT LcdBkSpc
      
      IF tmpB1 = 3 THEN
        DO
          READ text, temp5                     ' read a character
          IF temp5 = 0 THEN EXIT                      ' if 0, string complete
          SEROUT LcdTx, LcdBaud, temp5
          INC text                                   ' point to next character
        LOOP
      ELSE
        DO WHILE tmpB1 > 0
          SEROUT LcdTx, LcdBaud, text
          DEC tmpB1
        LOOP
      ENDIF
      RETURN
    
  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-01-10 17:16
    well, i am fixing to give up. Google does not have my answers and I can't seem to understand how others have gotten it to work. I am so confused on how to use a byte and a word. From what I understand, a byte is a number 0 - 255, and a word can be a number 0 - 65535. To pass a byte to a sub or function, you need to grab it with __PARAM# and to pass a word, you need to grab it with __WPARAM12. Apparently, a byte cannot be shown on an LCD but a word can. Since a Byte is 0 - 255, each number (2, 5, 5) will have to be saved in different addresses in an eeprom??? I have tried to save the number 12 to the eeprom, but I never get 12 back. To display the byte that is received back from the eeprom, it will have to be converted to a word which I have no idea how to do. I have browsed through all the help files and examples and have only found how to either display a full string of text OR display a number.....not both.

    I need to be able to....

    1 - Save a 0 - 255 number to the eeprom.
    2 - Read the number back.
    3 - Display the number on the LCD along with some pre-defined text.
    4 - Use the number in a few math functions and display the result on the LCD.
  • soshimososhimo Posts: 215
    edited 2009-01-10 17:40
    @Eagle - might I gently suggest that you start off small and perhaps study a little bit about the architecture of computers, microprocessers, and microcontrollers. The fact that you are confused about bit/byte/word and storage sizes implies that you might not be as familiar with the architecture of computers to understand whats going on. It's important to know a little bit about that little chip you are programming. If you treat it as a black box then everything seems like magic, but once you learn how it "thinks" and "talks" then you can probably figure a lot of these things out on your own and that's always a much better feeling than having your hand held IMHO. The primary reason you are not finding what you need is because you are asking too broad of a question from google. Basically you are asking google "what the heck is programming and what is this SX/B thing?" Of course google is going to give you a bunch of stuff because you asked such a broad question. The fact is, if you learn a bit more about the architecture you will be in a much better position to ask the question you need to ask to get the answer you want.

    That being said, I would start off on in the tutorial section. Download all the labs. Read any and all manuals and books that came with your SX/B - there might be something valuable in there. I know the homework board had a book with it call "What's a Microcontroller" that would be a big help for you as well. The only thing I can suggest is read, read, read.

    I forgot to add.

    byte, word, int, long - these are all storage sizes and are high level contstructs. This is an 8bit microcontroller so it only understands bytes. Technically a word should be 8 bits since a word is defined as the inherit storage size of the architecture - in this case 8 bits, but I think they treat it as an int with 16 bits.

    So, to recap:

    bit = 1 binary bit, 1 or 0
    nibble = 4 bits or half a byte
    byte = 8 bits

    char = byte or 8 bits
    word = 2 bytes or 16 bits
    int = 2 bytes or 16 bits
    long = 4 bytes or 32 bits

    Unsigned just means that the MSB is used for the value and not a sign bit which doubles your positive range at the sacrifice of your negative range. All storage types (char, word, int, long) can be unsigned or signed.

    More editing - sorry
    The reason you have to pass the parameter differently is because of the storage size. Again, if you read a bit about how functions are called (low level stuff) you will realize that the called function has to retrieve the parameters from registers (or stack, but that's a different beast). Since the registers are 8 bits only you have to fill two registers in order to pass a word or integer value (it requires 2 moves of data from program memory into registers). On the caller side, the caller has to also know how to retrieve those values, hence why you declare the subs and functions before hand. The compiler will generate code to properly push and pull those parameters based on your declarations and proper usage of PARAM.

    Post Edited (soshimo) : 1/10/2009 5:53:08 PM GMT
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2009-01-10 17:56
    Sometimes you need to walk away and have a pop, blow it off, come back later.

    My suggestion is that you get the LCD'ing right, all on its own.· Get comfortable with displaying some CONstants (CON), VARs, or just plug numbers.· Then you can progress to the I2C reading/writing.·

    You can save 255 in one address, but·displaying a number of two or more digits will require parsing, separating,·that number's digits and sending each digit's ASCII code.

    Post Edit -- Another member was Replying while I was in the process and my missive here is unrelated.

    Quitting is always an option.

    Post Edited (PJ Allen) : 1/10/2009 6:03:27 PM GMT
  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-01-10 18:56
    thanks for the info! I got up for a bit to cool off and now I have restarted all my code and am working with just the LCD right now. I will wait for the eeprom later. After viewing the SEROUT example, I have been able to come up with the ability to put text and variables on the screen at the same time [noparse]:)[/noparse] I only have one problem that I cannot figure out.... Anywhere that a character is not defined on the LCD, it puts a \. On line 1, I have the text "Working..." but on the display it shows "Working...\\\\\\". Any ideas what is causing that?

    DEVICE          SX28, OSCXT2, TURBO, STACKX, OPTIONX
    FREQ            4_000_000
    
    
    ' -------------------------------------------------------------------------
    ' IO Pins
    ' -------------------------------------------------------------------------
    
    LcdTx        VAR    RA.2            ' LCD serial connection
    
    
    ' -------------------------------------------------------------------------
    ' Constants
    ' -------------------------------------------------------------------------
    
    LcdBaud        CON    "T19200"        ' or T2400, or T9600
    
    LcdBkSpc        CON     $08                 ' move cursor left
    LcdRt           CON     $09                 ' move cursor right
    LcdLF           CON     $0A                 ' move cursor down 1 line
    LcdCls          CON     $0C                 ' clear LCD (need 5 ms delay)
    LcdCR           CON     $0D                 ' move pos 0 of next line
    LcdBLon         CON     $11                 ' backlight on
    LcdBLoff        CON     $12                 ' backlight off
    LcdOff          CON     $15                 ' LCD off
    LcdOn1          CON     $16                 ' LCD on; no crsr, no blink
    LcdOn2          CON     $17                 ' LCD on; no crsr, blink on
    LcdOn3          CON     $18                 ' LCD on; crsr on, no blink
    LcdOn4          CON     $19                 ' LCD on; crsr on, blink on
    LcdLine1        CON     $80                 ' move to line 1, column 0
    LcdLine2        CON     $94                 ' move to line 2, column 0
    
    text        VAR    Byte
    idx         VAR     Byte             ' loop counter
    line1         VAR     Byte(16)         ' line 1 buffer
    line2         VAR     Byte(16)         ' line 2 buffer
    
    temp1        VAR    Byte            ' subroutine work vars
    temp2        VAR    Byte
    temp3        VAR    Byte
    temp4        VAR    Byte
    temp5        VAR    Byte
    
    
    
    
    ' =========================================================================
      PROGRAM Start
    ' =========================================================================
    
    
    ' -------------------------------------------------------------------------
    ' Subroutine Declarations
    ' -------------------------------------------------------------------------
    
    wait        SUB    1, 2            ' delay in milliseconds
    LCD_OUT        SUB    1, 2            ' send byte {+ count} to LCD
    LCD_STR        SUB    2            ' send string to LCD
    UPDATE_L1     SUB     0             ' update line 1 of LCD
    UPDATE_L2     SUB     0             ' update line 2 of LCD
    
    ' -------------------------------------------------------------------------
    ' Program Code
    ' -------------------------------------------------------------------------
    
    Start:
      PLP_A = %0111                    ' pull up unused pins
      PLP_B = %00000000
      PLP_C = %00000000
    
      HIGH LcdTx
      wait 100                    ' let LCD initialize
      LCD_OUT LcdOn1                ' no cursor or blink
      LCD_OUT LcdCls                ' clear the LCD
      wait 5
      text = 255
      PUT line1, "Working..."
      PUT line2, "Var :"
    
    Main:
      temp1 = text / 100
      temp2 = __REMAINDER
      temp1 = temp1 + "0"
      PUT line2(6), temp1
      temp1 = temp2 / 10
      temp2 = __REMAINDER
      temp1 = temp1 + "0"
      PUT line2(7), temp1
      temp1 = temp2 + "0"
      PUT line2(8), temp1
    
      UPDATE_L1
      UPDATE_L2
      wait 1000
      text = text - 1
      if text = 0 then
        text = 255
      endif
      GOTO Main
    
    
    ' -------------------------------------------------------------------------
    ' Subroutine Code
    ' -------------------------------------------------------------------------
    
    ' Use: WAIT_MS milliseconds {, multiplier }
    ' -- multiplier is optional
    
    wait:
      temp1 = __PARAM1                              ' get milliseconds
      IF __PARAMCNT = 1 THEN                        ' if no multiplier
        temp2 = 1                                   '   set to 1
      ELSE                                          ' else
        temp2 = __PARAM2                            '   get multiplier
      ENDIF
      IF temp1 > 0 THEN                             ' no delay if either 0
        IF temp2 > 0 THEN
          PAUSE temp1 * temp2                       ' do the delay
        ENDIF
      ENDIF
      RETURN
    
    SUB UPDATE_L1
      LCD_OUT LcdLine1
      wait 5
      FOR idx = 0 TO 15
        LCD_OUT line1(idx)                 ' transfer buffer
      NEXT
    ENDSUB
    
    SUB UPDATE_L2
      LCD_OUT LcdLine2
      wait 5
      FOR idx = 0 TO 15
        LCD_OUT line2(idx)                 ' transfer buffer
      NEXT
    ENDSUB
    
    LCD_OUT:
      temp1 = __PARAM1                 ' char to send
      IF __PARAMCNT = 1 THEN             ' if no repeats specified
        temp2 = 1                     ' - set to 1
      ELSE
        temp2 = __PARAM2                 ' - save repeats
      ENDIF
    
      DO WHILE temp2 > 0
        SEROUT LcdTx, LcdBaud, temp1        ' transmit to LCD
        DEC temp2
      LOOP
      RETURN
    
    ' -------------------------------------------------------------------------
    
    ' Use: LCD_STR [noparse][[/noparse] string | label ]
    ' -- "string" is an embedded literal string
    ' -- "label" is DATA statement label for stored z-String
    
    LCD_STR:
      temp3 = __PARAM1                              ' get string offset
      temp4 = __PARAM2                              ' get string base
      DO
        READ temp4 + temp3, temp5                   ' read a character
        IF temp5 = 0 THEN EXIT                      ' if 0, string complete
        LCD_OUT temp5                            ' send the byte
        INC temp3                                   ' point to next character
        temp4 = temp4 + Z                           ' update base on overflow
      LOOP
      RETURN
    
  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-01-10 19:47
    ok, I got it [noparse]:)[/noparse] I changed the LCD_OUT to :

    LCD_OUT:
      temp1 = __PARAM1                 ' char to send
      IF __PARAMCNT = 1 THEN             ' if no repeats specified
        temp2 = 1                     ' - set to 1
      ELSE
        temp2 = __PARAM2                 ' - save repeats
      ENDIF
    
      DO WHILE temp2 > 0
        IF temp1 = $00 THEN
         SEROUT LcdTx, LcdBaud, " "
        ELSE
         SEROUT LcdTx, LcdBaud, temp1        ' transmit to LCD
        ENDIF
        DEC temp2
      LOOP
      RETURN
    



    Time to get up and do something else before tackling the EEPROM :|
  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-01-10 20:47
    I thank everyone who helped me understand this better! I was finally able to display text and variables on the LCD, write to the EEPROM, read from the eeprom and use the data in math formulas [noparse]:)[/noparse] I wound up having to redo all the code, but it was well worth it!

    One think I found out is that Google does not always have the answer!
Sign In or Register to comment.