Hello again,
after correcting the function that was causing the errors reading the sensor I'm back again with some problems, now Timer related.
I'm using Timer2 to cause an interruption every 1ms. But when I read my sensor using SMBus that time changes and I don't know why.
The function that reads values from the sensor:
HAL_StatusTypeDef MLX90614_ReadData(struct MLX90614 *mysensor)
{
HAL_StatusTypeDef return_value;
uint8_t data_read[3];
uint16_t aux;
float ambient_temperature;
float ir_temperature;
data_read[0] = MLX90614_TA;
return_value = HAL_SMBUS_Master_Transmit_IT(&hsmbus1,MLX90614_ADDRESS,&data_read[0],1,SMBUS_FIRST_FRAME);
while(HAL_SMBUS_GetState(&hsmbus1) != HAL_SMBUS_STATE_READY);
if (return_value != HAL_OK)
{
return return_value;
}
return_value = HAL_SMBUS_Master_Receive_IT(&hsmbus1,MLX90614_ADDRESS,&data_read[1],2,SMBUS_LAST_FRAME_NO_PEC);
while(HAL_SMBUS_GetState(&hsmbus1) != HAL_SMBUS_STATE_READY);
if(return_value != HAL_OK)
{
return return_value;
}
if ((data_read[2] & 0x80) == 0x80)
{
return HAL_ERROR;
}
aux = data_read[1] + (data_read[2] << 8);
ambient_temperature = aux * 0.02 - 273.15;
mysensor->local_temperature = ambient_temperature;
data_read[0] = MLX90614_TOBJ1;
return_value = HAL_SMBUS_Master_Transmit_IT(&hsmbus1,MLX90614_ADDRESS,&data_read[0],1,SMBUS_FIRST_FRAME);
while(HAL_SMBUS_GetState(&hsmbus1) != HAL_SMBUS_STATE_READY);
if (return_value != HAL_OK)
{
return return_value;
}
return_value = HAL_SMBUS_Master_Receive_IT(&hsmbus1,MLX90614_ADDRESS,&data_read[1],2,SMBUS_LAST_FRAME_NO_PEC);
while(HAL_SMBUS_GetState(&hsmbus1) != HAL_SMBUS_STATE_READY);
if(return_value != HAL_OK)
{
return return_value;
}
if ((data_read[2] & 0x80) == 0x80)
{
return HAL_ERROR;
}
aux = data_read[1] + (data_read[2] << 8);
ir_temperature = aux * 0.02 - 273.15;
mysensor->ir_temperature = ir_temperature;
return return_value;
}
The interrupt handler:
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM2)
{
HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_3);
millis++;
if(millis >= 999)
{
millis = 0;
print = 1;
}
}
}
Part of the main cycle:
(...)
while (1)
{
if(print == 1)
{
if(MLX90614_ReadData(mymlx90614) == HAL_OK)
{
sprintf(msg_send,"Local temperature: %.2f IR Temperature: %.2f\r\n",mymlx90614->local_temperature,mymlx90614->ir_temperature);
HAL_UART_Transmit(&huart2, (uint8_t*)msg_send, strlen(msg_send), 0xFFFF);
}
print = 0;
}
}
(...)
Timer 2 configuration:
static void MX_TIM2_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim2.Instance = TIM2;
htim2.Init.Prescaler = 31;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 999;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
If I comment the function call in the while loop the led blinks every 1ms or so. If I don't the led blinks every 4ms.
I sincerely do not know what is going on.
I'm using the internal clock (32MHz), a prescaler of 32 and a counter period of 1000. That should result in an interrupt every 1ms.
Do the I2C interrupts interact in some way with the Timer interrupts?
Thank you again,
Miguel