docs

a slatepencil documentail site

View on GitHub

simple calculator

hardware

wire

matrix keyboard AT89S42
1,2,3,A P3.0
4,5,6,B P3.1
7,8,9,C P3.2
*,0,#,D P3.3
1,4,7,* P3.4
2,5,8,0 P3.5
3,6,9,# P3.6
A,B,C,D P3.7

code

/*******************************************
* Name: Matrix keyboard scan LED display
* OSC: 11.0592 MHz
4x4 matrix keyboard displayed as
1,2,3,A
4,5,6,B
7,8,9,C
E,0,F,D
*******************************************/
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int

uchar code table[17] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x00}; // 0~9 & A~F & "no display"

void delay(uint xms)
{
    uint i, j;
    for (i = xms; i > 0; i--)
        for (j = 112; j > 0; j--)
            ;
}

void display(uchar num)
{
    P0 = table[num];
}

void keyscan4x4()
{
    uchar temp, key;
    ///////////// Scan Line 1 //////////////////
    P3 = 0xfe; // 1111 1110 P3.0 as LOW
    temp = P3;
    temp = temp & 0xf0; // 1111 0000

    if (temp != 0xf0)
    {
        delay(10);
        temp = P3;
        temp = temp & 0xf0;
        if (temp != 0xf0)
        {
            temp = P3;
            switch (temp)
            {
            case 0xee: // 1110 1110
                key = 1;
                break;
            case 0xde: // 1101 1110
                key = 2;
                break;
            case 0xbe: // 1011 1110
                key = 3;
                break;
            case 0x7e: // 0111 1110
                key = 10;
                break;
            }
            while (temp != 0xf0)
            {
                temp = P3;
                temp = temp & 0xf0;
            }
            display(key);
        }
    }

    ////////// Line 2 scan ///////////////
    P3 = 0xfd; // 1111 1101 P3.1 as LOW
    temp = P3;
    temp = temp & 0xf0; // 1111 0000

    if (temp != 0xf0)
    {
        delay(10);
        temp = P3;
        temp = temp & 0xf0;
        if (temp != 0xf0)
        {
            temp = P3;
            switch (temp)
            {
            case 0xed: // 1110 1101
                key = 4;
                break;
            case 0xdd: // 1101 1101
                key = 5;
                break;
            case 0xbd: // 1011 1101
                key = 6;
                break;
            case 0x7d: // 0111 1101
                key = 11;
                break;
            }
            while (temp != 0xf0)
            {
                temp = P3;
                temp = temp & 0xf0;
            }
            display(key);
        }
    }

    ////////// Line 3 scan ///////////////
    P3 = 0xfb; // 1111 1011 P3.2 as LOW
    temp = P3;
    temp = temp & 0xf0; // 1111 0000

    if (temp != 0xf0)
    {
        delay(10);
        temp = P3;
        temp = temp & 0xf0;
        if (temp != 0xf0)
        {
            temp = P3;
            switch (temp)
            {
            case 0xeb: // 1110 1011
                key = 7;
                break;
            case 0xdb: // 1101 1011
                key = 8;
                break;
            case 0xbb: // 1011 1011
                key = 9;
                break;
            case 0x7b: // 0111 1011
                key = 12;
                break;
            }
            while (temp != 0xf0)
            {
                temp = P3;
                temp = temp & 0xf0;
            }
            display(key);
        }
    }

    ////////// Line 4 scan ///////////////
    P3 = 0xf7; // 1111 0111 P3.3 as LOW
    temp = P3;
    temp = temp & 0xf0; // 1111 0000

    if (temp != 0xf0)
    {
        delay(10);
        temp = P3;
        temp = temp & 0xf0;
        if (temp != 0xf0)
        {
            temp = P3;
            switch (temp)
            {
            case 0xe7: // 1110 0111
                key = 14;
                break;
            case 0xd7: // 1101 0111
                key = 0;
                break;
            case 0xb7: // 1011 0111
                key = 15;
                break;
            case 0x77: // 0111 0111
                key = 13;
                break;
            }
            while (temp != 0xf0)
            {
                temp = P3;
                temp = temp & 0xf0;
            }
            display(key);
        }
    }
}

void main()
{
    P2 = P2 | 0xf0;
    while (1)
    {
        keyscan4x4();
    }
}