1. You can now buy finished microcontroller project from us, Check out the Store for the complete list of projects.
  2. Need a custom project, Send us some details about your project. So that we can quote the price for it.

Keypad interfacing with ARM7 lpc2148

Discussion in 'Project Doubts' started by mithil_j, Jan 31, 2013.

  1. mithil_j

    mithil_j New Member

    Likes Received:
    0
    Trophy Points:
    0
    I wrote a program for interfacing of keypad and lcd with ARM7 lpc2148. But the problem is its displaying only one row. Means if it enters the loop in which ROW 0 is grounded then it will display only keys 0,1,2,3 for any key pressed. If I deactivate that loop and wtever next loop is there here it will be ROW1 grounded, then it will display keys that is 4,5,6,7 for any key pressed. Here is my code, anyone please help me.

    Code (Text):
    1.  
    2. #include <lpc22xx.h>
    3. #define O1 0X00038000
    4. #define O2 0X00034000
    5. #define O3 0X0002C000
    6. #define O4 0X0001C000
    7. #define CLR 0x0003C000
    8. #define col0 0x00
    9. #define col1 0x01
    10. #define col2 0x02
    11. #define col3 0x03
    12.  
    13. void delay(int);
    14. void init_lcd();
    15. void Delay(unsigned long b);
    16. void write_command(int cmd);
    17. void write_data(int dat);
    18. void lcd_data(int l);
    19. void lcd_command(char cmd);
    20.  /* printlcd (int keypad[int m][int n])
    21. //int m,n;
    22. {
    23.    int m,n;
    24.    lcd_data(*(*(keypad+m)+n));
    25.   Delay(20000);
    26.  
    27.  } */
    28.   printlcd (unsigned int x)
    29. //int m,n;
    30. {
    31. //   int m,n;
    32.    lcd_data(x);
    33.   Delay(20000);
    34.  
    35.  }  
    36.  
    37.  
    38. int main()
    39. {
    40.     int keypad [4] [4] = {'0', '1', '2', '3', '4', '5', '6', '7',
    41.         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    42.     unsigned int row,column;
    43.     IODIR0 = 0x0003C000;
    44.      init_lcd();
    45.     //IOCLR0 = 0x0003FC00;  /* Clear all the port pins*/
    46.     //IOCLR0 = 0x00003C00;   /* P0.13, P0.12, P0.11 and P0.10 are input as COLUMNS */
    47.     //IOSET0 = 0x0003C000;  /* P0.17,P0.16,P0.15,P0.14  AS OUTPUT as ROWS*/
    48.    
    49.     while(1)
    50.     {
    51.      do
    52.    {
    53.     IO0CLR = 0x0003C000;  //Ground all the rows at once
    54.        column = IOPIN0;
    55.        column &= 0X00003C00; //MASK USED BITS
    56.    } while(column != 0X00003C00); //CHECK UNTIL ALL KEYS ARE OPEN
    57.    
    58.      do
    59.     {
    60.     do
    61.      {
    62.       delay(20);    //Call delay
    63.       column = IOPIN0;  // See if any key is pressed
    64.       column &= 0X00003C00;// Mask unused bits
    65.      } while(column == 0X00003C00); // Keep checking for key press
    66.     delay (20);
    67.     column = IOPIN0;     //Read columns
    68.     column &= 0X00003C00;    // mask unused bits
    69.     } while (column == 0X00003C00);  // Wait for Keypress
    70.    
    71.     while(1)
    72.      {
    73.       IOSET0 = 01;     //ground row0
    74.       column = IOPIN0;   //Read colums
    75.       column &= 0X00003C00;  //Mask unused bits
    76.       if(column != 0X00003C00) //Column detected
    77.        {
    78.         row = 0;    // Save row lacation
    79.         break;     // Exit while loop
    80.        }  
    81.      
    82.       IOSET0 = 02;    // Ground row1
    83.       column = IOPIN0;   // Read columns      
    84.       column &= 0X00003C00;  //Mask unused bits
    85.       if(column != 0X00003C00) //Column detected
    86.        {
    87.         row = 1;    // Save row lacation
    88.         break;     // Exit while loop
    89.        }
    90.       IOSET0 = 03;    // Ground row2
    91.       column = IOPIN0;   // Read columns      
    92.       column &= 0X00003C00;  //Mask unused bits
    93.       if(column != 0X00003C00) //Column detected
    94.        {
    95.         row = 2;    // Save row lacation
    96.         break;     // Exit while loop
    97.        }
    98.       IOSET0 = 04;    // Ground row3
    99.       column = IOPIN0;   // Read columns      
    100.       column &= 0X00003C00;  //Mask unused bits
    101.       if(column != 0X00003C00) //Column detected
    102.        
    103.         row = 3;    // Save row lacation
    104.         break;     // Exit while loop
    105.        
    106.      }
    107.     if (column == 0x00003800)
    108.      printlcd(keypad [row] [col0]);
    109.     else if(column == 0x00003400)
    110.      printlcd(keypad [row] [col1]);
    111.     else if (column == 0x00002C00)
    112.     printlcd (keypad [row] [col2]);
    113.     else if (column == 0x00001C00)
    114.      printlcd(keypad [row] [col3]);
    115.    }
    116. }
    117.  void delay(int n)      /* generates one milisecond delay  */
    118.   {
    119.    int i,j;
    120.    for (i=1; i<=n; i++);
    121.    for(j=0; j<=100000; j++);
    122.   }
    123. void  init_lcd() {            
    124.   IO1DIR  |= 0x00FE0000;    
    125.   Delay(200000) ;
    126.   write_command(0x30 << 16);
    127.   Delay(100000);
    128.   write_command(0x30 << 16);
    129.   Delay(100000);
    130.   write_command(0x30 << 16);
    131.   Delay(100000);
    132.   write_command(0x20 << 16);
    133.   lcd_command(0x01);                          /* clear display */
    134.   lcd_command(0x06);                          /* auto address inc */
    135.   lcd_command(0x0c);                          /* cursor off */
    136.   lcd_command(0x80);                          /* first location */
    137.   }
    138. void write_command(int cmd) {
    139.   IO1CLR  |= 0x00f00000;                  /* Clear D4-D7  */
    140.   IO1CLR  |= 0x00040000;                 /* Read/Write = 0     */
    141.   IO1CLR  |= 0X00020000;                    /* Register Select = 0,Command */
    142.   IO1SET  |= 0x00f00000 & cmd;        /* Set D4-D7    */
    143.   IO1SET  |= 0X00080000;                 /* Enable = 1      */  
    144.   Delay(30000);
    145.   IO1CLR  |= 0x00080000;                  /* set E to low       */
    146.   }
    147.  void write_data(int dat) {
    148.   IO1CLR  |= 0x00f00000;                  /* Clear D4-D7  */
    149.   IO1CLR  |= 0x00040000;      /* Read/Write = 0    */
    150.   IO1SET  |= 0X00020000;        /* Register Select = 1,Data */
    151.   IO1SET  |= 0x00f00000 & dat;     /* Set D4-D7    */
    152.   IO1SET  |= 0X00080000;     /* Enable = 1     */
    153.   Delay(30000);               //delay ~2ms
    154.   IO1CLR  |= 0x00080000;      /* Set E to low      */
    155.   }
    156. void lcd_data(int dat)
    157. {
    158.   write_data(dat << 16);
    159.   write_data(dat << 20);
    160.   }
    161. void lcd_command(char cmd)
    162. {
    163.   write_command(cmd << 16);
    164.   write_command(cmd << 20);
    165.   }
    166.  
    167.  
    168.  void Delay(unsigned long b)
    169. {
    170.   while (--b!=0);  
    171.  }
    172.  
    173.  
    174.  
    175.  
    Tags:
Loading...

Share This Page

Loading...