MikeJuiletGolf
Member
Hi Guys. I’m developing a set of knobs and buttons to control X-plane while flying a Robin DR40 in VR. I’ve got the radio stack working fine, just some learning and understanding issues but of course I’ve run out of pins in my Teensy 3.6.
So for the main panel I’m using 7 KY-040 type encoders one with push button and a MCP23017. Wrote some nice code to read and interpret the encoders based on some very useful code contributed by tonton81 in this post "https://forum.pjrc.com/threads/52650-Teensy-with-Encoders-and-MCP23017". But my code didn’t work so I’ve checked and tested the circuit, found some bugs and fixed them then stripped the code back and back until I’ve just got a snippet that reads and displays the bits from the MCP. It seems that my problem is that the pins on the MCP keep resetting to high after I’ve turned the encoder knob. I’ve got to the limits of my knowledge and need some help in getting this to work.
The screen dumps are produced by turning the knob approx. two clicks per second clockwise in the first image and ccw in the second. i get the same result, on the correct bits if i plug the encoder in to other pin pairs.
The two pullup resistors on the i2c bus give 2.2 k Ohms in series, tested with multimeter (haven’t got a 2.2 k one) the pullup on reset is 10 k.
Sorry haven't learnt how to insert my code in a neat little box yet:
/*
* Simplified code to test reading bits from MCP
*/
/////////GLOBAL//////////
#include <Wire.h>
uint16_t allPinsNew = 0b1111111111111111; //2 byte variable initalized to 1111111111111111
uint16_t allPinsOld = 0b1111111111111111; //2 byte variable initalized to 1111111111111111
unsigned long oldTime = 0; //millis store
//////////SETUP//////////
void setup() {
Serial.begin(115200);
delay(1000); // some startup time
Serial.println("understanding_reading_MCP23017");
Wire.begin(); // enable Wire bus (mandatory)
delay(1000); // some startup time
Wire.beginTransmission(0x20); // start a new session to chip address
Wire.write(0x0C); //set the register pointer (pullups on bankA);
Wire.write(0xFF); // set pins 0-7 pullups enabled
Wire.write(0xFF); // set pins 8-15 pullups enabled
Wire.endTransmission(); // setup complete
}
//////////THE LOOP//////////
void loop() { // start of LOOP
if ( millis() > oldTime + 50) { // start time check passed
oldTime = millis();
Wire.beginTransmission(0x20); // start a new session to chip
Wire.write(0x12); //set the register pointer
Wire.endTransmission();
Wire.requestFrom(0x20, 2); //request TWO bytes from the pointed location from above
allPinsNew = (uint16_t)(Wire.read() << 8) | Wire.read(); //bit shift to fill variable
if ((allPinsNew ^ allPinsOld) != 0) { //a pin has changed
//print the bit pattern
Serial.println(" 111111");
Serial.println(" 54321098|76543210");
debugBitPrint("allPinsOld", allPinsOld);
debugBitPrint("allPinsNew", allPinsNew);
Serial.println();
delay(500);
allPinsOld = allPinsNew;
}
}
} //end of LOOP
/////////FUNCTIONS////////
void debugMessage(String text1, int value) {
Serial.print(text1);
Serial.print(": ");
Serial.println(value);
}
void debugBitPrint(String text1, uint16_t value) {
Serial.print(text1);
Serial.print(": ");
for (int i = 15; i > -1; i--)
{
Serial.print(bitRead(value, i));
if (i == 8) Serial.print("|");
}
Serial.println();
}
So for the main panel I’m using 7 KY-040 type encoders one with push button and a MCP23017. Wrote some nice code to read and interpret the encoders based on some very useful code contributed by tonton81 in this post "https://forum.pjrc.com/threads/52650-Teensy-with-Encoders-and-MCP23017". But my code didn’t work so I’ve checked and tested the circuit, found some bugs and fixed them then stripped the code back and back until I’ve just got a snippet that reads and displays the bits from the MCP. It seems that my problem is that the pins on the MCP keep resetting to high after I’ve turned the encoder knob. I’ve got to the limits of my knowledge and need some help in getting this to work.
The screen dumps are produced by turning the knob approx. two clicks per second clockwise in the first image and ccw in the second. i get the same result, on the correct bits if i plug the encoder in to other pin pairs.
The two pullup resistors on the i2c bus give 2.2 k Ohms in series, tested with multimeter (haven’t got a 2.2 k one) the pullup on reset is 10 k.
Sorry haven't learnt how to insert my code in a neat little box yet:
/*
* Simplified code to test reading bits from MCP
*/
/////////GLOBAL//////////
#include <Wire.h>
uint16_t allPinsNew = 0b1111111111111111; //2 byte variable initalized to 1111111111111111
uint16_t allPinsOld = 0b1111111111111111; //2 byte variable initalized to 1111111111111111
unsigned long oldTime = 0; //millis store
//////////SETUP//////////
void setup() {
Serial.begin(115200);
delay(1000); // some startup time
Serial.println("understanding_reading_MCP23017");
Wire.begin(); // enable Wire bus (mandatory)
delay(1000); // some startup time
Wire.beginTransmission(0x20); // start a new session to chip address
Wire.write(0x0C); //set the register pointer (pullups on bankA);
Wire.write(0xFF); // set pins 0-7 pullups enabled
Wire.write(0xFF); // set pins 8-15 pullups enabled
Wire.endTransmission(); // setup complete
}
//////////THE LOOP//////////
void loop() { // start of LOOP
if ( millis() > oldTime + 50) { // start time check passed
oldTime = millis();
Wire.beginTransmission(0x20); // start a new session to chip
Wire.write(0x12); //set the register pointer
Wire.endTransmission();
Wire.requestFrom(0x20, 2); //request TWO bytes from the pointed location from above
allPinsNew = (uint16_t)(Wire.read() << 8) | Wire.read(); //bit shift to fill variable
if ((allPinsNew ^ allPinsOld) != 0) { //a pin has changed
//print the bit pattern
Serial.println(" 111111");
Serial.println(" 54321098|76543210");
debugBitPrint("allPinsOld", allPinsOld);
debugBitPrint("allPinsNew", allPinsNew);
Serial.println();
delay(500);
allPinsOld = allPinsNew;
}
}
} //end of LOOP
/////////FUNCTIONS////////
void debugMessage(String text1, int value) {
Serial.print(text1);
Serial.print(": ");
Serial.println(value);
}
void debugBitPrint(String text1, uint16_t value) {
Serial.print(text1);
Serial.print(": ");
for (int i = 15; i > -1; i--)
{
Serial.print(bitRead(value, i));
if (i == 8) Serial.print("|");
}
Serial.println();
}