Skip to main content
prashanth.mohan22
Associate III
May 7, 2019
Solved

if conditional logic not working

  • May 7, 2019
  • 2 replies
  • 1305 views
/*
 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 topic has been closed for replies.
    Best answer by After Forever

    Make timer_flag volatile:

    volatile uint8_t timer_flag = 0;

    2 replies

    After Forever
    After ForeverBest answer
    Senior III
    May 7, 2019

    Make timer_flag volatile:

    volatile uint8_t timer_flag = 0;

    prashanth.mohan22
    Associate III
    May 8, 2019

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

    Ozone
    Principal
    May 8, 2019

    There are plenty of good C language tutorials around.