MCU chip model STC89C516
need
Required knowledge
1. About the delay function
void Delay100ms() //100s delay function with crystal frequency 12mhz
{
unsigned char i, j;
i = 195;
j = 138;
do
{
while (--j);
} while (--i);
}
It can delay the corresponding time of the previous statement, and use the auxiliary program to generate
2. About shifting in bit operations in C51 language
The symbols are ">>", "<<"
a< a>>m, a and m must be integer expressions, and m>=0. , shift the integer a to the right by m bits, shift the lower bits out, and fill the higher bits with 0 like a=0x45=01010100B, b=0x3b=00111011B, Then a<<2=01010000B=0x50, b>>2=00001110B=0x0e. 3. About eliminating vibration of buttons The switch used for common buttons is a mechanical elastic switch. When the mechanical contact is opened and closed, due to the elastic effect of the mechanical point, the button switch will not be immediately and stably connected when closed, nor will it be disconnected all at once when disconnected. Therefore, there will be a series of jitters at the moment of closing and disconnecting. The jitter time is generally a few tenths of a second to several seconds. Key jitter can cause the key to be misread multiple times. In order to ensure that the CPU processes a key closure only once, debounce is necessary. There are two ways to debounce a button, one is hardware debounce and the other is software debounce. In order to make the circuit simpler, software debounce is usually used. Generally speaking, a simple button debounce is to read the state of the button first. If the button is pressed, delay 10ms, and read the state of the button again. If the button is still pressed, it means that the button has been pressed. Commonly used software de-jitter methods for microcontrollers: 1. First set the IO port to a high level (since the development board IO has a pull-up resistor, the default IO is a high level). 2. Read the IO port level to confirm whether any button is pressed. 3. If the IO level is low, delay for several milliseconds. 4. Read the IO level again. If it is still low, it means the corresponding button is pressed. 5. Execute the program of the corresponding key Circuit Connection Flowing lights Button control software design 1. Flowing lights The D1-D8 indicators light up one by one in a cycle. According to the implementation principle of the running light, that is, the IO port outputs low levels one by one from low to high or from high to low, we can combine the shift operation and the loop. First, led=~0x01. Since the LED is lit at a low level, the result after inverting 0X01 is 0XFE, and the corresponding binary number is 1111 1110, that is, the lowest bit is 0. Therefore, the D1 indicator light will light up at the beginning, and then it will enter the while loop after a delay. Since 8 LEDs need to be lit up from D1->D8 in a cycle, the for loop statement can be used to loop 8 times. Each time the cycle is repeated, the lighted lamp moves one position to the right, that is, the low level output by port P0 needs to be shifted one position to the left. Therefore, the statement P0=~(0x01< #include "reg52.h" #include #define led P0 void Delay100ms() //@12.000MHz { unsigned char i, j; i = 195; j = 138; do { while (--j); } while (--i); } void main () { unsigned char b; led = ~0x01; Delay100ms(); while (1) { for(b=0;b<8;b++) { led = ~(0x01< Delay100ms(); } } } 2. Button control Define the control pins of key K1 and LED1, set the LED to level and turn off the D1 indicator. Then enter the while loop, execute the keycheck() function in the loop body, that is, determine whether the key K1 is pressed. If it is pressed, delay 10ms (delay function) to eliminate the jitter, and then determine whether the key K1 is pressed again. If K1 is indeed pressed, execute the LED state flip control, and finally determine whether the key is released. #include "reg52.h" #include sbit k1=P1^0; sbit led=P0^0; typedef unsigned int u16; typedef unsigned char u8; void delay() //@12.000MHz Use delay function to eliminate key jitter { unsigned char i, j; i = 20; j = 113; do { while (--j); } while (--i); } void keycheck() { if (k1==0) { delay(); //Jitter elimination if (k1==0) { led=~led; } while (!k1); } } void main () { led=1; while (1) { keycheck(); } } Effect Reference: Baidu Encyclopedia, Jinzhong 51 MCU Development Strategy
Previous article:【51 MCU】Ordinary I/O port simulates SPI port C language program
Next article:51 single chip microcomputer learning four serial port communication
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Why can byte addresses overlap with bit addresses?
- Creative Cube
- 10 ways to dissipate heat from PCB!!!
- LOTO Lesson 3: Frequency Response Curve Plotting --- 3 groups of RC low-pass filter tests with different parameters
- Three major programming languages for the development of the Internet of Things
- [Shanghai Hangxin ACM32F070 development board + touch function evaluation board evaluation] Transplant RT-Thread Nano
- Since it is so similar to inductors, why can you only use ferrite beads in your circuit?
- Device tree interrupt node
- Can CC2541 support 10 external interrupts?
- PCB failure analysis and some cases