Shop OBEX P1 Docs P2 Docs Learn Events
How to pass a variable from spin to assembly and vice-versa? — Parallax Forums

How to pass a variable from spin to assembly and vice-versa?

LandryLandry Posts: 17
edited 2008-03-25 12:02 in Propeller 1
Hi,
In this piece of code that i'm using, i seem not be be able to update the value of the variable _comparatorPosition from the spin part to use in the asm part for comparison. Can anybody tell me what I'm doing wrong?

CON
  _CLKMODE = XTAL1 + PLL16X
  _XINFREQ = 5_000_000
  MAX_LENGTH = 400
  
  PIN_ENCODER_BOX_A = 3
  PIN_ENCODER_BOX_B = 4
  'PIN_ENCODER_BOX_I = 5
    
  'PIN_ENCODER_PP_A = 5
  'PIN_ENCODER_PP_B = 6
  'PIN_ENCODER_PP_I = 8
 
OBJ
  _Encoders     : "Quadrature Encoder"
  _BS2          : "BS2_Functions"
  _Serial       : "Extended_FDSerial"
  
VAR
  long _position[noparse][[/noparse]2]
  long Stack   
                     
PUB Main | triggerSign, input, Duration, cntMin
  _BS2.start(31, 30)
  _Serial.start(1, 0, 0, 9600)
  _Serial.str(string("Position calculator..."))  
 
  _Serial.SetDelimiter(" ")
  _Serial.str(string("Enter the trigger position value:"))
  repeat
    _triggerPosition := _Serial.RxDec 
    if _triggerPosition 
      _Serial.dec(_triggerPosition)
      quit
  _Serial.str(string("Enter duration of the pulse:"))
  repeat
    Duration := _Serial.RxDec 
    if Duration 
      _Serial.dec(Duration)
      quit
  cntMin := 400
  Delay := (Duration * (clkfreq / 1_000_000)  * 2 - 1250) #> cntMin
  _Serial.dec(Delay)  
  
  'Encoder   
  _Encoders.start(PIN_ENCODER_BOX_A, 1, 0, @_position)
  
   _comparatorPosition := 0
   cognew(@Trigger, 0)
    
  repeat
    _comparatorPosition := _position[noparse][[/noparse]0]
 
DAT
                        org 0
                       
Trigger                 cmp     _triggerposition, _comparatorPosition   wz
              if_nz     jmp     #:notEqual
                        or      dira, Led1 
                        xor     outa, Led1
                        or      dira, Pin    
                        mov     Time, cnt
                        add     Time, #9
                        waitcnt Time, Delay
                        xor     outa, Pin   'high
                        waitcnt Time, Delay 'wait untill time
                        xor     outa, Pin   'low
                        jmp     #trigger                      
        :notEqual       or      dira, Led2
                        xor     outa, Led2
                        jmp     #trigger 
 
Pin                     long    |< 6
Led1                    long    |< 16
Led2                    long    |< 23
Delay                   long    0
_triggerposition        long    0
_comparatorPosition     long    0
Time                    res     1

Comments

  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-03-25 11:46
    When you start a new cog with an assembly program the code gets copied from the hub ram into the cog ram. So, the spin program is changing _comparatorPosition in the hub but not in the cog and the assembly program is checking the _comparePosition in the cog but not in the hub.

    To get around this you need to use the hub access instructions. In this case rdlong. You also need the address of _comparatorPosition and a way of passing it to the cog. So you need to change your cogne to this to pass in the address of _comparatorPosition
    cognew(@Trigger,@_comparatorPosition)
    


    Note that this passes in the hub address and not the cog address. Than you need to change the first line of your assembly routine to this
    Trigger              rdlong _comparatorPosition,par 'This gets the long from the hub and stores it at _comparatorPosition in the cog
                     cmp     _triggerposition, _comparatorPosition   wz
    



    Hope this helpssmile.gif
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-03-25 11:50
    Just another note.

    Since you are setting _triggerPosition before you start the cog and never change it after you start the new cog the value you write there will get copied into the cog and will therefore work as expected.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2008-03-25 11:54
    Steps 3 and 4 should be of interest:

    http://forums.parallax.com/showthread.php?p=647408
  • LandryLandry Posts: 17
    edited 2008-03-25 11:55
    thanks for the reply. I will try it and let you know the result.
  • LandryLandry Posts: 17
    edited 2008-03-25 12:00
    thanks stevenmess2004,

    it works just fine.
  • deSilvadeSilva Posts: 2,967
    edited 2008-03-25 12:02
    smile.gif
Sign In or Register to comment.