2024-07-20 02:10 PM
Hi,
My one microsecond timer configured by CubeMx running on NUCLEO-C031C6 (STM32C031C6) is 3 times slower than it should be. The code is attached below.
the call "delay_us(60000000);" takes 3+ minutes to complete - it should be done in one minute.
The LL timer (TIM3) init and interrupt code is generated by CubeMx..The system clock is 48 MHZ in CubeMx.
How to fix it?
Thanks,
Rong
void MX_TIM3_Init(void)
{
LL_TIM_InitTypeDef TIM_InitStruct = {0};
/* Peripheral clock enable */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
/* TIM3 interrupt Init */
NVIC_SetPriority(TIM3_IRQn, 0);
NVIC_EnableIRQ(TIM3_IRQn);
/* USER CODE BEGIN TIM3_Init 1 */
/* USER CODE END TIM3_Init 1 */
TIM_InitStruct.Prescaler = 0;
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_InitStruct.Autoreload = 48;
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
LL_TIM_Init(TIM3, &TIM_InitStruct);
LL_TIM_EnableARRPreload(TIM3);
LL_TIM_SetClockSource(TIM3, LL_TIM_CLOCKSOURCE_INTERNAL);
LL_TIM_SetTriggerOutput(TIM3, LL_TIM_TRGO_RESET);
LL_TIM_DisableMasterSlaveMode(TIM3);
/* TIM3 interrupt Init */
}
/* USER CODE BEGIN 1 */
unsigned long u_Count = 0;
/* USER CODE END 1 */
/**
* @brief This function handles TIM3 global interrupt.
*/
void TIM3_IRQHandler(void)
{
/* USER CODE BEGIN TIM3_IRQn 0 */
if (u_Count)
{
u_Count--;
}
if (LL_TIM_IsActiveFlag_UPDATE(TIM3))
LL_TIM_ClearFlag_UPDATE(TIM3);
/* USER CODE END TIM3_IRQn 0 */
}
void delay_us(const unsigned long us_Count)
{
u_Count = us_Count;
LL_TIM_EnableIT_UPDATE(TIM3);
LL_TIM_EnableCounter(TIM3);
while (u_Count !=0)
{
}
LL_TIM_DisableIT_UPDATE(TIM3);
LL_TIM_DisableCounter(TIM3);
}
int main(void)
{
static unsigned int i = 0;
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM3_Init();
MX_SPI1_Init();
MX_USART2_UART_Init();
delay_us(60000000);
}
Solved! Go to Solution.
2024-07-21 06:37 AM
Perhaps you could review examples?
STM32Cube_FW_C0_V1.0.1\Projects\NUCLEO-C031C6\Examples_LL\TIM\TIM_TimeBase_Init
/* Enable counter */
LL_TIM_EnableCounter(TIM3);
2024-07-21 07:59 AM
Thank you so much. It works once I have "LL_TIM_EnableCounter(TIM3);"!!!