CCS News

Higher Accuracy PWMs with PIC16F1574 and PIC16F1764

Monday 14 March, 2016

Ever wanted more accuracy in the output of your PWMs? The new PIC16F1574 and PIC16F1764 family of devices both have a 16-bit PWM peripheral. The advantage of using a 16-bit PWM is that it increases the resolution of the PWM signal allowing for more precise duty cycles. Additionally each 16-bit PWM peripheral has an independent timer, this allows for generating the PWM without using one of the device's timers. The CCS C Compiler supports the new 16-bit PWM peripheral using either built-in functions or the #use pwm() library. The following is an example of how to use the #use pwm() library to setup the 16-bit PWM peripheral on a PIC16F1574:

#include <16F1574.h>
#fuses NOWDT

#use delay(internal=8MHz)

#use pwm(output=PIN_C3, frequency=122Hz, bits=16, duty=50)

void main()
{
for(;;);
}

Because the 16-bit PWM peripheral has an independent timer and registers for setting the PWM signal's phase and offset, the following built-in functions have been added to the CCS C Compiler to set the period, phase and offset:

SET_PWMx_PERIOD() //x being the PWM peripheral
SET_PWMx_PHASE() //x being the PWM peripheral
SET_PWMx_OFFSET() //x being the PWM peripheral

The following is an example of how to use the built-in functions to setup the 16-bit PWM peripheral:

#include <16F1574.h>
#fuses NOWDT
#use delay(internal=8MHz)

#pin_select PWM1=PIN_C3

void main()
{
set_pwm1_period(0xFFFF); //8.192ms period, ~122Hz frequency
set_pwm1_duty(0x7FFF); //50% duty

setup_pwm1(PWM_STANDARD);

for(;;);
}

The new built-in functions are available in IDE compilers and the PCM command-line compilers starting with version 5.056.

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 http://www.ccsinfo.com.

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