TI Launchpad + Ultrasonic Range Finder (MB EZ1)
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
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.