CCS News

PID Module Introduced for the PIC16F1614 Family

Friday 22 May, 2015

Built-in functions have been added to the CCS C Compiler for setting up and using the PID module introduced for the PIC16F1614 family of devices. The PID module is a mathematics module that can perform a variety of operations, most prominently acting as a PID (Proportional-Integral-Derivative) controller.

The module accomplishes the task of calculating the PID algorithm by utilizing user-provided coefficients along with a multiplier and accumulator. The main benefit of using the PID module is for the PID calculation to be completed much faster with the hardware module than in software. For example when running the PIC® from the 32 MHz internal oscillator the built-in HW PID function was measured to take 12.8 us, compared to a software PID function which was measure to take 216 us. This is approximately 1/16 of the time.

The following are the built-in functions that have been added for the PID module:

  • setup_pid() - used to setup the PID module and set the user-input coefficients.
  • pid_get_result() - used to input the set point and feedback from the external system to the PID module, start the calculation, and to retrieve the result to input in the external system.
  • pid_read() - used to read various PID module registers.
  • pid_write() - used to write various PID module registers.
  • pid_busy() - used to check if PID module is busy or not-busy, finished, with calculation.

The user-input coefficients, K1, K2 and K3, are calculated from the three classic PID coefficients Kp, Ki and Kd with the following equations:



T is the sampling period.

The following is an example of how to setup and use the PID module:

void main(void) {
pid_struct_t PIDOutput;
unsigned int16 ADCReading;
signed int16 K1 = 7, K2 = -6, K3 = 0;
unsigned int16 SetPoint = 500;
unsigned int16 PWMDuty;

setup_pid(PID_MODE_PID, K1, K2, K3);

//Setup ADC
setup_adc_ports(sAN3, VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(3);

//Setup PWM 3
setup_timer_4(T4_CLK_INTERNAL | T4_DIV_BY_32, 249, 1); //1ms period, from 32 MHz clock
set_pwm3_duty(0); //0% duty
setup_pwm3(PWM_ENABLED | PWM_OUTPUT | PWM_TIMER4);

while(TRUE) {
delay_ms(50);

ADCReading = read_adc();
pid_get_result(SetPoint, ADCReading, &PIDOutput);

PIDOutput.u &= 0x07;

if(PIDOutput.u >= 4) //PIDOutput is negative, set PWMDuty to Minimum
PWMDuty = 0;
else if(PIDOutput.u != 0) //PIDOutput > Maximum, set PWMDuty to Maximum
PWMDuty = 1000;
else if(PIDOutput.l > 1000) //PIDOutput > Maximum, set PWMDuty to Maximum
PWMDuty = 1000;
else
PWMDuty = PIDOutput.l;

set_pwm3_duty(PWMDuty);
}
}

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

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.