Home > other >  How to use single chip timing control LED
How to use single chip timing control LED

Time:09-17

How to use single chip timing control LED, LED light when press the K1 for 3 seconds and then go out, when the press K2 LED light for 5 seconds and then go out, LED light when press the K3 10 seconds and then go out

CodePudding user response:

Each key gives different time value problem

CodePudding user response:

Generally is equipped with a timer, 1 ms into an interruption, then define a countdown variable timeDown, if timeDown greater than zero, the decline in the timer interrupt,
Read K1, LED lighting, set timeDown=3000, and then determine whether timeDown continuously in a while is equal to zero, zero LED goes out, the other in the same way,

CodePudding user response:

The statement is good ah

CodePudding user response:

51 series, for example,
1, define a 50 ms timer (T0 and T1), work any method (note that reset time constant and timing accumulated count), completes the initialization,
For example, using T0, way 1, using interrupt mode
TMOD=0 x01;
TH0=(65536-50000)/256;//time constant implementation can be a variety of
TL0=(65536-50000) % 256;
ET0=1;//T0 interruption allows
EA=1;//always interrupt switch
TR0=1;//start T0
2, define a global variable
Int lightCountDown=1;
3, the definition of T0 interrupt response handler
Void t0_int (void) interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000) % 256;
If (lightCountDown & gt; 0) lightCountDown -;
If (lightCountDown==0)
{
LightCountDown=1;
//todo closed LED
}
}
4, with keys in determination occurs, further judgment K1 and K2 and K3, set lightCountDown to 60 or 100 or 200, respectively, and then open the led control, make its bright

If a single function, do not use interrupt mode can also be in the foreground main loop to achieve the same effect, also can be in after judgment with keys and open time and interrupt handling,

CodePudding user response:

1, using a state machine + timer;
2, the use of time

CodePudding user response:

The simplest switch (key);//(judgment, press any key)
Case 1: led light delay 3 s led out;
Case 2: led light delay led out 5 s,
Case 3: led light delay 10 s led out;
  • Related