Shop OBEX P1 Docs P2 Docs Learn Events
opinion sought — Parallax Forums

opinion sought

ArchiverArchiver Posts: 46,084
edited 2004-03-10 23:07 in General Discussion
Seeking opinions on code. I am writing 5 words to EEPROM, then later
reading those 5 words. The snippet posted below does work, but I am
simply seeking opinions wether or not there is a more "elegant" way
to achieve the same thing


<SNIP>
FOR run = 0 TO 9
again
IF IN6 = 1 THEN again
HIGH 0
PAUSE 1
RCTIME 0,1,load
WRITE run,load.LOWBYTE
run = run + 1
WRITE run,load.HIGHBYTE
PAUSE 2000 'poor mans debounce
NEXT


'READ PORTION
FOR run = 0 TO 9
READ run,load.LOWBYTE
run = run + 1
READ run,load.HIGHBYTE
SEROUT sPin,Baud,[noparse][[/noparse]"DATA,TIME,", DEC x, ",", DEC load,CR]
x = x + 1
NEXT

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2004-03-10 22:30
    You can make your code more elegant by taking advantage of our style
    guidelines and features of the 2.1 compiler. Specifically:

    What is IN6? Why not give it a name with the PIN declaration?

    Did you know you can WRITE words to EEPROM like this:

    WRITE eeAddr, Word value1, Word value2, Word value3, Word value4, Word
    value5

    ... and you can READ them back like this:

    READ eeAddr, Word value1, Word value2, Word value3, Word value4, Word
    value5

    When using $PBASIC 2.5 syntax you are able to WRITE and READ Word-sized
    values to EEPROM, and handle
    multiple values in the same WRITE/READ instruction. You can even mix
    types (Words and Bytes). When
    writing a Word, it is done low-byte, then high-byte (the address in
    taken care of by the compiler).

    You can find our style guidelines ("The Elements of PBASIC Style") in
    the v2.1 compiler help file or online in our Downloads section.

    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office


    Original Message
    From: coiltap2003 [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=afPkS-6JeoOkA-1NVd5UGys9KqRQjLx_Z76OTtNOIx7BmQjeiigtw8aLA-ySxiWsGzobzns0RA]smartdim@a...[/url
    Sent: Wednesday, March 10, 2004 2:48 PM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] opinion sought


    Seeking opinions on code. I am writing 5 words to EEPROM, then later
    reading those 5 words. The snippet posted below does work, but I am
    simply seeking opinions wether or not there is a more "elegant" way
    to achieve the same thing


    <SNIP>
    FOR run = 0 TO 9
    again
    IF IN6 = 1 THEN again
    HIGH 0
    PAUSE 1
    RCTIME 0,1,load
    WRITE run,load.LOWBYTE
    run = run + 1
    WRITE run,load.HIGHBYTE
    PAUSE 2000 'poor mans debounce
    NEXT


    'READ PORTION
    FOR run = 0 TO 9
    READ run,load.LOWBYTE
    run = run + 1
    READ run,load.HIGHBYTE
    SEROUT sPin,Baud,[noparse][[/noparse]"DATA,TIME,", DEC x, ",", DEC load,CR]
    x = x + 1
    NEXT
  • ArchiverArchiver Posts: 46,084
    edited 2004-03-10 23:07
    Don't use RUN as a variable; it is a reserved word. More below

    >Seeking opinions on code. I am writing 5 words to EEPROM, then later
    >reading those 5 words. The snippet posted below does work, but I am
    >simply seeking opinions wether or not there is a more "elegant" way
    >to achieve the same thing
    >
    >
    ><SNIP>
    >FOR run = 0 TO 4
    >again
    >IF IN6 = 1 THEN again
    >HIGH 0
    >PAUSE 1
    >RCTIME 0,1,load
    >WRITE run,load.LOWBYTE
    >run = run + 1
    >WRITE run,load.HIGHBYTE
    >PAUSE 2000 'poor mans debounce
    >NEXT
    >

    HIGH 0 ' init for RCtime first time
    FOR cat = 0 TO 4 ' 5 words
    DO:LOOP WHILE in6 ' hold for in6=0
    RCTIME 0,1,load
    HIGH 0 ' init RCtime right after
    WRITE cat*2, Word load ' words take two bytes
    PAUSE 2000 'poor mans debounce
    NEXT
    ' now values are in eeprom from 0 to 9


    >'READ PORTION
    >FOR run = 0 TO 9
    >READ run,load.LOWBYTE
    >run = run + 1
    >READ run,load.HIGHBYTE
    >SEROUT sPin,Baud,[noparse][[/noparse]"DATA,TIME,", DEC x, ",", DEC load,CR]
    >x = x + 1
    >NEXT


    'READ PORTION
    FOR cat = 0 TO 4 ' 5 words
    READ cat*2, Word load
    SEROUT sPin,Baud,[noparse][[/noparse]"DATA,TIME,", DEC x, ",", DEC load,CR]
    x = x + 1
    NEXT
Sign In or Register to comment.