简体   繁体   中英

Global variables in C on STM32

I'm working on a project where I need to use one variable declared in a certain file (say mylib.c) in the main function using 'extern'. All headers are included with guard words to avoid multiple inclusions. The variable is a structure(defined in mylib.h) which members are only floats and integers.It's initialized at the beginning of the main function.

After entering the main's loop, and doing some work, some members that aren't concerned get random values.

So,I removed extern from the declaration in main, and instead placed it in the declaration in mylib.c. And it worked.

Sim808.h

#ifndef _SIM808_H
#define _SIM808_H
typedef struct{
uint8_t GPRS_Active;

float gsm_latitude;
float gsm_longitude;
}SIM808;
void sendCmd(const char cmd[]);
void sim808_init(void);
void parse_gsm_location(uint8_t* line);
#endif

Sim808.c

#include "sim808.h"
SIM808 sim808;
void parse_gsm_location(uint8_t* line)
{
uint8_t commas=0,index=0;
uint16_t err;
if((err=atoi((const char*)line+12))!=0)
{
    printf("No coordinates received\n");
    if(err==404 || err==601)
        sim808.GPRS_Active=0;

    return;
}
while (line[index]!= '\0' && index <50)
    {
    if(line[index]==',')
    {
        commas++;
        switch (commas)
        {
            case 1:
                sim808.gsm_longitude=atof((const char*)(line+index+1));
                printf("Long:%f\n",sim808.gsm_longitude);   
            break;
            case 2:
                sim808.gsm_latitude=atof((const char*)(line +index+1));
                printf("Longitude%f     Latitude%f\n",sim808.gsm_longitude,sim808.gsm_latitude);
            break;
            case 3:
                sscanf((const char*)(line+index+1),"%4d/%2d/%2d",    (int*)&sim808.gsmDate.year,(int*)&sim808.gsmDate.month,
                    (int*)&sim808.gsmDate.day);
            break;
            case 4:
                sscanf((const char*)(line+index+1),"%2d/%2d/%2d",
                    (int*)&sim808.gsmTime.hours,(int*)&sim808.gsmTime.minutes,(int*)&sim808.gsmTime.seconds);
            break;
        }
    }
        index++;
    }
}

main.c

#include "sim808.h"
extern SIM808 sim808;

int main(void)
{
  uint8_t response[150];
 //init functions
 while(1)
 {
  if(sim808.GPRS_Active==1)
    {
        sendCmd("AT+CIPGSMLOC=1,1\r\n");
        HAL_UART_Receive(&huart4,response,2,60000);//max response time is 1 min
        HAL_UART_Receive(&huart4,response,150,1000);//we dont need first 2 chars
        parse_gsm_location(response);
        memset((void*)response,0,150);
    }
    else
        sim808_init();
  }
}

As you can see,the member GPRS_Active can only receive 1 or 0 in my code. Using printf, it turned to become 242 after the first iteration. Can someone explain? Can this be a compiler bug? Thanks.

The chance it is a compiler issue is really small. More likely is that your variable is modified by some part of your code. Try to avoid using global variables as they have the largest scope.

Do you use somewhere local variable with same name?
Have you checked map file or in debugger where it is placed?

You can use debugger feature datawatch where you break if data at certain address changes to help you track this issue.

When I use global variables, I do not declare them in the .h file to avoid issues of multiple inclusions. You can of course find some tricks to declare them in the .h, but I think it makes things so complicated. So try this:

In mylib.c:

int myGlobalVariable;

In main.c

extern int myGlobalVariable;

int main(void)
{
    myGlobalVariable = 5;
}

If you still have issues, try to increase the size of your stack. If the stack is not big enough, it could be overwritten by other data.

If you use memset or memcpy in any of your code, make sure that the length parameter is correct. memset and memcpy are quite dangerous and you could easily write in some part of the memory that you don't really want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM