cancel
Showing results for 
Search instead for 
Did you mean: 

Generating single pulse on GPIO pin

honza
Associate II
Posted on July 12, 2012 at 11:25

I want to generate single pulse with given length on GPIO pin on STM32F4 Discovery kit. I wanted to use timer in OnePulse mode. So I was planning to use TIM4 and TIM12, becouse I need 6 independent pins for pulses. But I am a little confused from the example in Standard Peripherals Library. There's used a timer as a trigger for pulse. But I want to trigger pulse from my code, not from timer. How can I do that?

I want to have function like this: void GeneratePulse(uint8_t pin, uint16_t duration). How should I configure timer to get this kind of behaviour?

Thanks in advance.
11 REPLIES 11
arnold_w
Senior
Posted on August 12, 2015 at 17:33

Thanks for your reply and advice. I don't have that example, but I found a similar one: STM32Cube_FW_F4_V1.5.0\Projects\STM324x9I_EVAL\Examples\TIM\TIM_OnePulse

When I take that code and modify (I'm using timer 3, channel 4 and want my pulse on pin B1) I end up with the following:

// Enable GPIOB & TIM3 clocks
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_TIM3_CLK_ENABLE();
// Set up GPIOB1 (timer 3, channel 4) in alternate function (AF) mode
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Timer handler declaration */
TIM_HandleTypeDef TimHandle;
/* Compute the prescaler value, to have TIM4Freq = 45 MHz */
uint32_t 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 = TIM3;
TimHandle.Init.Period = 0xFFFF;
TimHandle.Init.Prescaler = uwPrescalerValue;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_StatusTypeDef status;
if((status = HAL_TIM_OnePulse_Init(&TimHandle, TIM_OPMODE_SINGLE)) != HAL_OK)
{
INV_UART4_TransmitNullTerminatedString((uint8_t*) ''

HAL_TIM_OnePulse_Init error. Error Code: '');
INV_UART4_TransmitDecNumberAsASCII((int32_t)status);
}
TIM_OnePulse_InitTypeDef sConfig;
/*##-2- Configure the Channel 4 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((status = HAL_TIM_OnePulse_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4, TIM_CHANNEL_2)) != HAL_OK)
{
INV_UART4_TransmitNullTerminatedString((uint8_t*) ''

HAL_TIM_OnePulse_ConfigChannel error. Error Code: '');
INV_UART4_TransmitDecNumberAsASCII((int32_t)status);
}
/*##-3- Start the One Pulse mode ###########################################*/
if((status = HAL_TIM_OnePulse_Start(&TimHandle, TIM_CHANNEL_4)) != HAL_OK)
{
/* Initialization Error */
INV_UART4_TransmitNullTerminatedString((uint8_t*) ''

HAL_TIM_OnePulse_Start error. Error Code: '');
INV_UART4_TransmitDecNumberAsASCII((int32_t)status);
}

I don't get any pulse and no error messages either. It worries me is that the HAL_TIM_OnePulse_ConfigChannel function takes a forth input parameter called InputChannel, but in my case an input channel is not applicable.
arnold_w
Senior
Posted on August 14, 2015 at 09:37

On another forum (

http://www.mikrocontroller.net/topic/343940

) they recommend that the timer is started manually:

TIM3->CR1 |= TIM_CR1_CEN; // Start Timer 3

However, I tried that and it still doesn't work.