Quantcast
Channel: MSP low-power microcontrollers
Viewing all articles
Browse latest Browse all 62309

Forum Post: Problem: Compute distance with msp430, interrupt issue

$
0
0
Hello everyone. I'm trying to compute distance using msp430G2553 and SRF05. The connections pin are shown below: SRF05 ======> MSP430 trigger =======> P1.7 echo =======> P2.1 I didn't know why the interruption on P2.1 happened a lot of times (one time for if(P2IN & BIT1 == BIT1) and many times for else after). And the distance value was wrong. Here is my code (I use CCS 6.2.0). Sorry, I don't know how to embed .rar file. The uart_puts() and uart_put_num() just write a string and a number to terminal, respectively. /* Description: compute distance and show on the terminal via UART * CONNECT: VCC --> 5V * TRIGGER --> P1.7 * ECHO --> P2.1 * GND --> GND * NC --> no connect */ // khong nhay vao ngat #include //#include #include "uart.h" #define trigger BIT7; #define echo BIT1; void timer_init(void); void trig(void); unsigned int t1=0, t2=0, distance=100; void main(void){ WDTCTL = WDTPW | WDTHOLD; BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; P1DIR |= trigger; // trigger ouput P1OUT &= ~trigger; P2DIR &= ~echo P2SEL |= echo; // uart_init(); timer_init(); _BIS_SR(GIE); // global interrupt enable // uart_puts("------\r\n"); while (1){ trig(); // uart_puts("-----1\r\n"); __delay_cycles(30000); // 30ms time out // uart_puts("----2\r\n"); } } void timer_init(void){ TA1CTL = TASSEL_2 + MC_2; // SMCLK; continuos mode, 0 --> FFFF TA1CCTL1 = CM_3 + CAP + CCIS_0 + SCS+ CCIE; // falling edge & raising edge, capture mode, capture/compare interrupt enable // P2.1 (echo) is connected to CCIA (capture/compare input ) TA1CCTL1 &= ~ CCIFG; } void trig(void){ P1OUT |= trigger; __delay_cycles(20); P1OUT &= ~trigger; } #pragma vector = TIMER1_A1_VECTOR __interrupt void compute_distance(void){ uart_puts("--6\r\n"); // Just test switch(_even_in_range(TA1IV,0x0e)){ case TA1IV_NONE: break; // no more interrupts pending, exit ISR here. case TA1IV_TACCR1: { // CCR1 interrupt if (P2IN & BIT1 == BIT1) { // detect rising edge t1 = TA1CCR1; } else { // and falling edge t2 = TA1CCR1; if (t2 > t1){ distance = (t2-t1)/58; uart_put_num(distance,0,0); uart_puts("\r\n"); } } break; } case TA1IV_TACCR2: break; // CCR2 interrupt case TA1IV_6: break; case TA1IV_8: break; case TA1IV_TAIFG: break; // timer overflow default: break; } TA1CCTL1 &= ~ CCIFG; }

Viewing all articles
Browse latest Browse all 62309

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>