欢迎来到天天文库
浏览记录
ID:57891959
大小:34.50 KB
页数:6页
时间:2020-09-02
《PID控制电机程序.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、AVR单片机PID控制电机程序2011-04-1020:53#include#include#include#includesfrwICR1=0x26; //补充定义16位寄存器ICR1地址为0x26(mega16.h中未定义) unsignedinticp_v1;unsignedinticp_v2;unsignedc
2、haricp_n;unsignedcharmax_icp; //量程定义字biticp_ok;bittime_4ms_ok;bitfreq_ok;bitbegin_m; //定时器1溢出,重新测量标志字bitfull_ok; //定时器1溢出longfv;structPID{ unsignedintSet
3、Point; //设定目标DesiredValue unsignedintProportion; //比例常数ProportionalConst unsignedintIntegral; //积分常数IntegralConst unsignedintDerivative; //微分常数DerivativeConst unsignedintLastE
4、rror; //Error[-1] unsignedintPrevError; //Error[-2] unsignedintSumError; //SumsofErrors }; structPIDspid; //PIDControlStructurelongrout;
5、 //PIDResponse(Output) longrin; //PIDFeedback(Input)voidPID_Init(structPID*pp) { memset(pp,0,sizeof(structPID)); //全部初始化为0}unsignedintPID_Calc(structPID*pp,unsignedintNextPoint) { unsignedintdError,Error; E
6、rror=pp->SetPoint-NextPoint; //偏差 pp->SumError+=Error; //积分 dError=pp->LastError-pp->PrevError; //当前微分 pp->PrevError=pp->LastError; pp->LastError=Error; return(pp->Proportion*Error
7、 //比例项 +pp->Integral*pp->SumError //积分项 +pp->Derivative*dError); //微分项 } //Timer2比较匹配中断服务,4ms定时interrupt[TIM2_COMP]voidtimer2_comp_isr(void) { #asm("sei") //开放全局中断,允许中断嵌套 time_4ms_ok=1;}//Time
8、r1溢出中断服务interrupt[TIM1_OVF]voidtimer1_ovf_isr(void){ full_ok=1;}//Timer1输入捕捉中断服务interru
此文档下载收益归作者所有