I am using stm32f103 for project and keil ide for the compilation. I have configured timer 2 having 16 MHz clk frequency. I need to generate interrupt at 100 ms so I have configured Prescaler val as 24 and period val as 63999. but getting at 95 or 112 ms
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-07 12:46 AM
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-07 04:34 AM
The duration of the timer should be measured as directly as possible, e.g. by switching and measuring on a GPIO.
printf is not suitable for this use case, especially if its output is sent to Linux, which is not an RTOS with a predictable response time.
Regards
/Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-07 01:21 AM
Welcome, @PGare.1​, to the community!
do you use HSI as clock source?
Regards
/Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-07 02:57 AM
Thanks for reply. No We are using HSE as clock source
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-07 03:13 AM
OK.
Did you check the frequency coming from HSE using e.g. the pin MCO?
How did you measure the duration of TIM2?
Regards
/Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-07 03:38 AM
//**********************************In main.c********************************//
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_USART1_UART_Init();
MX_TIM2_Init();
while(1)
{
if (TimerOut == 1)
{
TimerOut = 0;
printf("A\n");
}
fflush(stdout);
}
}
/**
* @brief TIM2 Initialization Function
* @param None
* @retval None
*/
void MX_TIM2_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = 24;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 63999;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.RepetitionCounter = 0;
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();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 460800; //19200
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
//*************************************In stm32f1xx_it.c******************//
volatile OUint8 TimerOut = 0;
void TIM2_IRQHandler(void)
{
__HAL_TIM_CLEAR_IT(&htim2, TIM_IT_UPDATE);
TimerOut = 1;
}
//************************************In stm32f1xx_hal_msp.c*******************//
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM2)
{
__HAL_RCC_TIM2_CLK_ENABLE();
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
}
}
with help of USART1. Printing A on serial terminal when interrupt get generated. timestamp is logged in minicom utility on linux
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-07 04:34 AM
The duration of the timer should be measured as directly as possible, e.g. by switching and measuring on a GPIO.
printf is not suitable for this use case, especially if its output is sent to Linux, which is not an RTOS with a predictable response time.
Regards
/Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-16 10:12 PM
Thank you