View previous topic :: View next topic |
Author |
Message |
danko
Joined: 24 Nov 2013 Posts: 2
|
Is there a software way to count pulses of pwm pins? |
Posted: Sun Nov 24, 2013 4:00 am |
|
|
Hi all, I want to run very precise PWM.
Can I start and stop PWM, after a number of pulses. Is there a software way to count pulses of pwm pins?
Thanks ! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9228 Location: Greensville,Ontario
|
|
Posted: Sun Nov 24, 2013 6:12 am |
|
|
Yes
Tie the PWM output pin to an input pin that can be used as an interrupt source.In the ISR just increment a variable.
Now as to how good this will work...
...technically it does work, however due to ISR overhead and othr issues the 'precision' may not be acceptable for your application which we know nothing about or the PIC you've chosen.
hth
jay |
|
|
danko
Joined: 24 Nov 2013 Posts: 2
|
|
Posted: Sun Nov 24, 2013 8:05 am |
|
|
I need a 64 KHz PWM , after each 32 pulse must stop for a period of 32 pulses. I have not chosen a specific PIC, a 12F1840 seems good. Is there a "software" way, for example: to check the timer register, or something But there is no problem to do it with external interrupt. Thanks. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Nov 24, 2013 8:34 am |
|
|
Depends on which PIC you choose.
Assuming you use Timer2 for the timebase, you can use the post scaler to generate an interrupt after several PWM cycles.
With say a 18F1320 you could interrupt after 16 cycles. Significantly reduces the work load.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Mon Nov 25, 2013 9:35 am |
|
|
Alternatively, feed the PWM output back in, as the clock input to any other timer. Clear the timer, start the PWM, and then set the pulse width to '0', when the timer reaches 31 (remember the pulse width updates on the _next_ PWM pulse the 32nd pulse will have already started when you count 31).
If you want, you could use a timer than can feed a CCP, and program this to interrupt when the count gets to 31.
Best Wishes |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Nov 26, 2013 8:04 am |
|
|
I was thinking along these lines.
Let Timer2 post scaler interrupt every 16 Timer2 cycles, then loop as:-
1) Wait for two interrupts, turn PWM on.
2) Wait for two more interrupts, turn PWM off.
3) Loop back to 1.
There will be a delay from interrupt to turning PWM on/off.
If both delays are identical you should get exactly what you want.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Tue Nov 26, 2013 8:20 am |
|
|
Yes. That should work.
Best Wishes |
|
|
|