本帖最后由 司空落星 于 2014-7-15 22:51 编辑
本人想把一个正弦信号输入给stm32l151 的PB5引脚,想通过比较器实现把正弦波转为方波信号,然后通过定时器2的输入捕获功能测得方波的脉宽,我的这个思路可行吗?哪位大虾能给我详细的分析一下该如何实现呢?
下面是函数实现部分,向PB5输入一个正弦波,发现一直都进不了定时器中断函数,是哪里没有配置好吗?
/*******************************
*定时器2中断处理函数
*******************************/
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC4) == SET)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_9);
GPIO_SetBits(GPIOB,GPIO_Pin_9);
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4|TIM_IT_Update);/*清除中断标志位*/
}
}
/*******************************
*定时器2输入捕获配置
*******************************/
void TIM_Config(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
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_ITConfig(TIM2, TIM_IT_CC4, ENABLE);
TIM_Cmd(TIM2, ENABLE);
TIM2->SR = 0;
}
/*******************************
*比较器配置函数
*******************************/
void COMP_Config(void)
{
COMP_InitTypeDef COMP_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_COMP, ENABLE);
COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_1_2VREFINT;
COMP_InitStructure.COMP_OutputSelect = COMP_OutputSelect_TIM2IC4;
COMP_InitStructure.COMP_Speed = COMP_Speed_Fast;
COMP_Init(&COMP_InitStructure);
COMP_Cmd(ENABLE);/**/
}
|