下面的代码求指导,或者有哪位大神会msp430g2553实现两个捕获口来求时间差,求帮助~
#include <msp430g2553.h>
#include "12864ser.h"
//#include "math.h"
unsigned int capture0_value = 0;
unsigned int capture1_value = 0;
float diff_time_value = 0;
float diff_distance = 0;
unsigned char flag_cnt = 0;
void main()
{
unsigned char s1[] ={"声音定位系统"};
unsigned char s2[] ={"X 轴:"};
unsigned char s3[] ={"Y 轴:"};
WDTCTL = WDTPW + WDTHOLD; //stop watching dog
BCSCTL1 = CALBC1_16MHZ; //设定cpu
DCOCTL = CALDCO_16MHZ; //配置DCOCLK产生16M时钟
BCSCTL2 |= SELM_1 + DIVM_0 + DIVS_0; //主时钟MCLK选择DCOCLK,不分频.子时钟SMCLK选择DCOCLK,不分频
P2DIR |= BIT3 + BIT4; //开输出
P1DIR &= ~(BIT1 + BIT2 + BIT6 + BIT7); //four input p1.4~p1.7
P1SEL |= BIT1 + BIT2;
TACTL = TASSEL_2 + TACLR + MC_1; //SMCLK,up mode
//capture 0
TACCTL0 = CCIE + CM_2 + CCIS_0 + CAP + SCS ; //neg. edge,enabled interrupt,CCIxA,capture mode,
//capture 1
TACCTL1 = CCIE + CM_2 + CCIS_0 + CAP + SCS ; //neg. edge,enabled interrupt,CCIxA,capture mode,
TACCR0 = 16;
TACCR1 = 16;
_BIS_SR(GIE); // Enter LPM0 w/ interrupt
init_lcd(); //初始化
delay_ms(10);
// wr_string(uchar x,uchar y0,uchar *p);
wr_string(1,0,s1); //第二个参数是行号
delay_ms(1000);
wr_string(0,1,s2);
delay_ms(1000);
wr_string(0,2,s3);
delay_ms(1000);
wr_float(3,1,123.564);
delay_ms(1000);
wr_float(3,2,169.2145);
delay_ms(1000);
while (1)
{
if (flag_cnt == 2)
{
flag_cnt = 0; //clear flag
if(capture0_value - capture1_value <= 0)
diff_time_value = capture1_value - capture0_value;
else
diff_time_value = capture0_value - capture1_value;
diff_distance = diff_time_value *340 * 1.087 / 1000; //uints:mm
}
}
}
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A0 (void)
{
switch (TAIV)
{
case 0x02:
capture1_value = TACCR1;
flag_cnt++;
case 0x04:break; //capture 2
case 0x0a:break;
}
}
#pragma vector = TIMER0_A1_VECTOR
__interrupt void Timer_A1 (void)
{
capture0_value = TACCR0;
flag_cnt++;
}
此帖出自
小平头技术问答
的确没有看到TA0.2的引脚……如果P2.4或者P2.5中有空闲的引脚试试TA1.2吧。
一周热门 更多>