Quantcast
Viewing all articles
Browse latest Browse all 215

Interfacing keypad with ARM7 MIcrocontroller

Image may be NSFW.
Clik here to view.
Keypad-interface-with-ARM7-Microcontroller-LPC2124
Keypad interface with ARM7 Microcontroller
Keypads are the most commonly used input device in many embedded system since it possess simple design and also comes at affordable cost. This makes the concept of keypad interfacing with a Microcontroller very important. We are about to see the tutorial of interfacing keypad with the ARM7 Microcontroller ( LPC2124).


The above design uses a 4x3 keypad to take input and a 16x2 LCD to display the input value fed by the keypad. If you are not familiar with LCD interfacing with ARM controller kindly go through this tutorial  "16x2 LCD interface with ARM7 Microcontroller"


POLLING:

Keypad interfacing can be done with the Microcontroller by means polling and interrupts. Since interrupts will not fit when interfacing large number of keys so polling becomes the ultimate choice. Polling is nothing but making the rows of the keypad as input and column as output or vice versa.

In this method each row will be made low one at a time and the corresponding logic transition in column value will be monitored by the Microcontroller. So for example if key '5' pressed at any moment the Microcontroller will realize this when row 2 and column 2 is low at same instant. Note that you can monitor either row or column with the Microcontroller for logic transition.

4x3 KEYPAD: 

The above diagram shows the construction of a simple keypad using push buttons. Whereas i have used a ready made 4x3 keypad in the above design, you can opt for any of it. You can also expand the keypad configuration by adding more switches to it.

CODE:

The below code was built using Keil uVision 4 and built in such a way to scan the keypad and display the input values in the LCD connected to the Controller.

  1. #include<lpc21xx.h>
  2. void delay(void);
  3. void lcd(unsigned char,int b);
  4. char keypad(void);
  5. char c;
  6. int main(void)
  7. {
  8.    PINSEL0=PINSEL2=0;
  9.    IODIR0=0x000003ff;
  10.    IODIR1=0x00780000;
  11.    lcd(0x38,0);
  12.    lcd(0x0f,0);
  13.     while(1)
  14.      {
  15.        c=keypad();                            //Obtaining values from keypad
  16.        lcd(c,1);
  17.       }
  18. }

  19. void lcd(unsigned char a,int b)         //LCD Subroutine
  20.   {
  21.     IOSET0=a<<0;
  22.     IOSET0=b<<8;
  23.     IOSET0=1<<9;
  24.     delay();
  25.     IOCLR0=1<<9;
  26.     IOCLR0=b<<8;
  27.     IOCLR0=a<<0;
  28.   }

  29. char keypad(void)                         //Keypad Scan
  30.  {  
  31.    while(1)
  32.    {
  33.       IOCLR1|=(1<<19);               //Making row1 LOW
  34.       IOSET1|=(1<<20)|(1<<21)|(1<<22); //Making rest of the rows '1'
  35.       if(!(IOPIN1&(1<<16)))             //Scan for key press
  36.        {
  37.         while(!(IOPIN1&(1<<16)));
  38.         return '1';                           //Returning value to display
  39.        }
  40.       if(!(IOPIN1&(1<<17)))
  41.        {
  42.          while(!(IOPIN1&(1<<17)));
  43.          return '2';
  44.        }
  45.       if(!(IOPIN1&(1<<18)))
  46.        {
  47.          while(!(IOPIN1&(1<<18)));
  48.          return '3';
  49.        }

  50.       IOCLR1|=(1<<20);
  51.       IOSET1|=(1<<21)|(1<<22)|(1<<19);
  52.       if(!(IOPIN1&(1<<16)))
  53. {
  54.         while(!(IOPIN1&(1<<16)));
  55.         return '4';
  56.       }
  57.       if(!(IOPIN1&(1<<17)))
  58. {
  59.         while(!(IOPIN1&(1<<17)));
  60.         return '5';
  61.      }
  62.       if(!(IOPIN1&(1<<18)))
  63. {
  64.         while(!(IOPIN1&(1<<18)));
  65.         return '6';
  66.      }

  67.       IOCLR1|=(1<<21);
  68.       IOSET1|=(1<<22)|(1<<20)|(1<<19);
  69.       if(!(IOPIN1&(1<<16)))
  70. {
  71.         while(!(IOPIN1&(1<<16)));
  72.         return '7';
  73.      }
  74.       if(!(IOPIN1&(1<<17)))
  75. {
  76.        while(!(IOPIN1&(1<<17)));
  77.        return '8';
  78.     }
  79.       if(!(IOPIN1&(1<<18)))
  80. {
  81.         while(!(IOPIN1&(1<<18)));
  82.         return '9';
  83. }

  84.       IOCLR1|=(1<<22);
  85.       IOSET1|=(1<<19)|(1<<20)|(1<<21);
  86.       if(!(IOPIN1&(1<<16)))
  87. {
  88.         while(!(IOPIN1&(1<<16)));
  89.         return '*';
  90. }
  91.       if(!(IOPIN1&(1<<17)))
  92. {
  93.         while(!(IOPIN1&(1<<17)));
  94.         return '0';
  95. }
  96.       if(!(IOPIN1&(1<<18)))
  97. {
  98.         while(!(IOPIN1&(1<<18)));
  99.         return '#';
  100.    }
  101. }
  102.  
  103. void delay(void)                                    //Delay loop
  104. {
  105.   unsigned int i;
  106.   for(i=0;i<=20000;i++);
  107. }

Like this article? Why not share it with others through social sites. We will post more of these ARM tutorials here , Follow/Subscribe to let us deliver these articles directly into your inbox/ timeline. Have queries? Feel free to post it below, we are happy to help you.  

Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 215

Trending Articles