使用launchpad捕获外部脉冲;TACTL做定时时钟,TACCTL0捕获脉冲;但捕获中断始终进不去,外部脉冲由信号发生器提供1K的信号,下面是程序,大家帮我参考下:
#include <MSP430G2553.H>
unsigned int Cap_Tar;
unsigned int Cap_First;
unsigned int Cap_Last;
unsigned int time;
unsigned int pulse;
unsigned int F;
char flag=0;
int main( void )
{
WDTCTL = WDTPW + WDTHOLD; // 停止看门狗
P1DIR |= 0X41;
P1DIR &= ~BIT1; //设置P1.1为输入
P1SEL |= BIT1; //P1.1给外设TA0使用
//P1REN |= BIT1;
TACTL = TASSEL_1 + MC_2 + TAIE; // 时钟源选择 ACLK,计数模式选择连续计数模式,主计数器计满中断允许
TACCTL0 = CM_1 + SCS + CCIS_0 + CAP + CCIE;
//上升沿捕获+CCI0A(P1.1)+捕获模式+中断使能
while(1)
{
_BIS_SR(LPM3_bits + GIE); // 进入低功耗模式3,打开全局中断
if(flag==1)
{
P1OUT ^= 0X40;
flag=0;
}
}
}
// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A0 (void)
{
if(Cap_Tar==0)
{
Cap_First = TACCR0; //捕获时间阀值内第一个上升沿的TAR
Cap_Tar++;
flag=1;
}
else
{
Cap_Last = TACCR0;
Cap_Tar++;
flag=1;
}
}
// Timer_A2 Interrupt Vector (TA0IV) handler
#pragma vector=TIMER0_A1_VECTOR
__interrupt void Timer_A1(void)
{
switch( TA0IV )
{
case 2:break;
case 10:
{
if(Cap_Tar==0)
{
pulse=0; //脉冲数为0
}
else
{
pulse = Cap_Tar - 1; //计算脉冲数
time = Cap_Last - Cap_First; //计算脉冲周期
Cap_Tar = 0; //清零
Cap_Last= 0;
Cap_First=0;
}
LPM3_EXIT;
}
//P1OUT ^= 0x41; // 主计数器计满溢出
break;
}
}
此帖出自
小平头技术问答
ACLK时钟源没有配置到底频率是多少?晶体还是能够自动切换到VLO?
一周热门 更多>