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

code execution problem (nrf 51)

I have some very basic code that does not appear to be executing correctly.

There are two versions of code that I would think should have the same result. They do not, and I am trying to figure out why.

Option A:

volatile uint8_t in_pole_flag = 1;    
in_pole_flag = 1;
if(in_pole_flag == 0)    //  <=========== difference point
{
    init_and_loop_funct();
} 

Option B:

volatile uint8_t in_pole_flag = 1;    
in_pole_flag = 0;
if(false)    //  <=========== difference point
{
    init_and_loop_funct();
}

Running the "init_and_loop_funct()" function results in the chip running in a high current mode (many modules initialized and running), while continuing without running the function results in a low current idle mode. Option "A" results in the "init_and_loop_funct()" function executing, while option "B" does not. Why do these two code segments result in different execution?

I would very much like to toggle the execution of the "init_and_loop_funct()" function with a flag, but the above behavior is making this impossible. Any help would be greatly appreciated.

Parents
  • I agree with Sigurd. Your declaring in_pole_flag as volatile might imply that you think some other thread or peripheral could be writing 1 to it between the time you set it to 0 and the time you check its value. (Or you could be just telling the compiler to not to optimize out any code. You could read the assembly code to make sure it meets your expectations.)

Reply
  • I agree with Sigurd. Your declaring in_pole_flag as volatile might imply that you think some other thread or peripheral could be writing 1 to it between the time you set it to 0 and the time you check its value. (Or you could be just telling the compiler to not to optimize out any code. You could read the assembly code to make sure it meets your expectations.)

Children
No Data
Related