This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Current consumption of if condition in for(;;) loop

Hello,

as I couldn't manage to get my SPI Measurement working flawlessly when called from inside a timer handler I tried to work around it by just setting a flag inside the timer handler and then call the measurement from inside the main()-function in the for(;;)-loop after the power_manage();-function by putting the code inside if(flag) { SPI-Stuff; flag=false; }

int main(void)
{ 

    for (;;)
    {
      power_manage();	
				
			if(test_flag) {
lots of code
				test_flag=false;
			}

    }
}

What is the current consumption of this? In what state is the chip when constantly checking the if flag?

Parents
  • Your first assumption is correct. The CPU will sleep in the power_manage call. On intrrupt, it will wake up, check the flag and go back to sleep in the power_manage call.

    For example when SPI peripheral has received data, it will fire interrupt and execute the SPI interrupt handler. In the interrupt handler, lets assume you set test_flag=true. When the SPI handler finishes execution, execution will continue in the main loop after the power_manage() call and execute the code inside your if condition since the flag is true. If there is some other interrupt waking up the chip that does not set test_flag=true, it will just check the test_flag in the main loop and then go back to sleep.

Reply
  • Your first assumption is correct. The CPU will sleep in the power_manage call. On intrrupt, it will wake up, check the flag and go back to sleep in the power_manage call.

    For example when SPI peripheral has received data, it will fire interrupt and execute the SPI interrupt handler. In the interrupt handler, lets assume you set test_flag=true. When the SPI handler finishes execution, execution will continue in the main loop after the power_manage() call and execute the code inside your if condition since the flag is true. If there is some other interrupt waking up the chip that does not set test_flag=true, it will just check the test_flag in the main loop and then go back to sleep.

Children
Related