博文

目前显示的是标签为“loop”的博文

Does all microcontroller code run in loop?

图片
Short answer: almost all applications on microcontrollers are designed to run “forever”, usually inside some kind of loop – but not every single line of code is literally inside a while(1) loop. Let’s unpack that a bit. 1. Why “infinite loop” is so common Microcontrollers usually: Power up Run some startup code (set up stack, clocks, memory) Call your main() Then are expected to run as long as the device has power Unlike a PC program, there’s no operating system saying “OK, you’re done now, exit”. So firmware is typically structured as: int main ( void ) { setup_peripherals(); init_stuff(); while ( 1 ) { // “superloop” read_inputs(); update_state(); control_outputs(); } } This is why you see so many: while(1) in bare-metal C for(;;) in some code loop() function in Arduino (which the core actually calls in a loop internally) So: the top-level logic is usually in a forever loop . 2. But not everything ...