新手入门
- 积分
- 25
- 金钱
- 25
- 注册时间
- 2014-8-27
- 在线时间
- 0 小时
|
5金钱
void DC_TIM5_Cap_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM5_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource1, GPIO_AF_TIM5);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 84-1; //1M Hz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_2;
// TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1; //改为通道1
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIM5,&TIM5_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearFlag(TIM5,TIM_IT_CC1|TIM_IT_CC2);
TIM_ITConfig(TIM5,TIM_IT_CC1|TIM_IT_CC2,ENABLE);
TIM_Cmd(TIM5,ENABLE);
}
u8 TIM5CH1_CAPTURE_STA = 0; //输入捕获状态
u16 TIM5CH1_CAPTURE_VAL = 0; //输入捕获值
static u8 k = 0,i;
void TIM5_IRQHandler(void)
{
if ((TIM5->SR & TIM_IT_CC1) && (TIM5->DIER & TIM_IT_CC1)) //捕获 1 发生捕获事件
{
if(TIM5CH1_CAPTURE_STA == 1) //捕获到一个下降沿
{
TIM5CH1_CAPTURE_STA = 0;
TIM5CH1_CAPTURE_VAL = TIM5->CCR1;
// TIM5CH1_CAPTURE_VAL = TIM5->CCR1;
TIM5->CCER &= ~(1<<1); //设置为上升沿捕获
TIM_CAP_SET[k++] = (u8)(TIM5CH1_CAPTURE_VAL / 1000);
}
else //捕获上升沿
{
// TIM5CH1_CAPTURE_STA=0; //清空
TIM5CH1_CAPTURE_VAL = 0;
TIM1->CNT = 0;
TIM5CH1_CAPTURE_STA = 1; //标记捕获到了上升沿
TIM5->CCER |= 1<<1; //设置为下降沿捕获
}
}
if ((TIM5->SR & TIM_IT_CC2) && (TIM5->DIER & TIM_IT_CC2)) //捕获 1 发生捕获事件
{
if(TIM5CH1_CAPTURE_STA == 1) //捕获到一个下降沿
{
TIM5CH1_CAPTURE_STA = 0;
TIM5CH1_CAPTURE_VAL = TIM5->CCR2;
// TIM5CH1_CAPTURE_VAL = TIM5->CCR1;
TIM5->CCER &= ~(1<<1); //设置为上升沿捕获
TIM_CAP_SET[k++] = (u8)(TIM5CH1_CAPTURE_VAL / 1000);
}
else //捕获上升沿
{
// TIM5CH1_CAPTURE_STA=0; //清空
TIM5CH1_CAPTURE_VAL = 0;
TIM1->CNT = 0;
TIM5CH1_CAPTURE_STA = 1; //标记捕获到了上升沿
TIM5->CCER |= 1<<1; //设置为下降沿捕获
}
}
TIM5->SR = ~(TIM_IT_CC1|TIM_IT_CC2); //清除中断标志位
}
求助大神,PC1口有连续变化的高电平,想通过TIM5输入捕获到高电平持续时间,但是使用通道2时,只能进入使能TIM_IT_CC1中断,使用通道1时,只能进入使能TIM_IT_CC2中断,并且TIM5_CCR1,TIM5_CCR2都锁存不到TIM5_CNT的值,捕获的到电平长度,不知道什么原因,板子是STM32F429。
|
|