cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f3 discovery, timer input capture problem

esovo1
Associate II
Posted on September 29, 2014 at 15:25

Hei,

I'm sending a 3 us pulse to trigger the sensor. Than I have a time frame to reconfigure the pin in input capture, in order to measure the pulse. The pulse is working, but when I configurate the pin in input capture the pin is in a high output state.

Why is the pin HIGH

? Shouldn't it be low. Here is the code:

/* Includes ------------------------------------------------------------------*/
#include ''main.h''
/* strucutures ---------------------------------------------------------*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Private function prototypes -----------------------------------------------*/
void Pin_Config(void);
void TIM_Config(void);
char a=0;
int main(void)
{
Pin_Config();
TIM_Config(); 
while (1);
}
void Pin_Config(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); // GPIOB clock enable
/*PA.01 configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PB6= TIM4_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_2); //Connect TIM pins to AF2, PB6= TIM4_CH1
}
void TIM_Config(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //TIM4 clock enable
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 71; // 1M Hz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
TIM_TimeBaseStructure.TIM_Period = 203;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* TIM4 PWM2 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM1; //PWM1MODE: CNT>CCR1 -> OCREF==0 else OCREF==1 
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 200;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // begining output is low
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
NVIC_Init(&NVIC_InitStructure); 
TIM_ClearFlag(TIM4, TIM_FLAG_CC1); // Clear the CC1 flags
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE); // Enable the TIM4 receive interrupt
TIM_Cmd(TIM4, ENABLE); //Enable TIM counter 
}
void TIM4_IRQHandler(void)
{
// Configure PWM Input Capture //
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; // PA6 TIM3_CH2
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //Falling, Rising
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8; // every 8 rising edge is a capture
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
} 
// Unused functions that have to be included to ensure correct compiling
// ###### DO NOT CHANGE ######
// =======================================================================
uint32_t L3GD20_TIMEOUT_UserCallback(void)
{
return 0;
}
uint32_t LSM303DLHC_TIMEOUT_UserCallback(void)
{
return 0;
}
// ============================================================================================================================

#stm32 #tim #tim #stm32
12 REPLIES 12
Posted on September 29, 2014 at 16:24

I don't know, but if you don't actually service the interrupt, you'll never be able to leave.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
esovo1
Associate II
Posted on October 01, 2014 at 11:40

clive1 you are right, but I can't find any explanation why is the pin high.

esovo1
Associate II
Posted on October 01, 2014 at 12:00

here is the code with the int. flags cleared:

int main(void)
{
Pin_Config();
TIM_Config();
while (1);
}
void Pin_Config(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); // GPIOB clock enable
/*PA.01 configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PB6= TIM4_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_2); //Connect TIM pins to AF2, PB6= TIM4_CH1
}
void TIM_Config(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //TIM4 clock enable
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 71; // 1M Hz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 203;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* TIM4 PWM2 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM1; //PWM1MODE: CNT>CCR1 -> OCREF==0 else OCREF==1 
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 200;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // begining output is low
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
NVIC_Init(&NVIC_InitStructure); 
TIM_ClearFlag(TIM4, TIM_FLAG_CC1); // Clear the CC1 flags
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE); // Enable the TIM4 receive interrupt
TIM_Cmd(TIM4, ENABLE); //Enable TIM counter
}
void TIM4_IRQHandler(void)
{
if (TIM4 ->SR & TIM_IT_CC1 ) TIM4 ->SR &= (~TIM_IT_CC1); 
// Configure PWM Input Capture //
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; // PA6 TIM3_CH2
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //Falling, Rising
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8; // every 8 rising edge is a capture
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
//Checks whether the TIM interrupt has occurred or not.
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) == SET) TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);
}

Posted on October 02, 2014 at 15:55

Hi,

Would you mind giving a little more information about your issue so we can better explain it to our development team? That would help more easily figure out where a problem could be.

Regards,

Heisenberg.

Posted on October 02, 2014 at 16:21

Start perhaps with posting the content of timer's registers read out after being switched to the input capture mode.

JW
Posted on October 02, 2014 at 18:35

Does the sensor hold the pin high? What does the pin do if the sensor is NOT connected? And without the sensor with the pin having a weak pull-down resistor?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
esovo1
Associate II
Posted on October 03, 2014 at 14:28

Hei,

I'm very thankful for the help and  also I'm bit occupied this week. So, I will post it on Wednesday, 8.10, with more details and what have I done till now… I haven't yet connected the sensor with this code.

Posted on October 08, 2014 at 09:19

I've just realized from the title of your first post, that you are using a STM32f3 discovery board - on that, there is a 10k pullup on PB6. Have you removed that?

JW