CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Is there a software way to count pulses of pwm pins?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
danko



Joined: 24 Nov 2013
Posts: 2

View user's profile Send private message

Is there a software way to count pulses of pwm pins?
PostPosted: Sun Nov 24, 2013 4:00 am     Reply with quote

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: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Nov 24, 2013 6:12 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Nov 24, 2013 8:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Nov 24, 2013 8:34 am     Reply with quote

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: 19506

View user's profile Send private message

PostPosted: Mon Nov 25, 2013 9:35 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Nov 26, 2013 8:04 am     Reply with quote

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: 19506

View user's profile Send private message

PostPosted: Tue Nov 26, 2013 8:20 am     Reply with quote

Yes. That should work.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group