View Full Version : BASIC Stamp Encoder Tracking at 37 KHz

Phil Pilgrim (PhiPi)
02-05-2007, 07:07 AM
There have been a couple threads lately about incremental (quadrature) encoder tracking using the BASIC Stamp. The Stamp by itself is not capable of tracking encoder pulses while trying to perform other tasks. This is where an external coprocessor can be helpful. Parallax's BASIC Stamp 2pe Motherboard (http://www.parallax.com/detail.asp?product_id=28300) (the "MoBoStamp-pe") has two such Atmel AVR coprocessors on board, which are ideally suited for encoder tracking. The only thing missing has been the firmware to do it. Well, here's the firmware.

The attached zip includes an uploadable firmware hex file for the MoBoStamp-pe, along with a PDF documenting how it's used. Since I didn't have an actual encoder handy to test the new firmware, I had to simulate one using another micro. I chose the Propeller as a surrogate encoder, since it's easy to write fast programs for it in Spin. Anyway, I was able to get it to output a 37.5KHz, two-channel quadrature encoder signal, which the AVR slurped up and tracked without difficulty. The photo below, shows the experimental setup.


The top set of boards comprises the MoBoStamp-pe with attached 7Seg-DB (proto) and PWR-I/O-DB daughterboards. The bottom set includes a MoBoProp (proto) Propeller motherboard and PWR-I/O-DB daughterboard. The three wirewrap wires comprise two signal lines (the encoder outputs) and ground. Each motherboard is being powered from the USB programming port.

The Propeller is sending quadrature encoder pulses to the AVR on the MoBoStamp-pe. The BASIC Stamp on the MoBoStamp-pe is reading the encoder "position" from the AVR and displaying it in hex on the 7-segment LED display. The following PBASIC program is all that's required to do this:

' {$STAMP BS2pe}
' {$PBASIC 2.5}

cnt VAR Word


OWOUT 10, 0, ["GR"] 'Start counter. Enter "read" mode.
OWIN 10, 0, [cnt.LOWBYTE, cnt.HIGHBYTE] 'Read the counter.
OWOUT 6, 2, [HOME, HEX4 cnt] 'Display the count on the LED.

In order to verify that no pulses were missed, I programmed the Propeller to send them in groups of $1000 (4096), then pause. It would send 16 such groups in the "forward" direction, then 16 groups in "reverse". The pauses made it possible to read the display and ascertain that every pulse had been accounted for.

The firmware uses only a third of the AVR's program space and leaves two spare pins, so additional features are possible, such as: Tracking for two encoders.
Tachometer output to the BASIC Stamp with a programmable divider.
Reference pulse tracking.
Position comparator output(s).
Programmable count modulus.
More than 16 bits of count.
I'll wait to see what the community wants and add features as the demand dictates.


Post Edited (Phil Pilgrim (PhiPi)) : 2/5/2007 1:34:26 AM GMT

02-05-2007, 09:33 AM
That is nice Phil, I wouldn't know where to start on something like that. I need to check out the MoBo and see what else it can do.

Jeff T.

02-05-2007, 10:19 PM
problem solved ,,,great job,,,,and invented a virtual encoder!!

encoders wont help me much (carpet with trow rugs on top to linolium) but keeping the wheel speed matched might

would the prop be a better choice long term ,, is spin easy to get the hang off.и i,m having trouble combing sensors ,ramping ,ect,

Problems are the "roads" of life,
solutions are only "onramps" to the next problem

иииииииииииииииииииииииииииииииииииииииииииии "Brad Smith"

02-07-2007, 01:14 PM
An AVR in a Parallax product? Holy cow. So is an AVR forum next? http://forums.parallax.com/images/smilies/smile.gif Seriously, how did that come about? Good choice though, 32 registers is very nice.


Phil Pilgrim (PhiPi)
02-07-2007, 04:07 PM

It came about originally, simply because I wanted an A/D converter on the board. That's all. But it turns out to be less expensive to include micros with A/D's than just an A/D by itself. Go figure. So I looked at the various micros that were available with A/D's. Once I started down that road, it became apparent that an onboard micro, or two, could do a lot more than just A/D conversion. And that's where the generalized coprocessor idea came from.

But why an AVR?

Well, there wasn't room on the board for any more resonators, so it had to be one with a fast internal clock. It also had to be programmable using just the BASIC Stamp port pins and no funny voltages — just Vdd, be it 3.3V or 5V. That's so firmware could be uploaded directly via the USB port instead of using a special programming adapter. And, it had to be small enough to fit two of them on the board — one dedicated to each daughterboard socket. The ATTINY13 fit the requirements to a "T" and included two hardware PWMs to boot. 'Handy for D/A conversion and motor control! And, coming soon, modulated IR with programmable carrier frequency and power output!

I'd never programmed an AVR before and soon learned that it's quite different from a PIC or SX. The AVR architecture trades the register and instruction set regularity we've come to love in the PIC and SX for codespace optimization. Admittedly, the AVR instruction set seemed odd at first. And getting used to which registers could be used for what kinds of operations took awhile. But I've been pleasantly surprised by how much programming can be squeezed into the ATTINY13's 1K byte codespace.

Yes, the AVR is an odd bird among Parallax's other offerings. But it fit the job at hand and complements its host BS2pe BASIC Stamp nicely. Most MoBoStamp users will never have to program the AVR themselves. Each daughtercard will have its own AVR firmware drivers, where necessary, which can simply be downloaded from the web and uploaded to the board.