Shop OBEX P1 Docs P2 Docs Learn Events
Can anyone point me to a spin object to output x pulses over a period of y seconds? — Parallax Forums

Can anyone point me to a spin object to output x pulses over a period of y seconds?

youngdaveyoungdave Posts: 70
edited 2012-02-27 10:29 in General Discussion
Can anyone point me to a spin object to output x pulses over a period of y seconds?
TIA David Young

Comments

  • idbruceidbruce Posts: 6,197
    edited 2012-02-27 02:05
    youngdave

    You don't really need an object for this, it can easily be accomplished with a watchdog cog, a REPEAT loop, and WAITCNT or you could use a timer for PWM. If you are really looking for an object, search the OBEX for PWM.

    Bruce
  • Mike GMike G Posts: 2,702
    edited 2012-02-27 04:57
    youngdave, take a look at counters.

    Application Notes with sample code and excellent documentation.
    http://www.parallaxsemiconductor.com/an001

    Forum sticky - scroll down to PE Kit Tools -> Transmit Square Wave Frequencies
    Propeller Education Kit Labs, Tools,·and Applications
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2012-02-27 08:17
    Take a look at NCOburst.spin, which uses a pair of counters to do something like that.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-27 08:32
    Depending on how short the pulses need to be, you could do as Bruce suggests and just use waitcnt to control the pin(s).

    Here's a link to a post with code I use to flash LEDs.

    This is the method that does all the work.
    PRI LedFlash
    '' Runs in its own cog.
      repeat
        repeat result from 0 to _HighestLedPin  ' check each pin
          if active[result] == _Deactivate  ' should pin be deativated?
            outa[result] := 0
            dira[result] := 0
          if active[result] == _Active  ' is pin active?
            dira[result] := 1
            if state[result] == _Off
              
              if cnt - time[result] > offInterval[result]  ' has pin been off long enough?
                time[result] += offInterval[result]
                state[result] := _On
                outa[result] := 1            
            else
              if cnt - time[result] > onInterval[result]  ' has pin been on long enough?          
                time[result] += onInterval[result]
                state[result] := _Off
                outa[result] := 0
                if count[result] <> -1
                  if --count[result] =< 0                         ' decrement count if not -1
                    active[result] := _Inactive           
                    dira[result] := 0 
    
    

    The object lets you any number of the IO pins. I wrote this so I could flash multiple LEDs (up to 32) at different rates using one cog. Since it's written in Spin and test each pin to see if it's an active pin, it's relatively slow (but fast enough for flashing LEDs).

    The object I linked to lets you set the on time, off time and how many times you want the LED/pin to turn on and off. It should be easy to modify to use time as a parameter instead of the number of pulses.

    Let us/me know if you need help converting it to your needs (assuming it's fast enough for the pulses you want to make).
  • idbruceidbruce Posts: 6,197
    edited 2012-02-27 10:29
    youngdave

    Here is a quick and easy example of using a timer.
    CON
      _CLKMODE = XTAL1 + PLL16X
      _XINFREQ = 5_000_000  
      
    PUB SetThePulse | PulsePin, Second, Minute, HighTime, LowTime, Counter 
      PulsePin := 1 'This is the IO pin which will send the pulse
      Second := CLKFREQ
      Minute := Second * 60
      HighTime := CLKFREQ   'Establish high time using CLKFREQ, Second, Minute, or a combination
      'of all of these with the proper operators.
      LowTime := CLKFREQ    'Establish low time using CLKFREQ, Second, Minute, or a combination
      'of all of these with the proper operators.
      'Can anyone point me to a spin object to output x pulses over a period of y seconds?
      'Without further input from you, we will assume that you can determine the proper math.
      'But in this example, we have assumed high pulse width of 1 second and a cycle offset
      'of 1 second.  And in this example, Pin #1 will go high 10 times over a 20 second period.
    
      CTRA[30..26] := %00100 ' Configure Counter A to NCO
      CTRA[5..0] := PulsePin
      FRQA := 1
      DIRA[PulsePin]~~
      Counter := CNT 'Get the current clock count
      
      REPEAT 10
        PHSA := -HighTime
        WAITCNT(Counter += LowTime)
    
Sign In or Register to comment.