[SOLVED] [moved] How can I link keyboard matrix's input with a display device using C?

Status
Not open for further replies.

OneForSorrow

Newbie level 2
Joined
Jul 28, 2015
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
31
Greetings fellows, as the title says I want to create a program that can read the input of keyboard matrix control and afterwards I want to pass the data in a display device (e.g 7-segment display or Led Matrix). I have tried but it was in vain, here is the code.Note that I am working in Phillips 8051 microcontroller and the compiler I use is Keil uVision.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
//#include <reg51.h>//This is the library of Keil where the registers and port of 8051 are included and supported
 
int main()
{
    char input;//This is the input for our switch
    char P2;
 
    while(1)// An infinite loop in order to constantly get input for our code
    {
        scanf("%c",&input);//here we pass our input for our switch
 
        switch(input)
        {
            case '0' :
                     P2=0x00;//P2 is the port where the data for almost every display device is destined
                    break;
 
            case '1' :
                     P2=0x01;
                     break;
            case '2' :
                     P2=0x02;
                     break;
            case '3' :
                     P2=0x03;
                     break;
            case '4' :
                     P2=0x04;
                     break;
            case '5' :
                     P2=0x05;
                     break;
            case '6' :
                     P2=0x06;
                     break;
            case '7' :
                     P2=0x07;
                     break;
            case '8' :
                     P2=0x08;
                     break;
            case '9' :
                     P2=0x09;
                     break;
            case 'A' :
                     P2=0x0A;
                     break;
            case 'B' :
                     P2=0x0B;
                     break;
            case 'C' :
                     P2=0x0C;
                     break;
            case 'D' :
                     P2=0x0D;
                     break;
            case 'E' :
                     P2=0x0E;
                     break;
            case 'F' :
                     P2=0x0F;
                     break;
 
 
 
            default :
                     break;
 
        }
        return 0;
    }
}



Sorry for my bad English and bad format of the thread it is my first try.
Thank you for the time you've dedicated.:smile:
 
Last edited by a moderator:

I'm not familiar with this library, but the point about a matrix keyboard is that each row is energised one at a time (port outputs) and for each, all of the columns are scanned (port inputs) to see if a key has been pressed in that row (or vice-versa). Accordingly, the key representation must be worked out from the active row and detected column each time, unless you are using some sort of external keyboard controller which presents the key ID pre-packaged in ASCII or some parallel format. Your code doesn't appear to expect or control a matrix pad; I would say it is expecting serial ASCII.

OK. When you've actually worked out how to get your keypad read, you seem to be sending binary values to the display? If it's just a display without a decoder then you'll only drive some specific segments - it's not what you want to do if you're expecting to see recognisable numbers on the display. Your method will only work if the display is fed via a decoder - many of which aren't happy to show A-F as you hope. If you want to drive the display directly without a decoder, then you will need to change the output pattern to suit which segments you want to light. Your choice. The hardware implementation is up to you!

These are two separate problems: debugging will show you when you've read a good keypress (and it's value). Then concentrate on making the display do what you want it to.
 

This is a simple example of using the Keypad Library. It supports keypads with 1..4 rows and 1..4 columns. The code being returned by Keypad_Key_Click() function is in range from 1..16. In this example, the code returned is transformed into ASCII codes [0..9,A..F] and displayed on Lcd. In addition, a small single-byte counter displays in the second Lcd row number of key presses


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
unsigned short kp, cnt, oldstate = 0;
char txt[6];
 
// Keypad module connections
char  keypadPort at PORTD;
// End Keypad module connections
 
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
 
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
 
void main() {
  cnt = 0;                                 // Reset counter
  Keypad_Init();                           // Initialize Keypad                              
  ANSEL  = 0;                              // Configure AN pins as digital I/O
  ANSELH = 0;
  Lcd_Init();                              // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                     // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);                // Cursor off
  Lcd_Out(1, 1, "1");
  Lcd_Out(1, 1, "Key  :");                 // Write message text on LCD
  Lcd_Out(2, 1, "Times:");
 
  do {
    kp = 0;                                // Reset key code variable
 
    // Wait for key to be pressed and released
    do
      // kp = Keypad_Key_Press();          // Store key code in kp variable
      kp = Keypad_Key_Click();             // Store key code in kp variable
    while (!kp);
   // Prepare value for output, transform key to it's ASCII value
    switch (kp) {
      //case 10: kp = 42; break;  // '*'   // Uncomment this block for keypad4x3
      //case 11: kp = 48; break;  // '0'   
      //case 12: kp = 35; break;  // '#'
      //default: kp += 48;
 
      case  1: kp = 49; break; // 1        // Uncomment this block for keypad4x4
      case  2: kp = 50; break; // 2
      case  3: kp = 51; break; // 3
      case  4: kp = 65; break; // A
      case  5: kp = 52; break; // 4
      case  6: kp = 53; break; // 5
      case  7: kp = 54; break; // 6
      case  8: kp = 66; break; // B        
      case  9: kp = 55; break; // 7
      case 10: kp = 56; break; // 8
      case 11: kp = 57; break; // 9
      case 12: kp = 67; break; // C
      case 13: kp = 42; break; // *
      case 14: kp = 48; break; // 0
      case 15: kp = 35; break; // #
      case 16: kp = 68; break; // D
 
    }
 
    if (kp != oldstate) {                  // Pressed key differs from previous
      cnt = 1;
      oldstate = kp;
      }
    else {                                 // Pressed key is same as previous
      cnt++;
      }
 
    Lcd_Chr(1, 10, kp);                    // Print key ASCII value on LCD
 
    if (cnt == 255) {                      // If counter varialble overflow
      cnt = 0;
      Lcd_Out(2, 10, "   ");
      }
 
    WordToStr(cnt, txt);                   // Transform counter value to string
    Lcd_Out(2, 10, txt);                   // Display counter value on LCD
  } while (1);
}

 
Last edited by a moderator:

Re: [moved] How can I link keyboard matrix's input with a display device using C?

I tried to work things out with this. It's input is encoded and it happens to be the bits P1.3-P1.0 of Port 1. The sollution I worked is bad but it works I simply added this line of code P2=P1; meaning whatever value I press it passes to Port 2 which inputs the data of 7-segment display.

Unfortunately I don't have a schematic the all thing is that I have some assembly codes and my task is to create the similar things in C. P2 is the port where the 8051 model I use sends data in the display devices that are attached to it.

Thank you for your time but unfortunately I messed things a bit it want about ASCII. I had to find where does the input of keyboard matrix is send in order to identify their destination.
 
Last edited by a moderator:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…