Shop OBEX P1 Docs P2 Docs Learn Events
Need help with CODE — Parallax Forums

Need help with CODE

O.A.A.O.A.A. Posts: 25
edited 2010-01-12 19:34 in Propeller 1
Can someone please tell me what is happening in the following code segment :



DAT

··· ScratchPad byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

'······················ · · 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1

PUB Main

·· DIRA := %00001111_00000011_10100000_11111111

·· OUTA := %00000000_00000000_00000000_00000000

·· waitcnt(2_000_000 + cnt)

·· workVariable := word[noparse][[/noparse]@ScratchPad][noparse][[/noparse]21]

·· If workVariable <> 0

···· Beep

···· Beep

PRI Beep

·· repeat 2

····· !outa[noparse][[/noparse]Beeper]

····· waitcnt(5_000_000 + cnt)


For some reason "workVariable" is not zero for index values 17 and onwards.

workVariable := word[noparse][[/noparse]@ScratchPad][noparse][[/noparse]17]· ·is· ·-24573

workVariable := word[noparse][[/noparse]@ScratchPad][noparse][[/noparse]19]· ·is···· 13750

workVariable := word[noparse][[/noparse]@ScratchPad][noparse][[/noparse]21]· ·is··· · 7738

workVariable := word[noparse][[/noparse]@ScratchPad][noparse][[/noparse]23]· ·is·· -28353

workVariable := word[noparse][[/noparse]@ScratchPad][noparse][[/noparse]25]· ·is···· 30423

·Thanks.

Post Edited (O.A.A.) : 1/12/2010 6:48:59 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-12 18:43
    First of all, you've defined "ScratchPad" as a sequence of bytes, yet you're referencing them as WORDs. You probably want to use the same keyword in both cases, either BYTE or WORD. There are cases where you might want to mix sizes, but you need to know what you're doing in that case.
  • O.A.A.O.A.A. Posts: 25
    edited 2010-01-12 19:04
    Thank you mike.
    The definition of Scratchpad is as followws:
    ' ScratchPad(0) : indx
    ' ScratchPad(1) : RAC Rudder Angle Change Index
    ' ScratchPad(2) : RLim Rudder Limit
    ' ScratchPad(3) : Gain
    ' ScratchPad(4) : DBnd Rudder Deadband
    ' ScratchPad(5) : CRud Counter Rudder
    ' ScratchPad(6) : SHF Standing Helm Factor
    ' ScratchPad(7) : SpdF Speed Factor
    ' ScratchPad(8) : Mom Momentum Index
    ' ScratchPad(9) : RudMax.LowByte
    ' ScratchPad(10) : RudMax.HighByte
    ' ScratchPad(11) : Mode
    ' ScratchPad(12) : Bearing.LowByte
    ' ScratchPad(13) : Bearing.HighByte
    ' ScratchPad(14) : Speed
    ' ScratchPad(15) : Track.LowByte
    ' ScratchPad(16) : Track.HighByte
    ' ScratchPad(17) : TrackPrev.LowByte
    ' ScratchPad(18) : TrackPrev.HighByte
    ' ScratchPad(19) : omega.LowByte
    ' ScratchPad(20) : omega.HighByte
    ' ScratchPad(21) : omegaPrev.LowByte
    ' ScratchPad(22) : omegaPrev.HighByte
    ' ScratchPad(23) : SHelm.LowByte
    ' ScratchPad(24) : SHelm.HighByte
    ' ScratchPad(25) : RudPos.LowByte
    ' ScratchPad(26) : RudPos.HighByte
    ' ScratchPad(27) : cogreg i.e. 00011111 indicates cogs 0,1,2,3,4 are active
    ' ScratchPad(28) : startup
    ' ScratchPad(29) : err == 99 --> No NMEA data, reset needed
    ' ScratchPad(30) : unused
    ' ScratchPad(31) : unused
    Upto index value 17 the BYTE and WORD are all zero.
    For index value 17 and onwards I get the above stated values.
    O.A.Ardali
  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-12 19:08
    You have a byte array. Don't use WORD to access it unless you know exactly what you're doing. WORD requires an address aligned on a word boundary which you don't have. In addition, the subscript field (WORD[noparse][[/noparse]<address>][noparse][[/noparse]<subscript>]) is in terms of words, not bytes. It's multiplied by 2 before adding the result to the address.

    Post Edited (Mike Green) : 1/12/2010 7:13:12 PM GMT
  • O.A.A.O.A.A. Posts: 25
    edited 2010-01-12 19:32
    Thank you Mike.
    I have overlooked the fact that the subscript field is in terms of words which of course causes an overflow outside the data block.
    Thanks again.
  • SamMishalSamMishal Posts: 468
    edited 2010-01-12 19:34
    O.A.A.

    Change your code to Either
    DAT
    [b][color=red]    ScratchPad byte 0[noparse][[/noparse]32][/color][/b]
    '                           0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1 
    PUB Main 
       DIRA := %00001111_00000011_10100000_11111111 
       OUTA := %00000000_00000000_00000000_00000000 
       waitcnt(2_000_000 + cnt) 
    [b][color=red]   workVariable := Byte[noparse][[/noparse]@ScratchPad][noparse][[/noparse]21] [/color][/b]
       If workVariable <> 0
         Beep
         Beep
    PRI Beep
       repeat 2 
          !outa[noparse][[/noparse]Beeper]
          waitcnt(5_000_000 + cnt)
    
    

    OR
    DAT
    [b][color=red]    ScratchPad Word 0[noparse][[/noparse]32][/color][/b]
    '                           0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1 
    PUB Main 
       DIRA := %00001111_00000011_10100000_11111111 
       OUTA := %00000000_00000000_00000000_00000000 
       waitcnt(2_000_000 + cnt) 
    [b][color=red]   workVariable := Word[noparse][[/noparse]@ScratchPad][noparse][[/noparse]21] [/color][/b]
       If workVariable <> 0
         Beep
         Beep
    PRI Beep
       repeat 2 
          !outa[noparse][[/noparse]Beeper]
          waitcnt(5_000_000 + cnt)
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Samuel

    www.RobotBASIC.com
    ·
Sign In or Register to comment.