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 support@ccsinfo.com

Best approach to variable software PWM generator

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



Joined: 14 Nov 2015
Posts: 8

View user's profile Send private message

Best approach to variable software PWM generator
PostPosted: Fri Jun 03, 2016 8:02 am     Reply with quote

I need a pwm software generator with a frequency range from 10Hz to 500Hz and a progammable duty from 0% to 100%

Im happy with a duty jumps of even 5%(5%-10%-15%..)

Im using a PIC18F2550 with 20MHz external crystal and the goal is to control the frequency&duty sending the values via usb from a PC application, i already have the communication working so the only problem is the pwm part

the output will control a NPN transistor which will turn on/off a LED thus the output signal its not critical at all

So far my idea:
With tmr0 interrupts
The lowest period will be 2ms(1/500Hz) since i want at least a 5% resolution duty cycle i need to have at least 20 counts, so the clock must overload at a minimum of 2/20= 100uSec, then the rest of the program have to change the output_pin if needed

I assume that's reasonable? I have no idea if that's so fast or reachable taken into account that the usb communication have some interrupts aswell i think
Ttelmah



Joined: 11 Mar 2010
Posts: 19225

View user's profile Send private message

PostPosted: Fri Jun 03, 2016 8:23 am     Reply with quote

That is totally reasonable.

A search here will find examples of low frequency PWM's using this approach.

How fast are you clocking the CPU?.

With a 20MHz crystal on this chip, you have a choice of:

20MHz, 10MHz, 6.66MHz, 5MHz, 48MHz, 24MHz, 16MHz, 12MHz, 8MHz.....

48MHz obviously gives you the most 'time' (cycles) to handle everything.
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

View user's profile Send private message Send e-mail

PostPosted: Fri Jun 03, 2016 3:08 pm     Reply with quote

CCS have a built in function for PWM

Example:
Code:
setup_timer_2(T2_DIV_BY_16,130,2);//262 us overflow, 524 us interrupt
setup_ccp1(CCP_PWM);
set_pwm1_duty(dutycycle);

The above works for me at 32MHz
You have to check how low can be the period with different clock frequencies

Best wishes
Joe
Ttelmah



Joined: 11 Mar 2010
Posts: 19225

View user's profile Send private message

PostPosted: Sat Jun 04, 2016 1:00 am     Reply with quote

You are missing the point.

He wants a PWM, well _below_ the minimum frequency the standard hardware PWM offers. The CCS function only gives what the hardware supports.

There are enhanced PWM generators on some chips with wider ranges (all the PIC24's/30's and a few PIC16's). But on other chips you have to generate the PWM, by having a 'tick' interrupt at the shortest interval involved, and deciding in this whether each pin should be on or off. This is what he is asking. There is example code for this in the code library, and in quite a few posts here.
joergn



Joined: 15 Jul 2010
Posts: 15
Location: Hamburg Germany

View user's profile Send private message Visit poster's website

PostPosted: Sat Jun 04, 2016 4:22 am     Reply with quote

If a counter based PWM is done, you may want to avoid visible flicker of a PWM based LED driver, if other interrupts are may delaying such port changes.
You can disable such other interrupts when coming closer to port change action or you work with priority of interrupt. If the Timer interrupt routine is very fast and the other interrupts are also very fast there will be probably no interference.
My newest LED driver demo board for new US patented LED junction temp measurement is done by PIC16F688 20MHz running as a single channel PWM out of a timer with 350Hz.
TXRX is done by external USB bridge to keep the MCU cost low. Wink
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