CCS News

Tech Note: Software PWM - A Simple and Effective Alternative to CCP For PCM and PCH Compilers

Thursday 04 December, 2014

The CCS C Compiler pulse width modulation (PWM) library (#use pwm()) has been improved to provide software, bit-banged PWM control over any I/O pin without limitations. This new feature provides accurate PWM as well as customizable setup for period, frequency, duty cycle, and more... There are three unique features that make it versatile; setting it apart from using the hardware PWM peripheral:

  • The ability to assign multiple output pins to the same signal
  • The ability to explicitly set logic level of the output (or outputs) when the PWM is inactive
  • The tristate operating mode

Setting up the software PWM is simple and easy. Any desired options for duty cycle, period, frequency, etc. may be set while only using one line of code. Below is an example that illustrates the setup of a typical PWM with two outputs, a frequency of 1 kHz, and a duty cycle of 75%.

#use pwm(output=pin_a4, output=pin_b3, timer=1, frequency=1kHz, duty=75)

The signal will now output on both pins A4 and B3. Adding or removing more output pins is as simple as adding or removing output=X options.

Once running, the function pwm_set_duty_percent() may be used to change duty cycle. A stream identifier may be used in the #use pwm(), and other PWM functions, which allow multiple PWM pins to operate at the same time.

When disabled, the output pins of the Software PWM are capable of holding specific logic levels: high, low, and input. These options make the software PWM dependable even while disabled. The option to designate a specific disable level, may be added to the setup. Below is an example that designates a disable level using the PWM which was set up previously.

#use pwm(output=pin_a4, output=pin_b3, timer=1, frequency=1 kHz, duty=75, disable_level=low)

Now, whenever the built-in function pwm_off() is called, both pins a4 and b3 will be driven low.

An alternate operating mode for the software PWM is the tristate mode. In tristate mode, the compiler will force all output pins, associated with the PWM signal, to become inputs during the duty high time - instead of forcing them high. Activating tristate mode requires that only one simple option, 'tristate', be added to the setup.

The CCS C-Aware IDE will display a message indicating the output of the software PWM just as it does with hardware PWM. This feature is useful since the compiler may not be able to hit the exact frequency desired due to speed or processing limits of the target microcontoller used. Using the code from the examples above, the following is the message displayed by the IDE at compile time as well as the actual output as observed using a logic analyzer:

IDE message containing information on the Software PWM output

The actual output of the Software PWM

Like us on Facebook. Follow us on Twitter.

About CCS:

CCS is a leading worldwide supplier of embedded software development tools that enable companies to develop premium products based on Microchip PIC® MCU and dsPIC® DSC devices. Complete proven tool chains from CCS include a code optimizing C compiler, application specific hardware platforms and software development kits. CCS' products accelerate development of energy saving industrial automation, wireless and wired communication, automotive, medical device and consumer product applications. Established in 1992, CCS is a Microchip Premier 3rd Party Partner. For more information, please visit

PIC® MCU, MPLAB® IDE, MPLAB® ICD2, MPLAB® ICD3 and dsPIC® are registered trademarks of Microchip Technology Inc. in the U.S. and other countries.