Jump to content
guybarash

request: ultrasonic example

Recommended Posts

hi, 

i'm new to the msp430 and i'm using the msp430G2553 on the launchpad.

I need a sample code the read distance measure from maxBotix Maxsonar (Lv-MaxSonar-Ez). 

doesn't really matter what the code does as long as it read distance. 

thx

Share this post


Link to post
Share on other sites


#include <msp430.h>

#include <stdint.h>

 

static const unsigned long smclk_freq = 16000000UL; // SMCLK frequency in hertz

static const unsigned long bps = 9600UL; // Async serial bit rate

 

static uint32_t upd = 0; // Ultrasonic Pulse Duration (500 ns resolution)

//

#pragma vector = TIMER1_A1_VECTOR // Timer 1A vectored interrupt

__interrupt void timer1_a1_isr(void) //

{ //

static uint32_t rise = 0; // Pulse rise time

static uint32_t fall; // Pulse fall time

//

switch(TA1IV) { // TA0IV must be read to reset interrupt

case 0x02: // 0x02 == CCR1 (capture/compare)

if(TA1CCTL1 & CCI) { // - Rising edge

rise = TA1CCR1; // Save rise time

fall = 0; // Reset fall time

} else { // - Falling edge

if(rise) { // Make sure rising edge has occurred

// Handle possible pending overflow interrupt

if((TA1CTL & TAIFG) && (TA1CCR0 < 0x1000))

fall += TA1CCR0; //

fall += TA1CCR1; // Get fall time, add to overflow time

if(!upd) upd = fall - rise; // Update time if mainline code is ready for it

rise = 0; // Clear rise time to ensure next rising edge is used

} //

} //

break; //

case 0x0A: // 0x0A == TAIFG (overflow)

fall += TA1CCR0; // Update overflow time

break; //

} //

} //

 

void putc(const unsigned c) // Output single char to serial

{ //

while(!(IFG2 & UCA0TXIFG)); // Wait for ready (not busy)

IFG2 &= ~UCA0TXIFG; // Reset busy flag

UCA0TXBUF = c; // Tx char

} //

//

void puts(const char *s) { while(*s) putc(*s++); } // Output string to serial

//

void print_u32(uint32_t n, const unsigned dp) // Print 32 bit unsigned with optional decimal place

// 6 decimal digits (0 -> 999999)

{ //

unsigned c; //

c = '0'; while(n >= 100000UL) n -= 100000UL, ++c; putc(c);

if(dp == 5) putc('.'); //

c = '0'; while(n >= 10000) n -= 10000, ++c; putc(c);

if(dp == 4) putc('.'); //

c = '0'; while(n >= 1000) n -= 1000, ++c; putc(c);

if(dp == 3) putc('.'); //

c = '0'; while(n >= 100) n -= 100, ++c; putc(c);

if(dp == 2) putc('.'); //

c = '0'; while(n >= 10) n -= 10, ++c; putc(c); //

if(dp == 1) putc('.'); //

c = '0'; while(n) --n, ++c; putc(c); //

} //

 

void main(void) //

{ //

WDTCTL = WDTPW | WDTHOLD; // Disable watchdog reset

DCOCTL = 0; // Run at 16 MHz

BCSCTL1 = CALBC1_16MHZ; //

DCOCTL = CALDCO_16MHZ; //

//

P1DIR = BIT2; // Setup GPIO

P1SEL = BIT1 | BIT2; // UART

P1SEL2 = BIT1 | BIT2; //

//

P2OUT = 0; //

P2DIR = BIT0; // Timer 1 capture/compare IO

P2SEL = BIT0 | BIT1; //

P2SEL2 = 0; //

//

const unsigned long brd = (smclk_freq + (bps >> 1)) / bps; // Bit rate divisor

UCA0CTL1 = UCSWRST; // Hold USCI in reset to allow configuration

UCA0CTL0 = 0; // No parity, LSB first, 8 bits, one stop bit, UART (async)

UCA0BR1 = (brd >> 12) & 0xFF; // High byte of whole divisor

UCA0BR0 = (brd >> 4) & 0xFF; // Low byte of whole divisor

UCA0MCTL = ((brd << 4) & 0xF0) | UCOS16; // Fractional divisor, oversampling mode

UCA0CTL1 = UCSSEL_2; // Use SMCLK for bit rate generator, release reset

//

// Timer 1 compare 0

TA1CCR0 = (16000000UL / (8 * 2 * 20)) - 1; // 20 Hz (50 ms)

TA1CCTL0 = OUTMOD_4; // Toggle mode

// Timer 1 capture 1

TA1CCTL1 = CM_3 | SCS | CAP | CCIE; // Rising and falling, sync, capture, interrupt

// Timer 1 config

TA1CTL = TASSEL_2 | ID_3 | MC_1 | TAIE; // SMCLK, /8, count up, overflow interrupt

//

_enable_interrupts(); // Enable interrupts

//

for(;;) { // For-ever

if(upd) { // Check for pulse duration measuement available

// Calculate inches

const uint32_t in = ((upd * 5534) + (upd >> 1)) >> 14;

// Calculate mm

const uint32_t mm = (upd * 5623) >> 16;

//

print_u32(in, 2); // Print inches

puts(" in "); //

print_u32(mm, 0); // Print mm

puts(" mm"); //

puts("\r\n"); //

upd = 0; // Ready for next

} //

} //

} //

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×