Shop OBEX P1 Docs P2 Docs Learn Events
Help! Confusing behavior of spin object when attempting to run in another cog — Parallax Forums

Help! Confusing behavior of spin object when attempting to run in another cog

I created an object that controls a DAC that is used to generate a control voltage.
I have a method in the object that launches spin code into another cog that should vary the DAC output.
Unfortunately, the code does not seem to work when I try to use the method.
I am confused about what is happening and I have attached sample files in the hope that someone more experienced might spot my error.

The hierarchy of the sample code is this ...

Control Test <- top level object
Control Object <- object that should be generating the output levels using the DAC714 object
DAC714 Object <- object that allows control of DAC714 chip
SPI_Spin <- library object for SPI interface - used for DAC714

I use Parallax Serial Terminal to in the sample code to provide some feedback.
See notes at bottom of Control Test.spin for more details.

Comments

  • First problem is that it is not possible (or shouldn't be) to use a cognew to start a method in a different object; calling object.startCycling is the proper way to do it.

    I connected your pins to a logic analyzer and saw that your routine does indeed set a constant output of 28000 for three seconds, and the data line sends 3000 and 30000 six times in the next 3 seconds, and then stops, but the clock doesn't toggle.

    The problem is that you're initializing the SPI pins from your 1st cog. Control Test calls control.initialize, which calls DAC.init, which sets pins data, A0, A1, and CLR to high outputs. You then set output level 28000 and all is good.
    You then start a new cog to cycle the output, but it is unable to because the 1st cog still has those pins set to output, and the SPI object (which is also being used by the 1st cog) leaves shiftOut with the clock set high.

    Simplest solution is to modify the shiftOut method so that it floats the clock and data pins on exit: dira[Dpin] := dira[Cpin] := 0, and place a pull-up resistor on the clock line.
  • Thanks so much for your assistance. I was able to tweak my code based on your feedback and now my project is back on track.
    I kept thinking the issue was related to variable scope. It never occurred to me that I had cogs fighting for control of the same output pins!
Sign In or Register to comment.