I'm learning to code on STM32F429ZI Nucleo board and I've been trying to read temperature from ADC1 temperature sensor using STMStudio.
The code I found in one of tutorials didn't work so I tried checking value of "check" variable at various parts of code and then I've noticed that, not only the value of "check" variable doesn't change anywhere inside the main function, but also the STMStudio doesn't see any variables declared inside of the main function.
Any idea why is that?
I've tried different code which uses button to light up LED to check if the board is ok and it worked just fine.
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
/* USER CODE BEGIN PV */
uint16_t SenseADC;
float check = 0;
float Temperature;
float Vsense;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* 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_ADC1_Init();
/* USER CODE BEGIN 2 */
HAL_ADC_Start(&hadc1);
check = 2;
const float V25 = 0.76; // [V]
const float Vsupply = 3.0; // [V]
const float ADCResolution = 4095;
const float avg_slope = 0.0025; // [V/deg. C]
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(HAL_ADC_PollForConversion(&hadc1,10) == HAL_OK){
check = 3;
SenseADC = HAL_ADC_GetValue(&hadc1);
Vsense=(SenseADC*Vsupply)/ADCResolution;
Temperature = (Vsense - V25)/avg_slope + 25;
HAL_ADC_Start(&hadc1);
}
}
}
The compiler must have noticed that the check
variable is only written and never read, and optimized away the writes as having no purpose. For debugging purposes, you can declare it as volatile
to force all writes to get through to memory.
The const
values in the program are likewise victims of the optimization, the compiler has used the values directly in the code, possibility precalculating parts of the expressions.