Hi all, I am working on implementing a scanning button matrix (see here). It works well, but the switching is slow enough to be in the audio range (about 11khz). This is an audio application so this just won't do. I'm working on speeding up the code but am not sure what to do. Basically, there are 64 "buttons" - 8 rows, 8 columns. The rows are inputs, the columns are outputs. Ideally, the outputs all normally sit in tri-state (disconnected) mode, but this is only partially possible on the mbed. The microcontroller sets one of the outputs as low, then runs through the column pins and looks for a low signal. If one is found, that means that that button is down. Then, it turns the column back to "tri-state" and checks the next column (this is explained better in the link above). In order to simulate tri-state on the mbed, I'm making the column pins DigitalInOuts. That way, "tri-state" is setting the pin as an input with mode(PullNone). Unfortunately, this means I have to toggle the pin mode at every iteration, which is much slower than just writing a value. Here is the code:
// Set up input pins
DigitalIn inStrings[8] = {p5, p6, p7, p8, p9, p10, p11, p12};
// Set up cyclic output pins
DigitalInOut outStrings[8] = {p13, p14, p15, p16, p17, p18, p19, p20};
// Indexing variables
int i;
int j;
// Setup pins
for (i = 0; i < numStrings; i++) {
// Pull-up resistors on input pins
inStrings[i].mode(PullUp);
// Set all out strings as input to being with
outStrings[i].input();
outStrings[i].mode(PullNone);
}
// For reading in values
char reading = 0;
while (true) {
// Check buttons
for (i = 0; i < numStrings; i++) {
// Make the i'th row output 0V
// The opposite of what the inputs are sitting at
outStrings[i].output();
outStrings[i].write(0);
for ( j = 0; j < numStrings; j++ ) {
// Read in value from j'th column
reading = inStrings[j].read();
if ( reading ) {
// Handle a button press (code omitted for clarity)
}
if ( !reading ) {
// Handle a button up (code omitted for clarity)
}
}
// OK, now we are done with this row, do it again.
// Make this row an input so that it doesn't affect
// future readings
outStrings[i].input();
outStrings[i].mode(PullNone);
}
}
I'd like to use BusInOut or PortInOut but I need to be able to set individual pins as inputs or outputs so this doesn't seem possible. Any tips? Is there any way I can make the outString default to PullUp when it is set to an input? Omitting that instruction makes the switching rate much faster but the code stops working. Thanks!
-Colin
Hi all, I am working on implementing a scanning button matrix (see here). It works well, but the switching is slow enough to be in the audio range (about 11khz). This is an audio application so this just won't do. I'm working on speeding up the code but am not sure what to do. Basically, there are 64 "buttons" - 8 rows, 8 columns. The rows are inputs, the columns are outputs. Ideally, the outputs all normally sit in tri-state (disconnected) mode, but this is only partially possible on the mbed. The microcontroller sets one of the outputs as low, then runs through the column pins and looks for a low signal. If one is found, that means that that button is down. Then, it turns the column back to "tri-state" and checks the next column (this is explained better in the link above). In order to simulate tri-state on the mbed, I'm making the column pins DigitalInOuts. That way, "tri-state" is setting the pin as an input with mode(PullNone). Unfortunately, this means I have to toggle the pin mode at every iteration, which is much slower than just writing a value. Here is the code:
I'd like to use BusInOut or PortInOut but I need to be able to set individual pins as inputs or outputs so this doesn't seem possible. Any tips? Is there any way I can make the outString default to PullUp when it is set to an input? Omitting that instruction makes the switching rate much faster but the code stops working. Thanks!
-Colin