Parallax Forums
  HomeLog InRegisterCommunity CalendarSearch the ForumHelp
   
Parallax Forums > Public Forums > Propeller Chip > Spin code help  Forum Quick Jump
 
New Topic Post Reply Printable Version
[ << Previous Thread | Next Thread >> ] | Show Newest Post First ]

Laserist1984
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Nov 2009
Total Posts : 6
 
   Posted 11/3/2009 9:46 AM (GMT -8)    Quote This PostAlert An Admin About This Post.
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

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Mar 2009
Total Posts : 596
 
   Posted 11/3/2009 10:21 AM (GMT -8)    Quote This PostAlert An Admin About This Post.

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
 

MagIO2
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Mar 2009
Total Posts : 596
 
   Posted 11/3/2009 12:02 PM (GMT -8)    Quote This PostAlert An Admin About This Post.
That should do the job:
PUB start | count
  dira[LED_CONTROL]~~ 'set LED control port to output
  WaitCnt( clkfreq + Cnt)

  ' This makes it an endless loop, you have to decide if you want that
  repeat      
    ' here is the counter which simply loops through the timing list in the dat section.
    ' As we have 6 values in the list, we loop from 0 to 5
    repeat count from 0 to 5
      ' this replaces your repeat until, which checks the input-pin.
      ' As mentioned this only works fine if the pulse on Y_INDEXPULSE is shorter than the waittime and the
      ' 260 microseconds. If not simply add a waitpne with the same parameters. That makes sure that the input
      ' pin was low before waiting for high
      waitpeq( 1<<Y_INDEXPULSE, 1<<Y_INDEXPULSE, 0 )
      ' here we wait for the time found in the table at position "count"
      waitcnt( long[ @waittime+count ] + cnt )
      outa[LED_CONTROL]~~             ' turn LED on
      WaitCnt(MicroSecond * 260 + Cnt)  ' LED on at first line of frame 
      outa[LED_CONTROL]~                ' turn LED off
    
DAT
MicroSecond long 80

waittime long 400                    ' waittime + 0, this is close to the shortest time you can wait
         long 80_000_000 * 8 / 1000  ' waittime + 1
         long 80_000_000 * 6 / 1000  ' waittime + 2
         long 80_000_000 * 5 / 1000  ' waittime + 3
         long 80_000_000 * 4 / 1000  ' ....
         long 80_000_000 * 7 / 1000
         ' 80_000_000/1000 is equal to 1 ms if you run the prop with 80MHz.
         ' If not you have to change 80_000_000. The multiplication is done 
         ' before because we don't want to loose digits due to integer-arithmetics
Back to Top
 

Laserist1984
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Nov 2009
Total Posts : 6
 
   Posted 11/3/2009 12:07 PM (GMT -8)    Quote This PostAlert An Admin About This Post.
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

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Nov 2009
Total Posts : 6
 
   Posted 11/3/2009 4:13 PM (GMT -8)    Quote This PostAlert An Admin About This Post.
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

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Nov 2009
Total Posts : 6
 
   Posted 11/4/2009 2:23 AM (GMT -8)    Quote This PostAlert An Admin About This Post.
hello,
please can someone tell me where the problem in the code mentioned above?
thanks
Back to Top
 

MagIO2
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Mar 2009
Total Posts : 596
 
   Posted 11/4/2009 3:02 AM (GMT -8)    Quote This PostAlert An Admin About This Post.
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
 

mpark
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined May 2007
Total Posts : 682
 
   Posted 11/4/2009 4:54 AM (GMT -8)    Quote This PostAlert An Admin About This Post.
MagIO2 said...

      waitcnt( long[@waittime] + cnt )     ' wait 10 millisecond 


Hey MagIO2, why not just

      waitcnt( waittime + cnt )     ' wait 10 millisecond 

?

Also, earlier you suggested
waitcnt( long[@waittimes+counter] + cnt )
but that won't work unless counter is a multiple of four. Just use
waitcnt( waittimes[counter] + cnt )
Back to Top
 

MagIO2
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Mar 2009
Total Posts : 596
 
   Posted 11/4/2009 7:21 AM (GMT -8)    Quote This PostAlert An Admin About This Post.
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
 
[ << Previous Thread | Next Thread >> ]
New Topic Post Reply Printable Version
 
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