加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

[转载]利用PID算法控制输出波形(atmega16,AD反馈)

(2011-10-30 21:27:04)
标签:

转载

分类: 学术研究
主函数
http://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gif http://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gif
#include<iom16v.h>
#include<macros.h>
#include"1602.h"
#include "ad.h"
#include"pid.h"
#define uchar unsigned  char
#define uint unsigned int

void actuator(double rDelta) // Dummy Actuator Function

   uchar b[4];
   uint i,ad;
   DDRD|=0X30;
   TCCR1A=0X63;
   TCCR1B=0X13;//相位修正PWM,周期为对应快速PWM的2倍,64分频
   OCR1A=600;//决定输出的频率
   OCR1B=100+rDelta;
   while(PINC|~BIT(2))                     //AD显示
     {
      GotoXY(0,0);
      ad=ad2543work(0);
      b[0]=ad/1000;
      b[1]=ad/100;
      b[2]=ad/10;
      b[3]=ad;
     
      for(i=0;i<4;i++)
      {
       write_dat(b[i]+0x30);
       }
       delay(100);
     }
   
 

void main(void)
{ uchar b[4];
   uint i,ad;
 
PID sPID; // PID Control Structure
double rOut; // PID Response (Output)
double rIn; // PID Feedback (Input)
lcd1602_init();
 ad_init();
PIDInit ( &sPID ); // Initialize Structure
sPID.Proportion =0.00003; // Set PID Coefficients
sPID.Integral =0.00006;
sPID.Derivative =0.00001;
sPID.SetPoint =800; // Set PID Setpoint
 

   DDRD|=0X30;
   TCCR1A=0X63;
   TCCR1B=0X13;//相位修正PWM,周期为对应快速PWM的2倍,64分频
   OCR1A=600;//决定输出的频率
   OCR1B=100;
   while(1)
{
   while(PINC|~BIT(2))                     //AD显示
     {
      GotoXY(0,0);
      ad=ad2543work(0);
      b[0]=ad/1000;
      b[1]=ad/100;
      b[2]=ad/10;
      b[3]=ad;
     
      for(i=0;i<4;i++)
      {
       write_dat(b[i]+0x30);
       }
       delay(100);
    
  rIn=ad2543work(0);               // Mock Up of PID Processing
  rOut = PIDCalc ( &sPID,rIn ); // Perform PID Interation
  if(sPID.SetPoint-rIn>30)
  OCR1B+=rOut;
  if(rIn-sPID.SetPoint>30)
  OCR1B-=rOut;
  }
 }
}
http://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gif http://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gif
PID控制(PID.h)
#ifndef _PID_h //如果到目前为止还没有定义过“_feed_dog_h”这个宏
#define _PID_h //则定义“_feed_dog_h”这个宏
#include <string.h>
typedef struct PID {
double SetPoint;      // 设定目标Desired value
double Proportion;    // 比例常数Proportional Const
double Integral;      // 积分常数Integral Const
double Derivative;    // 微分常数Derivative Const
double LastError;     // Error[-1]

double PrevError;    // Error[-2]
double SumError;    // Sums of Errors
} PID;

double PIDCalc( PID *pp, double NextPoint )
{double dError, Error;

Error = pp->SetPoint - NextPoint;           // 偏差
pp->SumError += Error;                   // 积分
dError = Error - pp->LastError;             // 当前微分

pp->PrevError = pp->LastError;
pp->LastError = Error;

return (pp->Proportion * Error // 比例项
+ pp->Integral * pp->SumError // 积分项
+ pp->Derivative * dError // 微分项
);
}

void PIDInit (PID *pp)
{
memset ( pp,0,sizeof(PID));
}

#endif
http://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gif http://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gif
ad.h

#ifndef _ad_h //如果到目前为止还没有定义过“_feed_dog_h”这个宏
#define _ad_h //则定义“_feed_dog_h”这个宏

#define data_in_1 PORTC|=BIT(0);
#define data_in_0 PORTC&=~BIT(0);
#define data_out_1 PORTC|=BIT(1);
#define data_out_0 PORTC&=~BIT(1);
#define EOC_on     PORTC|=BIT(2);
#define EOC_off    PORTC&=~BIT(2);
#define CS_on      PORTC|=BIT(3);
#define CS_off     PORTC&=~BIT(3);
#define Clock_on   PORTC|=BIT(4);
#define Clock_off  PORTC&=~BIT(4);
const unsigned char ad_chunnel_select[]=
 { 0x08,0x18,0x28,0x38,0x48,0x58,0x68,0x78,0x88,0x98,0xA8};

void  ad_init(void)
{
 DDRC=0xf9;PORTC=0xf9;
 }
unsigned int ad2543work(unsigned char chunnel_select)
{ unsigned int din;
  unsigned char dout,i;
  din=0;
  dout=ad_chunnel_select[chunnel_select];
 
  { Clock_off;
    CS_off ;
    for(i=0;i<12;i++)
     { if(dout&0x80)  {data_in_1;}
       else           {data_in_0;}
       Clock_on;
       dout<<=1;
       din<<=1;
       if(PINC&0x02)
       {
         din|=0x0001;
        }
       Clock_off;
      }
    CS_on;
    return(din);
    }
 }
 #endif
http://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gif http://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gifhttp://www/uc/myshow/blog/misc/gif/E___6724EN00SIGG.gif
1602.h

#define uchar unsigned  char
#define uint unsigned int
#define RSon   PORTB|=BIT(0);
#define RSoff  PORTB&=~BIT(0);
#define RWon   PORTB|=BIT(1);
#define RWoff  PORTB&=~BIT(1);
#define Enon   PORTB|=BIT(2);
#define Enoff  PORTB&=~BIT(2);
void delay(uint ms)
{
        uint i,j;
    for(i=0;i<ms;i++)
       {
       for(j=0;j<1141;j++);
       }
}
void write_com(uchar com)//写指令
{
       RSoff;
       RWoff;
       PORTA=com;
       Enon;
       delay(1);
       Enoff;
}

void write_dat(uchar dat)//写数据
   
       RSon;
       RWoff;
       PORTA=dat;
       Enon;
       delay(1);
       Enoff;

}
void move(void)    //进行移动
      uchar i;
        for(i=0;i<16;i++)
        {
            write_com(0x1C);
            delay(10);
        }
}
void GotoXY(unsigned char x, unsigned char y)
{
    if(y==0)
        write_com(0x80|x);
    if(y==1)
        write_com(0x80|(x-0x40));
}
void Print(unsigned char *str)
{
    while(*str!='')
    {
        write_dat(*str);
        str++;
    }
}
void  lcd1602_init(void)
{
      DDRA=0XFF;
      DDRB=0xff;
      Enoff;
     
      write_com(0X38);
      delay(5);
      write_com(0X01);
      delay(5);
      write_com(0X0C);
      delay(5);
      write_com(0X06);
      delay(5);
}
http://s2/middle/69e984edgab787c94fa61&690

http://s1/middle/69e984edgab7880cde830&690



0

  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有