Other Parts Discussed in Thread: CSD
Tool/software: Code Composer Studio
Hello everyone,
I'm trying to use the grove ultrasonic ranger for some distance measurement.
The plan is to send out a pulse of 10us and capture the high-time of the echo from the sensor and then calculate the distance.
However it seems that i can't get into the ISR (which i copied from one of the example code file of TA1). Im already using TA0 for some other job and therefore have to use TA1:
I configured TA1 in capture mode and on P1.2 (which is the Timer 1 CCR1 register according to the datasheet) I'm waiting to recieve to echo signal and trigger the ISR:
#include <msp430.h> #include "stdint.h" #include <string.h> #include <stdlib.h> unsigned int counter; unsigned int distance_cm; // Timer A1 interrupt service routine #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) #pragma vector = TIMER1_A0_VECTOR __interrupt void Timer1_A0_ISR(void) #elif defined(__GNUC__) void __attribute__ ((interrupt(TIMER1_A0_VECTOR))) Timer1_A0_ISR (void) #else #error Compiler not supported! #endif { if (TA1CCTL1 & CCI) // Rising edge { counter = TA1CCR1; // Copy counter value to variable } else // Falling edge { // Formula: Distance in cm = (Time in uSec)/58 distance_cm = (TA1CCR1 - counter)/58; } TA1CCTL1 &= ~CCIFG; // Clear interrupt flag - handled } // Clock System Setup void clock_init(){ CSCTL0_H = CSKEY >> 8; // Unlock CS registers CSCTL1 = DCOFSEL_0; // Set DCO to 1MHz CSCTL2 = SELA__VLOCLK | SELS__DCOCLK | SELM__DCOCLK; // Set SMCLK = MCLK = DCO // ACLK = VLOCLK CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // Set all dividers to 1 CSCTL0_H = 0; // Lock CS registers } //timer1A setup void timer_init(){ TA1CTL = TASSEL_2 | MC_2 ; // SMCLK, cont. mode TA1CCTL1 = CM_3 | SCS | CAP | CCIE; //capture on rising&falling edge, synchronize CCI1A with clock, // capture mode, capture interrupt enable } void peripherals_init(){ P1DIR |= BIT3; //set output for trigger P1DIR &= ~BIT2; //set input for echo P1SEL0 |= BIT2; //configure P1.2 as capture input (CCI1A) for TimerA1 P1SEL1 &= ~BIT2; } void main (void){ WDTCTL = WDTPW | WDTHOLD; //Stop Watchdog Timer PM5CTL0 &= ~LOCKLPM5; // This is Needed for MSP430FR5969! clock_init(); timer_init(); peripherals_init(); init_UART(); P1OUT &= ~BIT3; //keep trigger low // Welcome String over UART send_str("Ultrasonic Distance Measurement in cm: "); __bis_SR_register(GIE); while (1) { // measuring the distance P1OUT ^= BIT3; // trigger high __delay_cycles(10); // 10us wide P1OUT ^= BIT3; // trigger low __delay_cycles(500000); // 0.5sec measurement cycle } }