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

How to use single-chip microcomputer to input independent keys

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use single-chip microcomputer independent key input". In daily operation, I believe that many people have doubts about how to use single-chip microcomputer independent key input. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer the doubt of "how to use single-chip microcomputer independent key input"! Next, please follow the editor to study!

I. principle

In "52 single chip microcomputer independent key input", the key input is obtained by cyclic scanning in the main function. The disadvantage of this design is that the whole CPU main process has been occupied. In order to change this situation, this exercise uses interrupt mechanism to improve the design of independent keystroke input. When using interrupt, the main function does not need to scan key input; when a key is pressed, it first initiates an interrupt request to the single-chip microcomputer with the door, and handles the key press event through the interrupt function; when the key pops up, the external circuit initiates another interrupt request to the single-chip microcomputer, and handles the pop-up event of the key through another interrupt function.

Through this activity, you can learn:

The use of and gate circuits

Use of interrupts

II. Methods and steps

1. Electric circuit design

When a key is pressed, the corresponding pin is pulled down to a low level. By connecting these circuits with the gate circuit, when any one of the pins is a low level 0, the and gate will output a low level of 0, which happens to be used as an interrupt source for the keystroke event. Keystroke pressing and keystroke pop-up are mutually exclusive events, so the interrupt source pressed by the key can be used as the interrupt source of the keystroke pop-up event.

Details: search keywords AND for doors and NOT for non-doors.

two。 Program design

(1) start to enable the main function of the interrupt, and set to enable the internal and external part.

/ / enable interrupt EA = 1; / turn on interrupt (master switch) EX0 = 1; / / allow external interrupt 0 to interrupt IT0=1; / / trigger IT0=1 along the falling edge. / / * the falling edge trigger is triggered only once, while the low level trigger may be triggered repeatedly. EX1 = 1; / allow external interrupt 1 to interrupt IT1=1; / / falling edge triggers IT1=1. / / enable interrupt

(2) after pressing the interrupt function button to initiate the interrupt, temporarily turn off the interrupt, detect the key, and restart the interrupt after the detection is completed. The purpose of this design is to avoid multiple interrupts caused by keystroke jitter.

/ * * @ brief interrupt function when pressed * * / void onKeyDown () interrupt 0 {/ / temporarily close interrupt EA = 0; / / temporarily close interrupt if (P1B0 = = 0) {delayNms (10); / / eliminate jitter if (P1swim0 = = 0) {displayNum (0); goto FUNCEND }} if (P1Christ 1 = = 0) {delayNms (10); / / eliminate jitter if (P1N1 = = 0) {displayNum (1); goto FUNCEND;}} if (P1N1 2 = = 0) {delayNms (10) / / eliminate jitter if (P1N1 2 = = 0) {displayNum (2); goto FUNCEND;}} if (P1B3 = = 0) {delayNms (10); / / eliminate jitter if (P1B3 = = 0) {displayNum (3); goto FUNCEND }} if (P1N1 4 = = 0) {delayNms (10); / / eliminate jitter if (P1B4 = = 0) {displayNum (4); goto FUNCEND;}} if (P1B5 = = 0) {delayNms (10) / / Anti-jitter if (P1B5 = = 0) {displayNum (5); goto FUNCEND;}} if (P1B6 = = 0) {delayNms (10); / / de-jitter if (P1B6 = = 0) {displayNum (6); goto FUNCEND }} if (P1B7 = = 0) {delayNms (10); / / eliminate jitter if (P1B7 = = 0) {displayNum (7); goto FUNCEND;}} FUNCEND: / / restart interrupt EA = 1; / / restart interrupt}

(3) after the button pops up, the digital tube stops displaying.

/ * * @ brief interrupt function when pressing the key to lift * * / void onKeyUP () interrupt 2 {display4N (0x10, 0x10, 0x10, 0x10);}

III. Other points for attention

1. The declaration of the global variable used.

/ / the global variable sbit P1B0 = P1 ^ 0 × sbit P1B1 = P1 ^ 1 × sbit P1BP1 = P1 ^ 2 = P1 ^ 2 × sbit P1BP1B3 = P1 ^ 3 × sbit P1BP1B4 = P1 ^ 4 × sbit P1BP1room5 = P1 ^ 5 × sbit P1BP1room6 = P1 ^ 6 × SEBIT P1B7 = P1 ^ 7 Const unsigned char HexBCD [] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E, 0x7F}; / / global variables

two。 Other functions used.

/ * * @ brief delay ms milliseconds. 0 = 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);} / * * @ brief displays the given number n * * @ param n given number n * / void displayNum (int n) {UCHAR thousand; / / 1000-bit UCHAR hundred; / / 100-bit UCHAR decade; / / 10-bit UCHAR ones / / bit thousand = n / 1000; hundred = (n% 1000) / 100; decade = (n% 100) / 10; ones = n% 10; if (n < 1000) / / if n is less than 1000, the thousand bit thousand = 0xff is not displayed; if (n < 100th) / / if n is less than 100th, the hundred bit hundred = 0xff is not displayed If (n < 10) / / if n is less than 1010, then ten digits decade = 0xff; display4N (thousand, hundred, decade, ones) are not displayed;} at this point, the study on "how to use single-chip microcomputer independent keystroke input" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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