# How to understand the PWM capability of a microcontroller?

Status
Not open for further replies.

#### Flyback

##### Well-Known Member
Can the “Programmable switch mode controller” in the PIC16F1789 microcontroller provide a PWM output which is anything up to 25KHz, and with a duty cycle between 10% and 100%? (to control a 48V fan with)

PIC16F1789 microcontroller datasheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/41675A.pdf
Can I also have (simultaneously , on a different pin), a 200KHz , 50% duty cycle square wave output from the PWM module? (aswell as the above 25KHz PWM output.)
Also what is the difference between a “PWM channel” and a “PWM output”? –And surely, if a microcontroller has a PWM output capability, then it must have a “PWM timebase”. –On the microchip microcontroller selector tool, it says I can’t have a “PWM timebase”, but I can have a “PWM output”…does that make any sense?
Microchip microcontroller selector tool:
http://www.microchip.com/maps/microcontroller.aspx

Last edited:

#### geko

##### Active Member
Can the “Programmable switch mode controller” in the PIC16F1789 microcontroller provide a PWM output which is anything up to 25KHz, and with a duty cycle between 10% and 95%? (to control a 48V fan with)
You didn't say any of that in your original post. All the parts it presented in the results probably meet the requirement you specified. You can't expect the selector tool, or anyone else for that matter to 'know' the exact requirements you want from the PWM output.

#### Pommie

##### Well-Known Member
Most Helpful Member
As the PWM modules use timer2 for the frequency, they all have to run at the same speed. If you switch to something like the 18F43K22 then you can have two different frequencies however you would only have 7 bit resolution at 200kHz.

Mike.

#### Jon Wilder

##### Active Member
The PSMC module is not needed. For what you're trying to accomplish, one of the three Capture/Compare/PWM modules (CCP modules) can generate the variable duty cycle PWM that you need.

The first thing we need to do to set the PWM period is to write a value to register PR2. However, we must know what value to write to PR2 in order to yield a 25kHz PWM frequency, and this value is dependent on the PIC's oscillator clock frequency and the PWM frequency we are trying to achieve. The equation for this is -

(PR2 + 1) x (4 x Tosc) = PWM Period

where -

PR2 is the value written to PR2
Tosc is the period of your primary oscillator frequency (Fosc)

The easiest way to do this is to first figure out the period of a 25kHz signal. We can find this by reciprocating 25kHz -

1 / 25,000 = 40uS or microSeconds

We then find the period of our oscillator frequency. Assuming a 16MHz clock -

1 / 16,000,000 = 62.5nS or nanoSeconds

We then multiply this value by 4. This is because the instruction clock frequency is 1/4th the primary oscillator frequency -

62.5nS x 4 = 250nS

So our equation now becomes -

(PR2 + 1) x 250nS = PWM Period

Now to find the PR2 value, we simply divide the period of 25kHz by the period of the instruction clock, then subtract 1 -

(40uS / 250nS) - 1 = 159

With a 16MHz clock, a value of 159 written to PR2 will yield a PWM period of 40uS, which results in a PWM frequency of 25kHz. In assembly language, we would execute the following instructions -

Code:
               movlb              d'0'                              ;select register bank 0
movlw              d'159'                          ;set timer 2 for PWM period of 40uS w/16MHz Fosc
movwf              PR2
You would then enable Timer 2 by writing 0b00000100, or 0x04, to register T2CON. This enables timer 2 and sets it to overflow when the timer reaches the value of 159 -

Code:
              movlw               b'00000100'               ;enable timer 2, 1:1 prescale, 1:1 postscale
movwf               T2CON
Finally, write 0b00001100 to register CCP1CON to set up CCP1 in PWM mode, then write 0b00000100 to register TRISC to set up RC2 as an output (CCP1 is multiplexed onto PORTC,RC2) -

Code:
             movlb               d'5'                       ;select register bank 5
movlw               b'00001100'                ;CCP1 set up for PWM mode
movwf               CCP1CON
movlb               d'1'                       ;select register bank 1
bcf                 TRISC,RC2                  ;RC2 output enable, function is CCP1 PWM output
From there you would vary the duty cycle by writing a value from 0-159 to register CCPR1L. This will yield a variable 0-5V output on pin RC2/CCP1 in steps of 31.25mV.

As far as the 200kHz 50% duty cycle square wave goes, you can generate that on any I/O pin with a timer interrupt. Set the timer to interrupt every 2.5uS and XOR the pin on each interrupt.

Last edited:

#### Flyback

##### Well-Known Member
thanks, the 200khz does not have to be exactly 50%...anywhere between 20 and 60% is ok.

Thanks for the CCP info, by the way, if CCP modules can do "PWM" outputs, then why do some micro's have "PWM" outputs, why don't they all just have "CCP" outputs.?

..I mean , what is the point of a "PWM Module", which some micro's have, when a CCP module can do it?

Last edited:

#### misterT

##### Well-Known Member
Most Helpful Member
I have experimented with "random probability output". That is when, instead of linear counter, you have random number generator which is compared continuously to some value. This gives near maximum freq. around 50%. I expect random number generators to hit market soon.. as a microcontroller pheripheral.

#### Jon Wilder

##### Active Member
I think the PSMC you're referring to is specifically tailored for switch mode power supply applications. Most of Microchip's PIC line have CCP modules on chip, many with multiple CCP modules.

Status
Not open for further replies.

Loading