Before implementing the running light, we need to mention that before writing the C51 code, you need to refer to the circuit diagram. Take the running light as an example. Let's look at Figure 2. The positive poles of the eight LEDs are connected to the +5V voltage through a bus. Looking at Figure 1 again, we can see that the negative poles of the eight LEDs are connected to the 8 I/O ports of LED and P2. According to the conduction condition of the diode, the LED can only be lit when the IO port of the microcontroller outputs a low level.
Paste the code below:
#include #define uchar unsigned char #define uint unsigned int sbit LED1 = P2^0; sbit LED2 = P2^1; sbit LED3 = P2^2; sbit LED4 = P2^3; sbit LED5 = P2^4; sbit LED6 = P2^5; sbit LED7 = P2^6; sbit LED8 = P2^7; /* ********************************************* */ // Function name: Delay() // Function: millisecond delay /* ********************************************* */ void delay(uint z) { uint x,y; for(x = 0; x < z; x++) for(y = 0; y < 113; y++); } // Function name: main() // Function: Make an LED light flash void main(void) { while(1) { LED1 = 0; //light on delay(1000); //Continue on for 1s LED1 = 1; //light off delay(1000); //Continue to turn off for 1s LED2 = 0; delay(1000); LED2 = 1; delay(1000); LED3 = 0; delay(1000); LED3 = 1; delay(1000); LED4 = 0; //light on delay(1000); //Continue on for 1s LED4 = 1; //light off delay(1000); //Continue to turn off for 1s LED5 = 0; delay(1000); LED5 = 1; delay(1000); LED6 = 0; delay(1000); LED6 = 1; delay(1000); LED7 = 0; delay(1000); LED7 = 1; delay(1000); } } After reading the first two circuit diagrams, you will probably understand what the code means. First, use the second method of using sbit: sbit bit variable name = SFR name ^ variable bit address value, for example: sbit LED1 = P2^0, which means that LED1 is defined as the first bit of P2 port for bit operation. Then we wrote a millisecond delay function. Of course, the so-called delay here is an inaccurate delay. It is the "1 second" we tested after some debugging. If we want to truly and accurately achieve a one-second delay, we must also use a timer. Since the requirement here is not so accurate, we can use the millisecond delay function to achieve it. Finally, our main function is the function function. As far as the MCU is concerned, we all know that the biggest function of the MCU is control, and the control of the MCU is also reflected in the output of high and low levels. The reason why the MCU can achieve so many functions is also due to the surrounding expansion modules. In C51, 1 is a high level and 0 is a low level, which means off and on for LED lights. In addition, we also call the delay function to realize the running light. (Link: https://pan.baidu.com/s/1CtSpgtdKuNh5P3CmCqHuwA Extraction code: nb2i) Finally, I would like to remind everyone that the C51 code should be analyzed based on the specific circuit diagram of the specific microcontroller model. Do not copy it !!!
Previous article:Two peripheral circuits of the microcontroller: reset circuit and clock circuit
Next article:51 MCU Getting Started Tutorial (3) - Digital Tube Display
Recommended ReadingLatest update time:2024-11-15 14:49
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 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
- Bluetooth host and slave address mutual binding communication test
- [Evaluation and experience of Zhongke Yihaiwei EQ6HL45 development platform] + ip_pll routine and eLinx software debugging
- Let’s discuss the development trend and direction of RF GaN
- Today's live broadcast prize addition: Introduction to Keysight's new optical communication test solution
- Chat with Vicor engineers about how to improve throughput and uptime of automated test equipment?
- Questions about the waveform generated by amplitude modulation by AD9106
- [Technical Video Collection] Littelfuse's Efficient, Reliable, and Accurate Power Control and Circuit Protection Solutions
- Nuvoton N76E003AQ20 3*3 package 0.4BSC lib-AD
- Solution to ARM debugging DAbt_Handler problem
- Q2 Why does the MOS tube get hot and damaged?