Preface
It's been a long time since I wrote about the microcontroller series. Without further ado, let's get straight to the point! The experiment I'm going to explain and share this time is a famous running light experiment, but this experiment is to achieve "running water" by clicking a button yourself, which is different from my blog (Simulation Experiment of Microcontroller (AT89C51) - Running Light and Flashing Lights One by One (Input and Output)). If you are interested in pure coding running lights, you can also read this article. I have also launched a series of microcontroller knowledge point summaries and experimental sharing: Microcontroller Encyclopedia, and those who are interested can also pay attention to it for subsequent learning.
Experimental requirements and objectives
Experiment 1: Each time you press the S1 key on the independent keyboard, one of the eight LEDs connected to the P1 port lights up and moves down one position.
Experiment 2: When powered on, the LED connected to the P1.0 pin of L1 flashes. Pressing the switch once causes the next light to flash and light up, and the cycle continues.
Experiment 3: There are different effects when different switch button operations are implemented. I designed an experiment to achieve different effects when two different buttons are pressed. Combining the above two experiments, I obtained that when the first button is pressed, it jumps to the next LED light normally (without flashing), and when the second button is pressed, it jumps to the next LED light and flashes.
Experimental Circuit Diagram
This experiment mainly consists of a single-chip microcomputer, a light-emitting diode, and a 1K resistor. The circuit is as follows:
experiment procedure
experiment one
Experimental analysis: During the experiment, we need to pay attention to writing functions in two parts: a key() function that identifies whether a key is pressed and a move() function that executes the water light loop. In the key function, there is a global variable that increments itself, mainly for the shift operation in the move function.
Experiment code:
#include sbit BY1=P3^4; //Define the input terminal S of the key unsigned char count; //Key count, each press adds 1 unsigned char temp; unsigned char a,b; void delay10ms(void) //delay program { unsigned char i,j; for(i=20;i>0;i--) for(j=248;j>0;j--); } key() // Check if the button is pressed { if(BY1==0) //Judge whether the button is pressed { delay10ms(); //delay de-jitter if(BY1==0) //Confirm whether the button is pressed { count++; //key count plus 1 if(count==8) { count=0; } } while(BY1==0); //Key lock, count+1 each time you press it } } move() { a=temp< P1=a|b; } main() { count=0; temp=0xfe; P1=0xff; P1=temp; while(1) //Loop forever, scan to see if the key is pressed { key(); //Call key recognition function move(); //Call the move function } } The screenshots don't show the dynamic effects, but the videos can't be uploaded. Here I suggest that CSDN modify the mechanism so that we can upload our own recorded videos, and care about us niche bloggers. Experiment 2 The difference between Experiment 2 and Experiment 1 is that we need to make the light flash. The switch shifting operation is exactly the same as the previous experiment. We just need to modify the move() function. For the sake of comparison, I rewrote the move function into move1() function. The complete code of the experiment: #include sbit BY1=P3^4; //Define the input terminal S of the key unsigned char count; //Key count, each press adds 1 unsigned char temp; unsigned char a,b; void delay10ms(void) //delay program { unsigned char i,j; for(i=20;i>0;i--) for(j=248;j>0;j--); } key() // Check if the button is pressed { if(BY1==0) //Judge whether the button is pressed { delay10ms(); //delay de-jitter if(BY1==0) //Confirm whether the button is pressed { count++; //key count plus 1 if(count==8) { count=0; } } while(BY1==0); //Key lock, count+1 each time you press it } } move() { a=temp< P1=a|b; } //Move with flashing move1() { a=temp< while(1) //Enter an infinite loop and exit when a key is pressed { P1=a|b; delay10ms(); //delay P1=P1|0xff; // Make the LED state opposite delay10ms(); //delay if(BY1==0) //When the key is pressed, it will exit the loop and execute the key() function break; } } main() { count=0; temp=0xfe; P1=0xff; P1=temp; while(1) //Loop forever, scan to see if the key is pressed { key(); //Call key recognition function //move(); //Call the move function move1(); //Use the move and flash function } } Detailed explanation of move1() function: //Move with flashing move1() { a=temp< while(1) //Enter an infinite loop and exit when a key is pressed { P1=a|b; delay10ms(); //delay P1=P1|0xff; // Make the LED state opposite delay10ms(); //delay if(BY1==0) //When the key is pressed, it will exit the loop and execute the key() function break; } } The previous assignment of a and b is to make the corresponding lights light up. For example: When the button is pressed for the first time, count is assigned to 1, so a is equal to 0xfe(temp) shifted left by one bit, so a is represented by 1111 1100 in binary, and b is represented by 0000 0001 in binary, so the initial value of P1 is a|b, represented by 1111 1101 in binary. Corresponding to the circuit diagram, the second light is on, with a delay of 10ms, and the operation of P1=P1|0xff is to reverse the currently on light. In this way, in an endless loop, a flashing effect will appear. When a button is pressed, the loop will be jumped out, and other codes can be executed to achieve the purpose of the experiment. Experiment 3 Combining the previous two experiments, it is not difficult to conclude that the third experiment requires changing the key function to control two buttons, plus some code fine-tuning. Experiment code: #include sbit BY1=P3^4; //Define the input terminal S of the key sbit BY2=P3^5; unsigned char count; //Key count, each press adds 1 unsigned char temp; unsigned char a,b; void delay10ms(void) //delay program { unsigned char i,j; for(i=20;i>0;i--) for(j=248;j>0;j--); } key() // Check if the button is pressed { if(BY1==0) //Judge whether the button is pressed { delay10ms(); //delay de-jitter if(BY1==0) //Confirm whether the button is pressed { count++; //key count plus 1 if(count==8) { count=0; } } while(BY1==0); //Key lock, count+1 each time you press it } } key2() // Check if the button is pressed { if(BY2==0) //Judge whether the button is pressed { delay10ms(); //delay de-jitter if(BY2==0) //Confirm whether the button is pressed { count++; //key count plus 1 if(count==8) { count=0; } } while(BY2==0); //Key lock, count+1 each time you press it } } move() { a=temp< P1=a|b; while(1) //Add a loop to make this state continue { if(BY1==0||BY2==0) //When a key is pressed, it will exit the loop and execute the key() function break; } } //Move with flashing move1() { a=temp< while(1) //Enter an infinite loop and exit when a key is pressed { P1=a|b; delay10ms(); //delay P1=P1|0xff; // Make the LED state opposite delay10ms(); //delay if(BY1==0||BY2==0) //When a key is pressed, it will exit the loop and execute the key() function break; } } main() { count=0; temp=0xfe; P1=0xff; P1=temp; while(1) //Loop forever, scan to see if the key is pressed { key(); //Call key recognition function move(); //Call the move function key2(); move1(); //Use the move and flash function } } The key2() function is basically the same as the key() function, except for the most basic parameters. The main difference is that an infinite loop is added to the move function so that the light bulb can be continuously lit when it is not flashing, otherwise there will be some minor problems. Experimental Summary This experiment mainly explains how to control the on and off of our LED lights through an external button command, and realizes the operation of the running light. The experimental code is relatively simple, and I believe you can understand it thoroughly. After all, those who come in are not ordinary people, but masters.
Previous article:Detailed explanation of the interrupt system of the single-chip microcomputer (AT89C51) and the application experiment of the interrupt system
Next article:Single chip microcomputer (AT89C51) peripheral I/O input and output experiment
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Based on PSOC6 development board simulation I2C solution X-NUCLEO-IKS01A3 LPS22HH
- 【CH579M-R1】+OTA first experience
- I created a project with stm32cubemx and then used the module generated by keil RTE, but the code generated by RTE is repeated. How can I disable the duplication?
- 【ESP32-C3-DevKitM-1】LED PWM for ESP32-C3
- CS8626 pin diagram Filter-free, 50W mono Class D power amplifier HT8696 circuit diagram
- 【NUCLEO-L552ZE Review】+wifi module
- Questions about LM317 circuit
- Forward - I've learned a lot. Have you ever seen the cross-section of a BGA packaged chip?
- The most complete circuit testing process
- Driver recommendations for three-phase current-source PWM rectifiers