cancel
Showing results for 
Search instead for 
Did you mean: 

How to generate a 1000Hz signal on PC7 using TIM3 or TIM8 on STM32L476QE 80MHz. Using a prescaler 1000.

Eliso
Associate II

With the SW generated by MX as example I have a timer that seems to work, but not a signal on PC7.

20 REPLIES 20

As a test, set it as GPIO output and toggle.

JW

Mike_ST
ST Employee

I suggest that you show main.c and attach .ioc file so people might be able to help.

I did some change in SW generated by MX (lines marked as sujested) , but possibly I didn´t understood your suggestion. With this change does not work. void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if(htim->Instance==TIM8) { /* USER CODE BEGIN TIM8_MspPostInit 0 */ /* USER CODE END TIM8_MspPostInit 0 */ __HAL_RCC_GPIOC_CLK_ENABLE(); /**TIM8 GPIO Configuration PC7 ------> TIM8_CH2 */ GPIO_InitStruct.Pin = GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF3_TIM8; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /* USER CODE BEGIN TIM8_MspPostInit 1 */ /* USER CODE END TIM8_MspPostInit 1 */ } } void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { if(htim_base->Instance==TIM8) { /* USER CODE BEGIN TIM8_MspInit 0 */ /* USER CODE END TIM3_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_TIM8_CLK_ENABLE(); /* USER CODE BEGIN TIM3_MspInit 1 */ /* USER CODE END TIM8_MspInit 1 */ } } /** * @brief TIM_Base MSP De-Initialization * This function freeze the hardware resources used in this example * @param htim_base: TIM_Base handle pointer * @retval None */ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base) { if(htim_base->Instance==TIM8) { /* USER CODE BEGIN TIM8_MspDeInit 0 */ /* USER CODE END TIM3_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_TIM8_CLK_DISABLE(); /* USER CODE BEGIN TIM3_MspDeInit 1 */ /* USER CODE END TIM8_MspDeInit 1 */ } } /** * @brief TIM3 Initialization Function * @param None * @retval None */ void BuzzerInit(void) { TIM_ClockConfigTypeDef sClockSourceConfig = {0}; TIM_MasterConfigTypeDef sMasterConfig = {0}; TIM_OC_InitTypeDef sConfigOC = {0}; TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; /* USER CODE BEGIN TIM8_Init 1 */ /* USER CODE END TIM8_Init 1 */ htim8.Instance = TIM8; htim8.Init.Prescaler = 1000; htim8.Init.CounterMode = TIM_COUNTERMODE_UP; htim8.Init.Period = 39; htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim8.Init.RepetitionCounter = 0; htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim8) != HAL_OK) { Error_Handler(); } sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; if (HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK) { Error_Handler(); } if (HAL_TIM_OC_Init(&htim8) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig) != HAL_OK) { Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_TOGGLE; // SUGEST was TIM_OCMODE_TIMING; sConfigOC.Pulse = 0; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; if (HAL_TIM_OC_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) { Error_Handler(); } sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; sBreakDeadTimeConfig.DeadTime = 0; sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; sBreakDeadTimeConfig.BreakFilter = 0; sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE; sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH; sBreakDeadTimeConfig.Break2Filter = 0; sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_ENABLE; // SUGESTED was TIM_AUTOMATICOUTPUT_DISABLE; if (HAL_TIMEx_ConfigBreakDeadTime(&htim8, &sBreakDeadTimeConfig) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM8_Init 2 */ /* USER CODE END TIM8_Init 2 */ HAL_TIM_MspPostInit(&htim8); CLEAR_BIT(htim8.Instance->CR1, TIM_CR1_CEN); } Em sex., 9 de abr. de 2021 às 11:16, ST Community escreveu:

My point is, that your hardware might be faulty (bad solder joint, some other problem on your board), and you can easily find it out by toggling the pin as GPIO, exactly as when blinking a LED.

JW

Thanks for your answer. Em sex., 9 de abr. de 2021 às 11:27, ST Community escreveu:
Yes this is ever possible. But generation of 1000Hz by SW on the same HW and pin works. Em sex., 9 de abr. de 2021 às 12:01, ST Community escreveu:

Read out and check/post the TIM and relevant GPIO registers content.

Note that TIM8 is an Advanced Timer, so it has to have TIMx_BDTR.MOE set.

> CLEAR_BIT(htim8.Instance->CR1, TIM_CR1_CEN);

This will stop the timer.

JW

Hi annexed there is imagens of TIM8 and GPIOC registers now with MOE setted. Em sex., 9 de abr. de 2021 às 13:18, ST Community escreveu:

Image paste does not work here, even if it appears that it does.

Please upload it as a file - click on the "image" icon in the bar at bottom of edit window.

Please make sure the values are not displayed as decimal.

JW