Source Code File:"keypad.c"

//////////////////////////////////////////////
// Matrix Keypad Scanning Routine
//                       
// Author(s): David Hobday
// Date 11 December 2006
//
// Revision History:
// V1.0 11/12/2006 - Initial Release
//
//////////////////////////////////////////////

// Code written for Accord keypad AK-304-BBW
// with key layout as below:
// COL1 COL2 COL3
//   1    2    3   ROW 1
//   4    5    6   ROW 2
//   7    8    9   ROW 3
//   *    0    #   ROW 4
//

#include <system.h>

// define keypad connections
volatile bit row1port @PORTC.1;
volatile bit row1tris @TRISC.1;
volatile bit row2port @PORTC.6;
volatile bit row2tris @TRISC.6;
volatile bit row3port @PORTC.5;
volatile bit row3tris @TRISC.5;
volatile bit row4port @PORTC.3;
volatile bit row4tris @TRISC.3;

volatile bit col1port @PORTC.2;
volatile bit col1tris @TRISC.2;
volatile bit col2port @PORTC.0;
volatile bit col2tris @TRISC.0;
volatile bit col3port @PORTC.4;
volatile bit col3tris @TRISC.4;


rom char* keyPadMatrix = 
{ 
    '1','2','3',
    '4','5','6',
    '7','8','9',
    '*','0','#',
    0xFF
};

void ScanKeyMatrixInit()
{
    // we scan the keypad by turning on the row outputs and 
    // reading the columns 
    row1tris = 0;
    row2tris = 0;
    row3tris = 0;
    row4tris = 0;
    col1tris = 1;
    col2tris = 1;
    col3tris = 1;
}

char ScanKeyMatrix()
{
    // This routine returns the first key found to be
    // pressed during the scan.
    char key = 0, row;
    
    for( row = 0b00000001; row < 0b00010000; row <<= 1 )
    {       
        {   // turn on row output
            row1port = row.0;
            row2port = row.1;
            row3port = row.2;
            row4port = row.3;
        }
        
        // read colums - break when key press detected
        if( col1port )
            break;
        key++;
        if( col2port )
            break;
        key++;
        if( col3port )
            break;
        key++;
    }

    row1port = 0;
    row2port = 0;
    row3port = 0;
    row4port = 0;
        
    return keyPadMatrix[ key ]; 
}


http://www.sourceboost.com

Copyright © 2006 SourceBoost Technologies