dsPIC® DSC Implementation of a PID Loop

Last modified by Microchip on 2024/01/29 21:20

Overview

Many different techniques can be used to implement a Proportional, Integral and Derivative (PID) loop in a dsPIC®.  The approach that has been used in the current module is to introduce some advanced techniques to take the maximum possible advantage of the dsPIC itself. The reason for this is that in the production of a product, the designer is almost always finding ways to optimize the performance of the system, usually in terms of response time and transient response behavior.

You will become familiar with the relevant internal registers and will learn how to design with fast and efficient PID code. You will also learn how to squeeze the maximum accuracy from the system. The importance of getting the maximum accuracy will lead to maximum system performance.

The following figures present how a fast-, high-performance PID is implemented using the specific dsPIC hardware (DSP engine).
The architectural blocks used are:

  • RAM, for storing coefficients and input error values
  • Two 16-bit registers (W4 and W5) used to store data
  • Two 16-bit registers (W8 andW10) to store pointers to RAM locations
  • One 40-bit accumulator

fast-, high-performance PID is implemented using the specific dsPIC hardware

The Coefficients area in RAM stores the Ka, Kb, Kc coefficients of the system. These values are fixed and will not be changed during the system operation.

The Coefficients area in RAM stores the Ka, Kb, Kc coefficients of the system

The Errors area in RAM stores the error values. What is shown is the buffer status before a new PID output is computed (it will be understood later).

The Errors area in RAM stores the error values

e(n-1) and e(n-2) are equal to zero at n=0; then they are updated at each step.

At time n, the accumulator contains the previous value of the PWM active period.

the accumulator contains the previous value of the PWM active period

Below is the high-level block diagram of the PID implementation

high level block diagram of the PID implementation

Back to Top

dsPIC® Digital Signal Controllers (DSCs) Implementation Operations

The first operation is to set the two pointers to point at the first location of each buffer in the RAM. W8 in the following diagram is the pointer to the coefficients buffer; W10 is the pointer to the error buffer.

W8 in the following diagram is the pointer to the coefficients buffer; W10 is the pointer to the error buffer

The new error value is computed as the difference between the reference voltage (Vref) and the new output voltage read by the Analog-to-Digital Converter (ADC) (ADCBUF1). See the accompanying image.

new error value is computed as the difference between the reference voltage (Vref) and the new output voltage read by the Analog-to-Digital Converter (ADC)

Back to Top

movsac Instruction

The movsac instruction performs several different tasks. The accompanying diagram and steps demonstrate the movsac instruction.

diagram and steps demonstrate the movsac instruction.

Move the RAM contents.

Moves the content of the RAM location pointed by W8 into register W4.
Moves the content of the RAM location pointed by W10 into register W5.
Move the RAM contents

Back to Top


Update the Working registers.

Updates the W8 register so that it is now pointing to the following RAM location.

Updates the W10 register so that it is now pointing to the following RAM location.

Update the Working registers.

Updates the W10 register so that it is now pointing to the following RAM location

Back to Top

Proportional-Integral-Derivative (PID) Term Computation

Below are the computation steps of the first term of the PID.

The W4 and W5 registers' content is multiplied and the result is added to the 40-bit accumulator B.

The W4 and W5 registers' content is multiplied and the result is added to the 40-bit accumulator B

Back to Top


Moves the content of the RAM location pointed by W8 into register W4.

Back to Top


Moves the content of the RAM location pointed by W10 into register W5.

Moves the content of the RAM location pointed by W10 into register W5.

Back to Top


Updates the W8 register so that it is now pointing to the following RAM location.

Back to Top


Updates the W10 register so that it is now pointing to the following RAM location.

Updates the W10 register so that it is now pointing to the following RAM location.

Back to Top


The W4 and W5 registers' content are multiplied and the result is added to the 40-bit accumulator B.

The W4 and W5 registers' content are multiplied and the result is added to the 40-bit accumulator B.

 

Back to Top


Moves the content of the RAM location pointed to by W8 into register W4.

Back to Top


Moves the content of the RAM location pointed to by W10 into register W5.

Moves the content of the RAM location pointed to by W10 into register W5.

Back to Top


Updates the W10 register, so that it is now pointing to the previous RAM location.

Updates the W10 register, so that it is now pointing to the previous RAM location.

Back to Top

Store Accumulator Instruction

The accompanying diagram shows the operation of the store accumulator instruction, sac.r.

shows the operation of the store accumulator instruction, sac.r

We store the high part (from 16th to 31st bit) of the accumulator into the W0 register. Before saving it, the value is shifted 8-bit positions to the left and the resultant value is rounded. See the accompanying diagram.

Before saving it, the value is shifted 8-bit positions to the left and the resultant value is rounded.

Back to Top


Copy the content of register W5 [that is e(n-1)] into the last location of the error buffer.

Copy the content of register W5 [that is e(n-1)] into the last location of the error buffer.

Back to Top


The content of the RAM location pointed to by [W10 -4] is then copied into the W5 register.

The content of the RAM location pointed to by [W10 -4] is then copied into the W5 register.

Back to Top


The pointer W10 is decremented, to point to the previous location of the error buffer

The pointer W10 is decremented, to point to the previous location of the error buffer

Back to Top


The content of register W5 (that is the current error ethumb_down) is stored in the RAM location pointed to by W10. The purpose of the operations performed in the last few diagrams is to make room for the next value of the error that will arrive in the next sampling period.

The content of register W5 (that is the current error e(n)) is stored into the RAM location pointed to by W10

Back to Top

Example

The number of cycles required to perform the PID operations as indicated in the previous diagrams can be explained using the following example:

If the dsPIC DSC is running at 40 MIPS, each machine cycle equals 1/40 MHz = 25 ns. As a consequence, the total time required to perform the described task is 13 x 25 ns = 325 ns.

Back to Top