新手上路
- 积分
- 33
- 金钱
- 33
- 注册时间
- 2022-2-3
- 在线时间
- 18 小时
|
楼主 |
发表于 2024-2-22 11:56:09
|
显示全部楼层
TIM_ICInitTypeDef TIM2_ICInitStructure;
void TIM2_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能TIM5时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
//初始化定时器5 TIM5
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向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM5输入捕获参数
TIM2_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 选择输入端 IC1映射到TI1上
TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM2_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM2, &TIM2_ICInitStructure);
TIM2_ICInitStructure.TIM_Channel = TIM_Channel_2; //CC1S=01 选择输入端 IC1映射到TI1上
TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM2_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM2, &TIM2_ICInitStructure);
TIM2_ICInitStructure.TIM_Channel = TIM_Channel_3; //CC1S=01 选择输入端 IC1映射到TI1上
TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM2_ICInitStructure.TIM_ICFilter = 0;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM2, &TIM2_ICInitStructure);
TIM2_ICInitStructure.TIM_Channel = TIM_Channel_4; //CC1S=01 选择输入端 IC1映射到TI1上
TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM2_ICInitStructure.TIM_ICFilter = 0;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM2, &TIM2_ICInitStructure);
//中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_ITConfig(TIM2,TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE);//允许更新中断 ,允许CC1IE捕获中断
TIM_Cmd(TIM2,ENABLE ); //使能定时器5
}
// 定时器2中断服务程序
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) // 捕获1发生捕获事件
{
electrical_machinery_encoder2++;
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) // 如果当前是高电平,那么下降沿, 反之设置上升沿
{
TIM_OC1PolarityConfig(TIM2, TIM_ICPolarity_Falling); // CC1P=1 设置为下降沿捕获
}
else
{
TIM_OC1PolarityConfig(TIM2, TIM_ICPolarity_Rising); // CC1P=0 设置为上升沿捕获
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET) // 捕获2发生捕获事件
{
electrical_machinery_encoder2++;
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1)) // 如果当前是高电平,那么下降沿, 反之设置上升沿
{
TIM_OC2PolarityConfig(TIM2, TIM_ICPolarity_Falling); // CC2P=1 设置为下降沿捕获
}
else
{
TIM_OC2PolarityConfig(TIM2, TIM_ICPolarity_Rising); // CC2P=0 设置为上升沿捕获
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET) // 捕获3发生捕获事件
{
electrical_machinery_encoder1++;
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2)) // 如果当前是高电平,那么下降沿, 反之设置上升沿
{
TIM_OC3PolarityConfig(TIM2, TIM_ICPolarity_Falling); // CC3P=1 设置为下降沿捕获
}
else
{
TIM_OC3PolarityConfig(TIM2, TIM_ICPolarity_Rising); // CC3P=0 设置为上升沿捕获
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET) // 捕获4发生捕获事件
{
electrical_machinery_encoder1++;
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3)) // 如果当前是高电平,那么下降沿, 反之设置上升沿
{
TIM_OC4PolarityConfig(TIM2, TIM_ICPolarity_Falling); // CC4P=1 设置为下降沿捕获
}
else
{
TIM_OC4PolarityConfig(TIM2, TIM_ICPolarity_Rising); // CC4P=0 设置为上升沿捕获
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
}
}
|
|