AnsweredAssumed Answered

Flag in struct is not working properly

Question asked by Frederico Marcelino on Jul 16, 2015
Latest reply on Jul 16, 2015 by Frederico Marcelino

Hello everyone,

 

I'm using systick interrupt of the MKL05Z32 every 100us to build a base time in my program like this.

 

void SysTick_Handler(){

...

     if(!--time200ms){

          time200ms=TIME_200_MS;

          flag.change=1;

     }

...

}

 

So I hope I get the flag "flag.change" set every 200ms. This flag is declared inside a struct with many others like this:

 

struct{

     unsigned char change:1;

}flag;

 

I'm using this flag to change a pwm frequency. I want to use 2 frequencies and change each other every 200ms. So my code is:

 

void FTM1_IRQHandler(){

 

     TPM1_SC |= TPM_SC_TOF_MASK;

 

     if(flag.change){

          flag.change=0;

 

          if(!step.pwm){

               //Configure to frequency 1

               step.pwm=1;

          }

          else{

               //Configure to frequency 2

               step.pwm=0;

          }

     }

}

 

Unfortunately this is not working properly. It got this result:

 

NewFile0.png

 

Sometimes it seems the variable "flag.change" is not cleared in the FTM1 interrupt, leading to another frequency change in the next FTM1 interrupt. The strange thing is the waveforms change, thery are not periodic, meaning the strange behavior of flags.

 

I did many things to test and I saw when I changed the "flag.change" to a variable "flagChange" declared as unsigned char I got this:

 

NewFile1.png

The above figure is the result I expect to have. It is okay I know it is working this way, but I'm not satisfied because there are many others flags in structs and I want they work properly. So I would like to understand what is happening.

 

Facts:

1-Variable "flag.change" and " step.pwm" are used only in these functions.

2-Variables are set every 200ms properly.

 

Any ideas will be appreciated. Thanks.

Outcomes