- Main Page
- Data Structures
- Files
- Related Pages

- File List
- Globals

# main.c File Reference

## Detailed Description

Example of use of general PID implementation for AVR.

Example of how to setup and use the general PID implementation in pid.c.

- File: main.c
- Compiler: IAR EWAAVR 4.11A
- Supported devices: All AVR devices can be used.
- AppNote: AVR221 - Discrete PID controller

Author:
  Atmel Corporation: http://www.atmel.com
  Support email: avr@atmel.com

$Name$

Revision
  456

$RCSfile$

Date
  2006-02-16 12:46:13 +0100 (to, 16 feb 2006)

Definition in file main.c.

`#include <inavr.h>`
`#include <ioavr.h>`
`#include "stdint.h"`
`#include "pid.h"`

Include dependency graph for main.c:

Go to the source code of this file.

## Data Structures
struct | GLOBAL_FLAGS
| Flags for status information. More...
## Defines
#define | K_D 0.00
#define | K_I 0.00
#define | K_P 1.00
#define | TIME_INTERVAL 157
## Functions
int16_t | Get_Measurement (void)
| Read system process value.
int16_t | Get_Reference (void)
| Read reference value.
void | Init (void)
| Init of PID controller demo.
void | main (void)
| Demo of PID controller.
void | Set_Input (int16_t inputValue)
| Set control input to system.
__interrupt void | TIMER0_OVF_ISR (void)
| Timer interrupt to control the sampling interval.
## Variables
GLOBAL_FLAGS | gFlags
| Flags for status information.
PID_DATA | pidData
| Parameters for regulator.

## Define Documentation

#define K_D 0.00

Todo:

Definition at line 37 of file main.c.

Referenced by Init().

#define K_I 0.00

Todo:

Definition at line 35 of file main.c.

Referenced by Init().

#define K_P 1.00

The K_P, K_I and K_D values (P, I and D gains) need to be modified to adapt to the application at hand

Todo:

Definition at line 33 of file main.c.

Referenced by Init().

#define TIME_INTERVAL 157

Specify the desired PID sample time interval With a 8-bit counter (255 cylces to overflow), the time interval value is calculated as follows: TIME_INTERVAL = ( desired interval [sec] ) * ( frequency [Hz] ) / 255

Todo:

Definition at line 57 of file main.c.

Referenced by TIMER0_OVF_ISR().

## Function Documentation

int16_t Get_Measurement | ( | void |  | ) |

Read system process value.

This function must return the measured data

Definition at line 99 of file main.c.

Referenced by main().

    00100 {
    00101   return 4;
    00102 }

int16_t Get_Reference | ( | void |  | ) |

Read reference value.

This function must return the reference value. May be constant or varying

Definition at line 90 of file main.c.

Referenced by main().

    00091 {
    00092   return 8;
    00093 }

void Init | ( | void |  | ) |

Init of PID controller demo.

Definition at line 75 of file main.c.

References K_D, K_I, K_P, pid_Init(), pidData, and SCALING_FACTOR.

Referenced by main().

    00076 {
    00077   pid_Init(K_P * SCALING_FACTOR, K_I * SCALING_FACTOR , K_D * SCALING_FACTOR , &pidData);
    00078
    00079   // Set up timer, enable timer/counte 0 overflow interrupt
    00080   TCCR0A = (1<<CS00);
    00081   TIMSK0 = (1<<TOIE0);
    00082   TCNT0 = 0;
    00083 }

Here is the call graph for this function:

void main | ( | void |  | ) |

Demo of PID controller.

Definition at line 117 of file main.c.

References FALSE, Get_Measurement(), Get_Reference(), gFlags, Init(), pid_Controller(), pidData, GLOBAL_FLAGS::pidTimer, and Set_Input().

    00118 {
    00119   int16_t referenceValue, measurementValue, inputValue;
    00120   Init();
    00121   __enable_interrupt();
    00122
    00123   while(1){
    00124
    00125     // Run PID calculations once every PID timer timeout
    00126     if(gFlags.pidTimer)
    00127     {
    00128       referenceValue = Get_Reference();
    00129       measurementValue = Get_Measurement();
    00130
    00131       inputValue = pid_Controller(referenceValue, measurementValue, &pidData);
    00132
    00133       Set_Input(inputValue);
    00134
    00135       gFlags.pidTimer = FALSE;
    00136     }
    00137   }
    00138 }

Here is the call graph for this function:

void Set_Input | ( | int16_t | inputValue | ) |

Set control input to system.

Set the output from the controller as input to system.

Definition at line 109 of file main.c.

Referenced by main().

    00110 {
    00111   ;
    00112 }

__interrupt void TIMER0_OVF_ISR | ( | void |  | ) |

Timer interrupt to control the sampling interval.

Definition at line 62 of file main.c.

References gFlags, GLOBAL_FLAGS::pidTimer, TIME_INTERVAL, and TRUE.

    00063 {
    00064   static uint16_t i = 0;
    00065   if(i < TIME_INTERVAL)
    00066     i++;
    00067   else{
    00068     gFlags.pidTimer = TRUE;
    00069     i = 0;
    00070   }
    00071 }

## Variable Documentation

struct GLOBAL_FLAGS gFlags

Flags for status information.

Referenced by main(), and TIMER0_OVF_ISR().

struct PID_DATA pidData

Parameters for regulator.

Definition at line 48 of file main.c.

Referenced by Init(), and main().