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

Global variables are not updated

Hi, I have created a global variables header file that I use through out my project. The variables are static volatile and intialized to zero.

When I update them with a new value (i.e. a =150) and try to print them to serial console of Segger, in main(), the printed values are all zero.

If I update them and print them in a function, located in another source file of the project , the variables are updated correctly.

I attach the header file code here in case you see something that I don't

#ifndef GLOBAL_VARIABLES_H
#define GLOBAL_VARIABLES_H

#ifdef __cplusplus
extern "C" {
#endif

#include "nrf_drv_twi.h"
#include "app_error.h"
#include "ICM20948.h"      

#define TWI_INSTANCE_ID     0
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);

static volatile bool m_xfer_done = false;
static volatile ret_code_t err_code;
static volatile bool flag = false;

static volatile int16_t accel_X = 0;
static volatile int16_t accel_Y = 0;
static volatile int16_t accel_Z = 0;

static volatile int16_t gyro_X = 0;
static volatile int16_t gyro_Y = 0;
static volatile int16_t gyro_Z = 0;


static volatile uint16_t testVariable = 0;

#ifdef __cplusplus
}
#endif

#endif // GLOBAL_VARIABLES_H

Thank you

Parents
  • Since this is a .h file you are defining a unique instance of these variables for every time the header file is included. As they are "static" each of these instances is not visible to any other module, but each is visible withiin that c file which included the .h file; remove "static" and you will see a flood of errors about multiple definitions. So to use them as global variables place them in a single c or cpp file and delete the "static" prefix; also keep the variables in the .h file, but replace "static" with "extern" which tells each c file that these variables are indeed global.

Reply
  • Since this is a .h file you are defining a unique instance of these variables for every time the header file is included. As they are "static" each of these instances is not visible to any other module, but each is visible withiin that c file which included the .h file; remove "static" and you will see a flood of errors about multiple definitions. So to use them as global variables place them in a single c or cpp file and delete the "static" prefix; also keep the variables in the .h file, but replace "static" with "extern" which tells each c file that these variables are indeed global.

Children
No Data
Related