cancel
Showing results for 
Search instead for 
Did you mean: 

Code in not working in while(1) loop

Nsg1987
Associate III

Hello,

For below code, I am surprised that variable value is not updating. I cross checked the same in debug mode.

/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "adc.h"

#include "dac.h"

#include "dma.h"

#include "fdcan.h"

#include "hrtim.h"

#include "i2c.h"

#include "spi.h"

#include "tim.h"

#include "usart.h"

#include "gpio.h"

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

#include "core_algo.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

uint16_t nik_variable = 0; //Variable declared here

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc);

void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc);

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 * @retval int

 */

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_DMA_Init();

 MX_FDCAN1_Init();

 MX_HRTIM1_Init();

 MX_TIM1_Init();

 MX_USART3_UART_Init();

 MX_ADC1_Init();

 MX_ADC2_Init();

 MX_ADC3_Init();

 MX_TIM8_Init();

 MX_TIM20_Init();

 MX_DAC1_Init();

 MX_I2C1_Init();

 MX_SPI3_Init();

 MX_TIM2_Init();

 MX_ADC4_Init();

 MX_TIM3_Init();

 /* USER CODE BEGIN 2 */

       

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

 nik_variable = 500; //variable is not updating

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

The variable named nik_variable is not changing its value to 500 from initial value of 0. The increment of nik_variable was also done using nik_variable++ but the result remains the same.

1 ACCEPTED SOLUTION

Accepted Solutions
Peter BENSCH
ST Employee

Probably the variable has been optimised because you assign it once but don't do anything useful with it. If you set the compiler to "do not optimise", you should find it again.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

7 REPLIES 7
Peter BENSCH
ST Employee

Probably the variable has been optimised because you assign it once but don't do anything useful with it. If you set the compiler to "do not optimise", you should find it again.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

If it is changed under interrupt or callback, use volatile keyword​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi,

I am also facing the same problem after updating CUBE IDE... I tried using volatile and Optimization - None(O0) from properties menu... But still having the same issue and coding with ST product becomes harder.. 

A sample code which never executed is listed below. Is there any solution for it....

 

for(int SpicnT =0 ; SpicnT == 40 ; SpicnT++)
{
HAL_GPIO_WritePin(SPI6_CNV_GPIO_Port, SPI6_CNV_Pin, 0) ;
if (HAL_GPIO_ReadPin(SPI6_EOC_GPIO_Port, SPI6_EOC_Pin) == GPIO_PIN_SET) { goto WaiT1 ; }
}
 
WaiT1:

 

gbm
Lead II

It should not execute - the for () loop condition is always false.

MNapi
Senior III

are you sure that the code starts to execute in while loop at all ?  maybe it freezes before it even gets to while loop.

disable some code, start first with ADC and see what happens.

ManishNair
Associate III

Thank you, but that ( SpicnT == 40 ) was a typematic error  and the actual code is listed below. the loop is executing in another function when the commented code (// HAL_GPIO_WritePin(SPI6_CNV_GPIO_Port, SPI6_CNV_Pin, 0) 😉 was inserted. My board has an ethernet with 80 Single channel adcs connected in daisy chain (16 ADCs Chained x 5 SPIs), with 32KSPS. everything is working  excluding the adc convert/data ready synchronization, for which i am using the below timer code. core STM32H753. 

 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/*********************************************************************************/
if(htim -> Instance == TIM6)
{
for(int SpicnT =0 ; SpicnT <= 40 ; SpicnT++)
{
// HAL_GPIO_WritePin(SPI6_CNV_GPIO_Port, SPI6_CNV_Pin, 0) ;
if (HAL_GPIO_ReadPin(SPI6_EOC_GPIO_Port, SPI6_EOC_Pin) == GPIO_PIN_SET) { goto WaiT1 ; }
}
WaiT1:
 

That's the result of using HAL functions for trivial tasks. Do NOT compare anything with GPIO_PIN_SET constant. If you love HAL so much  that you must use it everywhere, then at least do "!= GPIO_PIN_RESET" instead. BTW your use of goto suggests that you need to learn a lot about C basics.