Let's Make Robots!

TI Launchpad + Ultrasonic Range Finder (MB EZ1)

Reads pulse width and calculates distance
main.c2.37 KB

I want to build a robot using the MSP430 value line (Texas Instruments Launchpad) that i bought for $4.30 a month ago.

There are a couple of code samples from TI and someone has already shared code to move up to 4 servos...

However i failed to find anything on how to read PWM signals, commonly used in ultrasonic range finders.

The Maxbotix Maxsonar EZ1 has both PWM or analog outputs, and the best chip that comes in the lauchpad pack (MSP430G2231) has analog inputs. But many other range finders do not have analog outputs, and also, the other more basic chip that comes with launchpad (MSP430G2211) does not have analog inputs.

So, after reading the user manual and the datasheet, and searching a lot in forums i managed to understand and implement a program capable of reading the distance from a device that outputs a PWM signal.

So, i attached the code, and here goes my attempt to explain it:

While to read a pwm signal, in picaxe and in arduino it only takes one command, in launchpad we do not have libraries to help, so we need to do it all by hand...

A pwm signal is something like this:

   |-------------|    |------------|   |               <---- signal is at vcc (eg:+5v) level 

---|             |    |            |   |  . .  .   .

                 |----|            |---|              <---- signal is at gnd (eg: 0v) level




     pulse width



So, we want to know the time in microSeconds between a rising edge (when signal goes to +5v) and down edge (when signal goes to gnd)

We have to setup a timer to count time... then we need to get the rising edge to trigger an interrupt, take note of the time, then we need the down edge to trigger the interrupt, take note of the time and calculate the difference between the times.

That time difference is directly proportional to the distance in the range finder.

To be able to count microSeconds we need to setup the system clock to a high frequency.

1MHz makes the clock tick 1 000 000 times in one second, which means that each tick represents 1 / 1 000 000 second = 1 microSecond.

Then we need to use timer A and select the configured clock (1MHz) as the source for the timer.

Then we tell timerA to trigger an interrupt when it sees a rising edge.

In the interrupt we get the first measure of time and then tell timerA to trigger in both rising and falling edges.


I hope this makes sense.

I tried to comment the source very well and hope it helps. 


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Here's the continuations of this project: vagabundo, my wanderer robot 

You've been featured on 43oh! Thanks for your submission.

Great to see you make so much progress with LaunchPad.  Thanks for posting the code and explaination.

For ascii art, consider the <code> html tags. Disable rich text editing for a moment and insert then yourself. Always close your tags!

thanks, i updated it using your tip

Your code is well commented. It's good to see the Launchpad start to appear here. I'm still wrestling with learning c while I bugger about with mine.

I look forward to seeing how your Launchpad-based robot develops. Keep us updated!

the c part that is needed for this is not too much, almost only bitwise operations.

the hard part for me is understanding how to configure the chip to do what we need...

my next step will be to try driving two servos and reading the distance.. i have code for both, but putting it together might not be easy since they both use the timer...