ecap捕捉epwm波形的占空⽐及频率(总结)
经过修修改改的,终于初步弄明⽩了28335的ecap模块。现把带有详细注解的代码贴在这⾥,需要的朋友可以参考。
程序功能:
由ePWM3A(GPIO4)⼝发出⼀个占空⽐为0.5,频率为10KHz的⽅波,由eCAP1(GPIO24)进 ⾏波形捕捉,,28335实现⾃发⾃检。(⾥⾯有⼏个变量或者函数虽然定义了,但是没有⽤到,因为程序是在另⼀个程序基础上改的)
说明:
实验过程中⼀定要把GPIO4端⼝与GPIO24端⼝⽤杜邦线连起来。
eCAP1中中断设置中,仅允许了CAP4中断,另外三个中断未使能。
程序烧写到ram中运⾏,在Exression窗⼝观察变量T1,T2,正常的话T1的值应该是15000,T2的值是7500,freq=10000,duty=50。
补充⼀点吧,可能有⼈也会遇到这个问题,关于t1,t2,t3,t4的值怎么转换成频率(因为找了好⼏本书都没有说这⼀点),eCAP模块内部实际上通过计数来实现的,也就是说CAP1到CAP4⾥装载的计数值可以理解为系统时钟的周期数,或者说是系统时钟⽅波的个数(⼀个周期算⼀个的话)。这样的话T1的值就表⽰采集的波形⼀个周期内出现的系统时钟的周期数。个⼈觉得从这个⾓度来理解的话,对于计算频率会⽐较有帮助。
以下是实现上述功能的源代码。
#include"DSP2833x_Device.h"// DSP2833x Headerfile Include File
#include"DSP2833x_Examples.h"// DSP2833x Examples Include File
// Configure the start/end period for the timer
网上英语培训班
#define PWM3_TIMER_MIN 750
#define PWM3_TIMER_MAX 8000
// Prototype statements for functions found within this file.
interrupt void ecap1_isr(void);
void InitECapture(void);
void InitEPwmTimer(void);
void Fail(void);
// Global variables ud in this example
Uint32 ECap1IntCount;
Uint32 ECap1PassCount;
Uint32 EPwm3TimerDirection;
Uint32 t1=0,t2=0,t3=0,t4=0;//⽤于保存eCAP1,eCAP2,eCAP3,eCAP4的值
Uint32 T1=0,T2=0,T1_temp=0,T2_temp=0;//⽤于计算差值
Uint32 freq=0;
float duty=0;//⽤于计算频率和占空⽐
// To keep track of which way the timer value is moving
#define EPWM_TIMER_UP 1
#define EPWM_TIMER_DOWN 0
void main(void)
void main(void)
{
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2833x_SysCtrl.c file.
InitSysCtrl();
研究生考试时间安排
// Step 2. Initalize GPIO:
// This example function is found in the DSP2833x_Gpio.c file and
// illustrates how to t the GPIO to it's default state.
// InitGpio(); // Skipped for this example
InitEPwm3Gpio();//初始化ePWM3端⼝,GPIO4(EPWM3A)和GPIO5(EPWM3B) InitECap1Gpio();//初始化eCAP1端⼝, GPIO24
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
stabiloDINT;
// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2833x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER =0x0000;
IFR =0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
barrister/
/ Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not ud in this example. This is uful for debug purpos.
hope的用法
// The shell ISR routines are found in
// This function is found in
InitPieVectTable();
// Interrupts that are ud in this example are re-mapped to
// ISR functions found within this file.
EALLOW;// This is needed to write to EALLOW protected registers
PieVectTable.ECAP1_INT =&ecap1_isr;
EDIS;// This is needed to disable write to EALLOW protected registers
/
/ Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2833x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
InitEPwmTimer();// For this example, only initialize the ePWM Timers InitECapture();
// Step 5. Ur specific code, enable interrupts:
// Initalize counters:
ECap1IntCount =0;
ECap1PassCount =0;
// Enable CPU INT4 which is connected to ECAP1-4 INT:
township
IER |= M_INT4;
// Enable eCAP INTn in the PIE: Group 3 interrupt 1-6
PieCtrlRegs.PIEIER4.bit.INTx1 =1;
// Enable global Interrupts and higher priority real-time debug events:
EINT;// Enable Global interrupt INTM
ERTM;// Enable Global realtime interrupt DBGM
ERTM;// Enable Global realtime interrupt DBGM
// Step 6. IDLE loop. Just sit and loop forever (optional):
for(;;)
{
asm(" NOP");
}
thor}
void InitEPwmTimer()
{
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC =0;
EDIS;
男英文名字EPwm3Regs.TBPHS.half.TBPHS =0;//所有相位清零
EPwm3Regs.TBCTR =0;//时基计数器清零
EPwm3Regs.TBPRD =750;
EPwm3Regs.CMPA.half.CMPA =375;
EPwm3Regs.CMPB =300;
EPwm3Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;// 增减计数
EPwm3Regs.TBCTL.bit.PHSEN = TB_DISABLE;//禁⽤相位控制
EPwm3Regs.TBCTL.bit.PRDLD = TB_SHADOW;//采⽤影⼦寄存器
EPwm3Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE;//关闭同步信号
EPwm3Regs.TBCTL.bit.HSPCLKDIV =0x05;//TBCLK=1/10系统频率
EPwm3Regs.TBCTL.bit.CLKDIV =0;
EPwm3Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;//CMPA运⾏模式,映射模式
EPwm3Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;//CMPB运⾏模式,映射模式
EPwm3Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;//CMPA的映射寄存器装载时刻位 EPwm3Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;//CMPB的映射寄存器装载时刻位
EPwm3Regs.AQCTLA.bit.CAU = AQ_SET;
EPwm3Regs.AQCTLA.bit.CAD = AQ_CLEAR;
EPwm3Regs.DBCTL.bit.OUT_MODE = DB_DISABLE;//死区不设置
EPwm3Regs.TZSEL.all =0;
EPwm3Regs.TZCTL.all =0;
EPwm3Regs.TZEINT.all =0;
摩登家庭第二季下载
EPwm3Regs.TZFRC.all =0;
EPwm3Regs.TZFLG.all =0;
EPwm3Regs.TZCLR.all =0;
EPwm3Regs.ETSEL.all =0;
EPwm3Regs.ETPS.all =0;
EPwm3Regs.ETFLG.all =0;
EPwm3Regs.ETCLR.all =0;
EPwm3Regs.ETFRC.all =0;
EPwm3Regs.PCCTL.all =0;
EPwm3TimerDirection = EPWM_TIMER_UP;
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC =1;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC =1;
EDIS;
}
void InitECapture()
{
//配置外围寄存器
ECap1Regs.ECCTL1.bit.CAP1POL =0;// 捕获事件CEVT1极性选择,上升沿触发
ECap1Regs.ECCTL1.bit.CAP2POL =1;// 捕获事件CEVT2极性选择,下降沿触发
ECap1Regs.ECCTL1.bit.CAP3POL =0;// 捕获事件CEVT3极性选择,上升沿触发
ECap1Regs.ECCTL1.bit.CAP4POL =1;// 捕获事件CEVT4极性选择,下降沿触发
ECap1Regs.ECCTL1.bit.CTRRST1 =0;// Difference operation 捕获事件CEVT1发⽣时,计数器复位控制位。0⽆动作;1复位计数器
ECap1Regs.ECCTL1.bit.CTRRST2 =0;// Difference operation 捕获事件CEVT2发⽣时,计数器复位控制位。0⽆动作;1复位计数器
ECap1Regs.ECCTL1.bit.CTRRST3 =0;// Difference operation 捕获事件CEVT3发⽣时,计数器复位控制位。0⽆动作;1复位计数器
ECap1Regs.ECCTL1.bit.CTRRST4 =0;// Difference operation 捕获事件CEVT4发⽣时,计数器复位控制位。0⽆动作;1复位计数器
ECap1Regs.ECCTL1.bit.CAPLDEN =1;// Enable capture units 捕获事件发⽣时,CAP1--CAP4装载控制位,0禁⽌装载,1使能
ECap1Regs.ECCTL1.bit.PRESCALE =0;//事件预分频控制位,不分频
ECap1Regs.ECCTL2.bit.CAP_APWM =0;//捕获/APWM模式选择位,0 捕获模式,1 APWM模式
ECap1Regs.ECCTL2.bit.CONT_ONESHT =0;// 连续/单次捕获模式控制位,0连续模式,1单次模式
ECap1Regs.ECCTL2.bit.SYNCO_SEL =2;// Pass through 同步输出选择位,00,同步输⼊SYNC_IN作为同步输⼊SYNC_OUT信号;01,选择C TR=PRD事件作为同步信号输出;1x,禁⽌同步信号输出
ECap1Regs.ECCTL2.bit.SYNCI_EN =0;// Enable sync in 计数器TSCTR同步使能位,0,禁⽌同步;1,当外同步信号SYNCI输⼊或软件强制S/W 事件发⽣时,TSCTR装载CTRPHS的值
ECap1Regs.ECEINT.all =0x0000;// Disable all capture interrupts 屏蔽所有ecap中断
ECap1Regs.ECCLR.all =0xFFFF;// Clear all CAP interrupt flags 清除所有ecap中断标志
ECap1Regs.ECCTL2.bit.TSCTRSTOP =1;// Start Counter 计数器TSCTR控制位,0计数停⽌,1运⾏
ECap1Regs.ECEINT.bit.CEVT4 =1;// 4 events = interrupt 捕获CEVT4事件中断使能位,0禁⽌中断,1允许中断
//ECap1Regs.ECCTL2.bit.REARM = 1; // arm one-shot苏州软件测试培训
// ECap1Regs.ECCTL1.bit.CAPLDEN = 1; // Enable CAP1-CAP4 register loads 捕获事件发⽣时,CAP1--CAP4装载控制位,0禁⽌装载,1使能
}
interrupt void ecap1_isr(void)
{
ECap1PassCount++;
t1 = ECap1Regs.CAP1;
t2 = ECap1Regs.CAP2;
t3 = ECap1Regs.CAP3;
t4 = ECap1Regs.CAP4;
T1 = t3 - t1;//两个下降沿对应时间之差,即周期
T2 = t2 - t1;//占空⽐对应时间
if(ECap1PassCount >100)
{
T1_temp = T1;
T2_temp = T2*100;
freq =(150000/T1_temp)*1000;
duty = T2_temp/T1_temp;
ECap1PassCount =0;
}
ECap1Regs.ECCLR.all =0xFFFF;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP4;//清除PIE中断标志位
}
void Fail()
{
asm(" ESTOP0");
}
//=========================================================================== // No more.
//===========================================================================