This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/MSP430FR5959: HC-SR04 Interrupt syntax help

Part Number: MSP430FR5959

Tool/software: Code Composer Studio

Hello,

I am trying to configure my ultrasonic sensor to the MSP. After P1IES, the program loops inside the interrupt. I am not sure if it is a syntax issue or the set up of the interrupt. I found this sample code for the MSPG family, I converted some things on the syntax, but probably not all of it may be correct. Code is below

Thank You.

#include <msp430.h>

int miliseconds;

int distance;

long sensor;

void main(void)

{

  //CSCTL1 = DCOFSEL_0;                       // submainclock 1mhz

  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

  TA1CCTL0 = CCIE;                             // CCR0 interrupt enabled

  TA1CCR0 = 1000;                  // 1ms at 1mhz

  TA1CTL = TASSEL_2 + MC_1;                  // SMCLK, upmode

  //TA1CTL = TASSEL__ACLK | MC__CONTINUOUS;

  P1IFG  = 0x00;                //clear all interrupt flags

  P1DIR |= 0x01;                            // P1.0 as output for LED

  P1OUT &= ~0x01;                           // turn LED off

  __bis_SR_register(GIE);

  //_BIS_SR(GIE);                         // global interrupt enable

 while(1){

    P1IE &= ~0x01;          // disable interupt

    P1DIR |= 0x02;          // trigger pin as output

    P1OUT |= 0x02;          // generate pulse

    __delay_cycles(10);             // for 10us

    P1OUT &= ~0x02;                 // stop pulse

    P1DIR &= ~0x04;         // make pin P1.2 input (ECHO)

        P1IFG = 0x00;                   // clear flag just in case anything happened before

    P1IE |= 0x04;           // enable interupt on ECHO pin

    P1IES &= ~0x04;         // rising edge on ECHO pin

        __delay_cycles(30000);          // delay for 30ms (after this time echo times out if there is no object detected)

        distance = sensor/58;           // converting ECHO lenght into cm

        if(distance < 20 && distance != 0) P1OUT |= 0x01;  //turning LED on if distance is less than 20cm and if distance isn't 0.

        else P1OUT &= ~0x01;

 }

}

#pragma vector=PORT1_VECTOR

__interrupt void Port_1(void)

{

    if(P1IFG&0x04)  //is there interrupt pending?

        {

          if(!(P1IES&0x04)) // is this the rising edge?

          {

            TA1CTL|=TACLR;   // clears timer A

            miliseconds = 0;

            P1IES |= 0x04;  //falling edge

          }

          else

          {

            sensor = (long)miliseconds*1000 + (long)TA1R;    //calculating ECHO length

          }

    P1IFG &= ~0x04;             //clear flag

    }

}

#pragma vector=TIMER0_A0_VECTOR

__interrupt void Timer_A (void)

{

  miliseconds++;

}

  • Hello Alex,

    First I would recommend that you use example code that pertains to the device derivative you are using. This is what most likely prevented you from disabling the GPIO power-on default high-impedance mode to activate previously configured port settings (PM5CTL0 &= ~LOCKLPM5;) which could be causing your issue. Furthermore you are initializing Timer_A1 but using a Timer_A0 interrupt so really you should be running into the isr_trap infinite loop. The HC-SR04 also operates at 5 V which is not a voltage level supported by MSP430 devices (3.6 V maximum) so I would hope that there is a level shifter somewhere in between the two devices.

    Regards,
    Ryan

**Attention** This is a public forum