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

相关文章

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤

在C#中调用Python代码的两种实现方式

《在C#中调用Python代码的两种实现方式》:本文主要介绍在C#中调用Python代码的两种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#调用python代码的方式1. 使用 Python.NET2. 使用外部进程调用 Python 脚本总结C#调

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

Python使用date模块进行日期处理的终极指南

《Python使用date模块进行日期处理的终极指南》在处理与时间相关的数据时,Python的date模块是开发者最趁手的工具之一,本文将用通俗的语言,结合真实案例,带您掌握date模块的六大核心功能... 目录引言一、date模块的核心功能1.1 日期表示1.2 日期计算1.3 日期比较二、六大常用方法详

Java中&和&&以及|和||的区别、应用场景和代码示例

《Java中&和&&以及|和||的区别、应用场景和代码示例》:本文主要介绍Java中的逻辑运算符&、&&、|和||的区别,包括它们在布尔和整数类型上的应用,文中通过代码介绍的非常详细,需要的朋友可... 目录前言1. & 和 &&代码示例2. | 和 ||代码示例3. 为什么要使用 & 和 | 而不是总是使

Java强制转化示例代码详解

《Java强制转化示例代码详解》:本文主要介绍Java编程语言中的类型转换,包括基本类型之间的强制类型转换和引用类型的强制类型转换,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录引入基本类型强制转换1.数字之间2.数字字符之间引入引用类型的强制转换总结引入在Java编程语言中,类型转换(无论

Vue 调用摄像头扫描条码功能实现代码

《Vue调用摄像头扫描条码功能实现代码》本文介绍了如何使用Vue.js和jsQR库来实现调用摄像头并扫描条码的功能,通过安装依赖、获取摄像头视频流、解析条码等步骤,实现了从开始扫描到停止扫描的完整流... 目录实现步骤:代码实现1. 安装依赖2. vue 页面代码功能说明注意事项以下是一个基于 Vue.js

python中time模块的常用方法及应用详解

《python中time模块的常用方法及应用详解》在Python开发中,时间处理是绕不开的刚需场景,从性能计时到定时任务,从日志记录到数据同步,时间模块始终是开发者最得力的工具之一,本文将通过真实案例... 目录一、时间基石:time.time()典型场景:程序性能分析进阶技巧:结合上下文管理器实现自动计时

mybatis-plus 实现查询表名动态修改的示例代码

《mybatis-plus实现查询表名动态修改的示例代码》通过MyBatis-Plus实现表名的动态替换,根据配置或入参选择不同的表,本文主要介绍了mybatis-plus实现查询表名动态修改的示... 目录实现数据库初始化依赖包配置读取类设置 myBATis-plus 插件测试通过 mybatis-plu