本文主要是介绍28335 ePWM模块 中心移相与边沿移相代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
中心移相
最常见的中心移相示意图如下:
可以看到其中的PWM1作为主相,其余PWM2和PWM3都作为其的辅相。中心移相,顾名思义,就是移动相位参考高电平的中心,当占空比D=0.5时,其移相与我们认为的上升沿移相一致,也就是最常见的移相。
为方便理解,先给相关设置的结构体设置:
typedef struct {volatile struct EPWM_REGS* ePWMx;Uint32 Fs;double D;Uint32 td;
} ePWM_Master_Set_Struct;typedef struct {volatile struct EPWM_REGS* ePWMx;double D;double Phase;Uint32 td;
} ePWM_Slave_Set_Struct;
其中 ePWM_Master_Set_Struct 是主相设置函数,而 ePWM_Slave_Set_Struct 辅相设置函数。
这是初始化ePWM初始化设置具体实现函数及其定义
void ePWM_Master_Set_Flush(ePWM_Master_Set_Struct* pwm);//中心对齐移相 主要
void ePWM_Slave_Set_Flush(ePWM_Master_Set_Struct* master_pwm,ePWM_Slave_Set_Struct* slave_pwm);//中心对齐移相 次要
void ePWM_Master_Set_Flush(ePWM_Master_Set_Struct* pwm)
{EALLOW;// Initialization Time(TB)pwm->ePWMx->TBPRD=75e6/pwm->Fs;pwm->ePWMx->TBPHS.half.TBPHS = 0; //不进行移相pwm->ePWMx->TBCTR=0; //计数器清零pwm->ePWMx->TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; //上计数模式pwm->ePWMx->TBCTL.bit.PHSEN = TB_DISABLE; //主机模式pwm->ePWMx->TBCTL.bit.PRDLD = TB_SHADOW;pwm->ePWMx->TBCTL.bit.SYNCOSEL = TB_CTR_ZERO; //减到0时发出同步信号pwm->ePWMx->TBCTL.bit.HSPCLKDIV = TB_DIV1; // TBCLK = SYSCLKOUTpwm->ePWMx->TBCTL.bit.CLKDIV = TB_DIV1;pwm->ePWMx->CMPCTL.bit.SHDWAMODE = CC_SHADOW;pwm->ePWMx->CMPCTL.bit.SHDWBMODE = CC_SHADOW;pwm->ePWMx->CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; // load on CTR = Zeropwm->ePWMx->CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; // load on CTR = Zeropwm->ePWMx->AQCTLA.bit.CAD = AQ_SET; //计数比CA低时PWMA输出高pwm->ePWMx->AQCTLA.bit.CAU = AQ_CLEAR; //比CA高时PWMA输出低pwm->ePWMx->AQCTLB.bit.CAD = AQ_CLEAR; //计数比CA低时PWMB输出低pwm->ePWMx->AQCTLB.bit.CAU = AQ_SET; //计数比CA高时PWMB输出高pwm->ePWMx->CMPA.half.CMPA = pwm->ePWMx->TBPRD *pwm->D; // Set compare A value// Initialization Dead-Band(DB)pwm->ePWMx->DBCTL.bit.OUT_MODE=DB_FULL_ENABLE; //上升沿和下降沿都开启延迟pwm->ePWMx->DBCTL.bit.IN_MODE=DBA_ALL; //A作为源信号pwm->ePWMx->DBCTL.bit.POLSEL=DB_ACTV_HIC;//PWMA、B互补相反pwm->ePWMx->DBRED=pwm->td;//死区设置pwm->ePWMx->DBFED=pwm->td;// Initialization Trip-Zone(TZ)pwm->ePWMx->TZCTL.bit.TZA=2; //PWM-A\B均输出低电平pwm->ePWMx->TZCTL.bit.TZB=2;pwm->ePWMx->TZCLR.bit.OST=1; //清空OST标志位EDIS;
}void ePWM_Slave_Set_Flush(ePWM_Master_Set_Struct* master_pwm,ePWM_Slave_Set_Struct* slave_pwm)
{EALLOW;// Initialization Time(TB)slave_pwm->ePWMx->TBPRD=75e6/master_pwm->Fs;slave_pwm->ePWMx->TBPHS.half.TBPHS = fabs(slave_pwm->Phase)/180*slave_pwm->ePWMx->TBPRD; //设置移相角度,相对于主pwm提前角度slave_pwm->ePWMx->TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;slave_pwm->ePWMx->TBCTL.bit.PHSEN = TB_ENABLE;if(slave_pwm->Phase>=0)slave_pwm->ePWMx->TBCTL.bit.PHSDIR = TB_UP;elseslave_pwm->ePWMx->TBCTL.bit.PHSDIR = TB_DOWN;slave_pwm->ePWMx->TBCTL.bit.PRDLD = TB_SHADOW;slave_pwm->ePWMx->TBCTL.bit.SYNCOSEL = TB_SYNC_IN;slave_pwm->ePWMx->TBCTL.bit.HSPCLKDIV = TB_DIV1; // TBCLK = SYSCLKOUTslave_pwm->ePWMx->TBCTL.bit.CLKDIV = TB_DIV1;slave_pwm->ePWMx->CMPCTL.bit.SHDWAMODE = CC_SHADOW;slave_pwm->ePWMx->CMPCTL.bit.SHDWBMODE = CC_SHADOW;slave_pwm->ePWMx->CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;slave_pwm->ePWMx->CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;slave_pwm->ePWMx->AQCTLA.bit.CAD = AQ_SET; //计数比CA低时PWMA输出高slave_pwm->ePWMx->AQCTLA.bit.CAU = AQ_CLEAR; //比CA高时PWMA输出低slave_pwm->ePWMx->AQCTLB.bit.CAD = AQ_CLEAR; //计数比CA低时PWMB输出低slave_pwm->ePWMx->AQCTLB.bit.CAU = AQ_SET; //计数比CA高时PWMB输出高slave_pwm->ePWMx->CMPA.half.CMPA = slave_pwm->ePWMx->TBPRD *slave_pwm->D; // Set compare A value// Initialization Dead-Band(DB)slave_pwm->ePWMx->DBCTL.bit.OUT_MODE=DB_FULL_ENABLE; //上升沿和下降沿都开启延迟slave_pwm->ePWMx->DBCTL.bit.IN_MODE=DBA_ALL; //A作为源信号slave_pwm->ePWMx->DBCTL.bit.POLSEL=DB_ACTV_HIC;//PWMA、B互补相反slave_pwm->ePWMx->DBRED=slave_pwm->td;//死区设置slave_pwm->ePWMx->DBFED=slave_pwm->td;// Initialization Trip-Zone(TZ)slave_pwm->ePWMx->TZCTL.bit.TZA=2; //PWM-A\B均输出低电平slave_pwm->ePWMx->TZCTL.bit.TZB=2;slave_pwm->ePWMx->TZCLR.bit.OST=1; //清空OST标志位EDIS;
}
在程序运行中,进行调频、调宽、移相等操作就需要以下函数
void ePWM_Master_Set(ePWM_Master_Set_Struct* master_pwm);//中心对齐移相 主要
void ePWM_Slave_Set(ePWM_Slave_Set_Struct* slave_pwm,ePWM_Master_Set_Struct* master_pwm);//中心对齐移相 次要
void ePWM_Slave_Set(ePWM_Slave_Set_Struct* slave_pwm,ePWM_Master_Set_Struct* master_pwm)
{if(slave_pwm->Phase>=0)slave_pwm->ePWMx->TBCTL.bit.PHSDIR = TB_UP;elseslave_pwm->ePWMx->TBCTL.bit.PHSDIR = TB_DOWN;slave_pwm->ePWMx->TBPRD=75e6/master_pwm->Fs;slave_pwm->ePWMx->TBPHS.half.TBPHS = fabs(slave_pwm->Phase)/180*slave_pwm->ePWMx->TBPRD;slave_pwm->ePWMx->CMPA.half.CMPA = slave_pwm->ePWMx->TBPRD *slave_pwm->D;
}void ePWM_Master_Set(ePWM_Master_Set_Struct* master_pwm)
{master_pwm->ePWMx->TBPRD=75e6/master_pwm->Fs;master_pwm->ePWMx->CMPA.half.CMPA = master_pwm->ePWMx->TBPRD *master_pwm->D;
}
这里给个demo,实现上面3个pwm的初始化程序如下:
void Init_ePWM()
{InitEPwm1Gpio(); //初始化 A0-EPWM1A A1-EPWM1BInitEPwm2Gpio(); //初始化 A2-EPWM2A A3-EPWM2BInitEPwm3Gpio(); //初始化 A4-EPWM3A A5-EPWM3Bpwm1.ePWMx=&EPwm1Regs;pwm1.Fs=50e3; //50khzpwm1.D=0.25.; //0.25占空比pwm1.td=50; //0.5us 死区时间ePWM_Master_Set_Flush(&pwm1); //pwm1作为主pwm 初始化pwm2.ePWMx=&EPwm2Regs;pwm2.D=0.4; //0.4占空比pwm2.Phase=0; //不移相pwm2.td=50;ePWM_Slave_Set_Flush(&pwm1,&pwm2); //pwm2以pwm1作为主pwmpwm3.ePWMx=&EPwm3Regs;pwm3.D=0.4; //0.4占空比pwm3.Phase=-0.3*360; //滞后108°,这个参数范围为 -180°~+180°pwm3.td=50;ePWM_Slave1_Set_Flush(&pwm1,&pwm3); //pwm3以pwm1作为主pwmEALLOW;SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1; // Start all the timers syncedEDIS;
}
边沿移相
边沿移相示意图如下:
结构体和上面中心移相一致,实现代码如下:
void ePWM_Master1_Set_Flush(ePWM_Master_Set_Struct* pwm);//上升沿对齐移相 主要 D<=0.5
void ePWM_Slave1_Set_Flush(ePWM_Master_Set_Struct* master_pwm,ePWM_Slave_Set_Struct* slave_pwm);//上升沿对齐移相 次要 D<=0.5
void ePWM_Master1_Set_Flush(ePWM_Master_Set_Struct* pwm)
{EALLOW;// Initialization Time(TB)pwm->ePWMx->TBPRD=75e6/pwm->Fs;pwm->ePWMx->TBPHS.half.TBPHS = 0; //不进行移相pwm->ePWMx->TBCTR=0; //计数器清零pwm->ePWMx->TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; //上下计数模式pwm->ePWMx->TBCTL.bit.PHSEN = TB_DISABLE; //主机模式pwm->ePWMx->TBCTL.bit.PRDLD = TB_SHADOW;pwm->ePWMx->TBCTL.bit.SYNCOSEL = TB_CTR_ZERO; //减到0时发出同步信号pwm->ePWMx->TBCTL.bit.HSPCLKDIV = TB_DIV1; // TBCLK = SYSCLKOUTpwm->ePWMx->TBCTL.bit.CLKDIV = TB_DIV1;pwm->ePWMx->CMPCTL.bit.SHDWAMODE = CC_SHADOW;pwm->ePWMx->CMPCTL.bit.SHDWBMODE = CC_SHADOW;pwm->ePWMx->CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; // load on CTR = Zeropwm->ePWMx->CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; // load on CTR = Zeropwm->ePWMx->AQCTLA.bit.ZRO = AQ_SET; //计数0时PWMA输出高pwm->ePWMx->AQCTLA.bit.CAU = AQ_CLEAR; //比CA高时PWMA输出低pwm->ePWMx->AQCTLB.bit.PRD = AQ_CLEAR; //计数PRD时输出高pwm->ePWMx->AQCTLB.bit.CBD = AQ_SET; //计数比CB低时PWMB输出低pwm->ePWMx->CMPA.half.CMPA = pwm->ePWMx->TBPRD *pwm->D*2; // Set compare A valuepwm->ePWMx->CMPB = pwm->ePWMx->TBPRD *(1-pwm->D*2)+1; // Set compare B value// Initialization Dead-Band(DB)pwm->ePWMx->DBCTL.bit.OUT_MODE=DB_FULL_ENABLE; //A\B均开启死区pwm->ePWMx->DBCTL.bit.IN_MODE=DBA_RED_DBB_FED; //A上升沿,B下降沿源信号pwm->ePWMx->DBCTL.bit.POLSEL=DB_ACTV_HIC;//PWMA、B互补相反pwm->ePWMx->DBRED=pwm->td;//死区设置pwm->ePWMx->DBFED=pwm->td;// Initialization Trip-Zone(TZ)pwm->ePWMx->TZCTL.bit.TZA=2; //PWM-A\B均输出低电平pwm->ePWMx->TZCTL.bit.TZB=2;pwm->ePWMx->TZCLR.bit.OST=1; //清空OST标志位EDIS;
}void ePWM_Slave1_Set_Flush(ePWM_Master_Set_Struct* master_pwm,ePWM_Slave_Set_Struct* slave_pwm)
{EALLOW;// Initialization Time(TB)slave_pwm->ePWMx->TBPRD=75e6/master_pwm->Fs;slave_pwm->ePWMx->TBPHS.half.TBPHS = fabs(slave_pwm->Phase)/180*slave_pwm->ePWMx->TBPRD; //设置移相角度,相对于主pwm提前角度slave_pwm->ePWMx->TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;slave_pwm->ePWMx->TBCTL.bit.PHSEN = TB_ENABLE;if(slave_pwm->Phase>=0)slave_pwm->ePWMx->TBCTL.bit.PHSDIR = TB_UP;elseslave_pwm->ePWMx->TBCTL.bit.PHSDIR = TB_DOWN;slave_pwm->ePWMx->TBCTL.bit.PRDLD = TB_SHADOW;slave_pwm->ePWMx->TBCTL.bit.SYNCOSEL = TB_SYNC_IN;slave_pwm->ePWMx->TBCTL.bit.HSPCLKDIV = TB_DIV1; // TBCLK = SYSCLKOUTslave_pwm->ePWMx->TBCTL.bit.CLKDIV = TB_DIV1;slave_pwm->ePWMx->CMPCTL.bit.SHDWAMODE = CC_SHADOW;slave_pwm->ePWMx->CMPCTL.bit.SHDWBMODE = CC_SHADOW;slave_pwm->ePWMx->CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;slave_pwm->ePWMx->CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;slave_pwm->ePWMx->AQCTLA.bit.ZRO = AQ_SET; //计数0时PWMA输出高slave_pwm->ePWMx->AQCTLA.bit.CAU = AQ_CLEAR; //比CA高时PWMA输出低slave_pwm->ePWMx->AQCTLB.bit.PRD = AQ_CLEAR; //计数PRD时输出高slave_pwm->ePWMx->AQCTLB.bit.CBD = AQ_SET; //计数比CB低时PWMB输出低slave_pwm->ePWMx->CMPA.half.CMPA = slave_pwm->ePWMx->TBPRD *slave_pwm->D*2; // Set compare A valueslave_pwm->ePWMx->CMPB = slave_pwm->ePWMx->TBPRD *(1-slave_pwm->D*2)+1; // Set compare B value// Initialization Dead-Band(DB)slave_pwm->ePWMx->DBCTL.bit.OUT_MODE=DB_FULL_ENABLE; //A\B均开启死区slave_pwm->ePWMx->DBCTL.bit.IN_MODE=DBA_RED_DBB_FED; //A上升沿,B下降沿源信号slave_pwm->ePWMx->DBCTL.bit.POLSEL=DB_ACTV_HIC;//PWMA、B互补相反slave_pwm->ePWMx->DBRED=slave_pwm->td;//死区设置slave_pwm->ePWMx->DBFED=slave_pwm->td;// Initialization Trip-Zone(TZ)slave_pwm->ePWMx->TZCTL.bit.TZA=2; //PWM-A\B均输出低电平slave_pwm->ePWMx->TZCTL.bit.TZB=2;slave_pwm->ePWMx->TZCLR.bit.OST=1; //清空OST标志位EDIS;
}
同样类似的,也有程序运行中调频、调宽、调相的设置程序如下:
void ePWM_Master1_Set(ePWM_Master_Set_Struct* master_pwm);//上升沿对齐移相 主要 D<=0.5
void ePWM_Slave1_Set(ePWM_Slave_Set_Struct* slave_pwm,ePWM_Master_Set_Struct* master_pwm);//上升沿对齐移相 次要 D<=0.5
void ePWM_Slave1_Set(ePWM_Slave_Set_Struct* slave_pwm,ePWM_Master_Set_Struct* master_pwm)
{if(slave_pwm->Phase>=0)slave_pwm->ePWMx->TBCTL.bit.PHSDIR = TB_UP;elseslave_pwm->ePWMx->TBCTL.bit.PHSDIR = TB_DOWN;slave_pwm->ePWMx->TBPRD=75e6/master_pwm->Fs;slave_pwm->ePWMx->TBPHS.half.TBPHS = fabs(slave_pwm->Phase)/180*slave_pwm->ePWMx->TBPRD;slave_pwm->ePWMx->CMPA.half.CMPA = slave_pwm->ePWMx->TBPRD *slave_pwm->D*2; // Set compare A valueslave_pwm->ePWMx->CMPB = slave_pwm->ePWMx->TBPRD *(1-slave_pwm->D*2)+1; // Set compare B value
}void ePWM_Master1_Set(ePWM_Master_Set_Struct* master_pwm)
{master_pwm->ePWMx->TBPRD=75e6/master_pwm->Fs;master_pwm->ePWMx->CMPA.half.CMPA = master_pwm->ePWMx->TBPRD *master_pwm->D*2; // Set compare A valuemaster_pwm->ePWMx->CMPB = master_pwm->ePWMx->TBPRD *(1-master_pwm->D*2)+1; // Set compare B value
}
这里给个demo,实现上面3个pwm的初始化程序如下:
void Init_ePWM()
{InitEPwm1Gpio(); //初始化 A0-EPWM1A A1-EPWM1BInitEPwm2Gpio(); //初始化 A2-EPWM2A A3-EPWM2BInitEPwm3Gpio(); //初始化 A4-EPWM3A A5-EPWM3Bpwm1.ePWMx=&EPwm1Regs;pwm1.Fs=50e3; //50kHzpwm1.D=0.5; //50%占空比pwm1.td=50; //0.5us 死区ePWM_Master1_Set_Flush(&pwm1); //pwm1作为主pwmpwm2.ePWMx=&EPwm2Regs;pwm2.D=0.25; //25%占空比pwm2.Phase=0; //不移相pwm2.td=50; //注意这里死区要设置与主相一致,否则上升沿相位无法对齐ePWM_Slave1_Set_Flush(&pwm1,&pwm2); //pwm2以pwm1作为主pwmpwm3.ePWMx=&EPwm3Regs;pwm3.D=0.3; //30%占空比pwm3.Phase=-0.3*180; //滞后108° 范围-180°~+180°pwm3.td=50;ePWM_Slave1_Set_Flush(&pwm1,&pwm3); //pwm3以pwm1作为主pwmEALLOW;SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1; // Start all the timers syncedEDIS;
}
这篇关于28335 ePWM模块 中心移相与边沿移相代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!