Jump to content
43oh

HC - SR04 Ultrasound Module Driver Code


Recommended Posts

  • 2 weeks later...
  • 1 month later...

hi... this code is working perfectly wid 1 sensor.... but when m trying to use more den one sensors... it is considering all the conditions ... how to use it... please help#include "msp430.h"

#include "stdint.h"
 
//GPIO Pins
 
#define trigger_pin1            BIT1
#define echo_input_pin1       BIT3
#define led1                    BIT0
#define trigger_pin2            BIT3
#define echo_input_pin2       BIT4
#define led2                    BIT1
 
 
//Ultrasound parameters
 
#define timer_period1        62500 // 4 * 62500 = 250ms
#define trigger_pulse1        (timer_period1 - 10)
#define us_per_cm1            14.5 // Depends on module
#define time_to_trigger_us1    450
#define distance_check1        70
#define timer_period2        62500 // 4 * 62500 = 250ms
#define trigger_pulse2        (timer_period2 - 10)
#define us_per_cm2            14.5 // Depends on module
#define time_to_trigger_us2    450
#define distance_check2        70
 
//Statics
 
static uint16_t echo_pulse_counts1 = 0;
static uint16_t echo_pulse_counts2 = 0;
 
//Functions
 
void clk_setup_8mhz();
void setup_trigger_pulse1();
void setup_gpio_echo_interrupt1();
uint16_t get_distance_cm1();
 
void setup_trigger_pulse2();
void setup_gpio_echo_interrupt2();
uint16_t get_distance_cm2();
 
//Main
 
void main(void) {
 
    clk_setup_8mhz();
    setup_trigger_pulse1();
    setup_gpio_echo_interrupt1();
      setup_trigger_pulse2();
       setup_gpio_echo_interrupt2();
       P1DIR |= led1;
       P1OUT &= ~led1;
       P1DIR |= led2;
       P1OUT &= ~led2;
       clk_setup_8mhz();
 
 
    while(1)
    {
 
        if((get_distance_cm1() < distance_check1)&&(get_distance_cm2() > distance_check2))
        {
        P1OUT |= led1;
        P1OUT &=~ led2;
        }
 
 
        else if ((get_distance_cm2() < distance_check2)&&(get_distance_cm1() > distance_check1))
        {
    P1OUT |= led2;
    P1OUT &= ~led1;
        }
 
 
        else if (((get_distance_cm1() < distance_check1)&&(get_distance_cm2() < distance_check2))==1)
        {
              P1OUT |= led1;
              P1OUT |= led2;
        }
    else{
 
    P1OUT &= ~led1;
    P1OUT &=~ led2;
    }
 
    }
}
 // End of main
 
void clk_setup_8mhz() 
{
UCA0CTL1 |= UCSWRST;                      // **Put state machine in reset**
     UCA0CTL1 |= UCSSEL_2;                     // SMCLK
     UCA0BR0 = 109;                              // 1MHz 115200 (see User's Guide)
     UCA0BR1 = 0;                              // 1MHz 115200
     UCA0MCTL |= UCBRS_1 + UCBRF_0;             // Modulation UCBRSx=1, UCBRFx=0
     UCA0CTL1 &= ~UCSWRST;
     UCA0IV=0x02;                 // **Initialize USCI state machine**
     UCA0IE |= UCTXIE + UCRXIE;
}
 
void setup_trigger_pulse1() {
    P4DIR |= trigger_pin1;
    P4OUT &= ~trigger_pin1;
    P4SEL |= trigger_pin1;
    TB0CCR0 = timer_period1;//up mode it will count upto time period value..set//
    TB0CCTL1 = OUTMOD_7;//reset /set//
    TB0CCR1 = trigger_pulse1;//reset whn it counts to ccrx value
    TB0CTL = TBSSEL_2 + MC_1+ ID_2;//smclk,up mode,div by 4,//
    __enable_interrupt();
}
void setup_trigger_pulse2() {
    P4DIR |= trigger_pin2;
    P4OUT &= ~trigger_pin2;
    P4SEL |= trigger_pin2;
    TB0CCR0 = timer_period2;//up mode it will count upto time period value..set//
    TB0CCTL3 = OUTMOD_7;//reset /set//
    TB0CCR3 = trigger_pulse2;//reset whn it counts to ccrx value
    TB0CTL = TBSSEL_2 + MC_1+ ID_2;//smclk,up mode,div by 4,//
    __enable_interrupt();
}
 
void setup_gpio_echo_interrupt1() {
    P1DIR &= ~echo_input_pin1;
    P1OUT &= ~echo_input_pin1;
    P1IE |= echo_input_pin1;
       P1IES |= echo_input_pin1;
       P1IFG &= ~echo_input_pin1;
}
void setup_gpio_echo_interrupt2() {
    P1DIR &= ~echo_input_pin2;
    P1OUT &= ~echo_input_pin2;
    P1IE |= echo_input_pin2;
       P1IES |= echo_input_pin2;
       P1IFG &= ~echo_input_pin2;
}
 
uint16_t get_distance_cm1() {
    return (echo_pulse_counts1 - time_to_trigger_us1)/ us_per_cm1;
}
uint16_t get_distance_cm2() {
    return (echo_pulse_counts2 - time_to_trigger_us2)/ us_per_cm2;
}
 
#pragma vector=PORT1_VECTOR
__interrupt void PORT_1(void) {
    echo_pulse_counts1 = TB0R;
    P1IFG &= ~echo_input_pin1;
    echo_pulse_counts2 = TB0R;
    P1IFG &= ~echo_input_pin2;
 
this is my code for msp430f5438... plz help
Link to post
Share on other sites
  • 5 months later...

Wouldn't it be better to use a timer register in capture mode to get the value of the timer when the echo pulse goes up/down? It seems faster than using GPIO interrupts. I tried it here with the HC-SR04 and it works wonderfully, even when the object is at about 1cm from the sensor.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...