PDA

View Full Version : Unsolved Can anyone point me to a spin object to output x pulses over a period of y seconds?



youngdave
02-27-2012, 09:54 AM
Can anyone point me to a spin object to output x pulses over a period of y seconds?
TIA David Young

idbruce
02-27-2012, 10:05 AM
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 G
02-27-2012, 12:57 PM
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 (http://forums.parallax.com/showthread.php?89958-Propeller-Education-Kit-Labs-Tools-and-Applications)

Tracy Allen
02-27-2012, 04:17 PM
Take a look at NCOburst.spin (http://obex.parallax.com/objects/776/), which uses a pair of counters to do something like that.

Duane Degn
02-27-2012, 04:32 PM
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 (http://forums.parallax.com/showthread.php?137370-LED-Flasher) 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).

idbruce
02-27-2012, 06:29 PM
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)