|
MSP430F5438A TIMERA测量脉冲宽度
[复制链接]
新人刚接触这款单机,在用HC_SR04的时候需要测量脉冲宽度
不知道为什么 第二次进中断的 TA0CCR1只比第一次中断加了1.。
#include "msp430x54x.h"
typedef unsigned int uint;
typedef unsigned char uchar;
uint check=0,temp,temper,high,low;
double distamce=0;
uint over=0;
void Init_Port()
{
P1SEL |=BIT2;
P1DIR &=~BIT2;
P2DIR = BIT0;
P2OUT =0x00;
}
void Init_TimerA()
{
// TA0CCTL1=CAP+SCS+CCIS_0+CM_3+CCIE;
// TA0CCR0=20;
// TA0CTL = TASSEL__SMCLK + MC_1 +ID_3;
// TA0CCTL1 = CCIE + CM_3 + SCS + CAP;
TA0CTL = TASSEL__ACLK + MC_2 + TACLR + TAIE;
TA0CCTL1=CAP+SCS+CM_1+CCIE+CCIS_0; //输入上升沿捕获,CCI0A为捕获信号源
TA0CCR0 = 2000;
}
void main()
{
WDTCTL = WDTPW + WDTHOLD;
Init_Port();
Init_TimerA();
while(1)
{
int i=0;
P2OUT |= BIT0;
for(i=0;i<180;i++)
_NOP();
P2OUT &= ~BIT0;
for(i=0;i<18000;i++)
_NOP();
_EINT();
}
}
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_ISR(void)
{
switch(TA0IV)
{
case 2:
{
if(TA0CCTL1&CM_1)
{
low =TA0CCR1;
TA0CCTL1 &=~CM_1;
TA0CCTL1 |=CM_2;
}
else
{
high = TA0CCR1;
temp = high - low;
TA0CCTL1 &=~CM_2;
TA0CCTL1 |=CM_1;
// distamce=(double)(temp)/58;
}
}
break;
case 0x0e:
{
over++;
TA0CCR0+= 2000;
}
break;
default : break;
}
}
|
|