#include "msp430x14x.h"
unsigned int Signal_Start[16];
unsigned int Signal_Stop[16];
unsigned int SignalValue[]={0};
unsigned int CaptureValue = 0;
unsigned int i,j,k,count1,count2 = 0;
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P6DIR |= BIT2;P6OUT |= BIT2;
for(i=0;i<2000;i++);
BCSCTL1 |= DIVA_3;
P5DIR |= BIT6 + BIT5;
P5SEL |= BIT6 + BIT5;
P1SEL |= BIT1; //设置P10 输出
P1DIR &= ~BIT1; //设置P11 输入
CCTL0 |= CAP + CM_1 + SCS + CCIS_0 + CCIE; //捕获功能 上升沿捕获 同步捕获 P1.1输入 中断允许
TACTL |= TASSEL_2 + MC_2;
_EINT();
while(1)
{
for(j=0;j<16;j++)
{
if(Signal_Stop[j] > Signal_Start[j])
{
SignalValue[k] = Signal_Stop - Signal_Start[j];
k++;
}
}
for(j=0;j
{
CaptureValue = CaptureValue + SignalValue[j];
}
CaptureValue = CaptureValue/k;
}
return 0;
}
#pragma vector = TIMERA0_VECTOR
__interrupt void TimerA0_ISR(void)
{
if(CCTL0 & CCI) //捕获到上升沿 CCI = 0000 0000 0000 1000
{
Signal_Start[count1++] = TACCR0;
CCTL0 = (CCTL0&(~CM_1)) | CM_2; //由上升沿捕获 改为下降沿捕获
}
else
{
Signal_Stop[count2++] = TACCR0;
CCTL0 = (CCTL0&(~CM_2)) | CM_1; //由下升沿捕获 改为上降沿捕获
}
if(count2 == 16)
{
count1 = 0;
count2 = 0;
CCTL0 &= ~CCIE;
}
}