Spin code help
Laserist1984
Posts: 9
Hello Forum ,
I have little problem in how to start to make a delay to turn LED on .
I wrote this code
i will write the pseudo code what i want from this program to do:
repeat:- while ina[noparse][[/noparse]Y_INDEXPULSE]>0,turn LED on,wait 260micro, LED off: i't·done by the code above
··········- while ina[noparse][[/noparse]Y_INDEXPULSE]>0,wait·8ms to turn LED on,wait 260micro, LED off.
········· - while ina[noparse][[/noparse]Y_INDEXPULSE]>0,wait·6ms to turn LED on,wait 260micro, LED off.
········· - while ina[noparse][[/noparse]Y_INDEXPULSE]>0,wait·5ms to turn LED on,wait 260micro, LED off.
········· -while ina[noparse][[/noparse]Y_INDEXPULSE]>0,wait·4ms to turn LED on,wait 260micro, LED off.
········· -while ina[noparse][[/noparse]Y_INDEXPULSE]>0,wait·7 ms to turn LED on,wait 260micro, LED off.
how i can make it in intelligent way and nice in Spin ?
I hope that i describe good :-)
Thank you in advance
I have little problem in how to start to make a delay to turn LED on .
I wrote this code
PUB start|n dira[noparse][[/noparse]LED_CONTROL]~~ 'set LED control port to output WaitCnt(OneSecond*5 + Cnt) repeat repeat while ina[noparse][[/noparse]Y_INDEXPULSE]>0 ' wait for pulse outa[noparse][[/noparse]LASER_CONTROL]~~ ' turn LED on WaitCnt(MicroSecond * 260 + Cnt) ' LED on at first line of frame outa[noparse][[/noparse]LED_CONTROL]~ ' turn LED off
i will write the pseudo code what i want from this program to do:
repeat:- while ina[noparse][[/noparse]Y_INDEXPULSE]>0,turn LED on,wait 260micro, LED off: i't·done by the code above
··········- while ina[noparse][[/noparse]Y_INDEXPULSE]>0,wait·8ms to turn LED on,wait 260micro, LED off.
········· - while ina[noparse][[/noparse]Y_INDEXPULSE]>0,wait·6ms to turn LED on,wait 260micro, LED off.
········· - while ina[noparse][[/noparse]Y_INDEXPULSE]>0,wait·5ms to turn LED on,wait 260micro, LED off.
········· -while ina[noparse][[/noparse]Y_INDEXPULSE]>0,wait·4ms to turn LED on,wait 260micro, LED off.
········· -while ina[noparse][[/noparse]Y_INDEXPULSE]>0,wait·7 ms to turn LED on,wait 260micro, LED off.
how i can make it in intelligent way and nice in Spin ?
I hope that i describe good :-)
Thank you in advance
Comments
Reason: The ina might be read just a short moment before the pulse arrives. Then the comparison and the next repeat statement will be executed before the next ina. Waitpeq and waitpne have an equivalent PASM instruction. So, an internal comparator constantly checks the ina-register. As long as the wait-condition is not true, the COG is in a halt state.
Is it ensured that the pulse of that pin ends before your switch on - wait - switch off sequence is finished? Otherwise your wait until might react twice on the same pulse.
For your variable wait-time:
1. use a counter variable to count from 0 to 5 - for example in a repeat loop.
2. define a DAT section "waittimes" for example, which contains 6 long values that you have to add to cnt in the waitcnt-statement
DAT
waittimes long 400, clkfreq*8/1000, clkfreq*6/1000, clkfreq*5/1000 ...
3. Use these values in the waitcnt like
waitcnt( long[noparse][[/noparse]@waittimes+counter] + cnt )
Hope that helps. If you have further problems, ask again.
Have a nice day
what can the problem from ?
thanks in advance.
please can someone tell me where the problem in the code mentioned above?
thanks
Please note: Indentation is very important in SPIN. It replaces characters marking blocks as known in other programming languages. ( in C { 'do something } is a block·)
This will only wait for a pulse on Y_INDEXPULSE and then repeat forever. If you want it only to run if the input is high you need to change it a bit:
Hey MagIO2, why not just
?
Also, earlier you suggested
waitcnt( long[noparse][[/noparse]@waittimes+counter] + cnt )
but that won't work unless counter is a multiple of four. Just use
waitcnt( waittimes[noparse][[/noparse]counter] + cnt )
thanks for the comments.
I simply did not recheck that waitcnt-line, so it missed the evolution.
About the long: These things happen if you don't test the code before posting. I'm guilty for that.