Help with motor test loop using incrementing PWM pulse widths
macrobeak
Posts: 354
I am trying to implement a simple PWM generating loop to experiment with an electric motor.
I have a working H Bridge/motor system interfaced to the Prop, but I am stuck on the spin programming.
The PWM spin module to generate PWM pulses is quite straightforward, thanks to examples by Harpit Sandhu and others.
However, I would like to run a repeating loop with incrementing PWM pulse widths, allowing half a second or so motor run time for each pulse width.
My unsuccessful spin program is attached below.
Please could someone suggest the correct spin coding to achieve this.
Many thanks in advance . . .
===============================================================================================================================
[FONT="]'' PWM_5[/FONT]
[FONT="]'' 25/11/11 Labbook[/FONT]
[FONT="]'' Simple PWM control program based upon Sandhu[/FONT]
[FONT="]'' Chap 7 prog 2 [/FONT]
[FONT="] [/FONT]
[FONT="] [/FONT]
[FONT="]CON[/FONT]
[FONT="]_clkmode = xtal1 + pll2x[/FONT]
[FONT="]_xinfreq = 5_000_000[/FONT]
[FONT="] [/FONT]
[FONT="]VAR[/FONT]
[FONT="] long Cycle_Time[/FONT]
[FONT="] long period[/FONT]
[FONT="] long stack [50][/FONT]
[FONT="] long PWMPeriod[/FONT]
[FONT="] long PWMPulse[/FONT]
[FONT="] [/FONT]
[FONT="]OBJ[/FONT]
[FONT="] [/FONT]
[FONT="]PUB Main[/FONT]
[FONT="] PWMPeriod:=clkfreq/1000 'max PWM pulse width = 1ms, same as below[/FONT]
[FONT="] cognew (PWMgen(PWMPulse), @stack) 'start the PWM generation cog[/FONT]
[FONT="] repeat PWMPulse from 0 to PWMPeriod step 500 'repeat cycle for increasing pulse widths[/FONT]
[FONT="] waitcnt (40_000_000 + cnt) 'allow 0.5sec motor run steps for each pulse width [/FONT]
[FONT="] [/FONT]
[FONT="]PUB PWMgen(PulseWidth) 'Run the PWM generation in a separate cog[/FONT]
[FONT="] dira[7]~~[/FONT]
[FONT="] ctra[30..26] :=%00100 'PWM mode Apin - Prop7 output[/FONT]
[FONT="] ctra[5..0] :=7[/FONT]
[FONT="] frqa :=1[/FONT]
[FONT="] PulseWidth:=-5000[/FONT]
[FONT="] Cycle_Time:=clkfreq/1000 'max PWM pulse width = 1ms[/FONT]
[FONT="] period:=cnt[/FONT]
[FONT="] repeat[/FONT]
[FONT="] phsa:=Pulsewidth 'endless loop generating PWM pulses[/FONT]
[FONT="] period:=period + Cycle_Time[/FONT]
[FONT="] waitcnt (period)
==========================================================================================================================
[/FONT]
I have a working H Bridge/motor system interfaced to the Prop, but I am stuck on the spin programming.
The PWM spin module to generate PWM pulses is quite straightforward, thanks to examples by Harpit Sandhu and others.
However, I would like to run a repeating loop with incrementing PWM pulse widths, allowing half a second or so motor run time for each pulse width.
My unsuccessful spin program is attached below.
Please could someone suggest the correct spin coding to achieve this.
Many thanks in advance . . .
===============================================================================================================================
[FONT="]'' PWM_5[/FONT]
[FONT="]'' 25/11/11 Labbook[/FONT]
[FONT="]'' Simple PWM control program based upon Sandhu[/FONT]
[FONT="]'' Chap 7 prog 2 [/FONT]
[FONT="] [/FONT]
[FONT="] [/FONT]
[FONT="]CON[/FONT]
[FONT="]_clkmode = xtal1 + pll2x[/FONT]
[FONT="]_xinfreq = 5_000_000[/FONT]
[FONT="] [/FONT]
[FONT="]VAR[/FONT]
[FONT="] long Cycle_Time[/FONT]
[FONT="] long period[/FONT]
[FONT="] long stack [50][/FONT]
[FONT="] long PWMPeriod[/FONT]
[FONT="] long PWMPulse[/FONT]
[FONT="] [/FONT]
[FONT="]OBJ[/FONT]
[FONT="] [/FONT]
[FONT="]PUB Main[/FONT]
[FONT="] PWMPeriod:=clkfreq/1000 'max PWM pulse width = 1ms, same as below[/FONT]
[FONT="] cognew (PWMgen(PWMPulse), @stack) 'start the PWM generation cog[/FONT]
[FONT="] repeat PWMPulse from 0 to PWMPeriod step 500 'repeat cycle for increasing pulse widths[/FONT]
[FONT="] waitcnt (40_000_000 + cnt) 'allow 0.5sec motor run steps for each pulse width [/FONT]
[FONT="] [/FONT]
[FONT="]PUB PWMgen(PulseWidth) 'Run the PWM generation in a separate cog[/FONT]
[FONT="] dira[7]~~[/FONT]
[FONT="] ctra[30..26] :=%00100 'PWM mode Apin - Prop7 output[/FONT]
[FONT="] ctra[5..0] :=7[/FONT]
[FONT="] frqa :=1[/FONT]
[FONT="] PulseWidth:=-5000[/FONT]
[FONT="] Cycle_Time:=clkfreq/1000 'max PWM pulse width = 1ms[/FONT]
[FONT="] period:=cnt[/FONT]
[FONT="] repeat[/FONT]
[FONT="] phsa:=Pulsewidth 'endless loop generating PWM pulses[/FONT]
[FONT="] period:=period + Cycle_Time[/FONT]
[FONT="] waitcnt (period)
==========================================================================================================================
[/FONT]
Comments
Thanks for the response. Actually, I had moved on and made use of your article to run my motor experiment.
However I really would like some help to clear up my misunderstanding of spin programming in the above faulty program.
Regards
So, given that it's all the same file PWMgen could look at PWMPulse directly as it's a global variable (but shouldn't modify it to avoid confusing the repeat loop). Does that make sense?
When you post code please wrap it in [noparse] [/noparse] tags.