Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ******************************************************************************
- * @file TIM/TIM_TimeBase/Src/main.c
- * @author MCD Application Team
- * @version V1.2.7
- * @date 17-February-2017
- * @brief This sample code shows how to use STM32F4xx TIM HAL API to generate
- * 4 signals in PWM.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- /** @addtogroup STM32F4xx_HAL_Examples
- * @{
- */
- /** @addtogroup TIM_TimeBase
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- TIM_HandleTypeDef TimHandle;
- TIM_OC_InitTypeDef TIM_OCInitStructure;
- TIM_BreakDeadTimeConfigTypeDef TIM_BDTRInitStructure;
- TIM_ClockConfigTypeDef sClockSourceConfig;
- uint32_t timePeriod = 0;
- uint32_t duty1, duty2, duty3 = 0;
- /* Private function prototypes -----------------------------------------------*/
- static void SystemClock_Config(void);
- static void Error_Handler(void);
- static void TIM_Config(void);
- static void PWM_SetDutyCycle(TIM_HandleTypeDef *htim, uint32_t duty, uint32_t channel);
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program
- * @param None
- * @retval None
- */
- int main(void)
- {
- /* STM32F4xx HAL library initialization:
- - Configure the Flash prefetch, instruction and Data caches
- - Configure the Systick to generate an interrupt each 1 msec
- - Set NVIC Group Priority to 4
- - Global MSP (MCU Support Package) initialization
- */
- if(HAL_Init()!= HAL_OK)
- {
- /* Start Conversation Error */
- Error_Handler();
- }
- /* Configure the system clock to 168 MHz */
- SystemClock_Config();
- /* Configure LED3 and LED4 */
- BSP_LED_Init(LED3);
- BSP_LED_Init(LED4);
- /* Configure the Timer-1 output through respective GPIO pins */
- TIM_Config();
- /*##-1- Configure the TIM peripheral #######################################*/
- /* -----------------------------------------------------------------------
- In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1),
- since APB1 prescaler is different from 1.
- TIM3CLK = 2 * PCLK1
- PCLK1 = HCLK / 4
- => TIM3CLK = HCLK / 2 = SystemCoreClock /2
- To get TIM3 counter clock at 10 KHz, the Prescaler is computed as following:
- Prescaler = (TIM3CLK / TIM3 counter clock) - 1
- Prescaler = ((SystemCoreClock /2) /10 KHz) - 1
- Note:
- SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file.
- Each time the core clock (HCLK) changes, user had to update SystemCoreClock
- variable value. Otherwise, any configuration based on this variable will be incorrect.
- This variable is updated in three ways:
- 1) by calling CMSIS function SystemCoreClockUpdate()
- 2) by calling HAL API function HAL_RCC_GetSysClockFreq()
- 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
- ----------------------------------------------------------------------- */
- /* Compute the prescaler value to have TIM3 counter clock equal to 10 KHz */
- timePeriod = (uint32_t) ((SystemCoreClock /2) / 10000) - 1;
- /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */
- duty1 = (uint32_t) (((uint32_t) 5 * (timePeriod - 1)) / 10);
- /* Compute CCR2 value to generate a duty cycle at 25% for channel 2 */
- duty2 = (uint32_t) (((uint32_t) 25 * (timePeriod - 1)) / 100);
- /* Compute CCR3 value to generate a duty cycle at 12.5% for channel 3 */
- duty3 = (uint32_t) (((uint32_t) 125 * (timePeriod - 1)) / 1000);
- /* Set TIMx instance */
- TimHandle.Instance = TIM1;
- /* Initialize TIM3 peripheral as follow:
- + Period = 10000 - 1
- + Prescaler = ((SystemCoreClock/2)/10000) - 1
- + ClockDivision = 0
- + Counter direction = Up
- */
- TimHandle.Init.Period = timePeriod;
- TimHandle.Init.Prescaler = 0;
- TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- TimHandle.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
- TimHandle.Init.RepetitionCounter = 0;
- // if(HAL_TIM_Base_Init(&TimHandle) != HAL_OK)
- // {
- // /* Initialization Error */
- // Error_Handler();
- // }
- // sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- // HAL_TIM_ConfigClockSource(&TimHandle, &sClockSourceConfig);
- HAL_TIM_PWM_Init(&TimHandle);
- /* Channel 1, 2 and 3 Configuration in PWM mode */
- TIM_OCInitStructure.OCMode = TIM_OCMODE_PWM2;
- TIM_OCInitStructure.Pulse = duty1;
- TIM_OCInitStructure.OCFastMode = TIM_OCFAST_DISABLE;
- TIM_OCInitStructure.OCPolarity = TIM_OCPOLARITY_LOW;
- TIM_OCInitStructure.OCNPolarity = TIM_OCNPOLARITY_LOW;
- TIM_OCInitStructure.OCIdleState = TIM_OCIDLESTATE_RESET;
- TIM_OCInitStructure.OCNIdleState = TIM_OCNIDLESTATE_RESET;
- HAL_TIM_PWM_ConfigChannel(&TimHandle, &TIM_OCInitStructure, TIM_CHANNEL_1);
- TIM_OCInitStructure.Pulse = duty2;
- HAL_TIM_PWM_ConfigChannel(&TimHandle, &TIM_OCInitStructure, TIM_CHANNEL_2);
- TIM_OCInitStructure.Pulse = duty3;
- HAL_TIM_PWM_ConfigChannel(&TimHandle, &TIM_OCInitStructure, TIM_CHANNEL_3);
- /* Automatic Output enable, Break, dead time and lock configuration*/
- TIM_BDTRInitStructure.OffStateRunMode = TIM_OSSR_ENABLE;
- TIM_BDTRInitStructure.OffStateIDLEMode = TIM_OSSR_ENABLE;
- TIM_BDTRInitStructure.LockLevel = TIM_LOCKLEVEL_1;
- TIM_BDTRInitStructure.DeadTime = 10;
- TIM_BDTRInitStructure.BreakState = TIM_BREAK_ENABLE;
- TIM_BDTRInitStructure.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
- TIM_BDTRInitStructure.AutomaticOutput = TIM_AUTOMATICOUTPUT_ENABLE;
- HAL_TIMEx_ConfigBreakDeadTime(&TimHandle, &TIM_BDTRInitStructure);
- // /*##-2- Start the TIM Base generation in interrupt mode ####################*/
- // /* Start Channel1 */
- // if(HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK)
- // {
- // /* Starting Error */
- // Error_Handler();
- // }
- if(HAL_TIM_PWM_Start_IT(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- if(HAL_TIM_PWM_Start_IT(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- if(HAL_TIM_PWM_Start_IT(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- /* Main Output Enable */
- if(HAL_TIMEx_PWMN_Start_IT(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- if(HAL_TIMEx_PWMN_Start_IT(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- if(HAL_TIMEx_PWMN_Start_IT(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- /* Infinite loop */
- while (1)
- {
- // int i, j;
- //
- // for(i = 0; i<=50000; i++);
- //
- // PWM_SetDutyCycle(&TimHandle, 50, TIM_CHANNEL_3);
- }
- }
- void PWM_SetDutyCycle(TIM_HandleTypeDef *htim, uint32_t duty, uint32_t channel)
- {
- switch(channel)
- {
- case TIM_CHANNEL_1: htim->Instance->CCR1 = (uint32_t)((duty * timePeriod) / 100); break;
- case TIM_CHANNEL_2: htim->Instance->CCR2 = (uint32_t)((duty * timePeriod) / 100); break;
- case TIM_CHANNEL_3: htim->Instance->CCR3 = (uint32_t)((duty * timePeriod) / 100); break;
- case TIM_CHANNEL_4: htim->Instance->CCR4 = (uint32_t)((duty * timePeriod) / 100); break;
- }
- }
- /**
- * @brief Period elapsed callback in non blocking mode
- * @param htim: TIM handle
- * @retval None
- */
- void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
- {
- BSP_LED_Toggle(LED4);
- }
- /**
- * @brief This function is executed in case of error occurrence.
- * @param None
- * @retval None
- */
- static void Error_Handler(void)
- {
- /* Turn LED3 on */
- BSP_LED_On(LED3);
- while(1)
- {
- }
- }
- /**
- * @brief Configure the TIM1 Pins.
- * @param None
- * @retval None
- */
- void TIM_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* GPIOA, GPIOB and GPIOE clocks enable */
- //RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOE, ENABLE);
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOE_CLK_ENABLE();
- __TIM1_CLK_ENABLE();
- /* TIM1 clock enable */
- //RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
- /* GPIOA Configuration: Channel 1 and 3 as alternate function push-pull */
- GPIO_InitStructure.Pin = GPIO_PIN_8 | GPIO_PIN_10;
- GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
- GPIO_InitStructure.Pull = GPIO_NOPULL;
- GPIO_InitStructure.Alternate = GPIO_AF1_TIM1;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* GPIOA Configuration: Channel 2 as alternate function push-pull */
- GPIO_InitStructure.Pin = GPIO_PIN_11;
- HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
- /* GPIOB Configuration: BKIN, Channel 1N, 2N and 3N as alternate function push-pull */
- GPIO_InitStructure.Pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- /**
- * @brief System Clock Configuration
- * The system Clock is configured as follow :
- * System Clock source = PLL (HSE)
- * SYSCLK(Hz) = 168000000
- * HCLK(Hz) = 168000000
- * AHB Prescaler = 1
- * APB1 Prescaler = 4
- * APB2 Prescaler = 2
- * HSE Frequency(Hz) = 8000000
- * PLL_M = 8
- * PLL_N = 336
- * PLL_P = 2
- * PLL_Q = 7
- * VDD(V) = 3.3
- * Main regulator output voltage = Scale1 mode
- * Flash Latency(WS) = 5
- * @param None
- * @retval None
- */
- static void SystemClock_Config(void)
- {
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_OscInitTypeDef RCC_OscInitStruct;
- /* Enable Power Control clock */
- __HAL_RCC_PWR_CLK_ENABLE();
- /* The voltage scaling allows optimizing the power consumption when the device is
- clocked below the maximum system frequency, to update the voltage scaling value
- regarding system frequency refer to product datasheet. */
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /* Enable HSE Oscillator and activate PLL with HSE as source */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 8;
- RCC_OscInitStruct.PLL.PLLN = 336;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 7;
- if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
- clocks dividers */
- RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
- if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
- {
- Error_Handler();
- }
- /* STM32F405x/407x/415x/417x Revision Z devices: prefetch is supported */
- if (HAL_GetREVID() == 0x1001)
- {
- /* Enable the Flash prefetch */
- __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
- }
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Add Comment
Please, Sign In to add comment