Shop OBEX P1 Docs P2 Docs Learn Events
How do I write then read 000000 to the eprom? — Parallax Forums

How do I write then read 000000 to the eprom?

sir_hacksalotsir_hacksalot Posts: 15
edited 2013-06-03 08:43 in BASIC Stamp
Im using a 74HC165 to read the state of switches (things doing stuff). Id like to do some different "stuff" with that data as it changes states.

I figure that I could write the state of the switches read by the 74HC165 to the eeprom and read it later in the code to do "stuff" as it changes.

Ill be updating the eeprom every 10 ms. Ill hit the 100,000 writes real quick there so that will be a problem in the future i think.
as a side note: how do write and read to different locations so as to extend the life of my eeprom.

Question. How do I write 8 digits to eeprom? I can write up to 255 but I need to write 00000000 or any variation of those ones and zeros (bianary 0s and 1s to show open closed switches)\

Should I convert the 8 bit data to hex or asci and save that? If so how the heck to I do that?

Should I use a look down / up table to analyze the conditions of the 1s and 0s? that would be 256 total variations. Lots of ram for that.

Heres my code. simple. needs some help.
idx             VAR     Word            ' loop control
switches        VAR     Word            ' value(s)WHAT?

MemLocation     CON     5248           'somewhere on the eeprom
Memdata         CON     255            ' needs to be 8 bits




Write_EE:
  DEBUG CLS                                      ' clear screen
  WRITE MemLocation,Memdata                     ' single byte


Read_EE:
  FOR idx = MemLocation TO MemLocation           ' show raw bytes in EE
    READ idx, switches
    DEBUG "mem location data: ",DEC idx, " : ", DEC switches
  NEXT



END

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-05-30 16:19
    The issue is really quite simple if you don't look at the data as characters. No need to store characters when you have the 8-bits (1 byte) from the switches you can store that byte as it is and you still have the same information that came from the switches/shift-register.

    Also, are the switches changing every 10ms? If not you can always have it save them whenever they're updated only, extending the time before a write failure. Even then I would probably implement a save-on-power-loss feature or use battery backed RAM or FRAM if I need saves at that rate.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-30 16:32
    A common way to handle this sort of thing is to add a Real Time Clock (RTC) to the Stamp. There are several RTCs available with SPI interfaces that are easy to use with a Stamp. SparkFun has one or two on breakout boards and Adafruit may have some. The breakout boards often come with backup batteries so they'll retain their storage contents when the Stamp is off (and maintain the correct time and date). They often have 32 or 64 bytes or more of memory powered by the battery which you could use to save the switch settings during a power failure. Look at the Nuts & Volts Columns under the Resources tab on the main Parallax webpage.
  • sir_hacksalotsir_hacksalot Posts: 15
    edited 2013-05-30 16:45
    Holy Smile batman! I got it!. "Word" in the right place helps alot.
  • sir_hacksalotsir_hacksalot Posts: 15
    edited 2013-05-30 16:47
    Thanks Chris! btw. How would I update EEprom only when the switches change?
  • sir_hacksalotsir_hacksalot Posts: 15
    edited 2013-05-30 17:01
    Thank you very much for your prompt reply.:lol:

    Im still a little confused.

    Im using this code to get the data from the shift register.
    ' -----[ I/O Definitions ]-------------------------------------------------
    
    Clock           PIN     0                       ' shift clock (74HC165.2)
    SerData         PIN     1                       ' serial data (74HC165.7)
    Load            PIN     2                       ' input load (74HC165.1)
    
    
    ' -----[ Constants ]-------------------------------------------------------
    
    DelayTime       CON     20
    
    
    
    ' -----[ Variables ]-------------------------------------------------------
    idx             VAR     Byte
    switches        VAR     Byte                    ' switch data
    
    
    ' -----[ Initialization ]--------------------------------------------------
    
    Reset:
      HIGH Load                                     ' make output and high
    
      DEBUG CLS,
            "Switches  12345678", CR,
            "--------  --------", CR,
            "Status    ........", CR
    
    
    ' -----[ Program Code ]----------------------------------------------------
    
    Main:
      DO
        GOSUB Get_165                               ' get switch inputs
        DEBUG CRSRXY, 10, 2, BIN8 switches          ' display current status
        PAUSE DelayTime                             ' pad the loop a bit
    
        WRITE 400, switches
    
        PAUSE DelayTime                             'pad the write command
    
    
        FOR switches = 0 TO 0
    
        PAUSE 10
        NEXT
      LOOP
    
    
    ' -----[ Subroutines ]-----------------------------------------------------
    
    Get_165:
      PULSOUT Load, 5                               ' load switch inputs
      SHIFTIN SerData, Clock, MSBPRE, [switches]    ' shift them in
      RETURN
    
    

    This code outputs the 8 digits showing the states of the switches.
    I need to store that data every time it changes in the eeprom so i can read the changing state and add actions to the program.

    Id like to write it only as it changes as suggested by Chris, I have no Idea how to do that (yet).

    Is this var "switches" of "00001000" 8 seperate digits of which there can be 256 variation or is a single byte when written to the EEprom?

    I only get 4 bits back when I use the "word" function in my read write program. Do I need 8 bits for the zeros and ones?
  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-30 17:20
    Usually, you READ the EEPROM location first to see what's recorded there, then compare your new value to what's already there. You skip the WRITE if the new value and the old value are the same.
  • sir_hacksalotsir_hacksalot Posts: 15
    edited 2013-05-30 17:23
    Thank you mike!
  • bsjohnbsjohn Posts: 19
    edited 2013-05-30 19:24
    You could forgo writing and reading from eeprom and instead just store the value in variables. Compare two variable such as stored_switch_state and read_switch_state. If the values are different then interrogate the individual bits to see which switch changed. For example:
    stored_sw_val    VAR     Byte
    read_sw_vals      VAR     Byte
    
    stored_sw_val = %00000000 'stored state of switches
    read_sw_vals = %10101011  'simulated read sw values
    Monitor_sw:
    IF stored_sw_val = read_sw_vals THEN Monitor_sw  ' if different fall through and compare bits
    
    ' Now interegate each bit to find the switch that changed and do something
    ' See Aliases and modifiers in Basic Stamp manual
    IF stored_sw_val.BIT0 <> read_sw_vals.BIT0 THEN
      'do something
     DEBUG ? read_sw_vals.BIT0
    ENDIF
    
    IF stored_sw_val.BIT1 <> read_sw_vals.BIT1 THEN
      'do something
      DEBUG ? read_sw_vals.BIT1
    ENDIF
    
    IF stored_sw_val.BIT2 <> read_sw_vals.BIT2 THEN
      'do something
      DEBUG ? read_sw_vals.BIT2
    ENDIF
    
    IF stored_sw_val.BIT3 <> read_sw_vals.BIT3 THEN
      'do something
      DEBUG ? read_sw_vals.BIT3
    ENDIF
    
    IF stored_sw_val.BIT4 <> read_sw_vals.BIT4 THEN
      'do something
      DEBUG ? read_sw_vals.BIT4
    ENDIF
    
    IF stored_sw_val.BIT5 <> read_sw_vals.BIT5 THEN
      'do something
      DEBUG ? read_sw_vals.BIT5
    ENDIF
    
    IF stored_sw_val.BIT6 <> read_sw_vals.BIT6 THEN
      'do something
      DEBUG  ? read_sw_vals.BIT6
    ENDIF
    
    IF stored_sw_val.BIT7 <> read_sw_vals.BIT7 THEN
      'do something
      DEBUG ? read_sw_vals.BIT7
    ENDIF
    
    stored_sw_val = read_sw_vals ' Since the stored and read sw values are different
    ' replace stored switch value with read value
    
    Debug Output:
    read_sw_vals.BIT0 = 1
    read_sw_vals.BIT1 = 1
    read_sw_vals.BIT3 = 1
    read_sw_vals.BIT5 = 1
    read_sw_vals.BIT7 = 1

    Yes it's long but it saves repeated eeprom write cycles.
  • Hal AlbachHal Albach Posts: 747
    edited 2013-05-30 19:40
    I totally agree with Mike and Chris and suggest that you use a serial sram to store your switch readings. I've been browsing the Mouser online catalog and they are stocking a 64 KB SPI SRAM that operates from 2.7 to 5.5 Volts, comes in a PDIP form, and includes a Vbat pin for attaching a battery and costs a whopping $2.20 + shipping for one or $1.76 each for 10. You will be able to access the full memory using a single WORD variable with the SEROUT and SERIN command. Reading and writing is extremely fast and it will never wear out. The chip is from Microchip, 23LCV512. When I finish this post I'm going to order ten of them to replace the 23K256 which I'm currently trying to use but the 3.3 volt max limitation makes it harder to interface to a stamp.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-31 11:17
    You can't use SERIN and SEROUT with these SRAMs. SPI interfaces use the SHIFTIN and SHIFTOUT statements. These SPI SRAMs are still easy to use. I still favor the use of a RTC / SRAM chip like the one I suggested.
  • Hal AlbachHal Albach Posts: 747
    edited 2013-05-31 12:12
    Of course! I seem to make that mistake every once in a while. I guess I got carried away when I found an sram that wasn't SMD and 3.3 volt only.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-06-03 08:43
    Mike Green wrote: »
    You can't use SERIN and SEROUT with these SRAMs. SPI interfaces use the SHIFTIN and SHIFTOUT statements. These SPI SRAMs are still easy to use. I still favor the use of a RTC / SRAM chip like the one I suggested.

    Agreed. I have often added a DS1302 to a project just for the battery backed SRAM. I have had more than a few projects that required constant updating of a variable (or more) that needed to survive loss of power.
Sign In or Register to comment.