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。
ecap捕捉epwm波形的占空比及频率(总结)_第1张图片
补充一点吧,可能有人也会遇到这个问题,关于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 used 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)
{

// 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 set 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
   DINT;

// 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
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in DSP2833x_DefaultIsr.c.
// This function is found in DSP2833x_PieVect.c.
   InitPieVectTable();

// Interrupts that are used 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. User specific code, enable interrupts:

// Initalize counters:
   ECap1IntCount = 0;
   ECap1PassCount = 0;

// Enable CPU INT4 which is connected to ECAP1-4 INT:
   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

// Step 6. IDLE loop. Just sit and loop forever (optional):
   for(;;)
   {
       asm("          NOP");
   }

}


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;
   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,选择CTR=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.
//===========================================================================

你可能感兴趣的:(DSP)