I just bought a Teensy 3.1, and I'm trying to use the interrupt-based Encoder library (with the mods supplied by Paul in post #2). I'm using this on a small robot I'm building, and hooking it up to 2 & 3. I'm using this encoder (
http://www.pololu.com/product/2590) hooked up to their 100:1 gearmotor, and hooking it up to my logic analyzer and running the motor direct from a 7.4 volt battery, I'm seeing a very nice square sin wave on both A & B, at about 3.0 kHz each, offset 90 degrees.
I assume this is well within the capabilities of using the interrupt based encoders.
I had to add the following to direct_pin_read.h (after making the change in post 2):
Code:
#if defined(__AVR__) || defined(__MK20DX128__) || defined(__MK20DX256__)
as I assume the chip on the 3.1 is a __MK20DX256__
I'm not getting any useful data out of the encoder. I'm using the following code:
Code:
/* Encoder Library - Basic Example
* [url]http://www.pjrc.com/teensy/td_libs_Encoder.html[/url]
*
* This example code is in the public domain.
*/
#include <Encoder.h>
const int ledPin = 13;
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(2, 3);
// avoid using pins with LEDs attached
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
Serial1.begin(57600);
Serial1.println("Basic Encoder Test 2:");
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial1.println(newPosition);
}
digitalWrite(ledPin, HIGH); // set the LED on
delay(100); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(900); // wait for a second
}
I added to LED blink code to make sure it was actually running (which it is). I'm using an FT232 hooked up to pins 0 & 1 for logging. I get the same non-results on the output console regardless of whether the LED code is there or not.
I spin the motor by hand, and the encoder has a 5-spoke wheel on it, so that generates a roughly 400-500 Hz signal. I got the following output:
Code:
Basic Encoder Test 2:
0
1
I get the same output when I turn on the motor and drive it that way.
Does the interrupt-driven encoder code work on the Teensy 3.1 at all?
- Jon