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

Multiple PWM and Timer 2

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



Joined: 24 Sep 2006
Posts: 262

View user's profile Send private message AIM Address

Multiple PWM and Timer 2
PostPosted: Sat Jan 24, 2015 12:49 pm     Reply with quote

Some PICs (16F882) have 4 PWM outputs. Looking at the data sheets I see they use Timer 2 to set the PWM. How can you set 4 motors at different speeds if they all use Timer 2?

Now I am starting to wonder about a circuit I built (that works well) using a 16F722 that controls two servos, and another 16F722 that controls two motors. They all seem to work OK, but why?

Both these PIcs are fed servo-positions/motor-speeds via I2C and run the devices on their own without changing positions or speeds until an update is sent. The motor speed and direction is controlled by an L298 full bridge that is sent direction bits and the PWM from the PIC. Now I don't understand why everything works so well.

Are there glitches I'm not seeing because the updates are being sent a few times per second?
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Sat Jan 24, 2015 1:00 pm     Reply with quote

With PWM, it is the pulse width not the frequency that controls the target. One frequency, different pulse widths.

However 'beware'. A lot of chips with four PWM outputs, may mislead you. Some do have four independent PWM's, but most with four outputs, have these to allow complementary PWM's, not four separate channels. You need to look carefully in the data sheet at how many timing generators the chip has. The 16F882, actually has two timing generators, and five outputs. CCP1, has four outputs, from one PWM, offering half bridge and full bridge control of one motor. Then CCP2, offers another single output.
rovtech



Joined: 24 Sep 2006
Posts: 262

View user's profile Send private message AIM Address

PostPosted: Sat Jan 24, 2015 1:00 pm     Reply with quote

The data sheet for the 16F1503 states:

The period is common to all PWM modules, whereas the duty cycle is independently controlled.

So I guess as long as I use the same period they operate synchronously just changing the duty cycle. That explains why my circuit works.

However what if I want to run at different periods? Are there any PICs that have totally independant PWM outputs?
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Sat Jan 24, 2015 1:08 pm     Reply with quote

Yes. However you need to go 'up family'. Some of the PIC18's have a second 16bit timer and allow you to use this, and then the PIC24's can give multiple independent PWM's.

As a further comment though, I have to point out that PWM's are not designed for different frequencies. It is the pulse _width_ that is modulated. The constraints on frequency are usually relatively wide, with low frequency determined by things like motors going noisy if driven off a low frequency, and the upper frequency limited by how fast you can switch the FET's etc..
You can probably run a light (for example), off anything from perhaps 400Hz to 100KHz (with good FET driving), while a motor, can work off perhaps 5Khz to a similar upper limit. So it's perfectly sensible to just use one frequency for all the PWM's at perhaps something like 10 to 50KHz.

The times when variable frequencies are used are things like BLDC control. For these the frequencies are much lower, and are not directly generated by the PWM. You don't want a BLDC phase 'hard on' from a PWM. Instead you want the phase current to have something more like a sinusoidal output. This is commonly done by having a much faster PWM frequency (perhaps 50KHz), and modulating the pulse width of this to synthesise the sinusoidal output. If you do a search in Microchip, you will find a whole suite of application notes describing the driving of BLDC motors with three single frequency PWM's. Even better several PIC's are available with a triple channel PWM for exactly this reason. For the PIC16, if you look at AN1305, it shows the synthesis of a BLDC drive (on a PIC with the ECCP peripheral). It also refers to an older AN, that did this without using this peripheral. Timings are all controlled using the CCP, and you can see the way that the waveforms are synthesised to give the motor control.
guy



Joined: 21 Oct 2005
Posts: 297

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

PostPosted: Sun Jan 25, 2015 2:13 am     Reply with quote

You also need that ability (multiple frequencies of PWM signals) if you want to play polyphonic music (several notes played simultaneously). One approach is to use a common timebase of several micro-seconds (interrupt or polling-based) and calculate your frequencies as multiples of that timebase, to use a separate timer for each frequency, or actually do everything software-DIY based on a single timer.
Also there are tricks such as using the UART, SPI or other peripherals as PWM generators but this doesn't always fit your project.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Sun Jan 25, 2015 2:40 am     Reply with quote

Seriously, I wouldn't use PWM for that.
PWM, has very low timing resolution on the frequency. Usually only 8bit, and with multipliers from the prescalers. You couldn't even get close to a musically acceptable 'fit' from the PWM, for any but the most awfully 'tuneless' music....
It's not 'using a sledgehammer to crack a nut', it's using a pneumatic drill to try to do watch repairs. Completely the wrong tool.

This is why some PIC's have things like the programmable frequency generator, and there are several external chips that can do this. Look at PIC's with the NCO module, or look at some of the external synthesiser chips.
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