本帖最后由 milanbelief 于 2013-7-1 13:22 编辑
我这边想用Timer input XOR function 带上Hall sensors功能捕获 PA0_T2CH1和PA1_T2CH2上的脉冲数
但是一直都只能捕获PA0_T2CH1上的,下面是我写的驱动程序,请大侠们帮忙看下,谢谢!
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_ClocksTypeDef rcc_clks;
NVIC_InitTypeDef NVIC_InitStructure;
uint32_t pre_scale;
uint16_t i;
//RCC Configure
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
for (i = 0; i < CAPn; i++)
{
GPIO_InitStructure.GPIO_Pin = sBSP_CapChs.input.pin;
GPIO_Init(sBSP_CapChs.input.port, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(sBSP_CapChs.input.clk_scr, ENABLE);
}
//configure CH1/CH2/CH3 as XOR input
TIM_SelectHallSensor(sBSP_Caps.timx, ENABLE); //TIM2 XOR input enable
//Calculate the reference clock frequency
RCC_GetClocksFreq(&rcc_clks);
pre_scale = rcc_clks.PCLK1_Frequency / ref_freq;
pre_scale = (pre_scale < 1) ? 0 : pre_scale - 1;
pre_scale = (pre_scale > (UINT16_MAX - 1)) ? (UINT16_MAX - 1) : pre_scale;
sBSP_Caps.ref_freq = rcc_clks.PCLK1_Frequency / (pre_scale + 1);
// Timer Base Init:
TIM_TimeBaseInitStructure.TIM_Prescaler = pre_scale;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(sBSP_Caps.timx, &TIM_TimeBaseInitStructure);
// sBSP_Caps.timx->CCMR1 = sBSP_Caps.timx->CCMR1 & 0xfffc | 1;
// sBSP_Caps.timx->CCMR1 |= 0x7000;
// sBSP_Caps.timx->CCMR1 &= 0xfcff;
//ÉèÖÃΪTI1F_ED´¥·¢µÄ´Ó¸´Î»Ä£Ê½
TIM_SelectInputTrigger(sBSP_Caps.timx, TIM_TS_TI1F_ED);
TIM_SelectSlaveMode(sBSP_Caps.timx, TIM_SlaveMode_Reset);
TIM_UpdateRequestConfig(sBSP_Caps.timx, TIM_UpdateSource_Regular);
//Capture channel 1 init:'
// TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_TRC; //DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //capture perfromed every dege detect;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInitStructure.TIM_Channel = sBSP_CapChs[CAP1].tim_ch; //CAP1 channel
TIM_ICInit(sBSP_Caps.timx, &TIM_ICInitStructure);
// TIM_InternalClockConfig(sBSP_Caps.timx);
//Capture channel 2 init:'
// TIM_ICInitStructure.TIM_ICMode = TIM_ICMode_ICAP; //
// TIM_ICInitStructure.TIM_Channel = sBSP_CapChs[CAP2].tim_ch;; //
// TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //
// TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_TRC; //
// TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //
// TIM_ICInitStructure.TIM_ICFilter = 0x0; //
// TIM_ICInit(sBSP_Caps.timx, &TIM_ICInitStructure);
//Enable the CC1 Interrupt Request
TIM_ITConfig(sBSP_Caps.timx, CAP_IRQ_EVENTs, ENABLE);
//Enable the TIM2 global Interrupt
NVIC_InitStructure.NVIC_IRQChannel = CAP_NVIC_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 8;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*
* ISR Call Back Configure
*/
if (callback != (FXN_Handle) NULL)
{
sBSP_Caps.cbIsr = callback; //callback-->CapFilter
}
/* TIM enable counter */
//TIM_Cmd(sBSP_Caps.timx, ENABLE);
} |