This program demonstrates how to control an LED using a button on an MCU (Microcontroller Unit). The main function continuously checks if the button is pressed. If it is, the LEDs connected to port P0 will light up in a rotating sequence. If the button is not pressed, all LEDs will turn off.
The code includes the standard header file for the 8051 microcontroller, which is reg52.h
. A delay function is defined to create a time delay, which is essential for controlling the speed of the LED rotation. The button is connected to pin P3^2, and the program uses this pin to detect when the button is pressed.
In the main loop, the program checks the state of the button. If the button is pressed (key == 0
), the program initializes the P0 port with the value 0x7f
, which turns on the rightmost LED. Then, a loop runs eight times to shift the LED pattern one position to the right each time, creating a moving effect. After shifting, the leftmost bit is set to 1 to maintain the pattern.
If the button is not pressed, the P0 port is cleared, turning off all the LEDs. The delay function is implemented using a simple while loop that decrements the input value until it reaches zero, effectively creating a time delay.
This program is a basic example of how to interface a button with an LED display on an 8051 microcontroller. It can be modified or expanded to include additional features such as multiple buttons, different LED patterns, or more complex control logic.
HA series hood is suitable to small heavy duty connectors, it is widely used in machinery, wind energy, rail transportation, power systems, photovoltaic industry, communications industry, engineering equipment and other fields. Heavy Duty Connector is designed for modular production pre-wiring and satisfying severe environmental conditions, applied in rail transit,industrial automation and equipment manufacturing fields. Compared with traditional wiring methods,use of heavy duty connector can reduce site wiring operation after equipment or vehicle leaves the workshop,enhance production efficiency and reduce quality risk of site wiring.
Ha Series Connector,4-Pin Heavy Duty Connector,3 Pin Heavy Duty Connector,Industrial Heavy Duty
Kunshan SVL Electric Co.,Ltd , https://www.svlelectric.com