Now, this may be down to pin selection, but I wanted to ask here first, see if anyone has any insight...
I'm using some muxes (3x 4051's) with a design I started on an Uno, and am now trying to move over to the Bluepill.
Problem is, when I scan the keys on the Uno, everything works, but when I move the code (both attached below) and change the pins over to the Bluepill, it's giving me weird results.
It should (and does with the Uno) serial print the "numbers" of the keys, 0 to 24. But with the Bluepill, it gives me all the numbers on a mux (0-7 or 8-15, etc.).
I thought I had this working, but I moved the pins due to need the PWM pins for an RGB LED... Have I just chosen the wrong pins? or is it something I'm doing?
It's all running off a 3v3 LDO I've attached to the 5v rail of the Bluepill, other than that the main board has all the muxes and keys on, so there no differences apart from 3v3, but they should run with that logic level fine. Other than that I'm guessing its the pins?
The pins I'm using are PB12-15, and PB3-4. I chose these, because it didn't didn't get in the way of some other stuff I've got going on (SPI1 for a DAC, PA0-3 for Pots and PB6-8 for an RGB LED).
Any help would be hugely appreciated!
EDIT: I've tried this on the latest Arduino IDE, and just dipping my toe into PlatformIO, and its the same, and I'm using a bluepill with the HID Bootloader that's been working great so far!
Bluepill Code:
Code: Select all
// define pins to read buttons
#define MUX_SEL_A PB14
#define MUX_SEL_B PB13
#define MUX_SEL_C PB12
#define MUX_OUT_0 PB4
#define MUX_OUT_1 PB3
#define MUX_OUT_2 PB15
#define LED PC13 // LED
// this 32 bit number holds the states of the 24 buttons, 1 bit per button
uint32_t keyTable = 0xFFFFFFFF;
uint32_t oldkeyTable = 0xFFFFFFFF;
bool isActive[25];
void setup(){
// configure pins for multiplexer
pinMode(MUX_SEL_A, OUTPUT); // these are the select pins
pinMode(MUX_SEL_B, OUTPUT);
pinMode(MUX_SEL_C, OUTPUT);
pinMode(MUX_OUT_0, INPUT);
pinMode(MUX_OUT_1, INPUT);
pinMode(MUX_OUT_2, INPUT);
digitalWrite(MUX_SEL_A, 1); // multiplexer outputs, 8 each
digitalWrite(MUX_SEL_B, 1);
digitalWrite(MUX_SEL_C, 1);
//turn on LED
pinMode(LED, OUTPUT);
digitalWrite(LED, 1);
Serial.begin(9600);
}
void loop(void)
{
getButtons();
getKeys();
delay(5); // wait 10 ms
}
// this funcion reads the buttons and stores their states in the global 'buttons' variable
void getButtons(void){
int i;
keyTable = 0;
for (i = 0; i < 8; i++){
digitalWrite(MUX_SEL_A, i & 1);
digitalWrite(MUX_SEL_B, (i >> 1) & 1);
digitalWrite(MUX_SEL_C, (i >> 2) & 1);
keyTable |= digitalRead(MUX_OUT_2) << i;
}
keyTable <<= 8;
for (i = 0; i < 8; i++){
digitalWrite(MUX_SEL_A, i & 1);
digitalWrite(MUX_SEL_B, (i >> 1) & 1);
digitalWrite(MUX_SEL_C, (i >> 2) & 1);
keyTable |= digitalRead(MUX_OUT_1) << i;
}
keyTable <<= 8;
for (i = 0; i < 8; i++){
digitalWrite(MUX_SEL_A, i & 1);
digitalWrite(MUX_SEL_B, (i >> 1) & 1);
digitalWrite(MUX_SEL_C, (i >> 2) & 1);
keyTable |= digitalRead(MUX_OUT_0) << i;
}
keyTable |= 0x1000000; // masks 25th key
}
void getKeys() {
if (oldkeyTable != keyTable) {
for (int i = 0; i < 25; i++) { // read through the button array and if pressed assigns the number to the Key[] (lowest first... need to change that)
if ((!((keyTable >> i) & 1)) && !isActive[i]) {
Serial.println(i);
isActive[i] = true;
}
if ((((keyTable >> i) & 1)) && isActive[i]) {
isActive[i] = false;
}
}
}
oldkeyTable = keyTable;
}
Arduino Code:
Code: Select all
// define pins to read buttons
#define MUX_SEL_A 4
#define MUX_SEL_B 3
#define MUX_SEL_C 2
#define MUX_OUT_0 7
#define MUX_OUT_1 6
#define MUX_OUT_2 5
#define LED 8 // LED
// this 32 bit number holds the states of the 24 buttons, 1 bit per button
uint32_t keyTable = 0xFFFFFFFF;
uint32_t oldkeyTable = 0xFFFFFFFF;
bool isActive[25];
void setup(){
digitalWrite(10, HIGH); // This needs to be set here first as theres no physical pullup for the SPI SS (pin 10)
// configure pins for multiplexer
pinMode(MUX_SEL_A, OUTPUT); // these are the select pins
pinMode(MUX_SEL_B, OUTPUT);
pinMode(MUX_SEL_C, OUTPUT);
pinMode(MUX_OUT_0, INPUT);
pinMode(MUX_OUT_1, INPUT);
pinMode(MUX_OUT_2, INPUT);
digitalWrite(MUX_SEL_A, 1); // multiplexer outputs, 8 each
digitalWrite(MUX_SEL_B, 1);
digitalWrite(MUX_SEL_C, 1);
//turn on LED
pinMode(LED, OUTPUT);
digitalWrite(LED, 1);
Serial.begin(9600);
}
void loop(void)
{
getButtons();
getKeys();
delay(5); // wait 10 ms
}
// this funcion reads the buttons and stores their states in the global 'buttons' variable
void getButtons(void){
int i;
keyTable = 0;
for (i = 0; i < 8; i++){
digitalWrite(MUX_SEL_A, i & 1);
digitalWrite(MUX_SEL_B, (i >> 1) & 1);
digitalWrite(MUX_SEL_C, (i >> 2) & 1);
keyTable |= digitalRead(MUX_OUT_2) << i;
}
keyTable <<= 8;
for (i = 0; i < 8; i++){
digitalWrite(MUX_SEL_A, i & 1);
digitalWrite(MUX_SEL_B, (i >> 1) & 1);
digitalWrite(MUX_SEL_C, (i >> 2) & 1);
keyTable |= digitalRead(MUX_OUT_1) << i;
}
keyTable <<= 8;
for (i = 0; i < 8; i++){
digitalWrite(MUX_SEL_A, i & 1);
digitalWrite(MUX_SEL_B, (i >> 1) & 1);
digitalWrite(MUX_SEL_C, (i >> 2) & 1);
keyTable |= digitalRead(MUX_OUT_0) << i;
}
keyTable |= 0x1000000; // masks 25th key
}
void getKeys() {
if (oldkeyTable != keyTable) {
for (int i = 0; i < 25; i++) { // read through the button array and if pressed assigns the number to the Key[] (lowest first... need to change that)
if ((!((keyTable >> i) & 1)) && !isActive[i]) {
Serial.println(i);
isActive[i] = true;
}
if ((((keyTable >> i) & 1)) && isActive[i]) {
isActive[i] = false;
}
}
}
oldkeyTable = keyTable;
}