cancel
Showing results for 
Search instead for 
Did you mean: 

Using TIM1 as a PWM input

keith
Associate
Posted on September 04, 2014 at 18:13

I am trying to set up TIM1 on my STM32F407 to act as a PWM input but I'm not having any luck. I have successfully used TIM2 with an identical circuit but when I modify the code to run on TIM1 I never get the interrupt. Below is my setup and interrupt code:

static void Timer1Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
// Set up the PW Input
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
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_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_TIM1);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY-1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// Set up the Timer Structure
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
//TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM1, &TIM_ICInitStructure);
/* Select the Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(TIM1, TIM_TS_TI2FP2);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM1,TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(TIM1, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);
}
void TIM1_CC_IRQHandler(void)
{
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
/* Clear Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM1, TIM_IT_CC2);
/* Get the Input Capture value */
pulseWidthTemp = TIM_GetCapture2(TIM1) - TIM_GetCapture1(TIM1);
I1CTemp = TIM_GetCapture1(TIM1);
xSemaphoreGiveFromISR(ScanComplete,&xHigherPriorityTaskWoken);
if( xHigherPriorityTaskWoken != pdFALSE )
{
taskYIELD();
}
}

Any thoughts as to why this would work with TIM2 but not TIM1? I understand that TIM1 is a different hardware configuration but I think I should have it set up correctly. Thank you Keith
1 REPLY 1
Posted on September 04, 2014 at 18:44

TIM_TS_TI2FP2 != CH4

Consider using Input Capture Mode if you can't use CH1 or CH2 for PWM Input Mode

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..