The program of 51 single-chip microcomputer single-button control LED light emission is to use a button connected to P3_0 pin to control the two states of LED connected to P2_0, namely, on and off. Press the button once and the light will turn on, press the button again and the light will turn off. Press it again and the light will turn on again, press it again and the light will turn off again. This program comes from the single-chip microcomputer website: http://www.51hei.com, and it has been successfully run after testing.
#include "AT89X51.H"
void main(void) // Main program
{
bit mark; //Define bit variable
unsigned int n; //Define loop variable
while(1) //Infinite loop
{
if (P3_0==0) //If the button is pressed
{
for(n=0;n<1000;n++); //Delay for a while, waiting for the button to be fully pressed
mark=~mark; //Flip mark
while(!P3_0) //Wait for the button to pop up
for(n=0;n<1000;n++); //Wait for the button to pop up
}
P2_0=mark; //Turn on or off the LED light
}}
while(1) //Infinite loop
{
if(ldelay==1 ) //If the time overflow mark is 1, process the following statement
{
ldelay=0; //Clear the time overflow mark
P2=ledp[ledi]; //Read a table value and send it to P2 port
ledi++; //Point to the next table value
if(ledi==4)
{
ledi=0; //When it reaches the last light, switch to the first
sp--; //Speed level minus 1
if (sp==3) //If the speed level is reduced to 3
sp=23; //Restore the slowest level 23
}}}}
timer0() interrupt 1 //Timer 0 interrupt service program
{
static unsigned int kk; //Define static local variable
kk++; //Each interrupt service, kk increases by 1
if(kk==(100*sp)) //The minimum number of times is 100*sp=300, and the time is 300 times*0.25ms
//The maximum number of times is 2300 times, and the time is 2300 times*0.25ms
//If kk=(100*sp), execute the following statement
{
kk=0;
ldelay=1; //When the kk value is equal to the 100*sp value, set the ldelay flag to 1, and then process the light and display speed
} }
Reference address:Program for single-chip microcomputer to control LED lighting with a single button
#include "AT89X51.H"
void main(void) // Main program
{
bit mark; //Define bit variable
unsigned int n; //Define loop variable
while(1) //Infinite loop
{
if (P3_0==0) //If the button is pressed
{
for(n=0;n<1000;n++); //Delay for a while, waiting for the button to be fully pressed
mark=~mark; //Flip mark
while(!P3_0) //Wait for the button to pop up
for(n=0;n<1000;n++); //Wait for the button to pop up
}
P2_0=mark; //Turn on or off the LED light
}}
while(1) //Infinite loop
{
if(ldelay==1 ) //If the time overflow mark is 1, process the following statement
{
ldelay=0; //Clear the time overflow mark
P2=ledp[ledi]; //Read a table value and send it to P2 port
ledi++; //Point to the next table value
if(ledi==4)
{
ledi=0; //When it reaches the last light, switch to the first
sp--; //Speed level minus 1
if (sp==3) //If the speed level is reduced to 3
sp=23; //Restore the slowest level 23
}}}}
timer0() interrupt 1 //Timer 0 interrupt service program
{
static unsigned int kk; //Define static local variable
kk++; //Each interrupt service, kk increases by 1
if(kk==(100*sp)) //The minimum number of times is 100*sp=300, and the time is 300 times*0.25ms
//The maximum number of times is 2300 times, and the time is 2300 times*0.25ms
//If kk=(100*sp), execute the following statement
{
kk=0;
ldelay=1; //When the kk value is equal to the 100*sp value, set the ldelay flag to 1, and then process the light and display speed
} }
Previous article:Controlling DC motors with 51 single chip microcomputer
Next article:Single chip microcomputer controls the flashing of LED lights
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
MoreDaily News
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
Guess you like
- Signal linear transformation problem
- [GD32E231 DIY Contest] Part 4: Summary
- EPWM interrupt cannot be enabled
- Three closed loop servo motor motion control
- Two-input AND gate engineering files and test files
- Help others and achieve self-realization——EEWORLD Q&A List (11th issue)
- ST Motor Review_bygyp1
- Problem of assigning initial value to timer of C51 MCU
- UPS-IPGuard Pro P1902 front panel device wiring diagram:
- 2012 Competition Award-winning Papers, Source Code, PCB (Part 5)