Shop OBEX P1 Docs P2 Docs Learn Events
loop index problem — Parallax Forums

loop index problem

slcottslcott Posts: 27
edited 2010-01-30 04:40 in General Discussion
I'm trying to build a little handshaking protocol and am running into a problem with my loop.
The programming is a little different than the way I would program in C.

  
tmp1  VAR BYTE

...
...
...

FUNC SHIFTOUT_VAL
  'tmp0 = __PARAMCNT
  tmp1 = __PARAM1 
  'tmp2 = __PARAM2 
  'tmp3 = __PARAM3 
  'tmp4 = __PARAM4
  FOR idx = 7 TO 0 STEP -1
    IF SCLK = 1 THEN
      IF tmp1.idx <> 0 THEN
        HIGH MISO
      ELSE
        LOW MISO
      ENDIF
    ELSE
      pause 1
    ENDIF       
  NEXT
ENDFUNC





I get the error: "EXPECTED A VALUE BETWEEN 0 AND 7 "idx"

should i just make an array?
is there a convenient way to copy a byte into an array then?

any suggestions would really be appreciated.

Comments

  • JonnyMacJonnyMac Posts: 9,211
    edited 2010-01-28 02:06
    You cannot [noparse][[/noparse]directly] access a bit within a byte using a variable. Have a look at this routine, I believe it does what you want:

    SUB SHIFTOUT_VAL
      soVal         VAR     tmpB1           ' value to shift out, LSB first
      soIdx         VAR     tmpB2           ' loop control
    
      soVal = __param1
    
      FOR soIdx = 0 TO 7                    ' send 8 bits
        \ JB  SCLK, @$                      ' wait for low
        \ JNB SCLK, @$                      ' wait for leading edge
        \ nop                               ' let SCLK settle
        MiSo = soVal.0                      ' output LSB
        soVal = soVal >> 1                  ' prep for next bit
      NEXT
      ENDSUB
    


    Note that if you're trying to synchronize with an external clock (coming in on SCLK) you cannot insert arbitrary delays as you have. The above code waits for a rising edge of the SCLK line before outputting the LSB of the value to sent over in __param1.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • JonnyMacJonnyMac Posts: 9,211
    edited 2010-01-28 02:09
    Note, too, that the code above expects MiSo to be set in output mode.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • slcottslcott Posts: 27
    edited 2010-01-30 01:57
    what specifically does the ampersand accomplish? this code is really helping me out, btw. i've got a nice handshaking protocol working now.
  • JonnyMacJonnyMac Posts: 9,211
    edited 2010-01-30 04:12
    What ampersand (&)? If you mean @, it ensures the jump is to the correct page -- though usually overkill in small programs. For the best SX Assembly reference, get Guether Daubach's book.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • Martin HodgeMartin Hodge Posts: 1,246
    edited 2010-01-30 04:40
    Guenther's book is my SX bible. Highly recommended!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Martin Hodge
    webbolts.com
    forumexperts.com
Sign In or Register to comment.