i'm working on project need use 1 variable declared in file (say mylib.c) in main function using 'extern'. headers included guard words avoid multiple inclusions. variable structure(defined in mylib.h) members floats , integers.it's initialized @ beginning of main function.
after entering main's loop, , doing work, members aren't concerned random values.
so,i removed extern declaration in main, , instead placed in declaration in mylib.c. , 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 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 can see,the member gprs_active can receive 1 or 0 in code. using printf, turned become 242 after first iteration. can explain? can compiler bug? thanks.
the chance compiler issue small. more variable modified part of code. try avoid using global variables have largest scope.
do use somewhere local variable same name?
have checked map file or in debugger placed?
you can use debugger feature datawatch break if data @ address changes track issue.
Comments
Post a Comment