DSP无刷直流电动机驱动控制程序_第1页
DSP无刷直流电动机驱动控制程序_第2页
DSP无刷直流电动机驱动控制程序_第3页
DSP无刷直流电动机驱动控制程序_第4页
DSP无刷直流电动机驱动控制程序_第5页
已阅读5页,还剩31页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、2.4 无刷直流电动机驱动控制程序/#/ 无刷电机控制源程序/TMS320F2812/#/=/ 头文件调用/=#include "DSP28_Device.h"#include "math.h"#include "float.h"/=/ 常量附值/=#define Idc_max 3000/电流给定最大值#define Idc_min 0/电流给定最小值/=/ 标志位/=char Iab_Data=0;struct Flag_Bits / bits descriptionUint16 Send:1;/ 0串口发数Uint16 Test

2、:1;/ 1串口测试Uint16 Vflag:1;/ 2转速更新Uint16 SendEnd:1;/ 3串口发送结束Uint16 Sign1:1;/ 4上一次给定转向标志Uint16 Sign2:1;/ 5本次给定转向标志Uint16 Openint:1;/ 6启动标志Uint16 Adfrist:1;/ 7保留Uint16 Spdoff:1;/ 8保留Uint16 Zero:1;/ 9转速过零标志Uint16 Mode:3;/ 10-12保留Uint16 Dshow:1;/ 13保留Uint16 Sign:1;/ 14当前转向标志;union Flag_Reg Uint16 all;stru

3、ct Flag_Bits bit;FlagRegs;/= / 全局变量/= / 串口通信变量unsigned int Sci_Rx8=0,0,0,0,0,0,0,0; /unsigned int Sci_Tx8=0,0,0,0,0,0,0,0; /char T_pointer=0;/char R_pointer=0;/char T_length=1;/char R_length=0;/char a2=0;/ 控制参数unsigned int spd_kp=0;/unsigned int spd_ki=0;/unsigned int id_kp=0,id_ki=0; /signed int sp

4、d_given=0;/signed int spd_given1=0;/signed int spd_given2=0;/ 控制变量unsigned int cap1=0;/unsigned int cap2=0;/接收数据数组 发送数据数组 发送数据数组指针 接收数据数组指针 发送数据长度 接收数据长度转速环P 参数转速环I 参数电流PI 参数转速给定转速给定转速给定前次换向标志位本次换向标志位定时器 1 下溢中断次数转速给定unsigned int intruptcount=0; / unsigned int speed_given = 1000;/signed int Idc_given

5、2=0;/算得本次电流给定值signed int speed_e1=0;/前次转速误差signed int speed_e2=0;/本次转速误差signed int Ia_e1=0;/前次电流误差signed int Ia_e2=0;char t2first=0;/本次电流误差signed int COMP2=0;/ 转速反馈/装比较寄存器值char Spd_Data=0;/滤波计数int speed_counter=0;/速度环计数器unsigned int cap_a;/本次捕获单元数据unsigned int cap_b;/上次捕获单元数据long cap_c;/捕获单元数据差signe

6、d int BLDC_SPD10=0,0,0,0,0,0,0,0,0,0;/转速滤波signed int spd_fd_q1=0;/转速反馈临时变量unsigned int spd_fd_q0=0;/转速检测值char cc=0;/ 电流反馈unsigned int ia6=0,0,0,0,0,0;/Aunsigned int ib6=0,0,0,0,0,0;/Bsigned int ia_fd=0,ib_fd=0;/signed int Temp_filter=0;/unsigned int t1per=0;/ 临时变量signed long long1_tmp=0;signed long

7、long2_tmp=0;signed int u16_tmp1=0;signed int u16_tmp2=0;相电流反馈值相电流反馈值电流反馈临时变量周期寄存器数值/=/ 子程序声明/=interrupt void t1uf_int(void);interrupt void cap_int(void);void Ad(void);void speed(void);/void Ia_PI(void);/void speed_PI(void);/void bldc(void);void Check_Rxdata(void);void Sci_Send(signed int sci_delay);

8、char *Fen_Jie(signed int Send_Temp);计算速度电流环调节Ia速度环调节unsigned int Sci_Rx_check(unsigned int i_Rx,unsigned int *p_Rx);signed int DIV_CAL(long signed int dividend,signed int divisor);unsigned int U_DIV_CAL(long unsigned int udividend,unsigned int udivisor);/= / 主程序开始/= void main(void)/ 控制寄存器初始设置InitSys

9、Ctrl();/DINT;/IER = 0x0000;IFR = 0x0000;InitPieCtrl();/InitPieVectTable(); /InitGpio();/InitEv();/InitAdc_Eva(); /InitData();初始化系统 关全局中断初始化 PIE 中断初始化 PIE 中断矢量表初始化 Gpio 输入输出口初始化 Eva 的 T 和 T2初始化 ADEALLOW;/ This is needed to write to EALLOW protectedregistersPieVectTable.T1UFINT=&t1uf_int;/T1下溢中断地址

10、PieVectTable.CAPINT4=&cap_int;PieVectTable.CAPINT5=&cap_int;PieVectTable.CAPINT6=&cap_int;EDIS;/ This is needed to disable write to EALLOWprotected registers下溢中断使能PieCtrl.PIEIER2.bit.INTx6 = 1; /T1PieCtrl.PIEIER5.bit.INTx5 = 1PieCtrl.PIEIER5.bit.INTx6 = 1PieCtrl.PIEIER5.bit.INTx7 = 1IER

11、|= M_INT2;/EnableCPUInterrupt2IER |= M_INT4;/EnableCPUInterrupt4IER |= M_INT5;/EnableCPUInterrupt5EvbRegs.EVBIMRC.bit.CAP4INT=1;EvbRegs.EVBIMRC.bit.CAP5INT=1;EvbRegs.EVBIMRC.bit.CAP6INT=1;EvbRegs.CAPCONB.all=0x36FF;NOP;NOP;NOP;NOP;EvbRegs.CAPCONB.all=0xB6FF;EINT;/使能全局中断INTMERTM;/ Enable Global realt

12、ime interrupt DBG/ 等待中断(中断之外的时间内进行LCD 的发送和接收)for(;)SCI_CTL();/=/ 串口控制/=/*/ 接收数据检测void Check_Rxdata()switch(Sci_Rx0)转速给定和转速在线更新case 'm':/case 'v':FlagRegs.bit.Vflag=1;if(Sci_Rx1='-') FlagRegs.bit.Sign1=0; /转速为负值spd_given1 = Sci_Rx_check(4,Sci_Rx); else FlagRegs.bit.Sign1=1; /转

13、速为正值spd_given1 = Sci_Rx_check(3,Sci_Rx); if(Sci_Rx0='m')FlagRegs.bit.Sign2=FlagRegs.bit.Sign1;FlagRegs.bit.Sign=FlagRegs.bit.Sign1;break;case 's':/启动Sci_Tx0='a'Sci_Tx1='+'Sci_Tx2='0'Sci_Tx3='0'Sci_Tx4='0'Sci_Tx5='0'Sci_Tx6='0'Sc

14、i_Tx7='z'spd_given=spd_given1;Protect_Data();Motor_Start();FlagRegs.bit.Send=1;T_length=8;T_pointer=0;break;case 't':/Motor_Stop();break;default: break;停止/*/ 串口接收/ 发送判断/*SCI_CTL() if(SciaTx_Ready() = 1) && (FlagRegs.bit.Send = 1)/ 能发送 if(FlagRegs.bit.Test=0)/SCISciaRegs.SCITX

15、BUF = Sci_RxT_pointer;T_pointer+;/if(T_pointer=R_length) FlagRegs.bit.Test=1; FlagRegs.bit.Send=0; T_pointer=0; else/SCISciaRegs.SCITXBUF = Sci_TxT_pointer;T_pointer+;/if(T_pointer=T_length) FlagRegs.bit.Send = 0; T_pointer=0; if(T_pointer>=10) T_pointer=0; FlagRegs.bit.Send= 0; 测通状态发送数据准备好并且软件使发

16、送缓冲器数组指针+1非测通状态发送缓冲器数组指针+1R_pointer=0;接收数据准备好#if !SCIA_INT if(SciaRx_Ready() = 1)/FlagRegs.bit.SendEnd=0;FlagRegs.bit.Send= 0;Sci_RxR_pointer = SciaRegs.SCIRXBUF.all;R_pointer+;if(Sci_RxR_pointer-1='z')R_length=R_pointer-1;FlagRegs.bit.Send= 1;FlagRegs.bit.SendEnd=1;if(FlagRegs.bit.Test=1)Ch

17、eck_Rxdata();R_pointer = 0;if(R_pointer= 10)R_pointer = 0;#endif/* / 接收数据格式调整/* unsigned int Sci_Rx_check(unsigned int i_Rx,unsigned int *p_Rx) unsigned long data_Rx;unsigned int *p_tmp_Rx=p_Rx+i_Rx;data_Rx=1000*(*p_tmp_Rx)+100*(*(p_tmp_Rx+1)+10*(*(p_tmp_Rx+2)+(*(p_tmp_Rx+ 3)-53328;return(data_Rx);/

18、* / 发送数据格式调整char *Fen_Jie(signed int Send_Temp)unsigned int Temp;char s1,s2,s3,s4;char String_Tmp6=0,0,0,0,0,'0'if(Send_Temp< 0)String_Tmp0='-'Send_Temp=-Send_Temp;elseString_Tmp0='+'s1=(long)Send_Temp*2097)>>21;if(s1>=10) s1=9;Temp=Send_Temp-s1*1000;s2=(long)Temp

19、*5253)>>19;if(s2>=10) s2=9;Temp=Temp-s2*100;s3=(long)Temp*3277)>>15;if(s3>=10) s3=9;s4=Temp-s3*10;if(s4>=10) s4=9;String_Tmp1=s1+48;/千位String_Tmp2=s2+48;/百位String_Tmp3=s3+48;/十位/个位String_Tmp4=s4+48;return(String_Tmp);/* / 数据发送/* void Sci_Send(signed int sci_delay)/char *p_send=0

20、;a2+;if(a2>=sci_delay)&&(FlagRegs.bit.Send=0)&&(FlagRegs.bit.SendEnd=1) p_send=Fen_Jie(spd_fd_q0);Sci_Tx0='a'Sci_Tx1=*p_send;Sci_Tx2='0'Sci_Tx3=*(p_send+1);Sci_Tx4=*(p_send+2);Sci_Tx5=*(p_send+3);Sci_Tx6=*(p_send+4);Sci_Tx7='z'FlagRegs.bit.Send=1;a2=0;/=/ 电

21、机状态控制/= /*/ 数据处理/*Protect_Data()转速环P 参数转速环P 参数转速环P 参数转速环P 参数矢量控制开关频率5K 固定连续增减计数if(spd_kp<=0)spd_kp=1;/else if(spd_kp>30000) spd_kp=30000;if(spd_ki<=0)spd_ki=1;/else if(spd_ki>30000) spd_ki=30000;if(id_kp<=0)id_kp=1;/else if(id_kp>30000) id_kp=30000;if(id_ki<=0)id_ki=1;/else if(i

22、d_ki>30000) id_ki=30000;EvaRegs.T1PR =7500;/t1per=7500;EvaRegs.T1CON.all = 0X080C;/InitAdc_Eva();/*/ 电机停止/*Motor_Stop()EvaRegs.CMPR1=0;EvaRegs.CMPR2=0;EvaRegs.CMPR3=0;EvaRegs.ACTR.all=0X0FFF;InitData();FlagRegs.bit.Openint=0;EvaRegs.T1CON.bit.TENABLE=0;EvbRegs.T3CON.bit.TENABLE=0; /* / 电机启动/*Moto

23、r_Start() EvaRegs.ACTR.all=0X0999;FlagRegs.bit.Openint=1;t1per=7500;EvaRegs.T1PR =7500;/EvaRegs.T1CON.all = 0X080C;/EvbRegs.T3PR =0xffff;/EvbRegs.T3CON.all = 0X170C;/EvbRegs.EVBIMRA.all=0X0000; / T3EvbRegs.EVBIFRA.all=0X0FFFF;/GpioMuxRegs.GPBMUX.bit.CAP4Q1_GPIOB8=0;GpioMuxRegs.GPBMUX.bit.CAP5Q2_GPIO

24、B9=0;GpioMuxRegs.GPBMUX.bit.CAP6QI2_GPIOB10=0;cap2 = GpioDataRegs.GPBDAT.all & 0x0700; / cap2 = cap2>>8;cap2 = cap2&0x0007;if(FlagRegs.bit.Sign=1) cap2=7-cap2;else cap2=cap2;GpioMuxRegs.GPBMUX.bit.CAP4Q1_GPIOB8=1;GpioMuxRegs.GPBMUX.bit.CAP5Q2_GPIOB9=1;GpioMuxRegs.GPBMUX.bit.CAP6QI2_GPI

25、OB10=1;周期寄存器5khz连续增减计数周期寄存器连续增减计数下溢使能清中断标志位/将 cap456 设置为 io 口记录 cap4-6 初始状态/将 cap456 设置为 CAP 口EvaRegs.T1CON.bit.TENABLE=1;FlagRegs.bit.Vflag=1;/=/ 直流无刷电动机驱动双闭环主程序/=/*/ 驱动主程序/*void bldc()intruptcount+;speed();switch(int)cap2)case 4:Ad();/if(ia_fd<0) ia_fd=0; /speed_PI();/Ia_PI();/EvaRegs.ACTR.all

26、= 0x0F3E; /ia+,ib-EvaRegs.CMPR1=COMP2; break;case 5:Ad();/if(ia_fd<0) ia_fd=0; / speed_PI();/Ia_PI();/EvaRegs.ACTR.all = 0x03FE; /ia+,ic-EvaRegs.CMPR1=COMP2; break;case 1:Ad();if(ib_fd<0) ib_fd=0;ia_fd=ib_fd;speed_PI();Ia_PI();EvaRegs.ACTR.all = 0x03EF; /ib+,ic-EvaRegs.CMPR2=COMP2; break;case

27、3:Ad();if(ib_fd<0) ib_fd=0;ia_fd=ib_fd;speed_PI();Ia_PI();EvaRegs.ACTR.all = 0x0FE3; /ib+,ia-检测电流保护转速 PI 调节电流 PI 调节检测电流保护转速 PI 调节电流 PI 调节EvaRegs.CMPR2=COMP2;break;case 2:Ad();ia_fd=-ia_fd;if(ia_fd<0) ia_fd=0;speed_PI();Ia_PI();EvaRegs.ACTR.all = 0x0EF3; /ic+,ia- EvaRegs.CMPR3=COMP2;break;case

28、6:Ad();ib_fd=-ib_fd;if(ib_fd<0) ib_fd=0;ia_fd=ib_fd;speed_PI();Ia_PI();EvaRegs.ACTR.all = 0x0E3F; /ic+,ib- EvaRegs.CMPR3=COMP2;break;default: break;/*/ 电流反馈/*void Ad()char delay_zy=0;相电流检测值清零ia_fd=0,ib_fd=0;/ABfor(Iab_Data=0;Iab_Data<=5;Iab_Data+) iaIab_Data=AdcRegs.RESULT0>>4;ibIab_Data

29、=AdcRegs.RESULT1>>4;if(Iab_Data=0)for(delay_zy=0;delay_zy<=10;delay_zy+);else if(Iab_Data=1)if(ia0>ia1)Temp_filter= ia1;ia1 = ia0;ia0 = Temp_filter;if(ib0>ib1)Temp_filter= ib1;ib1 = ib0;ib0 = Temp_filter;elseif(iaIab_Data>ia1)Temp_filter= ia1;ia1 = iaIab_Data;iaIab_Data = Temp_filt

30、er;else if(iaIab_Data<ia0)Temp_filter= ia0;ia0 = iaIab_Data;iaIab_Data = Temp_filter;if(ibIab_Data<ib0)Temp_filter= ib0;ib0 = ibIab_Data;ibIab_Data = Temp_filter;else if(ibIab_Data>ib1)Temp_filter= ib1;ib1 = ibIab_Data;ibIab_Data = Temp_filter;ia_fd=(ia2+ia3+ia4+ia5)>>2;ib_fd=(ib2+ib3

31、+ib4+ib5)>>2;ia_fd=ia_fd-2133;ib_fd=ib_fd-2150;/*/ 转速反馈计算/*void speed(void)if(cap2 != cap1)&&(t2first=1)if(cap_c!= 0)long2_tmp = 1953125/cap_c;spd_fd_q1 = long2_tmp;intruptcount=0;switch(cap2)case 5:if(cap1=4) spd_fd_q1=-spd_fd_q1;break;case 4:if(cap1=6) spd_fd_q1=-spd_fd_q1; break;cas

32、e 6:if(cap1=2) spd_fd_q1=-spd_fd_q1; break;case 2:if(cap1=3) spd_fd_q1=-spd_fd_q1;break;if(cap1=1) spd_fd_q1=-spd_fd_q1;break;case 1:if(cap1=5) spd_fd_q1=-spd_fd_q1;break;default:break;BLDC_SPDSpd_Data=spd_fd_q1;if(Spd_Data=1)if(BLDC_SPD0>BLDC_SPD1)Temp_filter = BLDC_SPD1;BLDC_SPD1 = BLDC_SPD0;BL

33、DC_SPD0 = Temp_filter;elseif(BLDC_SPDSpd_Data>BLDC_SPD1)Temp_filter= BLDC_SPD1;BLDC_SPD1 = BLDC_SPDSpd_Data;BLDC_SPDSpd_Data = Temp_filter;else if(BLDC_SPDSpd_Data<BLDC_SPD0)Temp_filter= BLDC_SPD0;BLDC_SPD0 = BLDC_SPDSpd_Data;BLDC_SPDSpd_Data = Temp_filter;Spd_Data+;if(Spd_Data>=10)Spd_Data

34、=0;cc+;if(cc>=3)cc=0;spd_fd_q0=(BLDC_SPD2+BLDC_SPD3+BLDC_SPD4+BLDC_SPD5+BLDC_SPD6+BLDC SPD7+BLDC_SPD8+BLDC_SPD9)>>3;cap1=cap2;elsespd_fd_q0 = 0;elseif(intruptcount>=1500)intruptcount=0;spd_fd_q0=0;elsespd_fd_q0=spd_fd_q0;if(t2first=0) t2first=1;/*/ 转速调节器/*void speed_PI(void)unsigned int

35、abs_spd=0;signed int spd_ki_bldc=1;speed_counter+;if(speed_counter>=10)speed_counter=0;转速给定更新新转向和原有转向不同if(FlagRegs.bit.Vflag=1)/if(FlagRegs.bit.Sign1!=FlagRegs.bit.Sign2)/ FlagRegs.bit.Zero =1;/换转向标志位置位spd_given2=spd_given;spd_given=0;/缓存目标频率当前目标频率给定0转速给定更新标志位清零更新转向标志当前给定频率, 目标频率需要反给定新的目标频率换转向标志位

36、清零给定新的转向将本次误差用作下次使用FlagRegs.bit.Vflag =0;/FlagRegs.bit.Sign2=FlagRegs.bit.Sign1; /if(spd_fd_q0=0)&&(FlagRegs.bit.Zero=1) /向 ( 电机需要从正转到反转)spd_given=spd_given2;/FlagRegs.bit.Zero=0;/FlagRegs.bit.Sign=FlagRegs.bit.Sign1; /speed_e1 = speed_e2;/speed_e2 = spd_given - abs(spd_fd_q0);abs_spd=abs(sp

37、d_given)>>2;if(abs(speed_e2)<abs_spd)spd_ki_bldc=spd_ki;elseif(spd_given<100) spd_ki_bldc=12;/10*8else if(spd_given<150) spd_ki_bldc=10;else if(spd_given<200) spd_ki_bldc=8;else if(spd_given<250) spd_ki_bldc=6;else if(spd_given<300) spd_ki_bldc=5;else spd_ki_bldc=4;long1_tmp=

38、 (long)spd_kp * (speed_e2 - speed_e1);long2_tmp= (long)spd_ki_bldc * speed_e2;u16_tmp1=(long1_tmp+long2_tmp)>>10;Idc_given2=Idc_given2+u16_tmp1;if(Idc_given2 > Idc_max) /限幅Idc_given2 = Idc_max;else if(Idc_given2 < Idc_min)Idc_given2 = Idc_min;/*/ 电流调节器/*void Ia_PI(void)Ia_e1 = Ia_e2; /将本

39、次误差用作下次使用Ia_e2 = Idc_given2 - ia_fd;long1_tmp=(long)id_kp * (Ia_e2 - Ia_e1);long2_tmp=(long)id_ki * Ia_e2;u16_tmp1=(long1_tmp+long2_tmp)>>12;COMP2=COMP2+u16_tmp1;if(COMP2 < 0)COMP2 = 0;else if(COMP2 > (t1per-200)COMP2 = (t1per-200);/= /T1 下溢中断子程序/= interrupt void t1uf_int(void) IFR=0x000

40、0;/PieCtrl.PIEACK.all=0xffff;控制程序准备开始运行下溢中断标志位清零if(FlagRegs.bit.Openint=1) / bldc();GpioDataRegs.GPACLEAR.bit.GPIOA11=1;Sci_Send(400);EvaRegs.EVAIFRA.bit.T1UFINT=1; /T1 EINT;/=/ CAP4/5/6 中断子程序/=interrupt void cap_int(void)IFR=0x0000;PieCtrl.PIEACK.all=0xffff;GpioMuxRegs.GPBMUX.bit.CAP4Q1_GPIOB8=0; /

41、将 cap456 设置为 io 口GpioMuxRegs.GPBMUX.bit.CAP5Q2_GPIOB9=0;GpioMuxRegs.GPBMUX.bit.CAP6QI2_GPIOB10=0;cap2 = GpioDataRegs.GPBDAT.all & 0x0700; /记录 cap4-6 初始状态cap2 = cap2>>8;cap2 = cap2&0x0007;if(FlagRegs.bit.Sign=1) cap2=7-cap2;else cap2=cap2;if(EvbRegs.EVBIFRC.bit.CAP4INT=1)cap_a=EvbRegs.C

42、AP4FIFO;EvbRegs.EVBIFRC.bit.CAP4INT=1;else if(EvbRegs.EVBIFRC.bit.CAP5INT=1)cap_a=EvbRegs.CAP5FIFO;EvbRegs.EVBIFRC.bit.CAP5INT=1;else if(EvbRegs.EVBIFRC.bit.CAP6INT=1)cap_a=EvbRegs.CAP6FIFO;EvbRegs.EVBIFRC.bit.CAP6INT=1;cap_c=cap_a-cap_b;if(cap_c<0) cap_c=cap_c+0xffff;cap_b=cap_a;GpioMuxRegs.GPBM

43、UX.bit.CAP4Q1_GPIOB8=1; /将 cap456 设置为 CAP口GpioMuxRegs.GPBMUX.bit.CAP5Q2_GPIOB9=1;GpioMuxRegs.GPBMUX.bit.CAP6QI2_GPIOB10=1;EINT;InitData() char i;发送数据数组指针接收数据数组指针前次换向标志位本次换向标志位定时器 1 下溢中断次数算得本次电流给定值前次转速误差T_pointer=0;/R_pointer=0;/a2=0; for(i=0;i<8;i+) Sci_Rxi=0;Sci_Txi=0;cap1=0;/cap2=0;/intruptcount=0;/Idc_given2=0;/

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论