Send number of pulses with delay time using propeller hardware counter
propcog
Posts: 4
Hi,
I'm a newbie propeller user and a want to use the P8X32A for pulse generation.
When I run my code a counter is producing a number of PWM pulses with adjustable frequency and duty-cycle and waits a couple of mSec and starts again.
Problem is when it's restarting the counter it generates a pulse, after the count has reached max number of pulses and then waits... why is this happing?
Is it possible to insert a delay-time between the PWM hardware counter generator?
Thanks for your time,
Regards!
I'm a newbie propeller user and a want to use the P8X32A for pulse generation.
When I run my code a counter is producing a number of PWM pulses with adjustable frequency and duty-cycle and waits a couple of mSec and starts again.
Problem is when it's restarting the counter it generates a pulse, after the count has reached max number of pulses and then waits... why is this happing?
Is it possible to insert a delay-time between the PWM hardware counter generator?
Thanks for your time,
Regards!
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 ' CHECK THIS!!! pwm1pin = 16 pwm2pin = 17 VAR long parameter OBJ pwm1 : "pwmasm" PUB go | x pwm1.start(pwm1pin) pwm1.SetFreq(2) pwm1.SetDuty(50) pwm1.SetGatePulses(2) pwm1.SetGatePeriodms(1000) ------------------- { usage OBJ pwm : pwmAsm .... pwm.start( Pin) ' start pwm pwm.SetPeriod( period ) ' set pwm period in clock cycles pwm.SetFreq( freq ) ' set pwm frequency in Hz pwm.SetDuty( duty ) ' set duty in % pwm.SetGatePulses( pulses ) ' set number of pulses pwm.SetGatePeriod( period ) ' set waittime next start of pulses after mSec pwm.Stop } VAR long cogon, cog long sDuty ' order important (the variables are read from memory in this order) long sPinOut long sCtraVal long sPeriod long sGatePulses long sGatePeriod PUB Start( Pin) : okay 'start pwm on Pin longfill(@sDuty, 0, 4) sDuty := 50 ' default duty sPinOut := |< Pin sCtraVal := %00100 << 26 + Pin sPeriod := 1000 sGatePulses := 2 sGatePeriod := 40000000 okay := cogon := (cog := cognew(@entry,@sDuty)) > 0 PUB stop '' Stop object - frees a cog if cogon~ cogstop(cog) longfill(@sDuty, 0, 4) PUB SetPeriod(counts) ' set pwm period in clock cycles, frequency = (_clkfreq / period) sPeriod := counts PUB SetFreq( freq ) sPeriod := clkfreq / freq * sDuty / 100 PUB SetDuty(counts) if (counts < 0) counts := 0 if (counts > 100) counts := 100 sDuty :=counts*sPeriod/100 PUB SetGatePulses(counts) ' set pwm period in clock cycles, frequency = (_clkfreq / period) sGatePulses := counts PUB SetGatePeriod(counts) ' set pwm period in clock cycles, frequency = (_clkfreq / period) sGatePeriod := counts PUB SetGatePeriodms(msec) sGatePeriod := 80_000_000 / 1000 * msec DAT org entry mov t1,par 'get first parameter rdlong value, t1 add t1,#4 rdlong pinOut, t1 or dira, pinOut ' set pinOut to output add t1, #4 rdlong ctraval, t1 mov ctra, ctraval 'establish counter A mode and APIN add t1, #4 rdlong period, t1 add t1, #4 rdlong gpulses, t1 add t1, #4 rdlong gperiod, t1 mov frqa, #1 'set counter to increment 1 each cycle mov time, cnt 'record current time add time, period 'establish next period mov tmp, #0 mov ctra, ctraval :loop rdlong value, par 'get an up to date pulse width waitcnt time, period 'wait until next period cmp tmp, gpulses wz if_e mov ctra, #0 if_e mov tmp, #0 if_e waitcnt time, gperiod if_e or outa,#0 if_e mov ctra, ctraval neg phsa, value 'back up phsa so that it trips "value" cycles from now add tmp, #1 jmp #:loop 'loop for next cycle period res 1 time res 1 value res 1 t1 res 1 pinOut res 1 ctraval res 1 ctraoff res 1 gpulses res 1 gperiod res 1 tmp long 0
Comments
When the number of pulses are done I stop the counter and wait a couple of mSec, the I restart the counter. Don't know if this is the right approach??
Regards!
Don't know what happened but my first post is gone?
My calling parameters are these:
The PASM code is attached.
When the number of pulses are generated I stop the counter and wait for a few mSec. Then I restart the counter. But it generates an extra pulse?
Regards!
Note that PASM waitcnt target, delta waits first for target then adds delta to target.
Looks like your first post has been restored.
Looks like I have lots more to learn :nerd: Timing the output of the gated pulses etc. And I also want to update the values from the main SPIN code and adjust the counter on demand.
Regards!