6- "electronic" on the first chapter introduction _ everything from microcontroller -1.3.3 button

Label: Singlechipprogrammingteaching
175 people read comment(0) Collection Report
Classification:

1.3.3 button

A circuit diagram is to look at the following, or look at the P2 interface, here we pick up a light-emitting diode D1, and then a key S2, we want to realize the function: press the button S2, a light emitting diode D1 light emitting state change. In fact is the analog family light switch.


Of course, to achieve such a function is not so scaled, and MCU minimum system, the actual use of a simple closed switch, a current limiting resistor, a battery, a bulb is enough. But in this is not only in order to light a lamp, through this small case, we will draw the microcontroller development board system inside two very important concepts, "button to shake" and "interrupt""! The following content involves a lot of programming ideas, please read carefully.

When it comes to the button, the author can't help spitting. The author has asked (in fact, was also asked) such a problem: the assumption that the microcontroller P2.0 pin connected to the pin, P2.4 then led, how to use the key to control the light off?

Some people say: key interrupt! (this is after the documents, speak now too early, ZhangZui!)

Others say: in the microcontroller from the P2.0 directly connected to a wire to P2.4......

I say:......

Well, the author admitted that I was the first day of the study, I think so!

Why say this, because as the first day I said, when many people mistakenly believe that the beginner Singlechip microcontroller control circuit is achieved by pure, but in fact it is to rely on the identification and control of microcontroller pin level to achieve the various functions. SCM is a very listen to your words of the brain, you let it do what it does. For example, want to achieve the figure key control diode function, let the MCU has been monitoring the level of P2.0, S2 in the absence of press time, P2.0 through R4 directly connected to the VCC, the level must be high, if S2 is pressed, P2.0 will now ground level becomes low, so when the MCU P2.0 the pin level state into low level will know when a button is pressed, then let microcontroller P2.4 pin, let D1 change the luminous state of the whole process is like this, then we convert it to a more professional program flow diagram, as shown below.


And then according to the above process chart to write the program, the code is shown below:

#include<reg52.h>
S2=P2^0 sbit;
D1=P2^4 sbit;
/********************************************************
* * function name: Main (void)
* * Return: None
* * function description: key control lamp light off
**********************************************************/
Voidmain (void) / / the program starts running
{
D1=1; / / initialize, the diode does not emit light
While (1) / / loop detection
{
If (S2==0) / / if the detected P2.0 pin is low, the button is pressed
{
D1=~D1; / / diode conversion state, return to loop detection
}
}
}


Should be able to see, according to the flow chart to program will make the idea more clear, and later, when we can understand the importance of the program flow chart.

Well, a button control procedures on the light.

...

...

...

So, really made up?

Try to download the program to the microcontroller, run it, and see if you want the effect of it?

...

...

...

Well, tell you, this is not enough. You can try to press the button, will find the light and not as you think so obedient, sometimes really change the luminous state sometimes change, sometimes changed several times, which is to lead to trouble?

We look at the button down after the P2.0 tube foot level changes, the following figure is a schematic diagram of the ideal situation.

T0 time: press the button, P2.0 level from high to low.

T1 time: lift the button, P2.0 level from low to high.

When the microcontroller P2.0 pin detected low, D1 shift. You know, this occurs in the microcontroller in the fast, is probably the magnitude of microsecond, and the key press down then up, this time corresponds to the magnitude of milliseconds, so T0 to T1 in this time period, in fact, D1 has changed several times, this is not what we want the effect, we think the key once, D1 conversion time, that how to achieve it!

Thinking is the way, when the button is pressed down, the microcontroller begins to wait, wait until the button is raised to allow the D1 to change the state. Code changed to the following look.

#include<reg52.h>
S2=P2^0 sbit;
D1=P2^4 sbit;
/********************************************************
* * function name: Main (void)
* * Return: None
* * function description: key control lamp light off
**********************************************************/
Voidmain (void) / / the program starts running
{
D1=1; / / initialize, the diode does not emit light
While (1) / / loop detection
{
If (S2==0) / / if the detected P2.0 pin is low, the button is pressed
{
While (S2==0); / / when P2.0 is low, then wait for MCU
D1=~D1; / / diode conversion state, return to loop detection
}
}
}


Above added a sentence"While (S2==0);"The code is equivalent to"While (! S2);", meaning that when the statement is true (P2.0 for the low level), the microcontroller program in this wait, until the statement in brackets (P2.0 variable back to high), the program continues to run.

Well, run the program, see if you want to achieve the effect you want to.

...

...

...

So, the effect is really good?

...

...

...

Well, as if or not, the above procedures if loaded into the simulation software similar to Proteus (see the fifth chapter Proteus simulation software tutorial), the program does not run up a little. But the real microcontroller download to run up although no"While (S2==0);"This sentence is stronger when the code, but from time to time is not very obedient. What is the reason?

The reason is that a very famous saying, ah, reality is often far worse than the ideal!" Press the button will change the level, due to the stability of the circuit and shake hands to produce a very irregular shape, as shown below, will produce a jitter zone in a moment of press and lift, do not underestimate the jitter, it is enough to make a single misunderstanding, lead to the occurrence of D1 deflection.


So when writing key procedures tend to add a "shake" process, which is when the microcontroller P2.0 port detected low, single chip without any treatment, wait for a period of time (about 10ms), again P2.0 port, if the P2.0 port is still low, so that the I pressed the button to begin the conversion, D1 state. Rewrite the program, as shown below:

 

#include<reg52.h>
Uintunsigned int #define
S2=P2^0 sbit;
D1=P2^4 sbit;
 
/********************************************************
* * function name: delay (x uint)
* * Return: None
* * function description: delay function, delay of about X
**********************************************************/
Voiddelay (uint x) / / delay function, for a period of time against mcu.
{
I J, uint;
For (i=0; i<=x; i++)
For (j=0; j<=100; j++);
}
/********************************************************
* * function name: Main (void)
* * Return: None
* * function description: key control lamp light off
**********************************************************/
Voidmain (void) / / the program starts running
{
D1=1; / / initialize, the diode does not emit light
While (1) / / loop detection
{
If (S2==0) / / if the detected P2.0 pin is low, the button is pressed
{
Delay (10); / / delay.
If (S2==0)
{
While (S2==0);
D1=~D1; / / diode conversion state, return to loop detection
}
                   
}
}
}

Here to try again, see how the effect?

...

...

...

All right.

...

...

...

The effect should be good, if there are problems to be changed inside the function parameters delay value, try a few times can adjust the best effect. (here if I tell you, in fact, the program is still a problem, you will not go mad? -_-||)

Again, there are times when we write to shake the program is more trouble, you can add a button where the filter capacitor, as shown below. After the button is pressed, if there is jitter, the capacitor C will play a certain role in filtering, the effect will be much stronger, we can do the experiment to try to see.


There is a train of thought! We use the above key is automatically ejected, and a press down will not bounce switch, the switch is much simpler, press down has been conducted, and then click to lift, I explain to you that you will think that what it used to open the Commissioner so the the way.

There is such a

There is such a


There is such a


To be continued...

top
One
step on
Zero

Reference knowledge base

Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • Visit:8768 times
    • Integral:Four hundred and fifteen
    • Grade:
    • Rank:Thousands of miles away
    • Original:33
    • Reproduced:0
    • Translation:0
    • Comments:4
    Latest comments