Implementation

The example code is written for "Atmel START". It can be downloaded from "BROWSE EXAMPLES" entry of Atmel START for both Atmel Studio 7 and IAR IDE.

Double click the downloaded .atzip file and the project will be imported to Atmel Studio 7.

To import the project in IAR, refer to "Atmel START in IAR" , select ‘Atmel Start Output in External Tools → IAR'.

Figure 1. Block Diagram of Demo Application

In the figure above a simplified block diagram of the demo application is shown.

The PID controller uses a struct to store its status and parameters. This struct is initialized in main, and only a pointer to it is passed to the Init_PID() and PID() functions.

The PID() function must be called for each time interval T. This is done by a timer, which sets the PID_timer flag when the time interval has passed. When the PID_timer flag is set the main routine reads the desired process value (setPoint) and system process value, calls PID(), and outputs the result to the control input.

To increase the accuracy the p_factor, i_factor, and d_factor are scaled with a factor 1:128. The result of the PID algorithm is later scaled back by dividing by 128. The value 128 is used to allow for optimizing in the compiler.

PFactor=128Kp

Furthermore the effect of the IFactor and DFactor will depend on the sample time T.

IFactor=128KpTTi

DFactor=128KpTdT