接线
头文件
#ifndef HC_SR_H_
#define HC_SR_H_
#include<msp430.h>
#include"type.h"
#define Trig1(a) if(a==1) P1OUT |= BIT5; else P1OUT &= ~BIT5
void US_015_Init(void);
void Hc_sr_Open(void);
#endif
C文件
#include "hc_sr.h"
void US_015_Init(void)
{
P1OUT &= ~( BIT4 + BIT5 );
P1DIR |= BIT5;
P1SEL |= BIT4;
TA0CTL = TASSEL__SMCLK + ID__8 + MC_2 + TACLR + TAIE;
TA0CCTL3 = CM_1 + SCS +CAP + CCIE + CCIS_0;
}
void Hc_sr_Open(void)
{
Trig1(1);
__delay_cycles(250);
Trig1(0);
}
unsigned int cap_new = 0;
unsigned int cap_old = 0;
char cap_N = 0;
char state = 0x00;
float cap_data ;
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_ISR(void)
{
switch(__even_in_range(TA0IV,14))
{
case 0:break;
case 2:break;
case 4: break;
case 6:
state = TA0CCTL3 >> 14;
TA0CCTL3 &= ~CCIFG;
if( TA0CCTL3 & CM_1){
cap_new = TA0CCR3;
TA0CCTL3 &= ~CM_1;
TA0CCTL3 |= CM_2;
}else if ( TA0CCTL3 & CM_2)
{
cap_old = TA0CCR3;
cap_data =(float)( ( cap_old - cap_new ) * 0.34/10/6.25);
TA0CCTL3 &= ~CM_2;
TA0CCTL3 |= CM_1;
}else
break;
case 8: break;
case 10: break;
case 12: break;
case 14:
TA0CTL &= ~TAIFG;
if(cap_old < cap_new ){
cap_N += 1;
}
break;
default: break;
}
}
工程文件下载
工程文件