correct my own 3x4 keypad C program

Status
Not open for further replies.

kgavionics

Full Member level 3
Joined
Jun 12, 2012
Messages
167
Helped
7
Reputation
14
Reaction score
11
Trophy points
1,298
Location
Alberta.Canada
Visit site
Activity points
2,482
Hi guys
i'm trying to build a C program to scan my 3x4 matrix keypad.Before i describe the problem i want to let you know about the connection i made between the pic18f452 and the keypad.
The 3 rows are connected to PORTE (RE0 to RE2) and the 4 rows are connected to PORTC ( RC0 to RC3) .The rows and columns are connected to Vcc through 10k pull up resistors.

this is my code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <p18f452.h>
 
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF

#define column PORTE
#define row     PORTC
unsigned char highn;
unsigned char lown;
unsigned char keypress;

void main (void)

{
TRISB=0;
while (1)
{

TRISE=0;    //set column as outputs
TRISC=1;   //set row bits as inputs
column=0; // set column bits to 0
lown=row; //read rows
lown=lown<<1; // to get the 4 bits  lower nibbles
TRISE=1;  //set colum as inputs
TRISC=0;   //set row bits as outputs
row=0;     // set row bits to 0
highn=column; // read columns bits
highn =highn & 0x0f; // mask the higher bits
highn=highn<<4; // make lower bits as higher bits
keypress=lown | highn; // combine rows and columns
PORTB=keypress; // send the keypress to portb
}
}

My problem, when i press a key , nothing happens and the leds connected to PORTB remains off.
can someone help me please and tell me what's wrong with my program?
thanks in advance
 

Re: Help me to correct my own 3x4 keypad C program

Instead of a classical implementation wich performs scan at each line, you can adopt the following conception bellow:

At Keyboard.c file:

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
/* ********************************************************************** */
#define ROW_MASK        0x0F
#define COLUMN_MASK     0xF0
#define NO_KEY          0x00
/* ********************************************************************** */
unsigned int ScanKey ( void )
{
unsigned int ReadingKey                                ;
TRISx = COLUMN_MASK                                    ;  // Set high nible as Input and low nible as Output
PORTx = ROW_MASK                                       ;  // Output "1" value for low nible
Delay ( STEADY_OUTPUT )                                ;  // wait a few for charging parasitic capacitance on Port
ReadingKey = (~( PORTx | COLUMN_MASK ))                ;  // Read only high nible at "ReadingKey"
if ( ReadingKey )                                      ;  // If some key were pressed...
   {
   TRISx = ~COLUMN_MASK                                ;  // Set low nible as Input and high nible as Output
   PORTx =  COLUMN_MASK                                ;  // Output "1" value for high  nible
   Delay ( STEADY_OUTPUT )                             ;  // wait a few for charging parasitic capacitance on Port
   ReadingKey |= (~( PORTx | ROW_MASK ))               ;  // Read only low  nible at "ReadingKey" ( but now, add to previous read  )
   return ( ReadingKey )                               ;
   }
   else return ( NO_KEY )                              ;  // ...otherwise, returns "0"
}
/* ********************************************************************** */
//            KEY DEFINITIONS
//            ---------------
//
//          Px.4 Px.5 Px.6 Px.7
//                          
// Px.0    --0----1----2----3-                        
//           |    |    |    |
// Px.1    --4----5----6----7-
//           |    |    |    |     ROW_MASK ( 00001111 )
// Px.2    --8----9----A----B-
//           |    |    |    |
// Px.3    --C----D----E----F-
//
//             COLUMN_MASK ( 11110000 )
//
//
//
//           KEY MAPPING 
//           -----------
//
//   "0"    [ BIT0 + 0    + 0    + 0    + BIT4 + 0    + 0    + 0    ]
//   "1"    [ BIT0 + 0    + 0    + 0    + 0    + BIT5 + 0    + 0    ]
//   "2"    [ BIT0 + 0    + 0    + 0    + 0    + 0    + BIT6 + 0    ]
//   ...    [                        ...                            ]
//   "E"    [ 0    + 0    + 0    + BIT3 + 0    + 0    + BIT6 + 0    ]
//   "F"    [ 0    + 0    + 0    + BIT3 + 0    + 0    + 0    + BIT7 ]
//   ...
//   "0|3"  [ BIT0 + 0    + 0    + 0    + BIT4 + 0    + 0    + BIT7 ]
//   "4|6"  [ 0    + BIT1 + 0    + 0    + BIT4 + 0    + BIT6 + 0    ]
//
//    BITn = ( 1 << n )


This code brings some advantages to classical approach:
  • Allows reading combinations of multiple keys pressed.
  • A little faster, due just 2 nibble scans are performed, instead 16 bit scan, performed on the other.




+++
 
Last edited:

Re: Help me to correct my own 3x4 keypad C program

Thank guys for help.thank you nick703 for the link, it's too much complicated to understand.
the andre_temprom is more interesting because it looks much simpler, but i have question: the delay( STEADY_OUTPUT) function it's just a simple delay function that i must implement?
-DO i need pull ups resistor on every port pin?
can you clearly this for me please ?

Thanks in advance
 

Re: Help me to correct my own 3x4 keypad C program

...the delay( STEADY_OUTPUT) function it's just a simple delay function that i must implement?
This command is just an empirical arrange that was originally required on 51 core and I never removed it, but you can try.

...DO i need pull ups resistor on every port pin?...
No, on PIC core, with rare exceptions, outputs are not open-drain type.


+++
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…