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

Variable Not Incrementing with nrf_delay_ms()

I have the follow code in my application for a PCA10028 program from the nRF51 Development Kit (S110 v9.0):

int main(void)
{
    int i = 0;
    while(true)
    {
        SEGGER_RTT_printf(0, "%d\n", i);
        i++;

        nrf_delay_ms(1000);
    }
}

Output:

0
0
0
0

I want the variable "i" to increment 1,2,3... but it looks like during the nrf_delay_ms() the device is resetting my variable to 0 every time.

Is there any way to have this variable persisted without the use of "pstorage.h"?

Parents
  • pstorage? You're way out the weeds thinking about that, this is simple C, i's a variable, it should just work, in fact that code does work when I test it, as you'd totally absolutely expect it to.

    What compiler are you using and are you in debug or release mode?

    The only thing I can think of here is that the nrf_delay function (in assembler) doesn't appear in every case to tell the compiler it's using r0, so it's quite possible that 'i' is in r0 and is getting clobbered. That would be somewhat odd however, especially in a debug version.

    You could take a look at the assembler and see what it's doing. This code should "just work". Try a few things like making i static and volatile, see if it changes things.

Reply
  • pstorage? You're way out the weeds thinking about that, this is simple C, i's a variable, it should just work, in fact that code does work when I test it, as you'd totally absolutely expect it to.

    What compiler are you using and are you in debug or release mode?

    The only thing I can think of here is that the nrf_delay function (in assembler) doesn't appear in every case to tell the compiler it's using r0, so it's quite possible that 'i' is in r0 and is getting clobbered. That would be somewhat odd however, especially in a debug version.

    You could take a look at the assembler and see what it's doing. This code should "just work". Try a few things like making i static and volatile, see if it changes things.

Children
No Data
Related