 |
|
 |
| Parallax Forums > Public Forums > Propeller Chip > Spin code help | Forum Quick Jump
|
|  Laserist1984 Registered Member
        Date Joined Nov 2009 Total Posts : 6 | Posted 11/3/2009 9:46 AM (GMT -8) |   | |
Hello Forum ,
I have little problem in how to start to make a delay to turn LED on .
I wrote this code
PUB start|n dira[LED_CONTROL]~~ 'set LED control port to output
WaitCnt(OneSecond*5 + Cnt)
repeat repeat while ina[Y_INDEXPULSE]>0 ' wait for pulse outa[LASER_CONTROL]~~ ' turn LED on WaitCnt(MicroSecond * 260 + Cnt) ' LED on at first line of frame outa[LED_CONTROL]~ ' turn LED off i will write the pseudo code what i want from this program to do:
repeat:- while ina[Y_INDEXPULSE]>0,turn LED on,wait 260micro, LED off: i't done by the code above
- while ina[Y_INDEXPULSE]>0,wait 8ms to turn LED on,wait 260micro, LED off.
- while ina[Y_INDEXPULSE]>0,wait 6ms to turn LED on,wait 260micro, LED off.
- while ina[Y_INDEXPULSE]>0,wait 5ms to turn LED on,wait 260micro, LED off.
-while ina[Y_INDEXPULSE]>0,wait 4ms to turn LED on,wait 260micro, LED off.
-while ina[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 | | Back to Top | | |
 |  MagIO2 Registered Member
        Date Joined Mar 2009 Total Posts : 596 | Posted 11/3/2009 10:21 AM (GMT -8) |   | First of all you should replace the while ina - loop with the waitpne or waitpeq statement. Waitpeq and waitpne have better response times than you selfmade wait and reduce power consumption.
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[@waittimes+counter] + cnt )
Hope that helps. If you have further problems, ask again.
| | Back to Top | | |
  |  Laserist1984 Registered Member
        Date Joined Nov 2009 Total Posts : 6 | Posted 11/3/2009 12:07 PM (GMT -8) |   | Thank you alooot MagIo2 , i will try it and i will tell you about results . Have a nice day | | Back to Top | | |
 |  Laserist1984 Registered Member
        Date Joined Nov 2009 Total Posts : 6 | Posted 11/3/2009 4:13 PM (GMT -8) |   | it's seems something wrong happen , i am trying to make change in this code if i want to do: when the first time ina[Y_INDEXPULSE]>0 , LED will be ON 260 microsecond , LED off , after 10 millisecond LED is ON 260microsecond , LED off, after 10 millisecond LED is ON 260microsecond , LED off .....
PUB start dira[LED_CONTROL]~~ 'set LED control port to output
WaitCnt( clkfreq + Cnt)
if ina[Y_INDEXPULSE]>0
Repeat outa[LED_CONTROL]~~ ' turn LED on WaitCnt(MicroSecond * 260 + Cnt) ' LED on 260 microsecond outa[LED_CONTROL]~ ' turn LED off waitcnt( long[@waittime] + cnt ) ' wait 10 millisecond
DAT MicroSecond long 80
waittime long 80_000_000 * 10 / 1_000 ' 80_000_000/1000 is equal to 1 ms what can the problem from ?
thanks in advance. | | Back to Top | | |
 |  Laserist1984 Registered Member
        Date Joined Nov 2009 Total Posts : 6 | Posted 11/4/2009 2:23 AM (GMT -8) |   | hello, please can someone tell me where the problem in the code mentioned above? thanks | | Back to Top | | |
 |  MagIO2 Registered Member
        Date Joined Mar 2009 Total Posts : 596 | Posted 11/4/2009 3:02 AM (GMT -8) |   |
PUB start dira[LED_CONTROL]~~ 'set LED control port to output
WaitCnt( clkfreq + Cnt)
' if does not wait until pin is high, it just checks. And if statement is true, it executes the correctly indented code
if ina[Y_INDEXPULSE]>0
waitpeq( 1<<Y_INDEXPULSE, 1<<Y_INDEXPULSE, 0 )
' indentation ... only code correctly indented is repeated
repeat outa[LED_CONTROL]~~ ' turn LED on WaitCnt(MicroSecond * 260 + Cnt) ' LED on 260 microsecond outa[LED_CONTROL]~ ' turn LED off waitcnt( long[@waittime] + cnt ) ' wait 10 millisecond
DAT MicroSecond long 80
waittime long 80_000_000 * 10 / 1_000 ' 80_000_000/1000 is equal to 1 ms 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:
PUB start dira[LED_CONTROL]~~ 'set LED control port to output
WaitCnt( clkfreq + Cnt)
repeat waitpeq( 1<<Y_INDEXPULSE, 1<<Y_INDEXPULSE, 0 )
repeat while ina[Y_INDEXPULSE] ' the >0 only eats up runtime. Every number that is not 0 will keep the loop running outa[LED_CONTROL]~~ ' turn LED on WaitCnt(MicroSecond * 260 + Cnt) ' LED on 260 microsecond outa[LED_CONTROL]~ ' turn LED off waitcnt( long[@waittime] + cnt ) ' wait 10 millisecond
DAT MicroSecond long 80
waittime long 80_000_000 * 10 / 1_000 ' 80_000_000/1000 is equal to 1 ms | | Back to Top | | |
  |  MagIO2 Registered Member
        Date Joined Mar 2009 Total Posts : 596 | Posted 11/4/2009 7:21 AM (GMT -8) |   | Hi mpark ...
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. | | Back to Top | | |
| Forum Information | Currently it is Saturday, November 21, 2009 11:36 AM (GMT -8) There are a total of 393,860 posts in 55,536 threads. In the last 3 days there were 84 new threads and 711 reply posts. View Active Threads
| | Who's Online | This forum has 17692 registered members. Please welcome our newest member, old guy. 52 Guest(s), 13 Registered Member(s) are currently online. Details Siri, keith_kw, Jay Kickliter, Mike Green, Bob Lawrence (VE1RLL), Dogg, hover1, ErNa, Harley, Electronegativity, Tubular, Leon, MicroDirk |
Forum powered by dotNetBB v2.42EC SP2.02 dotNetBB © 2000-2009 |
|
|