比较器设置
RCC_APB1PeriphClockCmd(RCC_APB1Periph_COMP, ENABLE);
COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_1_4VREFINT; //dac1 or pb3
COMP_InitStructure.COMP_OutputSelect = COMP_OutputSelect_TIM2IC4; //比较器输出映射到IC4
COMP_InitStructure.COMP_Speed = COMP_Speed_Fast;
COMP_Init(&COMP_InitStructure);
定时器2设置
// 2 初始化TIM2参数
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//TIM_ARRPreloadConfig(TIM2 , ENABLE);
//TIM_SelectOutputTrigger(TIM2 , TIM_TRGOSource_Update);
// 3 初始化TIM2输入捕获通道
//RI input capture register (RI_ICR)缺省是0,所以The standard AFs dedicated to
//TIM2 are:PE12 or PB11
//see page 341 of reference manual, PB11-->TIMx_CHx-->TIx-->处理-->ICx
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; //去配置TI4/IC4 每个TIM都有IC1~4
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //选择输入端 IC4映射到 TI4上
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM_ICInitStructure.TIM_ICFilter = 0x00; //配置输入滤波器 不滤波
TIM_ICInit(TIM2, &TIM_ICInitStructure);
//初始化NVIC 中断优先级分组
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级2 级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 1 //从优先级0 级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig( TIM2,
//TIM_IT_Update |
TIM_IT_CC4,ENABLE
);
TIM_Cmd(TIM2,ENABLE );
不知那里错误, TIM2捕获不到比较器输出的信号, 我已将比较器设置成映射到TIM2 IC4
|