while (1);

Source: Internet
Author: User
while (1) is used in single chip microcomputer, while
(1);
Meaning: This is a dead loop, and the code no longer executes downward.
Application:
1. Generally in debugging code, in order to detect a part of the code is OK, to prevent the subsequent code interference execution results, will be in the observation point plus while (1);
2. Some code detects a run error, throws an error (printing, setting the error code), and then enters while (1);
3.  when the machine needs to be reset, stop feeding the watchdog, enter while (1); Force the watchdog to timeout, resulting in hardware reset while

(1) {code}
meaning: This will repeat the code usage in {}
:
1.  The main program uses this kind of architecture when the operating system is not in use
2. The process in the operating system, some of which will also use this schema 3 when performing tasks.
The code in {} constantly detects a condition and, when the condition is met, jumps out of the loop and continues down.

we use while (1) in the microcontroller, mostly to prevent the program from running,  Because a lot of time after the execution of a program MCU program pointer PC (is the program pointer) does not stop, still will continue to read the instructions from ROM and execute, this may appear program run flight situation, and then appear uncertain results, we add a while (1) Can let the program after execution in situ cycle, equivalent to stop in situ, to prevent running. 
In principle, if there is no end in the program, the program counter will continue to accumulate, thereby executing the instructions in the ROM space following the normal program, but generally this instruction is 0XFF, which depends on the meaning of this instruction, if it is empty will be carried out until the end, until the program counter overflow, Just do not know what the program counter overflow is a state, it is possible to return to the Reset status value, but also may directly let the single-chip die here does not work, this will look at the specific design of the SCM.
 while (1) 
{
   in the main program.
}
Otherwise the SCM program will continue to run by address, to 0FFFFH later-00000h.
If you do not add while (1) statement, the microcontroller is still normal operation, that is, the address in the back of what happened to make the program to form a dead cycle, not a gallop, it is also a coincidence.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.