Quantcast
Channel: MSP430 Technical Forums
Viewing all articles
Browse latest Browse all 2077

Ultrasound Garage Car Stopper

$
0
0

A Ultrasonic car stopper project I made for my garage to inform as to how far I should pull my car forward. This project uses a MSP430 launchpad and a HSC - 04 ultrasonic range module as well as a RGB LED and a photosensitive resistor.

 

Youtube video: (uploading)

 

 

Schematic here: Attached File  garage_msp430.pdf   40.14KB   5 downloads

 

Source here:

(i'll clean it up in abit, was having trouble with my debugger hence all the globals)

/*
 * Author: Nathan Zimmerman
 * Date: 6/22/13
 *
 * Description: A simple program to turn on a LED with the HC-SR04
 * module to turn on a LED if the module detects a object closer
 * than 100 centimeters
 *
 */

#include "msp430g2553.h"
#include "stdint.h"

//GPIO Pins

#define trigger_pin            BIT2
#define echo_input_pin        BIT1
#define pot                    BIT0

#define bled                BIT0
#define bled_setup            P2DIR
#define    bled_output            P2OUT
#define bled_setup_on        bled_setup |= bled
#define bled_on                bled_output |= bled
#define bled_off            bled_output &=~bled

#define rled                BIT1
#define rled_setup            P2DIR
#define    rled_output            P2OUT
#define rled_setup_on        rled_setup |= rled
#define rled_on                rled_output |= rled
#define rled_off            rled_output &=~rled

#define gled                BIT2
#define gled_setup            P2DIR
#define    gled_output            P2OUT
#define gled_setup_on        gled_setup |= gled
#define gled_on                gled_output |= gled
#define gled_off            gled_output &=~gled

//Ultrasound parameters

#define timer_period        62500 // 4 * 62500 = 250ms
#define trigger_pulse        (timer_period - 5)
#define us_per_cm            58
#define time_to_trigger_us    450
#define distance_check        100
#define count_clk_divider    4
#define bad_measurement        40000
#define max_distance        400 //centimeters

//Statics

static uint16_t echo_pulse_counts = 0;
static uint16_t distance_set_value =0;
static uint16_t adc_val=0;
static uint16_t distance=0;
//Functions

void clk_setup_1mhz();
void setup_trigger_pulse();
void setup_gpio_echo_interrupt();
void setup_adc_input();
uint16_t compare_distance_vs_threshold();
uint16_t get_adc_counts();
uint16_t get_distance_cm();

//Main

void main(void) {
    clk_setup_1mhz();
    setup_trigger_pulse();
    setup_gpio_echo_interrupt();
    setup_adc_input();

    bled_setup_on;
    gled_setup_on;
    rled_setup_on;
    bled_off;
    gled_off;
    rled_off;

    while(1)
    {
            distance = get_distance_cm();
            if(distance)    //Check for out of range measurement
            {
                if(distance<compare_distance_vs_threshold())
                {
                    gled_off;
                    rled_on;
                }
                else
                {
                    gled_on;
                    rled_off;

                }
            }
        }
    }


} // End of main

void clk_setup_1mhz() {
    BCSCTL1 = CALBC1_1MHZ;
    DCOCTL = CALDCO_1MHZ;
    WDTCTL = WDTPW + WDTHOLD;
}

void setup_trigger_pulse() {
    P1DIR |= trigger_pin;
    P1OUT &= ~trigger_pin;
    P1SEL |= trigger_pin;
    CCR0 = timer_period;
    CCTL1 = OUTMOD_7;
    CCR1 = trigger_pulse;
    TACTL = TASSEL_2 + MC_1+ ID_2;
    __enable_interrupt();
}

void setup_gpio_echo_interrupt() {
    P1DIR &= ~echo_input_pin;
    P1OUT &= ~echo_input_pin;
    P1IE |= echo_input_pin;
    P1IES |= echo_input_pin;
    P1IFG &= ~echo_input_pin;
}

void setup_adc_input() {
    ADC10CTL1 |= CONSEQ1;
    ADC10CTL0 |= ADC10SHT_2 + ADC10ON + MSC;
    ADC10AE0 |= pot;
    ADC10CTL0 |= ADC10SC + ENC;
}

uint16_t get_distance_cm() {
    if(echo_pulse_counts>bad_measurement)
        return 0;
    else
        return (echo_pulse_counts*count_clk_divider - (time_to_trigger_us))/ us_per_cm;
}

uint16_t get_adc_counts()
{
    return ADC10MEM;
}

uint16_t compare_distance_vs_threshold()
{

    uint32_t trip_threshold =0;

    adc_val=0;
    adc_val = get_adc_counts();
    _delay_cycles(1);
    trip_threshold = ((unsigned long)0x190*((unsigned long)adc_val))>>10;
    _delay_cycles(1);
    return (uint16_t)(trip_threshold & 0xFFFF);
}

#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void) {
    echo_pulse_counts = TAR;
    P1IFG &= ~echo_input_pin;
    _delay_cycles(10);
}
 

Viewing all articles
Browse latest Browse all 2077

Trending Articles