2019-06-06 06:42 AM
Hello,
I have a very basic question, I want to enable TIM6 to use it as a tool for generating delays, but I am getting trouble just making TIM6 to work.
Cube MX generated the following code:
static void MX_TIM6_Init(void)
{
/* USER CODE BEGIN TIM6_Init 0 */
/* USER CODE END TIM6_Init 0 */
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM6_Init 1 */
/* USER CODE END TIM6_Init 1 */
htim6.Instance = TIM6;
htim6.Init.Prescaler = 100;
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
htim6.Init.Period = 0;
htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM6_Init 2 */
/* USER CODE END TIM6_Init 2 */
}
In main() I have the following code:
__HAL_RCC_TIM6_CLK_ENABLE();
__HAL_TIM_ENABLE(&htim6);
__HAL_TIM_SET_COUNTER(&htim6, 0);
while(__HAL_TIM_GetCounter(&htim6) <= 100);
The program never leaves the while() so I assume that the TIMER is not working.
The micro it wokinrg at maximum speed 216Mhz.
What is the problem????
Thanks!
2019-06-06 07:18 AM
>>What is the problem????
Where do you call MX_TIM6_Init()? Wouldn't you need to enable the bus clock BEFORE you initialize the TIM, or in the MSP code?
2019-06-06 07:22 AM
Hi,
TIM6_init() is executed withing the code that CubeMX is generating:
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DAC_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_SPI2_Init();
MX_TIM6_Init();
/* USER CODE BEGIN 2 */
How should I enable the clock?
2019-06-06 07:34 AM
CubeMX usually enables it in MSP code. But it needs to be on. User a debugger where you can actually inspect the RCC and TIM6 registers.
Set the Period to something other than zero, perhaps 0xFFFF so it counts. Prescale can be zero (DIV1), Period cannot.
2019-06-06 07:40 AM
Hi!
The period was set to 0 in cube MX. That was the problem, I set it to 0xFFFF and now cames out of the while loop. Thanks so much for your help!
2019-06-06 08:01 AM
Another question:
I noticed that every time that the timer reaches 0xFFFF stop counting and then I need to manually enabled again with: __HAL_TIM_ENABLE(&htim6);
Is there any way that when I set the CNT value the counter start automatically?
Is it better to do a countdown and check for CNT == 0 instead of my actual approach?
__HAL_TIM_SET_COUNTER(&htim6, 0);
while(__HAL_TIM_GetCounter(&htim6) <= 100);
2019-06-06 08:31 AM
TimHandle.Instance = TIMx;
TimHandle.Init.Period = 0xFFFF;
TimHandle.Init.Prescaler = 100-1;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimHandle.Init.RepetitionCounter = 0;
TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
if (HAL_TIM_Base_Start(&TimHandle) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
2019-06-06 08:56 AM
Hi, this looks like my init function. Still I need to enable the timer every time after setting the CNT value.
2019-06-06 09:50 AM
Except it isn't.
Pretty sure it free-runs. Here I just read the starting value, and compute the delta to measure elapsed time.