Học liệu

Lập trình cảm biến siêu âm với MSP430

  • 31/10/2018
  • Học liệu

#include <msp430g2553.h>
#define trigger BIT1;
#define echo BIT0;
void timer_init(void);
void dis(void);
unsigned int t1=0, t2=0, delta=0;
void main(void) {
WDTCTL = WDTPW | WDTHOLD;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
P2DIR |= trigger;
P2DIR &=~BIT0;
P2SEL |= echo; //connect P2.0 (echo) to CCI0A (capture/compare input )
P2SEL2 &= ~echo; // chi thay doi 1 bit 2.0
P2DIR &= ~echo;
P1DIR |= BIT0+BIT6;
P1OUT &= ~(BIT0+BIT6);
timer_init();
_BIS_SR(GIE); 
while (1)
{
do_khoang_cach();
if (delta >=100)
{
P1OUT|=BIT0;
P1OUT&=~BIT6;
}
if(delta <=100)
{
P1OUT|=BIT6;
P1OUT&=~BIT0;
}
}

}
void timer_init(void)
{
TA1CTL = TASSEL_2 + MC_1 ; // continuos mode, 0 --> FFFF
TA1CCTL0 = CM_2 + CAP + CCIS_3 + SCS+ CCIE; // falling edge & raising edge, capture mode, capture/compare interrupt enable
TA1CCTL0 &= ~ CCIFG;
}

void dis(void)
{
P2OUT |= trigger;
_delay_cycles(200);
P2OUT &= ~trigger;
}
#pragma vector = TIMER1_A0_VECTOR
__interrupt void timer0(void){
if (P2IN & BIT0 ==1 )
t1 = TA1CCR1;
else { 
t2 = TA1CCR1;
if (t2 > t1)
{
delta = (t2*t1);
delta=delta+1;
}
}
TA1CCTL1 &= ~ CCIFG;
}

 

Các tin khác