2013-06-28 12:28 PM
Hello, Could somebody please explain why the code below (AN4099) does configure alternate function 2 for TIM2 (capture/compare channel 2) and yet configured channel 1 as well as an interrupt source? If it is desired to measure a signal low or high time, would you not expect one channel to be programmed as capturing both rising and falling edge? Thanks in advance! void SIRC_Init(void)
{ GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; TIM_ICInitTypeDef TIM_ICInitStructure; /* Clock Configuration for TIMER */ RCC_APB1PeriphClockCmd(IR_TIM_CLK, ENABLE); /* Enable Button GPIO clock */ RCC_AHBPeriphClockCmd(IR_GPIO_PORT_CLK, ENABLE); /* Pin configuration: input floating */ GPIO_InitStructure.GPIO_Pin = IR_GPIO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(IR_GPIO_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(IR_GPIO_PORT, IR_GPIO_SOURCE, GPIO_AF_2); /* Enable the TIM global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = IR_TIM_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* TIMER frequency input */ TIM_PrescalerConfig(IR_TIM, TIM_PRESCALER, TIM_PSCReloadMode_Immediate); /* TIM configuration */ TIM_ICInitStructure.TIM_Channel = IR_TIM_Channel; 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(IR_TIM, &TIM_ICInitStructure); /* Timer Clock */ TIMCLKValueKHz = TIM_GetCounterCLKValue()/1000; /* Select the TIM Input Trigger: TI2FP2 */ TIM_SelectInputTrigger(IR_TIM, TIM_TS_TI2FP2); /* Select the slave Mode: Reset Mode */ TIM_SelectSlaveMode(IR_TIM, TIM_SlaveMode_Reset); /* Enable the Master/Slave Mode */ TIM_SelectMasterSlaveMode(IR_TIM, TIM_MasterSlaveMode_Enable); /* Configures the TIM Update Request Interrupt source: counter overflow */ TIM_UpdateRequestConfig(IR_TIM, TIM_UpdateSource_Regular); IRTimeOut = TIMCLKValueKHz * SIRC_TIME_OUT_US/1000; /* Set the TIM auto-reload register for each IR protocol */ IR_TIM->ARR = IRTimeOut; /* Clear update flag */ TIM_ClearFlag(IR_TIM, TIM_FLAG_Update); /* Enable TIM Update Event Interrupt Request */ TIM_ITConfig(IR_TIM, TIM_IT_Update, ENABLE); /* Enable the CC2/CC1 Interrupt Request */ TIM_ITConfig(IR_TIM, TIM_IT_CC2, ENABLE); TIM_ITConfig(IR_TIM, TIM_IT_CC1, ENABLE); /* Enable the timer */ TIM_Cmd(IR_TIM, ENABLE);