Keypads are a part of HMI or Human Machine Interface and play really important role in a small embedded system where human interaction or human input is needed. Martix keypads are well known for their simple architecture and ease of interfacing with any microcontroller. In this part of tutorial we will learn how to interface a 4x4 matrix keypad with 8051 microcontroller. Also we will see how to program then in Embedded C language.
Normally the port pin is high but when a switch is pressed the controller pin gets a Low signal and we come to know that a switch has been pressed. One end of switch is connected to the port pin whereas the other end is connected to the Ground.
The adjoining figure shows the diagram of a matrix keypad and how it is interfaced with the controller.
As you can see no pin is connected to ground, over here the controller pin itself provides the ground. We pull one of the Column Pins low & check the row pins if any of the Pin is low then we come to know which switch is pressed.
Suppose we make column 1 pin low and while checking the rows we get Row 3 is low then we come to know switch 7 has been pressed.
Algorithm:
1. Start.
2. Make All Pins High.
3. Make Column 1 pin low.
4. Check if Row 1 is low, if yes then Switch 1 has been pressed.
5. Check if Row 2 is low, if yes then Switch 4 has been pressed.
6. Check if row 3 is low if yes then Switch 7 has been pressed.
7. Check if row 4 is low if yes then Switch 10 has been pressed.
8. Make Column 1 Pin high & Column 2 Pin Low.
9. Check if Row 1 is low, if yes then Switch 2 has been pressed.
10. Check if Row 2 is low, if yes then Switch 5 has been pressed.
11. Check if row 3 is low if yes then Switch 8 has been pressed.
12. Check if row 4 is low if yes then Switch 11 has been pressed.
13. Make Column 2 Pin high & Column 3 Pin Low.
14. Check if Row 1 is low, if yes then Switch 3 has been pressed.
15. Check if Row 2 is low, if yes then Switch 6 has been pressed
16. Check if row 3 is low if yes then Switch 9 has been pressed.
17. Check if row 4 is low if yes then Switch 12 has been pressed.
18. Make column 3 pin high.
19. Stop.
Please make sure at any time only one column Pin should remain Low.
Program to implement switch matrix
4*4 switch matrix
Normally the port pin is high but when a switch is pressed the controller pin gets a Low signal and we come to know that a switch has been pressed. One end of switch is connected to the port pin whereas the other end is connected to the Ground.
Interfacing:
In case of matrix Keypad, both the ends of switches are connected to the port Pin. Over here we have considered a 4x4 matrix keypad i.e. four rows and three columns. So in all twelve switches have been interfaced using just seven lines.The adjoining figure shows the diagram of a matrix keypad and how it is interfaced with the controller.
Figure 2: Connection with ports
As you can see no pin is connected to ground, over here the controller pin itself provides the ground. We pull one of the Column Pins low & check the row pins if any of the Pin is low then we come to know which switch is pressed.
Suppose we make column 1 pin low and while checking the rows we get Row 3 is low then we come to know switch 7 has been pressed.
Algorithm:
1. Start.
2. Make All Pins High.
3. Make Column 1 pin low.
4. Check if Row 1 is low, if yes then Switch 1 has been pressed.
5. Check if Row 2 is low, if yes then Switch 4 has been pressed.
6. Check if row 3 is low if yes then Switch 7 has been pressed.
7. Check if row 4 is low if yes then Switch 10 has been pressed.
8. Make Column 1 Pin high & Column 2 Pin Low.
9. Check if Row 1 is low, if yes then Switch 2 has been pressed.
10. Check if Row 2 is low, if yes then Switch 5 has been pressed.
11. Check if row 3 is low if yes then Switch 8 has been pressed.
12. Check if row 4 is low if yes then Switch 11 has been pressed.
13. Make Column 2 Pin high & Column 3 Pin Low.
14. Check if Row 1 is low, if yes then Switch 3 has been pressed.
15. Check if Row 2 is low, if yes then Switch 6 has been pressed
16. Check if row 3 is low if yes then Switch 9 has been pressed.
17. Check if row 4 is low if yes then Switch 12 has been pressed.
18. Make column 3 pin high.
19. Stop.
Please make sure at any time only one column Pin should remain Low.
Program to implement switch matrix
4*4 switch matrix
#include"REG52.h" sbit R1=P1^0; sbit R2=P1^1; sbit R3=P1^2; sbit R4=P1^3; sbit C1=P1^4; sbit C2=P1^5; sbit C3=P1^6; sbit C4=P1^7; unsigned char switch1(void); void main() { unsigned char sw; P2=0; while(1) { sw= switch1(); if(sw!=0) P2=sw; } } unsigned char switch1(void) { R1=R2=R3=R4=1; C1=C2=C3=C4=0; if((R1==0)||(R2==0)||(R3==0)||(R4==0)) { R1=R2=R3=R4=1; C1=C2=C3=C4=1; C1=0; if (R1==0) return(1); if (R2==0) return(2); if (R3==0) return(3); if (R4==0) return(4); C1=1; C2=0; if (R1==0) return(5); if (R2==0) return(6); if (R3==0) return(7); if (R4==0) return(8); C2=1; C3=0; if (R1==0) return(9); if (R2==0) return(10); if (R3==0) return(11); if (R4==0) return(12); C3=1; C4=0; if (R1==0) return(13); if (R2==0) return(14); if (R3==0) return(15); if (R4==0) return(16); C4=1; } else return (0); }
Comments
Post a Comment