The main thing about learning a single-chip microcomputer is to learn how to write a program. The function of the program is ever-changing. It is impossible to learn. Only by mastering certain methods can you use this method to write a new program.
Take the microcontroller program written in C language as an example. The program always starts from the main program and then executes sequentially to the end of main. It can be seen that the program must contain and can only contain one main program, which is often called the main program.
Main()
{
The content of the main program. . . . . .
}
In actual use, you also need to create a main loop body while or do while in the main program. The main loop body can be an infinite loop or a conditional loop, as follows:
Main() 2 I- l5 q. e, B" t4 i) ~9 H: U
{ " r h7 F) J5 G$ C5 C
  Â
While(1)
{
The program in the infinite loop will be executed cyclically * E& K) T7 c7 a7 u
}
}
//////////////////////////////////////////////////////////// ///////
Main()
{
   2 e p# y- J! V/ O
While(Flag)
{ $ Z8 Y6 y' }$ m5 z( k
The program in the conditional loop will loop or jump out of the loop according to the change in the value of Flag.
} " J% @0 q* p0 ?: s& C
} 4 Z2 E. ^* K2 [ u+ @. {
//////////////////////////////////////////////////////////// /////// . u" D6 h! E( m
Main() & ?6 l7 i! x# q+ W/ u
{
The program will execute sequentially
While(1); //stop here
     Â
}
//////////////////////////////////////////////////////////// //////
Generally, the first one is used the most, and all the programs that need to be executed cyclically are placed in the main loop, and then there are multiple conditional loop bodies that can be added internally.
What do you usually do at the beginning of the main program? In the single-chip microcomputer, when the c language enters the entry of the main program, it will automatically add some initialization work of the single-chip microcomputer, so that the single-chip microcomputer is in the state of preparation. But only the internal processing of the microcontroller does not necessarily seem to be what we need, so we need some initialization code for the beginning of the main program, such as the status of each port when booting, the initialization value of some variables declared, timer or other Peripheral initialization, etc., any part that needs to be set at the first time is completed in this part, and then to the while part of the main loop, since it is a loop, it means that the program in the loop body is sequential and executed cyclically, what statement needs Put it in this? That is the amount of ports, values, etc. that need to be changed at any time. For example, the clock and the clock are constantly changing. It is necessary to cyclically read the value of the clock and then update the data to the display device (digital tube or LCD or computer). , and then like the button, because we do not know when to press the button, all the easiest way is to constantly detect the change of the button port, this must also be placed in the main loop body to ensure the timeliness of the detection button
For example: After the power is turned on, the LED connected to P1.1 is lit, then the LED with this button is pressed off, the release button is lit, and the P3.4 port button is set.
Sbit LED=P1^1; * k/ ]- L( H. w: `1 B
Sbit KEY=P3^4; //Define the button port, which can be changed to other ports according to the hardware connection ) i* C; r* m" O- x! ~3 }
9 b9 Z, w* j3 ]
Main()
{ . ]! d& v6 [1 E7 z4 h& o
LED = 0; / / here to light the LED as an example, this statement is to initialize the port 8 p + h * Q4 B- m! @
While(1) / e. a: u( Q1 O. @6 |8 _
{ * w; W# F6 E+ \; r8 R P: I
If(KEY) //Check if the button port is 1, if it is 1 means it is not pressed, if it is 0, it means the button is pressed by % Q% G4 e$ X* ]3 e% ` L
LED=0;//The LED is lit when not pressed : [' b8 ~) k& Q3 h' C r
Else
LED=1;//The LED is off +]# L& U' D/ _) K' Y4 J
} % d) D6 g8 \9 a2 s5 K1 |$ _* P! o
}
original Air glow vape, many new style ,direct factory wholesale,High quality,new design.
popular in many maofferrtket.
offer sample for you check,please contact with me.
Air Glow Blastoff
Shenzhen Ousida Technology Co., Ltd , https://www.osdvape.com