Program for single-chip microcomputer to control LED lighting with a single button

Publisher:HeavenlyMelodyLatest update time:2012-12-01 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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

Previous article:Controlling DC motors with 51 single chip microcomputer
Next article:Single chip microcomputer controls the flashing of LED lights

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号