0
votes

I am using the libraries provided by C18 compiler to open and set the duty cycle for PWM usage. I noticed that the max PWM frequency I can get with 100% duty-cycle is about 13.5 KHz. The lower the duty-cycle the higher the PWM frequency. How can I achieve a higher PWM frequency with still 100% duty-cycle? Is it possible to at least get more than 13.5 KHz? I just can't figure out what I missing, maybe someone can help here, and I am using PIC18F87J1.

Here is the C18 C Compiler Libraries

Here is PIC18F87J1 datasheet

Here is a snippet of the code I am using regarding PWM.

 TRISCbits.TRISC1 = 0;
 OpenTimer2(TIMER_INT_OFF & T2_PS_1_1 & T2_POST_1_1);
 OpenPWM2(0x03ff);
 SetDCPWM2(255);

Your help is appreciated, thanks!

1
That duty cycle and frame rate are linked seems surprising, perhaps you have the timers misconfigured. Speaking generally without looking at the details of your platform, check that the processor clock is running at its maximum rate, that any available PLL clock multiplier is in use, and that you do not have any system, peripheral, or counter prescalar active, as these would reduce the clock going into the counter.Chris Stratton
The processor clock is running at 8 MHz and I don't think I have any other counter prescalar active that could effect the frequency. I did noticed that changing the prescalar value from 1:1 to 1:4 or higher gave me less pwm frequency.Ammar
Is the Duty cycle and pwm frequency inversely proportional to each other?Ammar
No, normally they are independent, with one timer (section) or mode generating the overall period and another generating the on-time. Are you sure the duty cycle is meaningfully changing, and that you aren't just varying the on-time, with the period being only trivially (one count or so) greater?Chris Stratton
Make sure that your setting of the OSCCON register is really giving you 8 MHz. Your chip also has an x4 PLL, if you start using that you will have a 32 MHz clock and increase your PWM timing or resolution.Chris Stratton

1 Answers

1
votes

For a start, you have the parameters to the functions reversed. Open() takes a char value less than 256, and Set() takes a 10-bit number.

That said, you have chosen the largest value (255) which gives the lowest frequency. As the datasheet explains, the Open() function takes a value for the period as the parameter. A higher frequency is equivalent to a shorter period, and vice versa.

Lastly, why would you want a duty cycle of 100%? That is the same as having the pin always high. In that case, frequency doesn't matter at all. Just turn the pin on and don't use PWM at all.

You haven't said what you are driving with this PWM, but generally speaking, having the frequency too high can cause problems. It can produce radio interference, overheat, and so on.

Your question indicates you misunderstand the purpose of PWM and what the terms refers to, so here is a tl;dr.

PWM simulates a voltage between 0 and Vcc by rapidly turning a pin high and low. The simulated voltage is proportional to the time_high/(time_high + time_low). The percent of time the pin is at Vcc is called the duty cycle. (So 100% duty is always on, giving Vcc volts. 0% duty is always off, giving 0 V.)

The rate at which this on/off cycle repeats is called the PWM frequency. If the frequency is too small (the period too long) the load will see the pin voltage fluctuating. The goal is to run the PWM fast enough to smooth out the voltage the load sees, but not so fast as to cause other problems. The available frequencies are appropriate for most applications. Also note that setting the frequency high (period small) will also reduce the accuracy of the duty cycle. This is explained in the datasheet. The reason is basically that the duty cycle must ultimately be converted to clock ticks on versus clock ticks off. The faster the frequency, the fewer ways to divide the clock ticks in each cycle.