b.heuju
Junior Member level 2
- Joined
- Dec 17, 2013
- Messages
- 20
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Location
- Nepal
- Activity points
- 168
I am trying to interface matrix keypad. I made my own having 5 buttons (3 rows and 2 columns).
Like this:
...D0 D1 D2
D3 * * *
D4 * *
(D3, D0 = LEFT)
(D3, D2 = RIGHT)
I only need 5 keys, so I left the part (D4, D2).
The 3 columns are connected to pins D0-D2 (starting from left) and the rows are connected to D3-D4 (starting from top).
I understood the way of scanning the key pressed and applied it. And it works for first row. What I did was,
D3 and D4 as o/p.
D0, D1, D2 as i/p.
I made pin D3 high, and checked for D0, D1, D2. The code I used is mentioned below (without the CLEARBIT line).
Note: This is not the complete code.
This function is called repeatedly for scanning the keys.
The problem that occured was, I had to use CLEARBIT(PORTD, PIND3) to reset the D3 pin to then make pin D4 high. But when I use CLEARBIT(PORTD, PIND3) the LEFT key does not work only the RIGHT key works. When I remove the CLEARBIT(PORTD, PIND3) line from the code, then both LEFT and RIGHT work perfectly. But this causes a problem, I can't scan for the next row because both D3 and D4 would be high without clearing pin D3. Also I am required to use pull down resistors at pins D0, D1, D2. It would not work without them.
What could be the problem? What am I doing wrong? Is there any other better way to scan the keys?
Thank You !
Like this:
...D0 D1 D2
D3 * * *
D4 * *
(D3, D0 = LEFT)
(D3, D2 = RIGHT)
I only need 5 keys, so I left the part (D4, D2).
The 3 columns are connected to pins D0-D2 (starting from left) and the rows are connected to D3-D4 (starting from top).
I understood the way of scanning the key pressed and applied it. And it works for first row. What I did was,
D3 and D4 as o/p.
D0, D1, D2 as i/p.
I made pin D3 high, and checked for D0, D1, D2. The code I used is mentioned below (without the CLEARBIT line).
Code:
uint8_t getKEY()
{
SETBIT(PORTD, PIND3);
if (PIND & (1<<PIND0))
{
return LEFT;
}
if (PIND & (1<<PIND2))
{
return RIGHT;
}
CLEARBIT(PORTD, PIND3);
}
Note: This is not the complete code.
This function is called repeatedly for scanning the keys.
The problem that occured was, I had to use CLEARBIT(PORTD, PIND3) to reset the D3 pin to then make pin D4 high. But when I use CLEARBIT(PORTD, PIND3) the LEFT key does not work only the RIGHT key works. When I remove the CLEARBIT(PORTD, PIND3) line from the code, then both LEFT and RIGHT work perfectly. But this causes a problem, I can't scan for the next row because both D3 and D4 would be high without clearing pin D3. Also I am required to use pull down resistors at pins D0, D1, D2. It would not work without them.
What could be the problem? What am I doing wrong? Is there any other better way to scan the keys?
Thank You !