51 single-chip microcomputer (AT89S52) matrix keyboard detection program

Source: Internet
Author: User

Matrix keyboard detection ideas: Set the line to output, set the line to input to get the column Code, set the line to input, and set the line to output to get the line code, the key value is determined by the row and column code.

This program can be improved: In the key state machine, the waiting button release statement is moved to the third State, which can further improve the efficiency of the Key Scan program.

# Include <reg52.h> # define uchar unsigned char # define uint unsigned intsbit dula = P3 ^ 0; sbit wela = P3 ^ 1; # define keyport (P1) # define keystate0 0 // key confirmation status # define keystate1 1 // The Key has no action status char keystate = 1; // The key status is initialized to uchar keynum = 0; void delayms (uint x) // 1 ms {uint y, z; for (y = x; y> 0; y --) for (z = 111; z> 0; z --);} uchar code table [] = {// co-yang digital tube 0xc0, 0xf9, 0xa4, 0xb0, 0x99,0x92,0x82, 0xf8, 0x80, 0x90, 0x88,0x83, 0xc6, 0xa1, 0x86, 0x8e }; Void smg () {dula = 0; wela = 0; P2 = 0x01; wela = 1; wela = 0; P2 = table [keynum]; dula = 1; dula = 0; delayms (1); P2 = 0x02; wela = 1; wela = 0; P2 = table [keynum]; dula = 1; dula = 0; delayms (1); P2 = 0x04; wela = 1; wela = 0; P2 = table [keynum]; dula = 1; dula = 0; delayms (1 ); p2 = 0x08; wela = 1; wela = 0; P2 = table [keynum]; dula = 1; dula = 0; delayms (1); P2 = 0x10; wela = 1; wela = 0; P2 = table [keynum]; dula = 1; dula = 0; delayms (1); P2 = 0x20; wela = 1; wela = 0; P2 = table [keynum]; dula = 1; dula = 0; de Layms (1); P2 = 0x40; wela = 1; wela = 0; P2 = table [keynum]; dula = 1; dula = 0; delayms (1 ); p2 = 0x80; wela = 1; wela = 0; P2 = table [keynum]; dula = 1; dula = 0; delayms (1);} void keyscan () {uchar temp1, temp2, column, row; switch (keystate) {case keystate0: // keystate0. Press the key to confirm the status. The next status is keystate1 (the buttons are not in the action state) keyport = 0x0f; temp1 = keyport; keyport = 0xf0; temp2 = keyport; if (temp1 | temp2 )! = 0xff) {temp1 | = temp2; if (! (Temp1 & 0x80) row = 3; else if (! (Temp1 & 0x40) row = 2; elseif (! (Temp1 & 0x20) row = 1; elseif (! (Temp1 & 0x10) row = 0; if (! (Temp1 & 0x08) column = 3; else if (! (Temp1 & 0x04) column = 2; elseif (! (Temp1 & 0x02) column = 1; elseif (! (Temp1 & 0x01) column = 0; keynum = column * 4 + row; while (! (Keyport = 0x0f | keyport = 0xf0); keystate = keystate1; break;} elsekeystate = keystate1; break; case keystate1: // keystate1. The buttons are not in the action state, the next state is keystate0 (the key is not in the action state) keyport = 0x0f; temp1 = keyport; keyport = 0xf0; temp2 = keyport; if (temp1 | temp2 )! = 0xff) {keystate = keystate0;} elsekeystate = keystate1; break ;}} void main () {while (1) {keyscan (); smg ();}}

 

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.