本帖最后由 qiangxiaochen 于 2014-8-6 17:22 编辑
最近在搞PWM占空比和频率的测量,思路是使用ECT模块的输入捕捉功能,话不多说,直接上代码:
//======================================================//
//ect.c
//======================================================//
#include "ect.h"
void ECTInit(void)
{
//ECT初始化
ECT_TIOS_IOS0 = 0; //Ch0 as input capture
ECT_TIE_C0I = 1; //Ch0 中断使能
ECT_TCTL4_EDG0A = 1; //检测上升沿,由于是测量PWM上空比,所以先开启上升沿,进入中断后,再开启下降沿检测
ECT_TCTL4_EDG0B = 0;
ECT_TSCR2 = 2; //总线分频
ECT_TSCR1 = 1; //开启定时器
}
#pragma CODE_SEG NON_BANKED
interrupt void VectorNumber_Vectch0 void ECT0_ISR(void)
{
static INT16U buf[4],RTime,Time,Dty,Fre;
static INT8U i;
ECT_TCTL4_EDG0B = 1; //开启下降沿检测,
ECT_TCTL1 = ECT_T标志寄存器1_C0F_MASK; //中断标志清除
buf[i++] = ECT_TC0; //记录触发中断时间点
if(i == 4)
{
i = 0;
RTime = buf[2] - buf[1]; //从1开始是因为第一次进入中断,开启下降沿检测时,在随后的一个下降沿里,中断不会响应,即下降沿开启后有一定的延时才会引起中,
Time = buf[3] - buf[1];
Dty =(INT8U)(RTime * 100ul / Time);
Fre =(INT16U)(11000000ul / Time);//11000000 = 11 * 10e6,是总线分频后的频率
//将ECT_TC0和TCNT清零,TCNT需要分开清零
ECT_TSCR1_TEN = 0;
ECT_TIOS_IOS0 = 1;
ECT_TCNTHi = 0;
ECT_TCNTLo = 0;
ECT_TC0 = 0;
ECT_TSCR1_TEN = 1;
ECT_TIOS_IOS0 = 0;
}
}
#pragma CODE_SEG DEFAULT
|