本帖最后由 bright2307 于 2014-10-23 14:59 编辑
问题是捕获的数据不对~~大家看一下~~时钟都打开了的啊~
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1|RCC_APB2Periph_GPIOA, ENABLE); //Open TIM4 clock
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //open gpioB clock
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //GPIO??
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
/*配置中断优先级*/
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; //////////这里如果设置为 TIM1_CC_IRQn(中断函数也改了) 的话没有数据,进不去中断!!!!!!!!!!
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM1_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM1_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM1, &TIM1_ICInitStructure); //PWM输入配置
TIM_SelectInputTrigger(TIM1, TIM_TS_TI1FP1); //选择有效输入端
TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Reset); //配置为主从复位模式
TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
TIM_ITConfig(TIM1, TIM_IT_CC1|TIM_IT_Update, ENABLE); //中断配置
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1|TIM_IT_Update); //清除中断标志位
TIM_Cmd(TIM1, ENABLE);
}
u16 period = 0;
u16 duty = 0;
//u16 IC2Value=0;
u8 CollectFlag = 1;
void TIM1_UP_IRQHandler(void)
{
if(CollectFlag){
if (TIM_GetITStatus(TIM1, TIM_IT_CC1) != RESET)//捕获1发生捕获事件
{
duty = TIM_GetCapture1(TIM1); //采集占空比
period = TIM_GetCapture2(TIM1); //采集周期
}
else
{
duty = 0;
period =0;
}
CollectFlag = 0;
}
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1|TIM_IT_Update); //清除中断标志位
}
采集到的数据都偏大~~~不知道为什么~~
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
,好的,我再看看定时器这一部分~~~问您一下,stm32的tim1最多可以同时采集几路PWM波啊?
一周热门 更多>