Shop OBEX P1 Docs P2 Docs Learn Events
array problem — Parallax Forums

array problem

svenssonsvensson Posts: 11
edited 2009-10-30 12:07 in Propeller 1
Hello,

My array code does a strange thing. I cannot get elements 2.0 and 3.0 with this code:

PUB Main
··
· F.start
· DAC.Init(0, 1, 2)·············· 'CS, SCK, SDI respectively
· array[noparse][[/noparse]0] := 2.0
· array[noparse][[/noparse]1] := 3.0
·
· repeat index from 0 to 1
· x := F.FDiv(array[noparse][[/noparse]index],const)
· x := F.FRound(x)

{floating-point values in SPIN are 32bit and integer are 32 bits but floating-point values have a complete
different bitpattern as integer-values so you have to convert the float-value of w to an integer-value as
except the float-functions all other methods expect integer-values as parameters}

· DAC.Set(0,x)··················· 'Set Channel A to 2V ( 12 bit D/A conv )
· waitcnt(20_000_000 + cnt)




But with this, it works ( I get a value on my DAQ output ), just by changing array[noparse][[/noparse]index] through array[noparse][[/noparse]0]?????????????????

PUB Main
··
· F.start
· DAC.Init(0, 1, 2)·············· 'CS, SCK, SDI respectively
· array[noparse][[/noparse]0] := 2.0
· array[noparse][[/noparse]1] := 3.0
·
· repeat index from 0 to 1
· x := F.FDiv(array[noparse][[/noparse]0],const)
· x := F.FRound(x)

{floating-point values in SPIN are 32bit and integer are 32 bits but floating-point values have a complete
different bitpattern as integer-values so you have to convert the float-value of w to an integer-value as
except the float-functions all other methods expect integer-values as parameters}

· DAC.Set(0,x)··················· 'Set Channel A to 2V ( 12 bit D/A conv )
· waitcnt(20_000_000 + cnt)


Can someone say what's wrong in the code?

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2009-10-29 14:22
    Did you maybe mean

    DAC.Set( index, x )

    ??


    You should use the button # if you poste code, because indentation gets lost otherwise. And indentation is an important information in SPIN.
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-10-29 14:30
    Hello Svenson,

    please use the archive-function of the propeller-tool to create a zip-file-archive that contains yopur COMPLETE code

    You find this in main-menu of the propeller-tool. See attached picture.


    I guess the bug is in the definition of the array. If you need an array of 10 elements you have to define

    VAR
      byte MyArray[noparse][[/noparse]10] 
    
    



    but the index-number run from 0 to 9

      MyArray[noparse][[/noparse]0] := 100 ' first element in the array has index ZERO
      MyArray := 100 'second elemet in the array has index ONE
      MyArray := 100 'third element in the array has index THREE
      MyArray := 100'etc
      MyArray := 100
      MyArray := 100
      MyArray[noparse][[/noparse]6] := 100
      MyArray[noparse][[/noparse]7] := 100
      MyArray[noparse][[/noparse]8] := 100
      MyArray[noparse][[/noparse]9] := 100 'tenth element in the array has index NINE
    
    



    for small code-snippets use the SPIN code formatter

    this creates code with a non-true-type standard-with-font so that indentions are easily too see

    best regards

    Stefan
    420 x 298 - 15K
  • svenssonsvensson Posts: 11
    edited 2009-10-30 08:07
    Hello,

    I think that isn't the problem. I don't know what's the problem. I make an array of 2 elements and I start with index 0. So I think that is ok!!!
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-10-30 10:19
    As I said in my post ... indentation is very important in SPIN.

    You need to indent both "x :="-lines plus the DAC.set and wait lines, so that they are inside of the loop! In your code the loop itself runs empty, executing no code. The rest is executed after the loop. Suppose the value of index will be 1, so you will only see the 3V.

    Post Edited (MagIO2) : 10/30/2009 10:24:44 AM GMT
  • svenssonsvensson Posts: 11
    edited 2009-10-30 12:07
    Thanks men!!!

    Now it works fine. I'm am used programming in C, that's maybe the reason why I forget to indent.

    Greetings,

    Sven

    ·
Sign In or Register to comment.