|
#include "msp430g2553.h"
#include "clock.h"
#include "12864.h"
char edge_flag=1,shu=0;
unsigned long count=0,num=0,led;
float time;
int over=0;
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
ACLK_32K;
DCO_16MHZ;
// Configure the TA0CCR1 to do input capture
TA0CCTL1 = CAP + CM_3 + CCIE + SCS + CCIS_0;
TA0CTL |= TASSEL_1 + MC_2 + TACLR+TAIE; // ACLK, Cont Mode; start timer
P2DIR|=BIT3;
P2OUT&=~BIT3;
P1SEL|=BIT2;
//P1OUT&=~0x01;
Ini_Lcd();
_BIS_SR(GIE);
while(1){
if(shu==1) P2OUT|=BIT3,delayus(30),P2OUT&=~BIT3,shu=0;//给超声波发trig
}
// return 0;
}
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_ISR (void){
switch(__even_in_range(TA0IV,0x0A)){
case TA0IV_TACCR1:{
//P1OUT^=0x01;
TA0CCTL1&=~CCIFG;//注意进入中断后 要手动清除标志
if(edge_flag==1){
count=TA0CCR1;//第一次捕获
edge_flag=0;
}else{//第二次捕获
if(TA0CCR1-count>0)
count=TA0CCR1-count;
else
count=65536+TA0CCR1-count;
edge_flag=1;
over=0;
/* led=count;
lcd_set_xy(2,4);
while(led){
Disp_SZ(led%10);
led/=10;
}*/
led=count/(float)32768*340/2;//计算时间
// led=time;
Clear_LCD();
lcd_set_xy(2,4);
Disp_SZ(led%10);
led/=10;
while(led){
Disp_SZ(led%10);
led/=10;
}
/* time-=led;
lcd_set_xy(3,4);
Disp_SZ(led%10);
led*=10;
while(led<1000){
Disp_SZ(led%10);
led*=10;
}*/
}
case TA0IV_TAIFG:
TA0CTL&=~TAIE;
if(edge_flag==0){
over++;
}
shu++;
break; // Vector 10: TAIFG
}
}
}
|
|