The most classic SCM matrix scanner C Language

Source: Internet
Author: User

/*************************************** **************************************** ***********
Keyboard 3*4 Scanner
Entry parameters:
Exit parameters:
Key code: EE de be, 7E, Ed, DD, BD, 7d, EB, DB, BB, 7b
0 1 2 3 4 5 6 7 8 9 A B
**************************************** **************************************** *************/
Void delay (uchar D );
Uchar keyv ()
{Uchar readkey,;
P2 = 0xf8; // determine the row and column position
If (P2 = 0xf8)
Return (keynum); // if no key is set, 0 is returned.
If (P2 = 0xf8) // judge again. The purpose is to ensure that the detection is correct.
Return (keynum );
Else
{Delay (0); // call the latency function to shake the front key.
For (A = 0; A <3; A ++) // The following is the classic calculated key value (determine the location of the closed key)
{P2 = _ cror _ (0xfb, );
If (p24 = 0) break;
If (P25 = 0) break;
If (P26 = 0) break;
If (P2 = 0) break;
}
Readkey = P2; // obtain the key value
For (; P2! = 0xf8; P2 = 0xf8); // wait until the key is released to shake along the key
Flag = 1;
Return (readkey); // return the primary function with a key value
}
}
/*************************************** **************************************** **********
Calculate and return the number key-value module so that the buttons can be directly determined when the subsequent program calls the module.
Other branches can be executed.
**************************************** **************************************** ***********/
Uchar keycode (void)
{Uchar key_code;
While (1)
{
Key_code = keyv ();
Switch (key_code)
{
Case 0xEE: keynum = 0; break;
Case 0xde: keynum = 1; break;
Case 0xbe: keynum = 2; break;
Case 0x7e: keynum = 3; break;
Case 0xed: keynum = 4; break;
Case 0xdd: keynum = 5; break;
Case 0xbd: keynum = 6; break;
Case 0x7d: keynum = 7; break;
Case 0xeb: keynum = 8; break;
Case 0xdb: keynum = 9; break;
Case 0xbb: keynum = 0x0a; break;
Case 0x7b: keynum = 0x0b; break;
Default: break;
}
Return (keynum );
}
}

 

 

# Include

Unsigned char code table [] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, // 0 ~ 4
0x92,0x82, 0xf8, 0x80, 0x90, // 5 ~ 9
0x88,0x83, 0xa7, 0xa1, 0x86, 0x8e}; // ~ F

Void keyscan ();
Void delay10ms (unsigned char time );
Void dispaly (unsigned char K );

Unsigned char key, temp;

Void main () // main program
{
While (1)
{
Keyscan ();
}
}

Void keyscan () // a key scan subroutine
{
P1 = 0xff;
P1_3 = 0;
Temp = p1;
Temp & = 0xf0;
If (temp! = 0xf0)
{
Delay10ms (1 );
Temp = p1;
Temp & = 0xf0;
If (temp! = 0xf0)
{
Temp = p1;
Temp & = 0xf0;

Switch (temp)
{
Case 0x70:
Key = 1; break;
Case 0xb0:
Key = 2; break;
Case 0xd0:
Key = 3; break;
}
Dispaly (key );
}
}

P1 = 0xff;
P1_2 = 0;
Temp = p1;
Temp & = 0xf0;
If (temp! = 0xf0)
{
Delay10ms (1 );
Temp = p1;
Temp & = 0xf0;
If (temp! = 0xf0)
{
Temp = p1;
Temp & = 0xf0;

Switch (temp)
{
Case 0x70:
Key = 4; break;
Case 0xb0:
Key = 5; break;
Case 0xd0:
Key = 6; break;
}
Dispaly (key );
}
}

P1 = 0xff;
P1_1 = 0;
Temp = p1;
Temp & = 0xf0;
If (temp! = 0xf0)
{
Delay10ms (1 );
Temp = p1;
Temp & = 0xf0;
If (temp! = 0xf0)
{
Temp = p1;
Temp & = 0xf0;

Switch (temp)
{
Case 0x70:
Key = 7; break;
Case 0xb0:
Key = 8; break;
Case 0xd0:
Key = 9; break;
}
Dispaly (key );
}
}

P1 = 0xff;
P1_0 = 0;
Temp = p1;
Temp & = 0xf0;
If (temp! = 0xf0)
{
Delay10ms (1 );
Temp = p1;
Temp & = 0xf0;
If (temp! = 0xf0)
{
Temp = p1;
Temp & = 0xf0;

Switch (temp)
{
Case 0x70:
Key = 14; break;
Case 0xb0:
Key = 0; break;
Case 0xd0:
Key = 15; break;
}
Dispaly (key );
}
}
}

// Latency Program
Void delay10ms (unsigned char time)
{
Unsigned char A, B, C;
For (A = 0; A for (B = 0; B <10; B ++)
For (C = 0; C <120; C ++)
;
}

Void dispaly (unsigned char K) // display program
{
P0 = table [k];
P2_1 = 0;
}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.