增强型脉宽调制ePWM实验
1 PWM简介
2 F28335的ePWM介绍
3 PWM输出配置步骤
4 硬件设计
5 软件设计
#include "epwm.h"
#include "leds.h"void EPWM1_Init(Uint16 tbprd)
{EALLOW;SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1; // Disable TBCLK within the ePWMSysCtrlRegs.PCLKCR1.bit.EPWM1ENCLK = 1; // ePWM1EDIS;InitEPwm1Gpio();EALLOW;SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0; // Stop all the TB clocksEDIS;// Setup SyncEPwm1Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE; // Pass through// Allow each timer to be sync'edEPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;EPwm1Regs.TBPHS.half.TBPHS = 0;EPwm1Regs.TBCTR = 0x0000; // Clear counterEPwm1Regs.TBPRD = tbprd;EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP; // Count upEPwm1Regs.TBCTL.bit.HSPCLKDIV=TB_DIV1;EPwm1Regs.TBCTL.bit.CLKDIV=TB_DIV1;// Setup shadow register load on ZEROEPwm1Regs.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;// Set Compare valuesEPwm1Regs.CMPA.half.CMPA = 0; // Set compare A valueEPwm1Regs.CMPB = 0; // Set Compare B value// Set actionsEPwm1Regs.AQCTLA.bit.ZRO = AQ_CLEAR; // Set PWM1A on ZeroEPwm1Regs.AQCTLA.bit.CAU = AQ_SET; // Clear PWM1A on event A, up countEPwm1Regs.AQCTLB.bit.ZRO = AQ_SET; // Set PWM1B on ZeroEPwm1Regs.AQCTLB.bit.CBU = AQ_CLEAR; // Clear PWM1B on event B, up countEPwm1Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO; // Select INT on Zero eventEPwm1Regs.ETSEL.bit.INTEN = 1; // Enable INTEPwm1Regs.ETPS.bit.INTPRD = ET_1ST; // Generate INT on 1st eventEALLOW;SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1; // Start all the timers syncedEDIS;
}void EPwm1A_SetCompare(Uint16 val)
{EPwm1Regs.CMPA.half.CMPA = val; //设置占空比
}
void EPwm1B_SetCompare(Uint16 val)
{EPwm1Regs.CMPB = val; //设置占空比
}void EPWM6_Init(Uint16 tbprd)
{EALLOW;SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0; // Disable TBCLK within the ePWMSysCtrlRegs.PCLKCR1.bit.EPWM6ENCLK = 1; // ePWM6EDIS;InitEPwm6Gpio();// Setup SyncEPwm6Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE; // Pass through// Allow each timer to be sync'edEPwm6Regs.TBCTL.bit.PHSEN = TB_DISABLE;EPwm6Regs.TBPHS.half.TBPHS = 0;EPwm6Regs.TBCTR = 0x0000; // Clear counterEPwm6Regs.TBPRD = tbprd;EPwm6Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP; // Count upEPwm6Regs.TBCTL.bit.HSPCLKDIV=TB_DIV1;EPwm6Regs.TBCTL.bit.CLKDIV=TB_DIV1;// Setup shadow register load on ZEROEPwm6Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;EPwm6Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;EPwm6Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;EPwm6Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;// Set Compare valuesEPwm6Regs.CMPA.half.CMPA = 0; // Set compare A valueEPwm6Regs.CMPB = 0; // Set Compare B value// Set actionsEPwm6Regs.AQCTLA.bit.ZRO = AQ_CLEAR; // Set PWM1A on ZeroEPwm6Regs.AQCTLA.bit.CAU = AQ_SET; // Clear PWM1A on event A, up countEPwm6Regs.AQCTLB.bit.ZRO = AQ_CLEAR; // Set PWM1B on ZeroEPwm6Regs.AQCTLB.bit.CBU = AQ_SET; // Clear PWM1B on event B, up countEPwm6Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO; // Select INT on Zero eventEPwm6Regs.ETSEL.bit.INTEN = 1; // Enable INTEPwm6Regs.ETPS.bit.INTPRD = ET_1ST; // Generate INT on 1st eventEALLOW;SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1; // Start all the timers syncedEDIS;
}void EPwm6A_SetCompare(Uint16 val)
{EPwm6Regs.CMPA.half.CMPA = val; //设置占空比
}
void EPwm6B_SetCompare(Uint16 val)
{EPwm6Regs.CMPB = val; //设置占空比
}
#ifndef EPWM_H_
#define EPWM_H_#include "DSP2833x_Device.h" // DSP2833x 头文件
#include "DSP2833x_Examples.h" // DSP2833x 例子相关头文件void EPWM1_Init(Uint16 tbprd);
void EPwm1A_SetCompare(Uint16 val);
void EPwm1B_SetCompare(Uint16 val);void EPWM6_Init(Uint16 tbprd);
void EPwm6A_SetCompare(Uint16 val);
void EPwm6B_SetCompare(Uint16 val);#endif /* EPWM_H_ */
#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File#include "leds.h"
#include "time.h"
#include "epwm.h"/*******************************************************************************
* 函 数 名 : main
* 函数功能 : 主函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void main()
{int i=0;unsigned char fx=0;InitSysCtrl();InitPieCtrl();IER = 0x0000;IFR = 0x0000;InitPieVectTable();// LED_Init();EPWM6_Init(500);while(1){if(fx==0){i++;if(i==300){fx=1;}}else{i--;if(i==0){fx=0;}}EPwm6A_SetCompare(i); //i值最大可以取499,因为ARR最大值是499.EPwm6B_SetCompare(300-i); //i值最大可以取499,因为ARR最大值是499.DELAY_US(10*1000);}
}