AnsweredAssumed Answered

Measure 2 signals on TIM2 CC1, CC2

Question asked by roofie01 on Feb 17, 2016
Latest reply on Feb 17, 2016 by roofie01
Hi,

On this link there's an example piece of code:

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https%3a%2f%2fmy.st.com%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fcortex_mx_stm32%2fMultiple%20PWM%20input%20capture%20on%20one%20timer&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B

Hoping someone could clarify a few things.
In the example posted above, it uses TIM2 over 4 channels to measure 4 signals. It is configured to use PD3,PD4,PD6,PD7.
I do not see in the documentation where those pins can be used as an alternate function on TIM2. Can you explain that please?

Secondly, if I wanted to measure at least 2 frequencies as configured in this example, do they need to be the same but differ only in phase? Can they be different frequencies - but within the range of the configuration?

I need to measure 2 different signals. For example, I'm using PA.5 on TIM2. When I run the code it works fine and measures the signal's frequency.
Following the above example, I've tried adding PA.6 in the same way. The interrupt handler is configured for both TIM_IT_CC1, and TIM_IT_CC2. But, with a signal applied to PA.6, there is nothing happening. Code is below. Can someone explain why it does not work?


//set up to capture pulses on PA5, PA6
void TIM2_Configuration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        
    /* TIM2 Periph clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    /* TIM2 GPIO clock enable */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);    
    
    /* TIM2 pin PA5, PA6 configuration */
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_5 | GPIO_Pin_6;
    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_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* Connect TIM2 pins to AF2 */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM2);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM2);    

    /* Time base configuration */
    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);        
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_Period = (uint32_t)0xFFFFFFFF;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
        
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising | TIM_ICPolarity_Falling;
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStructure.TIM_ICFilter = 0;
    TIM_ICInit(TIM2, &TIM_ICInitStructure);
    
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising | TIM_ICPolarity_Falling;    
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStructure.TIM_ICFilter = 0;
    TIM_ICInit(TIM2, &TIM_ICInitStructure);    

    //Set up TIM2 interrrupt
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);    
    
    /* TIM enable counter */
    TIM_Cmd(TIM2, ENABLE);

    /* Enable the CC2 Interrupt Request */
    TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);      
    TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);          
    
    TIM_ClearFlag(TIM2, TIM_FLAG_CC1 | TIM_FLAG_CC2);        
}

Outcomes