int main(void) { HAL_Init(); Nucleo_BSP_Init(); MX_TIM3_Init(); HAL_TIM_OnePulse_Start(&htim3, TIM_CHANNEL_1); while (1); }
/** * @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 */ HAL_Init(); /* Configure the system clock to 180 MHz */ SystemClock_Config(); /* Configure LED3 */ BSP_LED_Init(LED3); /* Compute the prescaler value, to have TIM4Freq = 45 MHz */ uwPrescalerValue = (uint32_t) (((SystemCoreClock / 2) / 45000000) - 1); /*##-1- Configure the TIM peripheral #######################################*/ /* Initialize TIMx peripheral as follow: + Prescaler = (SystemCoreClock/2)/45000000 to have TIMCLK = 45 MHz + Period = 0xFFFF + ClockDivision = 0 + Counter direction = Up */ TimHandle.Instance = TIMx; TimHandle.Init.Period = 0xFFFF; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_OnePulse_Init(&TimHandle, TIM_OPMODE_SINGLE) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the Channel 1 in One Pulse mode ##########################*/ sConfig.OCMode = TIM_OCMODE_PWM2; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; sConfig.Pulse = 16383; sConfig.ICPolarity = TIM_ICPOLARITY_RISING; sConfig.ICSelection = TIM_ICSELECTION_DIRECTTI; sConfig.ICFilter = 0; if(HAL_TIM_OnePulse_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1, TIM_CHANNEL_2) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-3- Start the One Pulse mode ###########################################*/ if(HAL_TIM_OnePulse_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* STM32F4xx HAL library initialization: - Configure the Flash prefetch - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /*##1 Configure the system clock to 180 MHz */ SystemClock_Config(); /* Configure LED3 */ BSP_LED_Init(LED3); /*##2 Compute the prescaler value, to have TIM5Freq = 18000000 Hz * TIM5CLK = (SystemCoreClock/2) * * Prescaler = (TIM5CLK /TIM5 counter clock) - 1 * * The prescaler value is computed in order to have TIM5 counter clock * set at 18000000 Hz. */ uwPrescalerValue = (uint32_t)(((SystemCoreClock/2)) / 18000000) - 1; /*##-3- Configure the TIM peripheral ####################################### * *-The external signal is connected to TIM5_CH2 pin (PA.01), * and a rising edge on this input is used to trigger the Timer. * *-The One Pulse signal is output on TIM5_CH1 (PA.00). * * The delay value is fixed to: * - Delay = CCR1/TIM5 counter clock * = 16383 / 18000000 [sec] * * The pulse value is fixed to : * - Pulse value = (TIM_Period - TIM_Pulse)/TIM5 counter clock * = (65535 - 16383) / 18000000 [sec] * * ------------------------------------------------------------------------ */ TimHandle.Instance = TIMx; TimHandle.Init.Period = 0xFFFF; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimHandle.Init.RepetitionCounter = 0; if (HAL_TIM_OnePulse_Init(&TimHandle, TIM_OPMODE_SINGLE) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the Channel 1 in One Pulse mode ##########################*/ sConfig.OCMode = TIM_OCMODE_PWM2; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; sConfig.Pulse = 16383; sConfig.ICPolarity = TIM_ICPOLARITY_RISING; sConfig.ICSelection = TIM_ICSELECTION_DIRECTTI; sConfig.ICFilter = 0; sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; if (HAL_TIM_OnePulse_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1, TIM_CHANNEL_2) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*##-4- Start the One Pulse mode ####################################### * * * The one pulse waveform can be displayed using an oscilloscope and it looks * like this. * * ____ * | | * CH2 _________________________| |_________________________________________ * * ___________________________ * | | * CH1 ______________________________________| |____ * <---Delay----><------Pulse---------------> * * * * * */ if (HAL_TIM_OnePulse_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK) { /* Starting Error */ Error_Handler(); } while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32L0xx HAL library initialization: - Configure the Flash prefetch, Flash preread and Buffer caches - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Low Level Initialization */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize LED2 */ BSP_LED_Init(LED2); /* Compute the prescaler value to have TIM2 counter clock equal to 16 MHz */ uwPrescalerValue = (SystemCoreClock / 16000000) - 1; /*##-1- Configure the TIM peripheral #######################################*/ /* Initialize TIMx peripheral as follow: + Prescaler = (SystemCoreClock/16000000) + Period = 65535 + ClockDivision = 0 + Counter direction = Up */ TimHandle.Instance = TIM2; TimHandle.Init.Period = 65535; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_OnePulse_Init(&TimHandle, TIM_OPMODE_SINGLE) != HAL_OK) { /* Initialization Error */ ErrorHandler(); } /*##-2- Configure the Channel 1 in One Pulse mode ##########################*/ sConfig.OCMode = TIM_OCMODE_PWM2; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; sConfig.Pulse = 16383; sConfig.ICPolarity = TIM_ICPOLARITY_RISING; sConfig.ICSelection = TIM_ICSELECTION_DIRECTTI; sConfig.ICFilter = 0; if(HAL_TIM_OnePulse_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1, TIM_CHANNEL_2) != HAL_OK) { /* Initialization Error */ ErrorHandler(); } /*##-3- Start the One Pulse mode #######################################*/ if(HAL_TIM_OnePulse_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK) { /* Initialization Error */ ErrorHandler(); } while (1) { } }
/** * @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 */ HAL_Init(); /* Configure the system clock to 168 MHz */ SystemClock_Config(); /* Configure LED3 */ BSP_LED_Init(LED3); /* Compute the prescaler value, to have TIM4Freq = 42 MHz */ uwPrescalerValue = (uint32_t) ((SystemCoreClock / 2) / 42000000) - 1; /*##-1- Configure the TIM peripheral #######################################*/ /* -------------------------------------------------------------------------- TIM4 configuration: One Pulse mode The external signal is connected to TIM4_CH2 pin (PB.07), The Rising edge is used as active edge, The One Pulse signal is output on TIM4_CH1 pin (PB.06) The TIM_Pulse defines the delay value The (TIM_Period - TIM_Pulse) defines the One Pulse value. TIM4 input clock (TIM4CLK) is set to 2 * APB1 clock (PCLK1), since APB1 prescaler is different from 1. TIM4CLK = 2 * PCLK1 PCLK1 = HCLK / 4 => TIM4CLK = HCLK / 2 = SystemCoreClock /2 TIM2CLK = SystemCoreClock/2, we want to get TIM2 counter clock at 42 MHz: Prescaler = (TIM2CLK / TIM2 counter clock) - 1 Prescaler = ((SystemCoreClock /2) /42 MHz) - 1 The Autoreload value is 65535 (TIM4->ARR), so the maximum frequency value to trigger the TIM4 input is 42000000/65535 = 641 Hz. The TIM_Pulse defines the delay value, the delay value is fixed to 390 us: delay = CCR1/TIM4 counter clock = 16383 / 42000000 = 390 us. The (TIM_Period - TIM_Pulse) defines the One Pulse value, the pulse value is fixed to 1.170 ms: One Pulse value = (TIM_Period - TIM_Pulse) / TIM4 counter clock = (65535 - 16383) / 42000000 = 1.170 ms. 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 --------------------------------------------------------------------------- */ /* Initialize TIMx peripheral as follow: + Prescaler = (SystemCoreClock/2)/42000000 + Period = 0xFFFF + ClockDivision = 0 + Counter direction = Up */ TimHandle.Instance = TIMx; TimHandle.Init.Period = 0xFFFF; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_OnePulse_Init(&TimHandle, TIM_OPMODE_SINGLE) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the Channel 1 in One Pulse mode ##########################*/ sConfig.OCMode = TIM_OCMODE_PWM2; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; sConfig.Pulse = 16383; sConfig.ICPolarity = TIM_ICPOLARITY_RISING; sConfig.ICSelection = TIM_ICSELECTION_DIRECTTI; sConfig.ICFilter = 0; if(HAL_TIM_OnePulse_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1, TIM_CHANNEL_2) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*##-3- Start the One Pulse mode #######################################*/ if(HAL_TIM_OnePulse_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }