cancel
Showing results for 
Search instead for 
Did you mean: 

i want to get the message from the uart every 5s. how can i configure timer based on that

soundarya_h_s
Associate III
clock frequency 170MHz

This is the code i have generated using TIM2 CH1

could you please set the prescalar, period,pulse to get output every 5s  

 

 

static void MX_TIM2_Init(void)

{

 

/* USER CODE BEGIN TIM2_Init 0 */

 

/* USER CODE END TIM2_Init 0 */

 

TIM_ClockConfigTypeDef sClockSourceConfig = {0};

TIM_MasterConfigTypeDef sMasterConfig = {0};

TIM_OC_InitTypeDef sConfigOC = {0};

 

/* USER CODE BEGIN TIM2_Init 1 */

 

/* USER CODE END TIM2_Init 1 */

htim2.Instance = TIM2;

htim2.Init.Prescaler = 169;

htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

htim2.Init.Period = 9999;

htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

if (HAL_TIM_Base_Init(&htim2) != HAL_OK)

{

Error_Handler();

}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)

{

Error_Handler();

}

if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)

{

Error_Handler();

}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)

{

Error_Handler();

}

sConfigOC.OCMode = TIM_OCMODE_PWM1;

sConfigOC.Pulse = 9999;

sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN TIM2_Init 2 */

//motor1State = MC_GetSTMStateMotor1();

HAL_TIM_Base_Start(&htim2);

HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);

//motor1Faults = MC_GetOccurredFaultsMotor1();

motor1Faults = MC_GetCurrentFaultsMotor1();

switch (motor1Faults)

{

case 0:

HAL_UART_Transmit_DMA(&huart2, "MC_NO_ERROR", 12);

break;

case 1:

HAL_UART_Transmit_DMA(&huart2, "MC_DURATION", 12);

break;

case 2:

HAL_UART_Transmit_DMA(&huart2, "MC_OVER_VOLT", 13);

break;

case 4:

HAL_UART_Transmit_DMA(&huart2, "MC_UNDER_VOLT", 14);

break;

case 8:

HAL_UART_Transmit_DMA(&huart2, "MC_OVER_TEMP", 13);

break;

case 10:

HAL_UART_Transmit_DMA(&huart2, "MC_START_UP", 12);

break;

case 20:

HAL_UART_Transmit_DMA(&huart2, "MC_SPEED_FDBK", 14);

break;

case 40:

HAL_UART_Transmit_DMA(&huart2, "MC_BREAK_IN", 12);

break;

case 80:

HAL_UART_Transmit_DMA(&huart2, "MC_SW_ERRORS",12);

break;

 

}

 

/* USER CODE END TIM2_Init 2 */

HAL_TIM_MspPostInit(&htim2);

 

}

2 REPLIES 2
Sarra.S
ST Employee

Hello @soundarya_h_s

Check the block diagram in your product datasheet, to know which bus the TIM2 is connected to (in your case it's APB1)

Here are the formulas you're going to need : 

To calculate the TIM clock frequency: TIM Clock = APB TIM clock/ (PSC+1)

PSC = 170MHz/1MHz) - 1 = 169

To have an event every 5 seconds, you should set the ARR to (5s/1us) - 1 = 4,999,999

pulse value is not relevant since you're not using the timer in PWM mode 

You also need to configure the timer to generate an interrupt on update event 

 

 

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.

Hello @Sarra.S 
can you provide the code for interrupt on update event 
should i add callback function or what should i add in the interrupt 
is the switch case should be present in the TIM2 init itself