Distance Sensor Application with MSP430
Yazan: ozturkgokhan
Hi, at my this project i made an sensor application. In the program sensor measures the distance then if it is in the range of mine, it reacts as a LED ON. My sensor is HC-SR04 that has approximately 4.5 meters distance long. It has one input (echo),one output(trigger) and the supply voltages on it. Working princible of sensor (HC-SR04) is like that; we are sending an output signal from echo pin about 20us then set trig as LOW. After that we are waiting the return signal from echo pin. By the way we are measuring the time between sended and recieved. After that we are calculating the time by using speed of the light from the measured time. Last step is converting the time to distance. That is the whole working princible. You can find the code at below. Good Luck.
// Ultrasonic rangefinder example for MSP430G2553 // - An LED is driven by P1.3 // - The trigger pulse is sent to the rangefinder via P1.2 // - The echo pulse is read from the rangefinder via P1.1 #include <msp430.h> void main(void) { int distance_cm; // Disable watchdog timer WDTCTL = WDTPW + WDTHOLD; BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; TA1CTL = TASSEL_2 + ID_0 + MC_2; P1DIR = 0x03; while(1) { distance_cm = read_distance_sensor_cm(); if (distance_cm < 50) P1OUT |= BIT3; // LED on else P1OUT &= ~BIT3; // LED off } } int read_distance_sensor_cm() { int echo_pulse_duration; // time in us int distance; // distance in cm // Send a 20us trigger pulse P1OUT |= BIT2; // trigger high __delay_cycles(20); // 20us delay P1OUT &= ~BIT2; // trigger low // Measure duration of echo pulse while ((P1IN & BIT1) == 0); // Wait for start of echo pulse TA1R = 0; // Reset timer at start of pulse while ((P1IN & BIT1) > 0); // Wait for end of echo pulse echo_pulse_duration = TA1R; // Current timer value is pulse length distance = 0.017 * echo_pulse_duration; // Convert from us to cm return distance; // Return distance in cm to calling function } //Special thank to Ali Gökoğlu
ozturkgokhan hakkında
Electrical and electronics engineer. In turkey.Aralık 6, 2014 tarihinde MSP430 içinde yayınlandı ve CODING, DISTANCESENSOR, Electronics, Embedded systems, HCSR04, MSP430, programming, SENSOR olarak etiketlendi. Kalıcı bağlantıyı yer imlerinize ekleyin. Yorum yapın.
Yorum yapın
Comments 0