cancel
Showing results for 
Search instead for 
Did you mean: 

if conditional logic not working

prashanth.mohan22
Associate III
/*
    SPC5 RLA - Copyright (C) 2015 STMicroelectronics
 
    Licensed under the Apache License, Version 2.0 (the "License").
    You may not use this file except in compliance with the License.
    You may obtain a copy of the License at
 
        http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
 
/* Inclusion of the main header files of all the imported components in the
   order specified in the application wizard. The file is generated
   automatically.*/
#include "components.h"
#include "pit_lld_cfg.h"
 
uint8_t timer_flag = 0;
/* Callback function for PIT Channel 1 */
void cb_pit_ch1(void){
	timer_flag = 1;
}
 
/*
 * Application entry point.
 */
int main(void) {
 
  /* Initialization of all the imported components in the order specified in
     the application wizard. The function is generated automatically.*/
  componentsInit();
 
  /* Enable Interrupts */
  irqIsrEnable();
 
  /* Start PIT driver */
  pit_lld_start(&PITD, pit_config);
 
  /* Enable PIT Channels */
  /* Channel 0 already enabled and used by system */
  pit_lld_channel_start(&PITD, 1U);
 
  /* Application main loop.*/
  for ( ; ; ) {
	  if(timer_flag == 1)
	  {
		  pal_lld_togglepad(PORT_C, PC_LED7);
		  timer_flag = 0;
	  }
  }
}

In the above autogenerated code, I'm setting timer_flag in timer callback and checking against to toggle an LED (line 52). But the code fails to pass the if condition i.e ( 1 == 1). I couldn't even debug why it's happening that way.

Kindly help me if there is any mistake in the code

This discussion has been locked for participation. If you have a question, please start a new topic in order to ask your question
1 ACCEPTED SOLUTION

Accepted Solutions

Make timer_flag volatile:

volatile uint8_t timer_flag = 0;

View solution in original post

4 REPLIES 4

Make timer_flag volatile:

volatile uint8_t timer_flag = 0;

prashanth.mohan22
Associate III

Thank you so much, it works now.. Could you please explain what's happening while not declaring it as volatile?

There are plenty of good C language tutorials around.