The circuit is below. When I push one of the switches, I want to get their own number(0~7).
This circuit has two problems.
1. Even if I properly connected 74HC138 with the switches, I pushed #0, #4 and #6 switch and I got wrong number(4, 6 and 0).
2. When I pushed #3 switch, I got 1 and 3 one after the other.
When I pushed #0 switch, I got 4. ←wrong
When I pushed #1 switch, I got 1. ←right
When I pushed #2 switch, I got 2. ←right
When I pushed #3 switch, I got 1 or 3. ←wrong
When I pushed #4 switch, I got 6. ←right
When I pushed #5 switch, I got 5. ←wrong
When I pushed #6 switch, I got 0. ←wrong
When I pushed #7 switch, I got 7. ←right
Note that I properly connected 74HC138 with the switches and I could build key matrix circuit by Arduino UNO 3 in the same way.
My code is below.
Code: Select all
//Mux control pins
int s0 = 12;
int s1 = 13;
int s2 = 14;
int controlPin[] = {s0, s1, s2};
int muxChannel[8][3] = {
{0, 0, 0}, //channel 0
{1, 0, 0}, //channel 1
{0, 1, 0}, //channel 2
{1, 1, 0}, //channel 3
{0, 0, 1}, //channel 4
{1, 0, 1}, //channel 5
{0, 1, 1}, //channel 6
{1, 1, 1}, //channel 7
};
int loop_0_7 = 0;
void setup() {
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
pinMode(16, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
for (int i = 0; i < 3; i ++) {
if(muxChannel[loop_0_7][i]) {
if(i==0) GPIO.out_w1ts = ((uint32_t)1 << 12);
else if(i==1) GPIO.out_w1ts = ((uint32_t)1 << 13);
else if(i==2) GPIO.out_w1ts = ((uint32_t)1 << 14);
}else{
if(i==0) GPIO.out_w1tc = ((uint32_t)1 << 12);
else if(i==1) GPIO.out_w1tc = ((uint32_t)1 << 13);
else if(i==2) GPIO.out_w1tc = ((uint32_t)1 << 14);
}
}
byte res = (GPIO.in >> 16) & 0x1;
if(!res){Serial.println(loop_0_7);delay(100);}
if(loop_0_7==7) loop_0_7=0;
else loop_0_7++;
}
Could you give some advice please?