9 years, 11 months ago.

waiting for a condition

hello all

I'm using LPC1768. I have a project that need a initialization from gps. the gps data are coming from uart. but I have a problem to make my program waiting for the initialization from the gps. here is my main program

main

void datagps (void){
    // some initialization program. 
    // when the initialization finish it will change the gps_init flag as 0
}

int main (void){
   gps.attach (&datagps);
   gps_init = 1;
   while (gps_init){
      // do nothing
   }
   while (1){
   // my main program
   }
}

I tried to put some code at the waiting loop like this.

loop

   while (gps_init){
      processor.putc ('a');
   }

and suddenly it works. the problem is uart at processor is needed and it cannot send anything until it is needed. thx before. sry for my english.

1 Answer

9 years, 11 months ago.

How did you define gps_init? It must be volatile, the compiler doesn't know it can be changed in an interrupt routine, and will keep checking the same value in a register instead of getting the latest from from its ram memory (if it isn't completely optimised away). So add 'volatile' in front of gps_init's definition and I assume it works.

Accepted Answer

thank you very much. it works. Yes I haven't put volatile before the definition.

posted by Sebastianus Kurniawan 25 May 2014