Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Realization of Matrix key Design method of single Chip Microcomputer

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the realization of single-chip microcomputer matrix key design method", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "the realization of single-chip microcomputer matrix key design method"!

I. principle

If each keystroke is represented by two bits, then a group of ports with 8 pins can achieve the input of C82O16 keys. The layout of the key is 4 × 4, and the key position of the key is expressed as the intersection of row and column, which can also accurately represent the key input.

II. Design steps

1. Design the circuit.

The keys are arranged in 4 rows x 4 columns. P1.0, P1.1, P1.2 and P1.3 connect the 1st, 2nd, 3rd and 4th rows of keys respectively, while P1.4, P1.5, P1.6 and P1.7 connect the 4th, 3rd, 2nd and 1st columns of keys respectively.

When a key is pressed, the corresponding pins of the corresponding row and column are connected and changed. Since each key corresponds to a unique row, the position of the key can be accurately determined.

two。 The idea of programming.

Output low level line by line, other pins output high level. For each row of output low level, if a key is pressed in a column, the corresponding pin will be pulled down to low level. The key column can be obtained by detecting the position of the low level. When the row and column are determined, the location of the key is obtained.

In this design, the program function is designed to display the row and row of keys on the dynamic digital tube when a button is pressed.

3. Key coding for rows and columns

(1) the code of P1 output when scanning the detection line

The line P1 being detected outputs P1.3P1.2P1.1P1.0 binary coded hexadecimal coded 11110111111100xFE21101111111010xFD31011111110110xFB40111111101110xF7

(2) the column corresponding to the code during detection

P1 input the column P1.7P1.6P1.5P1.4 binary code hexadecimal code * 111011100xEX4110111010xDX3101110110xBX2011101110x7X1*X corresponding to the pressed key indicates that the value varies depending on the row.

4. Program design

(1) Detection of the number of columns

/ * * @ brief to get the key column * * @ return returns 1-4 or F. F means there are no buttons. * / UCHAR getColumn () {UCHAR tmp = P1; tmp = tmp > > 4; / / Please consider with the P1 input table: what is this operation? Switch (tmp) {case 0xF / * 0b1111 * /: return 0xf; case 0xE / * 0b1110 * /: return 4; case 0xD / * 0b1101 * /: return 3; case 0xB / * 0b1011 * /: return 2; case 0x7 / * 0b0111 * /: return 1;}}

(2) Detection of the number of rows and keys

/ * * @ brief scans the input button and displays the serial number on the digital tube. * * / void inputKeyScan () {UCHAR row = 0x10, column = 0x10; UCHAR i; const UCHAR rows [] = {0xFE, 0xFD, 0xFB, 0xF7}; delayNms (20); / / Anti-jitter for (I = 0; I

< 4; i++) { P1 = rows[i]; if (getColumn() != 0xf) { row = 0x10, column = 0x10; row = i + 1; column = getColumn(); display4N(0x10, 0x10, row, column); } } display4N(0x10, 0x10, 0x10, 0x10); // 请思考这是为什么?} (3)主函数调用 /** * @brief 主函数 * */void main(){ P1 = 0xFF; while (1) { inputKeyScan(); }}三、用到的其它函数 分别在4个数码管上显示数字的函数 /** * @brief 分别在4根数码管上显示数字。每个数字的范围都是0-F。 * * @param n1 第一个数码管上显示的数字。 * @param n2 第二个数码管上显示的数字。 * @param n3 第三个数码管上显示的数字。 * @param n4 第四个数码管上显示的数字。 */void display4N(UCHAR n1, UCHAR n2, UCHAR n3, UCHAR n4){ P2 = 0x01; // 选择第一个数码管 if (n1 >

= 0 & & N1

< 0x10) P0 = HexBCD[n1]; // 显示n1 else P0 = 0xFF; delayNms(5); P2 = 0x02; // 选择第二个数码管 if (n2 >

= 0 & & N2

< 0x10) P0 = HexBCD[n2]; // 显示n2 else P0 = 0xFF; delayNms(5); P2 = 0x04; // 选择第三个数码管 if (n3 >

= 0 & & N3

< 0x10) P0 = HexBCD[n3]; // 显示n3 else P0 = 0xFF; delayNms(5); P2 = 0x08; // 选择第四个数码管 if (n4 >

= 0 & & N4 < 0x10) P0 = HexBCD [N4]; / / display n4 else P0 = 0xFF; delayNms (5);} so far, I believe you have a deeper understanding of the "single-chip microcomputer matrix key design method", might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report