How to pass a variable from spin to assembly and vice-versa?
Landry
Posts: 17
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?
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
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
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
Hope this helps
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.
http://forums.parallax.com/showthread.php?p=647408
it works just fine.