28335 ePWM模块 中心移相与边沿移相代码

2024-04-26 04:28

本文主要是介绍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模块 中心移相与边沿移相代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/936708

相关文章

python实现pdf转word和excel的示例代码

《python实现pdf转word和excel的示例代码》本文主要介绍了python实现pdf转word和excel的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一、引言二、python编程1,PDF转Word2,PDF转Excel三、前端页面效果展示总结一

在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码

《在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码》在MyBatis的XML映射文件中,trim元素用于动态添加SQL语句的一部分,处理前缀、后缀及多余的逗号或连接符,示... 在MyBATis的XML映射文件中,<trim>元素用于动态地添加SQL语句的一部分,例如SET或W

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

python多进程实现数据共享的示例代码

《python多进程实现数据共享的示例代码》本文介绍了Python中多进程实现数据共享的方法,包括使用multiprocessing模块和manager模块这两种方法,具有一定的参考价值,感兴趣的可以... 目录背景进程、进程创建进程间通信 进程间共享数据共享list实践背景 安卓ui自动化框架,使用的是

多模块的springboot项目发布指定模块的脚本方式

《多模块的springboot项目发布指定模块的脚本方式》该文章主要介绍了如何在多模块的SpringBoot项目中发布指定模块的脚本,作者原先的脚本会清理并编译所有模块,导致发布时间过长,通过简化脚本... 目录多模块的springboot项目发布指定模块的脚本1、不计成本地全部发布2、指定模块发布总结多模

SpringBoot生成和操作PDF的代码详解

《SpringBoot生成和操作PDF的代码详解》本文主要介绍了在SpringBoot项目下,通过代码和操作步骤,详细的介绍了如何操作PDF,希望可以帮助到准备通过JAVA操作PDF的你,项目框架用的... 目录本文简介PDF文件简介代码实现PDF操作基于PDF模板生成,并下载完全基于代码生成,并保存合并P

SpringBoot基于MyBatis-Plus实现Lambda Query查询的示例代码

《SpringBoot基于MyBatis-Plus实现LambdaQuery查询的示例代码》MyBatis-Plus是MyBatis的增强工具,简化了数据库操作,并提高了开发效率,它提供了多种查询方... 目录引言基础环境配置依赖配置(Maven)application.yml 配置表结构设计demo_st

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

Java调用Python代码的几种方法小结

《Java调用Python代码的几种方法小结》Python语言有丰富的系统管理、数据处理、统计类软件包,因此从java应用中调用Python代码的需求很常见、实用,本文介绍几种方法从java调用Pyt... 目录引言Java core使用ProcessBuilder使用Java脚本引擎总结引言python