Shop OBEX P1 Docs P2 Docs Learn Events
Write to and later read from EEPROM — Parallax Forums

Write to and later read from EEPROM

SpoppeSpoppe Posts: 3
edited 2013-05-02 20:45 in BASIC Stamp
Building a data logger. One program measures data and writes it to EEPROM. Another program later reads the data from the EEPROM. Using Word vars. but the program that reads always gets zeroes for the high byte. Low byte is ok. What is going on? How can i read the data correctly? I have confirmed that the writing program can immediately read the data correctly. This is on BS2.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-27 10:01
    Can't tell what's going on for you without source code. Please post. The examples shown in the manual (see READ / WRITE) do work.

    attachment.php?attachmentid=78421&d=1297987572

    Remember that you need the WORD prefix to read and write words like: WRITE <address>, WORD <data>
    Remember also that, if you're using WORD, that the addresses should increment by two.
  • SpoppeSpoppe Posts: 3
    edited 2013-04-28 09:17
    The symptom has changed. Now I cannot get the same data back immediately after writing it. Below I post the full code and then some sample output.
    ' PulseIntervalTimer2.bs2
    ' Measure the intervals between successive HIGH states on an input pin
    ' Pin 14 is the input pin
    ' An on-board astable multivibrator generates a pulse train
    ' which is fed to pin 14
    ' This method uses the pulsin command
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    locount VAR Word   'count of 2 usec intervals for low pulse
    hicount VAR Word   'count of 2 usec intervals for high pulse
    cyclecount VAR Word      ' count of 2 usec intervals for a cycle
    memloc VAR Word     'memory location
    memlochi VAR Word  'highest memory location to use
    memcontent VAR Word ' content read from a memory location
    memloc = 0
    memlochi = 10
    DO UNTIL memloc = memlochi+1
      PULSIN 14, 0, locount          ' count duration of low state
      PULSIN 14, 1, hicount          ' count duration of high state
      cyclecount = locount + hicount ' compute count for one cycle
      DEBUG ? cyclecount             ' echo cycle count to screen
      WRITE memloc, Word cyclecount  ' write cycle count to eeprom
      READ memloc, Word memcontent   ' then immediately read it and echo to screen
      DEBUG "memory location  ", DEC memloc, "  memory contents  ", DEC memcontent, CR
      memloc = memloc + 1
      LOOP
    '  Check what is read from memory to see if it is as expected
    memloc = 0
    DO  UNTIL memloc = memlochi + 1
      READ memloc, Word memcontent
      DEBUG "memory location  ", DEC memloc, "  memory contents  ", DEC memcontent, CR
      memloc = memloc + 1
      LOOP
    END
    
    

    Here is some output:

    cyclecount = 10283
    memory location 0 memory contents 10283
    cyclecount = 10282
    memory location 1 memory contents 10282
    cyclecount = 10283
    memory location 2 memory contents 10283
    cyclecount = 10282
    memory location 3 memory contents 10282
    cyclecount = 10280
    memory location 4 memory contents 10280
    cyclecount = 10283
    memory location 5 memory contents 10283
    cyclecount = 10281
    memory location 6 memory contents 10281
    cyclecount = 10282
    memory location 7 memory contents 10282
    cyclecount = 10283
    memory location 8 memory contents 10283
    cyclecount = 10283
    memory location 9 memory contents 10283
    cyclecount = 10283
    memory location 10 memory contents 10283
    memory location 0 memory contents 10795
    memory location 1 memory contents 11050
    memory location 2 memory contents 10795
    memory location 3 memory contents 10282
    memory location 4 memory contents 11048
    memory location 5 memory contents 10539
    memory location 6 memory contents 10793
    memory location 7 memory contents 11050
    memory location 8 memory contents 11051
    memory location 9 memory contents 11051
    memory location 10 memory contents 10283

    Steve
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2013-04-28 09:32
    You can't write a WORD, 16 bits, to one 8-bit register.
    You have to divide that WORD into two BYTEs and WRITE them into two registers.
    Same goes with READ.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-28 09:33
    When you write (or read) a word value, that takes two bytes. You have to increment the memory location by 2

    READ memloc, word memcontent
    WRITE memloc, word memcontent

    You have to do

    memloc = memloc + 2
  • SpoppeSpoppe Posts: 3
    edited 2013-05-02 20:45
    Thank you, Mike. That works perfectly.
Sign In or Register to comment.