第 7 章 单片机常用算法设计
DESCRIPTION
第 7 章 单片机常用算法设计. 7.1 单片机滤波算法的设计 7.2 信号处理的 FFT 变换 7.3 SPWM 正弦逆变算法的设计 7.4 PID 控制算法 7.5 51 单片机 PID 算法程序 7.6 模糊控制算法. 7.1 单片机滤波算法的设计. 电路的滤波分为 模拟滤波 与 数字滤波 。其中数字滤波器具有 精度高、高可靠性和高稳定性 的特点 , 因此被广泛应用。用数字滤波算法克服随机误差主要有如下 优点 : 数字滤波由软件程序实现 , 不需要硬件 , 因此 不存在阻抗匹配的问题 ; - PowerPoint PPT PresentationTRANSCRIPT
-
7 7.1 7.2 FFT7.3 SPWM7.4 PID7.5 51PID7.6
-
7.1 ,: , , ,, ,
-
A. , n n - 1y(n) y(n 1) , ,y(n) , ,y(n) ,y(n 1) y(n) ,
-
( A value ,new_value )#define A 10char value;char filter(){ char new_value; new_value = get_ad(); if ( ( new_value - value > A ) || ( value - new_value > A )) return value; return new_value;}
-
B. , n( n ) , ,
- #define N 11char filter(){ char value_buf[N] count,i,j,temp; for ( count=0;count
-
C. , N , Y , Y
- : . , , N , N ; . ,#define N 12char filter(){int sum = 0count; for ( count=0;count
-
D. , N , , , , N , N , ,
- #define N 12char value_buf[N]i=0;char filter(){ char count; int sum=0;value_buf[i++] = get_ad();if ( i == N ) i = 0;for ( count=0;count
-
E.,.a.1/2#define a 50char value;char filter(){ char new_value; new_value = get_ad(); return (100-a)*value + a*new_value;}
-
7.2 FFT(Fast Fourier TransfonnFFT) (Discrete Fourier TransformDFT)
-
FFTWNNN=2k,kN/2N/2DFTN/2^2NN/2DFTNDFTN+2N/22=N+N^2/2
- #include #include #include typedef struct{ double r; double i; }my_complex;//a2 #define NOT2POW(a) (((a)-1)&(a)||(a)
- if(NOT2POW(len)) return NULL;// for(;!(t&1);t>>=1) ex++; //len2ex y=(my_complex*)malloc(len*sizeof(my_complex)); if(!y) return NULL; //- for(i=0;i0){ j=1; } if(j>=i){ y[i]=x[j]; y[j]=x[i]; } } //y for(i=0;i
- y[j+k].r=tr+yr; y[j+k].i=ti+yi; y[j+k+t].r=tr-yr; y[j+k+t].i=ti-yi;}}} return y;} // int main() {int i,DATA_LEN; my_complex *x,*y; printf("FFT\n:"); scanf("%d",&DATA_LEN); x=(my_complex*)malloc(DATA_LEN*sizeof(my_complex)); for(i=0;i
- printf("...\n\t\t\n"); for(i=0;i
-
7.3 SPWMPWMPulse Width ModulationSPWMPWMSPWM,
-
SPWMA.,,,PWM,,,,
- SPWMvoid CalcSpwmWithArea(float32 a/**/,Uint16 w_Hz/**/,Uint32 z_Hz/**/){ //Uint16 tmp_PR; //T1 volatile Uint16 i,n,*p; float32 m,n1,n2; m = z_Hz/w_Hz ; // g_SPWM_Table.SpwmSize =(Uint16)m; //tmp_PR = g_T1_Clk /(2*z_Hz); // p=g_SPWM_Table.p_HeadTable; // n=m; m/=2; // n1=(float32)g_T1_Clk/(8.0*m*w_Hz); // n2=(float32)g_T2_Clk/(8.0*PI*w_Hz)*a; for(i=0;i
-
B., ,PWM.,,SPWM,
-
C.,,SPWM,,
-
D.,,SPWM,,,
- SPWMvoid CalcSpwmWithSym(float32 a/**/,float32 w_Hz/**/,float32 z_Hz/**/){Uint16 tmp_PR; //T1 volatile Uint16 i,n,*p;float32 m; m = z_Hz/w_Hz ; // g_SPWM_Table.SpwmSize =(Uint16)m; tmp_PR = g_T1_Clk /(2*z_Hz); // p=g_SPWM_Table.p_HeadTable; // for(i=0;i
- SPWMvoid CalcSpwmWithImSym(float32 a/**/,Uint16 w_Hz/**/,Uint32 z_Hz/**/){Uint16 tmp_PR; //T1 volatile Uint16 i,n,*p; float32 m; m = z_Hz/w_Hz ; // g_SPWM_Table.SpwmSize =(Uint16)m;tmp_PR = g_T1_Clk /(2*z_Hz); // p=g_SPWM_Table.p_HeadTable; // for(i=0;i
-
E.,,,PWM5,7
-
7.4 PIDPIDPIDPID;PID;PIDPIPD
-
PIDPID
-
e(t)TI()PID
-
PIDr(t)c(t)(P)(I)(D)PID PID
-
PIDPID
-
PID
-
P PI PD PID
-
PID
-
PIDPID
-
PID
-
/
-
7.5 51PID PIDPID
-
::u(n)k KP Ki Kd
-
e(k)(k=0,1,n) u(n)u(n)
-
#include #include 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;
-
PIDdouble 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)); }
-
double sensor (void) { return 100.0; } void actuator(double rDelta) {} void main(void) { PID sPID; double rOut; double rIn; PIDInit ( &sPID ); sPID.Proportion = 0.5 sPID.Derivative = 0.0; sPID.SetPoint = 100.0; for (;;) { rIn = sensor (); rOut = PIDCalc ( &sPID,rIn ); actuator ( rOut ); }}
-
PID
-
PID
-
typedef struct PID { int SetPoint; //long SumError; // double Proportion; //double Integral; //double Derivative; //int LastError; //Error[-1] int PrevError; //Error[-2] } PID;static PID sPID; static PID *sptr = &sPID;
-
/*PID*/ void IncPIDInit(void) { sptr->SumError = 0; sptr->LastError = 0; //Error[-1] sptr->PrevError = 0; //Error[-2] sptr->Proportion = 0; //sptr->Integral = 0; //sptr->Derivative = 0; //sptr->SetPoint = 0; }
-
/*PID*/ int IncPIDCalc(int NextPoint) { register int iError, iIncpid; // iError = sptr->SetPoint - NextPoint; // iIncpid = sptr->Proportion * iError //E[k] - sptr->Integral * sptr->LastError //E[k1] + sptr->Derivative * sptr->PrevError; //E[k2] sptr->PrevError = sptr->LastError; sptr->LastError = iError; // return(iIncpid); }
-
7.6
-
void main() { int a=0,e,ec; /*int nowpoint,p1,p2=1; FILE *in,*out; in=fopen("in.txt","r"); out=fopen("out.txt","w");*/ //while(!feof(in)) while(1) { //fscanf(in,"%d",&nowpoint); //p1=nowpoint; //e=0-nowpoint; //ec= p1-p2; printf("e:"); scanf("%d",&e); printf("ec:"); scanf("%d",&ec); a=Fuzzy(e,ec); //fprintf(out,"%d",a); //printf("%d:",p1); printf("e:%dec:%d",e,ec); printf("a: %d \n",a); //p2=p1; } //fclose(in); //fclose(out); }