“This is the 16th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

πŸŽƒ Describes independent buttons

  • Touch the button: equivalent to an electronic switch, press the switch on, loosen the switch off, the realization of the principle is to touch the internal metal shrapnel force to achieve the switch on and off.
  • English: Independent press Button
  • Understanding: the independent key has four “heads”, the independent key mainly has four parts: 1, the base, 2, metal shrapnel (the metal shrapnel is drum, when you press down it will become flat, when you release it will drum up), 3, is the head of the key, 4, is the metal cover. So in the same two pins it is actually a piece of metal that is connected internally, whether you press or not, the two pins before and after it are connected. It’s always conductive, with two points of contact outwards. When pressed, all four pins are connected, and when released, both sides are connected respectively, and they are disconnected.
  • As shown below:

​

πŸ“ Schematic diagram of independent keys

​

  • There are four separate buttons, one of which connects to the negative pole of the GND power supply, the other leads to the four SERIAL numbers connected to the MCU in the MCU board, and the other connects to the IO port. As shown below:

​

  • Then we know that when the MCU is powered on, all the IO ports are high level by default, so when the key is not pressed, the IO port is guaranteed to be high level, and when pressed, the IO port becomes low level. The register detects the level of the IO port and reads back into the register. If you release the “I” because it’s supposed to read this register then it’s actually a high level which is 1 by default, and vice versa. So from this phenomenon we can tell if our individual key is being pressed.

🎑 Independent button control LED is on and off

# include < REGX52. H > int main (void) {the while (1) {if (P3_1 = = 0 | | P3_0 = = 0) / / if K1 key or K2 press {P2_0 = 0; } else {P2_0=1; //LED1 output 1, off}}}Copy the code

Note: the network numbers of independent keys K1 and K2 are opposite, K1=P3_1 and K2=P3_0.

πŸŽ‰C51 data operation

​

Arithmetic operators: nothing to say about addition and subtraction, multiplication and division pay attention to the symbols! And division is a round, not a round, in mathematical terms, for example: five divided by two, in mathematical terms: 2.5, in C it’s 2, the smallest integer, and of course it’s an int. There are also single-precision floats and double-precision floats which are still equal to 2.5. So what do I mean by mod (%)? It’s very simple, but it’s still five divided by two. In elementary school, it’s two… 1, so this is actually 5%. 2 is 1. Or 102/10 = 10… 2 so 102%, 10 is 2, so that’s what it means to divide and mod. Assignment is to take the value or the result from the right hand side and assign it to the variable on your left.

Judgment operators: There is nothing to say about this, except that there is always a judgment expression in the value of an expression. If the value of your expression is true, then the parentheses (scope) inside the expression are executed. Notice here the sign of the equal judgment expression.

Logical operators: suppose a = 1, b = 0;

  • && is called the logical and operator. If neither operand is zero, the condition is true. (A && B) is false.
  • | | called logical or operator. If two operands have any nonzero, the condition is true (A | | B) is true.
  • ! This is called a logical non-operator. Used to reverse the logical state of the operand. If the condition is true, the logical nonoperator makes it false. ! (A && B) is true.

Operation:

  • Bitwise and operation, and operation by binary bits. Operation rules: 0&0=0; 1 = 0 0 &; 1 & 0 = 0; 1 &1 = 1;

  • Bitwise or operator that performs “or” operations on binary bits. Algorithm: 0 | 0 = 0; 0 | 1 = 1; 1 | 0 = 1; 1 | 1 = 1;

  • Xor operator that performs xor operations in binary bits. Operation rule: 0^0=0; 0 ^ 1 = 1; 1 ^ 0 = 1; 1 ^ 1 = 0;

  • Invert operator, “invert” by binary bits. Operation rule: ~0001 = 1110;

  • << binary left-shift operator. Move all bits of an operand to the left (discard the left bits and fill in the right zeros)

  • >> Binary right shift operator. The binary bits of a number are all moved right several bits, positive left complement 0, negative left complement 1, the right discard.

πŸŽ‰C51 Basic statement C51 Number C51 Data operation C51 Data operation operation

These statements are microcontroller and become more important language statements! Statements such as the if and switch statements, as well as while and for loop statements are very important in the MCU. These are the knowledge points that must be mastered! You can use more code, master these statements and loop statements. Any complex program can be implemented by sequence, selection, loop code, the above code is a typical if-else statement.

πŸŽ„ Independent button to control LED status

#include <REGX52.H> void Delay(unsigned int xms) { unsigned char i, j; while(xms) { i = 2; j = 239; do { while (--j); } while (--i); xms--; }} int main(void) {if(P3_1==0) {Delay(20); // while(P3_1==0); // Delay(20); P2_0=~P2_0; }}}Copy the code

πŸŽ‹ Key jitter

  • For the mechanical switch, when the mechanical contact is disconnected or closed, due to the elastic action of the mechanical contact, a switch will not be immediately connected stably when it is closed, nor will it be suddenly disconnected when it is disconnected, so a series of jitter will accompany the moment when the switch is closed or disconnected.

​

There will be some jitter when the key is closed, and it is a high level (1) if no key is pressed (all IO ports are high level by default when the MCU is powered on). When we press the button, it will turn into **(0), and because of its mechanical shock, it will pop open **, and then shake up and down a few times, and then it will become (0) stably. Jitter time in the figure above is also **(5 10)ms**. When jitter disappears it enters a steady low level (0), which depends on when you release the key. The jitter will also occur when the hand is released, and it will not suddenly become high level (1), and the jitter will occur for (5 10)ms. Finally, let go.

There are two ways to eliminate jitters:

  1. Add a line, the line through these circuits inside some triggers and so on, through some circuits to operate, the jitter for some overshoot, and then give us a single chip microcomputer point. More trouble!
  2. Through the software to carry out a delay function, the shake – off operation!

πŸ’ independent button control LED display binary

#include <REGX52.H> void Delay(unsigned int xms) { unsigned char i, j; while(xms--) { i = 2; j = 239; do { while (--j); } while (--i); } } int main(void) { unsigned char LEDNum = 0; while(1) { if(P3_1==0) { Delay(20); while(P3_1==0); Delay(20); LEDNum++; P2=~LEDNum; }}}Copy the code

  • The reason we use unsigned char is that the unsigned character (1 byte = 8 bits) corresponds to 8 bits of binary data, so the 8 bits of binary data correspond to port P2 and register 51.
  • Note: after the P2 port is powered on, it is the same as the IO power-on of the MCU. The default is high level! 1111, 1111,
  • If LEDNum is inverted to P2, it doesn’t change the value of the variable that is inverted to P2. It still doesn’t have the opposite value. On the right-hand side, it doesn’t change the value of LEDNum. It’s just taking the inverse where you take the inverse and it’s an rvalue and it doesn’t change.

πŸ₯ independent key control LED light shift

#include <REGX52.H> void Delay(unsigned int xms) { unsigned char i, j; while(xms--) { i = 2; j = 239; do { while (--j); } while (--i); } } int main(void) { unsigned char LEDNum = 0; P2 = 0xFE; While (1) {if(P3_1 == 0) // If (K1 = 0) -- LED shift {Delay(20); While (P3_1 == 0); // Delay(20); // key elimination LEDNum++; If (LEDNum>=8) {LEDNum=0; } P2 = ~(0x01<<LEDNum); } if(P3_0 == 0) // If (P3_0 == 0) While (P3_0 == 0); // Delay(20); If (LEDNum == 0) {LEDNum=7; } else { LEDNum--; } P2 = ~(0x01<<LEDNum); }}}Copy the code

  • P2 port must be capitalized, identifiers must be strictly case sensitive!
  • When defining a local variable, be sure to assign the initial value, which defaults to 0 for global variables.
  • K1 and K2 are misconnected here, K1 = P3_1; K2 = P3_0; K3 = P3_2; K4 = P3_3;

​