Thread: LCD Menu integration with 4x4 keypad (c programming)

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    LCD Menu integration with 4x4 keypad (c programming)

    Hi,

    I've been working on a project using MPLAB IDE v8.36.

    I'm trying to make a menu using the keypad for eg,

    1 pressed - measure heart rate
    2 pressed - exit

    We can't get the output when we press the allocated key.

    Here is our programme code below:

    Code:
    #include <p18f4520.h>
    #include <delays.h>
    
    unsigned int LCD(void);
    unsigned int EXIT(void);
    
    void Init_LCD(void);
    void w_ctr_8bit(char);
    void w_data_8bit(char);
    void Delay_1kcyc(void);
    
    #define LCD_DATA PORTD
    #define LCD_RS PORTBbits.RB1
    #define LCD_E PORTBbits.RB2
    
    unsigned char LCD_TEMP, i,j,k,m;
    char MESS[16]=" WELCOME TO ECG ";
    char MESS2[16]="1.MEASURE 2.EXIT";
    
    void main()
    
    {
    unsigned int x, y, value;
    ADCON1=0x0F;
    TRISA=0xFF;
    TRISB=0;
    TRISD=0;
    y=value;
    PORTD=1;
    Init_LCD();
    w_ctr_8bit(0b10000000);
    for (i=0; i<16; i++)
    w_data_8bit(MESS[i]);
    
    w_ctr_8bit(0b11000000);
    for (i=0; i<16; i++)
    w_data_8bit(MESS2[i]);
    
    LCD();
    
    while(1)
    { if (value==0b00000001)
    for(i=0;i<16;i++)
    {
    
    unsigned int EXIT();
    }
    }
    
    
    }
    
    
    
    
    
    
    unsigned int LCD(void)
    {
    char value, keyInputs;
    while(PORTAbits.RA4==0);
    keyInputs = PORTA & 0x0F;
    switch(keyInputs)
    {
    case 0: value='1';w_data_8bit(value);break;
    case 1: value='2';w_data_8bit(value);break;
    case 2: value='3';w_data_8bit(value);break;
    case 3: value='F';w_data_8bit(value);break;
    case 4: value='4';w_data_8bit(value);break;
    case 5: value='5';w_data_8bit(value);break;
    case 6: value='6';w_data_8bit(value);break;
    case 7: value='E';w_data_8bit(value);break;
    case 8: value='7';w_data_8bit(value);break;
    case 9: value='8';w_data_8bit(value);break;
    case 10: value='9';w_data_8bit(value);break;
    case 11: value='D';w_data_8bit(value);break;
    case 12: value='A';w_data_8bit(value);break;
    case 13: value='0';w_data_8bit(value);break;
    case 14: value='B';w_data_8bit(value);break;
    case 15: value='C';w_data_8bit(value);break;
    }
    Delay10KTCYx(50);
    return(value);
    }
    
    void Init_LCD()
    {
    w_ctr_8bit(0b00111000);
    w_ctr_8bit(0b00001100);
    w_ctr_8bit(0b00000110);
    w_ctr_8bit(0b00000001);
    w_ctr_8bit(0b00000010);
    w_ctr_8bit(0b10000010);
    }
    
    void w_ctr_8bit(char w)
    {
    LCD_RS = 0;
    LCD_E = 1;
    LCD_DATA = w;
    LCD_E = 0;
    Delay10TCYx(50);
    }
    
    void w_data_8bit(char w)
    {
    LCD_RS = 1;
    LCD_E = 1;
    LCD_DATA = w;
    LCD_E = 0;
    Delay10TCYx(50);
    }
    
    unsigned int EXIT(void)
    {
    char MESS3[16]="THANK YOU FOR";
    char MESS4[16]="USING ECG, BYE!";
    
    w_ctr_8bit(0b10000000);
    for (i=0; i<16; i++)
    w_data_8bit(MESS3[i]);
    
    w_ctr_8bit(0b11000000);
    for (i=0; i<16; i++)
    w_data_8bit(MESS4[i]);
    }
    We would appreciate help. Thanks ..
    Last edited by Salem; 02-05-2010 at 03:04 PM. Reason: Removed useless tags

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First thing is learning about formatting code.
    Code:
    #include <p18f4520.h>
    #include <delays.h>
    
    unsigned int LCD(void);
    unsigned int EXIT(void);
    
    void Init_LCD(void);
    void w_ctr_8bit(char);
    void w_data_8bit(char);
    void Delay_1kcyc(void);
    
    #define LCD_DATA PORTD
    #define LCD_RS PORTBbits.RB1
    #define LCD_E PORTBbits.RB2
    
    unsigned char LCD_TEMP, i,j,k,m;
    char MESS[16]=" WELCOME TO ECG ";
    char MESS2[16]="1.MEASURE 2.EXIT";
    
    void main()
    {
      unsigned int x, y, value;
      ADCON1=0x0F;
      TRISA=0xFF;
      TRISB=0;
      TRISD=0;
      y=value;
      PORTD=1;
    
      Init_LCD();
    
      w_ctr_8bit(0b10000000);
      for (i=0; i<16; i++)
        w_data_8bit(MESS[i]);
    
      w_ctr_8bit(0b11000000);
      for (i=0; i<16; i++)
        w_data_8bit(MESS2[i]);
    
      LCD();
    
      while(1)
      { 
        if (value==0b00000001)
          for(i=0;i<16;i++)
          {
            unsigned int EXIT();
          }
      }
    }
    
    unsigned int LCD(void)
    {
      char value, keyInputs;
      while(PORTAbits.RA4==0);
      keyInputs = PORTA & 0x0F;
      switch(keyInputs)
      {
      case 0: value='1';w_data_8bit(value);break;
      case 1: value='2';w_data_8bit(value);break;
      case 2: value='3';w_data_8bit(value);break;
      case 3: value='F';w_data_8bit(value);break;
      case 4: value='4';w_data_8bit(value);break;
      case 5: value='5';w_data_8bit(value);break;
      case 6: value='6';w_data_8bit(value);break;
      case 7: value='E';w_data_8bit(value);break;
      case 8: value='7';w_data_8bit(value);break;
      case 9: value='8';w_data_8bit(value);break;
      case 10: value='9';w_data_8bit(value);break;
      case 11: value='D';w_data_8bit(value);break;
      case 12: value='A';w_data_8bit(value);break;
      case 13: value='0';w_data_8bit(value);break;
      case 14: value='B';w_data_8bit(value);break;
      case 15: value='C';w_data_8bit(value);break;
      }
      Delay10KTCYx(50);
      return(value);
    }
    
    void Init_LCD()
    {
      w_ctr_8bit(0b00111000);
      w_ctr_8bit(0b00001100);
      w_ctr_8bit(0b00000110);
      w_ctr_8bit(0b00000001);
      w_ctr_8bit(0b00000010);
      w_ctr_8bit(0b10000010);
    }
    
    void w_ctr_8bit(char w)
    {
      LCD_RS = 0;
      LCD_E = 1;
      LCD_DATA = w;
      LCD_E = 0;
      Delay10TCYx(50);
    }
    
    void w_data_8bit(char w)
    {
      LCD_RS = 1;
      LCD_E = 1;
      LCD_DATA = w;
      LCD_E = 0;
      Delay10TCYx(50);
    }
    
    unsigned int EXIT(void)
    {
      char MESS3[16]="THANK YOU FOR";
      char MESS4[16]="USING ECG, BYE!";
    
      w_ctr_8bit(0b10000000);
    
      for (i=0; i<16; i++)
        w_data_8bit(MESS3[i]);
    
      w_ctr_8bit(0b11000000);
    
      for (i=0; i<16; i++)
        w_data_8bit(MESS4[i]);
    }
    Next, line 47 is a prototype for EXIT(), not a call to EXIT()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    Thanks for replying. So how do I call it as a function instead of a prototype?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Look at the other examples of calling functions that you've already managed.

    What an odd thing to say, it's like you've been given this code and told to fix it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    Salem, some qns abt keypad

    Hi Salem,

    In the above C code, is debouncing not required for the 4x4 keypad? why is it so?

    i'm also intending to use a 4x4 matrix keypad for my project and i really wish to learn how to use it well.

    Do u mind sparing some time to explain the keypad?

    Your help will be greatly appreciated.Thks

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Not my code, not my hardware - how would I know.

    Maybe it's debounced in hardware and maybe it isn't.
    That's down to you to figure out from reading the hardware specs.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM

Tags for this Thread