本帖最后由 张锋 于 2016-7-4 09:11 编辑
- #include "DSP28x_Project.h"
- #include "IQmathLib.h" //IQmath头文件
- #include "pid_reg3.h" //PID运算头文件
- #include "Example_posspeed.h" //速度检测头文件
- /*函数声明*/
- void InitPwm1(void);
- __interrupt void Epwm1Int_isr(void);
- __interrupt void CPU_Timer0_isr();
- void Adc_Config(void);
- #define EPWM1_DB 180 //死区时间 2us,180/90M
- PIDREG3 pid_i = PIDREG3_DEFAULTS; //电流环PI
- PIDREG3 pid_spd = PIDREG3_DEFAULTS; //速度环PI
- POSSPEED qep_posspeed=POSSPEED_DEFAULTS; //测速模块,初始化一些参数
- _iq spd = _IQ(0.0);
- _iq Ifb = _IQ(0.0); //电流反馈值
- _iq Iref; //电流给定值
- _iq Spdfb = _IQ(0.0); //速度反馈值
- _iq Spdref; //速度给定值
- _iq Duty = _IQ(0.0); //占空比
- Uint32 IsrCount = 0; //中断计数
- int main(void)
- {
- InitSysCtrl(); //初始化系统系统时钟,包括:PLL(90M), 看门狗时钟, 外设时钟的控制;
- DINT; //关总中断
- InitEPwm1Gpio(); //初始化PWM-Gpio
- InitEQep1Gpio(); //初始化EQEP-Gpio
- InitCpuTimers(); //初始化Timer,用于速度环
- InitPieCtrl(); //初始化PIE模块
- IER = 0X0000; //关闭CPU中断
- IFR = 0X0000; //清除全部的CPU中断标志
- InitPieVectTable(); //初始化中断向量表
- //中断入口选择
- EALLOW;
- PieVectTable.EPWM1_INT = &Epwm1Int_isr;
- PieVectTable.TINT0 = &CPU_Timer0_isr;
- EDIS;
- //关闭PWM时钟
- EALLOW;
- SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
- EDIS;
- InitPwm1(); //初始化Pwm1,电流环20k,50us
- InitAdc(); //初始化ADC,20k,由PWM触发
- ConfigCpuTimer(&CpuTimer0, 90, 2000); //速度环 2ms
- StopCpuTimer0(); //关闭Timer0
- //开启PWM时钟
- EALLOW;
- SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
- EDIS;
- IER |= M_INT1;
- IER |= M_INT3;
- PieCtrlRegs.PIEIER1.bit.INTx7 = 1; //使能PIE级TIMER0中断
- PieCtrlRegs.PIEIER3.bit.INTx1 = 1; //使能PIE级PWM1中断
- Adc_Config(); //配置ADC
- qep_posspeed.init(&qep_posspeed); //Eqep初始化
- //PID参数设定
- //初始化电流环PI参数
- pid_i.Kp = _IQ(0.3);
- pid_i.Ki = _IQ(0.0001);
- pid_i.Kd = _IQ(0.0);
- pid_i.Kc = _IQ(0);
- pid_i.OutMax = _IQ(0.499);
- pid_i.OutMin = _IQ(-0.499);
- Iref = _IQ(0.2);
- //初始化速度环PI参数
- pid_spd.Kp = _IQ(0.1);
- pid_spd.Ki = _IQ(0.001);
- pid_spd.Kd = _IQ(0.0);
- pid_spd.Kc = _IQ(0);
- pid_spd.OutMax = _IQ(0.99);
- pid_spd.OutMin = _IQ(-0.99);
- Spdref = _IQ(0.5);
- StartCpuTimer0(); //开启Timer0
- EINT; //使能总中断
- ERTM; //使能全局实时中断
- while (1)
- {
- ;
- }
- }
- //================================================
- //函 数 名: CPU_Timer0_isr
- //函数功能说明: 速度环的运算
- //调用函数: qep_posspeed.calc(); M法测速
- // PID_MACRO(); PID运算
- //访问的表: 无
- //输入参数: 无
- //返 回 值: 无
- //涉及的全局变量: CpuTimer0.InterruptCount TImer0中断计数
- // Iref 速度环输出传送给电流环给定,范围-1~+1,Q24
- //修改记录:
- //================================================
- __interrupt void CPU_Timer0_isr()
- {
- CpuTimer0.InterruptCount++; //中断计数
- /*速度环*/
- qep_posspeed.calc(&qep_posspeed);//M法测速,最终速度为 qep_posspeed.SpeedRpm_fr globalQ
- spd = qep_posspeed.Speed_fr; //速度,globalQ,标幺值
- pid_spd.Fdb = spd; //速度反馈
- pid_spd.Ref = Spdref; //速度给定
- PID_MACRO(pid_spd);
- Iref = pid_spd.Out;
- PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; //PIE中断回应
- }
- //================================================
- //函 数 名: Epwm1Int_isr
- //函数功能说明: 电流环运算
- //调用函数: PID_MACRO(); PID运算
- //访问的表: 无
- //输入参数: 无
- //返 回 值: 无
- //涉及的全局变量: IsrCount PWM1中断计数
- // Ifb 电流采样值,范围-1~1,Q24
- //修改记录:
- //================================================
- __interrupt void Epwm1Int_isr()
- {
- Uint32 temp;
- IsrCount++; //中断计数
- /*电流环*/
- temp = AdcResult.ADCRESULT1; //电流值
- // Ifb = ((AdcResult.ADCRESULT1<<12) - _IQ(0.5))<<1; //电流采样值为 (-0.5~0.5)*2 变为 -1~+1
- // Ifb = _IQdiv(_IQmpy(_IQ(3.3),_IQdiv(temp,4096)),_IQ(2));//
- Ifb = ((temp<<12) - _IQ(0.5))<<1; //电流采样值为 (-0.5~0.5)*2 变为 -1~+1
- pid_i.Fdb = Ifb;
- pid_i.Ref = Iref;
- PID_MACRO(pid_i);
- Duty = pid_i.Out;
- EPwm1Regs.CMPA.half.CMPA = (4500*(Duty+_IQ(0.5)))>>24;
- EPwm1Regs.ETCLR.bit.INT = 1; //清除中断标志
- PieCtrlRegs.PIEACK.all = PIEACK_GROUP3; //PIE中断回应
- }
- //================================================
- //函 数 名: InitPwm1
- //函数功能说明: PWM1模块初始化,设定周期为TBPRD=4500,对应20kHz,
- // 以及死区设置,死区时间EPWM1_DB=2us
- // PWM1中断设置,中断频率=PWM频率
- //调用函数: 无
- //访问的表: 无
- //输入参数: 无
- //返 回 值: 无
- //涉及的全局变量: 无
- //修改记录:
- //================================================
- void InitPwm1(void)
- {
- EPwm1Regs.TBPRD = 4500; //周期,频率20k = 90000/4500
- EPwm1Regs.TBPHS.half.TBPHS = 0x0000;
- EPwm1Regs.TBCTR = 0x0000;
- EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP; //增计数
- EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;
- EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1; //不分频,
- EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1;
- EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
- EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
- EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;
- EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;
- EPwm1Regs.CMPA.half.CMPA = 2250; //50%
- //动作设置
- EPwm1Regs.AQCTLA.bit.CAU = AQ_CLEAR; //复位
- EPwm1Regs.AQCTLA.bit.ZRO = AQ_SET; //置位
- EPwm1Regs.AQCTLB.bit.CAD = AQ_SET; //置位
- EPwm1Regs.AQCTLB.bit.CAU = AQ_CLEAR; //复位
- //死区设置
- EPwm1Regs.DBCTL.bit.OUT_MODE = DB_FULL_ENABLE;
- EPwm1Regs.DBCTL.bit.POLSEL = DB_ACTV_HIC;
- EPwm1Regs.DBCTL.bit.IN_MODE = DBA_ALL;
- EPwm1Regs.DBRED = EPWM1_DB;
- EPwm1Regs.DBFED = EPWM1_DB;
- //Pwm中断设置
- EPwm1Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO; //计数器 = 0 时触发中断
- EPwm1Regs.ETSEL.bit.INTEN = 1; //使能中断
- EPwm1Regs.ETPS.bit.INTPRD = ET_1ST; //每次都触发中断
- }
- //================================================
- //函 数 名: Adc_Config
- //函数功能说明: ADC模块设置
- // ADCSOC0 对应通道 ADCINA0
- // ADCSOC1 对应通道 ADCINA0
- // PWM1触发ADC,即ADC采样频率20k
- //调用函数: 无
- //访问的表: 无
- //输入参数: 无
- //返 回 值: 无
- //涉及的全局变量: 无
- //修改记录:
- //================================================
- //pwm触发ADC
- void Adc_Config(void)
- {
- EALLOW;
- AdcRegs.ADCSOC0CTL.bit.CHSEL = 0; //ADCSOC0 对应通道 ADCINA0
- AdcRegs.ADCSOC1CTL.bit.CHSEL = 0; //ADCSOC1 对应通道 ADCINA0
- AdcRegs.ADCSOC0CTL.bit.TRIGSEL = 5; //PWM1触发SOC0
- AdcRegs.ADCSOC1CTL.bit.TRIGSEL = 5; //PWM1触发SOC1
- AdcRegs.ADCSOC0CTL.bit.ACQPS = 6; //采样保持7个周期
- AdcRegs.ADCSOC1CTL.bit.ACQPS = 6; //采样保持7个周期
- EDIS;
- // 触发adc采样
- /* Set up Event Trigger with CNT_zero enable for Time-base of EPWM1 */
- EPwm1Regs.ETSEL.bit.SOCAEN = 1; /* Enable SOCA */
- EPwm1Regs.ETSEL.bit.SOCASEL = 1; /* Enable CNT_zero event for SOCA */
- EPwm1Regs.ETPS.bit.SOCAPRD = 1; /* Generate SOCA on the 1st event */
- EPwm1Regs.ETCLR.bit.SOCA = 1; /* Clear SOCA flag */
- }
复制代码
请大家看看这个程序对不对
此帖出自
小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>