Question ---- Single Chip Microcomputer matrix keyboard row and column inversion scanning method, single chip microcomputer Matrix

Source: Internet
Author: User

Question ---- Single Chip Microcomputer matrix keyboard row and column inversion scanning method, single chip microcomputer Matrix

I learned some questions about the C code written by others on the Internet using the matrix keyboard. I hope I can see some help.

 

Schematic diagram of matrix and independent keyboards:

Principle:For the matrix keyboard on the right of the figure, set the four lower bits of the P3 port (P3.0 ~ P3.3) and 4-bit (P3.4 ~ P3.7) Set 1 separately. If a key is pressed, a certain position of the corresponding P3 port will be pulled down from the high level, the row and column values are measured respectively to determine which button is pressed.

For example:First, assign the four low bits to the high level, that is, P3 = 1__1111. If S15 is pressed, P3.2 is pulled to the low level, that is, P3 = 1__1011, in this case, the four-digit high level is P3 = 1111_0000, so P3.5 is pulled into a low level, that is, P3 = 11000000000. Finally, add the two tested P3 values 1__1011 + 11000000000 = 11000001011 (the row and column values are the third row and the second column)

(1) The most common online statement:

1 P3 = 0x0f; // 0000 1111 2 if (P3 & 0x0f )! = 0x0f) // key detection 3 {4 delay (3); // software shake 5 if (P3 & 0x0f )! = 0x0f) // key detection 6 {7 row_value = P3 & 0x0f; // scan row value 8 P3 = row_value | 0xf0; 9 colume_value = P3 & 0xf0; // scan the column value 10 // while (P3 & 0xf0 )! = 0xf0); // float detection 11 return (row_value + colume_value); 12} 13}

(2 ):

P3 = 0x0f; // 0000 1111if (P3 & 0x0f )! = 0x0f) // key detection {delay (3); // if (P3 & 0x0f) of the software shake )! = 0x0f) // click the button to check {row_value = P3 & 0x0f; // scan the row value P3 = 0xf0; colume_value = P3 & 0xf0; // scan the column value // while (P3 & 0xf0 )! = 0xf0); // returns (row_value + colume_value );}}

 

(3 ):

P3 = 0x0f; // 0000 1111if (P3 & 0x0f )! = 0x0f) // key detection {delay (3); // if (P3 & 0x0f) of the software shake )! = 0x0f) // key detection {row_value = P3; // scan row value P3 = 0xf0; colume_value = P3; // scan column value // while (P3 & 0xf0 )! = 0xf0); // returns (row_value + colume_value );}}

 

(4) This is in line with the Principles and Examples I mentioned:

P3 = 0x0f; // 0000 1111if (P3! = 0x0f) // key detection {delay (3); // if (P3! = 0x0f) // key detection {value = P3; // scan row value P3 = 0xf0; value | = P3; // scan column value // while (P3! = 0xf0); // returns (value );}}

I have used the Development Board for actual testing. The above four are all correct.

My questions:I don't understand why you need to write common statements.& 0x0f, & 0xf0Or| 0xf0What is the special role of this writing? I feel that both writing and not writing are correct. Why should I write? (Especially when writing 1 and 4 seem quite different) ------------------ 22:05:02

 

 

 

 

In case of any errors, please point out that if there is any infringement, please inform us. If you need to reprint it, please indicate the source!

My blog: http://www.cnblogs.com/yllinux/

 

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.