AnsweredAssumed Answered

One Pulse Mode Output

Question asked by korf.tanner on Mar 13, 2012
Latest reply on Mar 19, 2012 by korf.tanner
I am working with a Ping ultrasonic sensor from Parallax that requires an input pulse of 2 microseconds to trigger the sensor. It then sends back a high signal for a period of time that is directly proportional to the distance it is measuring.

The One Pulse Mode (OPM) that the STM324xx has seems perfect to send the 2 microsecond input pulse. It seems that the reference manual states that I should configure the output pin as a PWM out. It also states the Output Compare Mode (OCM) could be used in One Pulse Mode. Below, I wrote some code to configure pin  PB4 as a PWM output. I cannot figure out how to configure it into a One Pulse however. I believe I need to configure the timer to TIM_OPMode_Single by calling the TIM_SelectOnePulseMode() function. Am I on the right track?

I am grateful of any help!

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
// GPIOC  clock enable
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
// GPIO Configuration: TIM3 to CH1(PB4), CH2(PB5)
GPIO_InitTypeDef GPIO_InitStructure;
  
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  
GPIO_Init(GPIOB, &GPIO_InitStructure);B1Periph_TIM3, ENABLE);
// Connect TIM3 pin to AF2  
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_TIM3);
uint32_t counterClock;
  
if(frequency < 1000) {
  counterClock = 100000;
}
else {
  counterClock = 28000000;
}
  
uint16_t PrescalerValue = (uint16_t) ((SystemCoreClock /2) / counterClock) - 1;
d_ARR = counterClock/frequency - 1;
// Time base configuration
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  
TIM_TimeBaseStructure.TIM_Period = d_ARR;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  
// PWM Mode configuration
TIM_OCInitTypeDef  TIM_OCInitStructure;
  
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  
// Channel 1
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
// TIM3 enable counter
TIM_Cmd(TIM3, ENABLE);

Outcomes