需要做一个编码器的程序,编码器为欧姆龙E6B2-CWZ1X 线性驱动器输出
只用其中的AB两相,使用定时器的输入捕获,但是两相的上升沿中断一直没有检测到。
/////////////////////////////////////////////////////定时器源文件
#include "time.h"
#include "sys.h"
#include "led.h"
//编码器
//定时器3通道1和2输入捕获配置
int16_t Count_pulse=0;
TIM_ICInitTypeDef TIM3_ICInitStructure;
void TIM3_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7; //PA6,7清除之前设置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA6,7 输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_6|GPIO_Pin_7); //PA6 下拉
//初始化定时器3 TIM3
TIM_TimeBaseStructure.TIM_Period = arr; //设定计数器自动重装值 999
TIM_TimeBaseStructure.TIM_Prescaler =psc; //不分频 0x0 计数器的频率为=72000/(999+1)=72kHZ
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM3输入捕获参数
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_1|TIM_Channel_2; //CC1S=01 选择输入端 IC1和IC2分别映射到TI1、IT2上
TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //通道1和2都是初始化为上升沿捕获
TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1、IT2上
TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM3_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM3, &TIM3_ICInitStructure);
//中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_ITConfig(TIM3, TIM_IT_Update|TIM_IT_CC1|TIM_IT_CC2,ENABLE);//使能定时器3输入捕获中断
TIM_Cmd(TIM3, ENABLE);//开启定时器3
}
u8 TIM3CH1_CAPTURE_STA_A=0; //A相输入捕获状态
u8 TIM3CH2_CAPTURE_STA_B=0; //B相输入捕获状态
//定时器3中断服务程序
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)//捕获1发生捕获事件
{
if(TIM3CH1_CAPTURE_STA_A&0X40)//捕获到一个下降沿
{
LED1=0;
TIM3CH1_CAPTURE_STA_A|=0X80;//标记成功捕获到一次下降沿
if(TIM3CH2_CAPTURE_STA_B&0x40)//如果在A相下降沿时,B相高电平,电机放线
Count_pulse++;
else if(TIM3CH2_CAPTURE_STA_B&0x80)
Count_pulse--;//电机收线
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
}
else//还未开始,第一次捕获上升沿
{
LED1=0;
TIM3CH1_CAPTURE_STA_A=0;//清空状态
TIM3CH1_CAPTURE_STA_A|=0X40; //标记捕获到了上升沿
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
}
}
////////////////////////////////////////////////////////////////////////////////
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)//捕获2发生捕获事件
{
if(TIM3CH2_CAPTURE_STA_B&0X40)//捕获到一个下降沿
{
TIM3CH2_CAPTURE_STA_B|=0X80;//标记成功捕获到一次下降沿
TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
}
else//还未开始,第一次捕获上升沿
{
TIM3CH2_CAPTURE_STA_B=0;//清空状态
TIM3CH2_CAPTURE_STA_B|=0X40; //标记捕获到了上升沿
TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC2P=1 设置为下降沿捕获
}
}
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1|TIM_IT_CC2|TIM_IT_Update); //清除中断标志位
}
///////////////////////////////定时器头文件
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void TIM3_Cap_Init(u16 arr,u16 psc);
#endif
劳烦路过的大神帮忙瞧瞧
Name:
Function:定时器初始化 TIM3_CH4
Description:
******************************************************************************************/
void Tim3_CAP_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟配置
NVIC_InitTypeDef NVIC_InitStructure; //中断控制器变量
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //NVIC配置
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 0; //不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
TIM_TimeBaseStructure.TIM_Period = 0xffffffff;//定时器重装载值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分频因子
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;//通道4
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//捕获选择
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPSC_DIV1;//捕获分频
TIM_ICInitStructure.TIM_ICFilter = 0;//捕获滤波
TIM_ICInit(TIM3,&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;//
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;//下降沿
TIM_ICInit(TIM3,&TIM_ICInitStructure);
TIM_Cmd(TIM3,DISABLE);//使能定时器
TIM_ITConfig(TIM3,TIM_IT_CC4,DISABLE);//使能捕获中断
TIM_ITConfig(TIM3,TIM_IT_CC3,DISABLE);
}
void Tim3_CAP_Disable(void)
{
TIM_Cmd(TIM3,DISABLE);//使能定时器
TIM_ITConfig(TIM3,TIM_IT_CC4,DISABLE);//使能捕获中断
TIM_ITConfig(TIM3,TIM_IT_CC3,DISABLE);
}
void Tim3_CAP_Enable(void)
{
// TDAT_GPIO_Capture(); //引脚设置输入捕获
Measure_Counts =0;
TIM3->CCR4 = 0;
TIM3->CCR3 = 0;//清零
TIM_Cmd(TIM3,ENABLE);
TIM_ITConfig(TIM3,TIM_IT_CC4,ENABLE);
TIM_ITConfig(TIM3,TIM_IT_CC3,ENABLE);
}
unsigned char flag_capture =0;
unsigned int capture_value0 =0,capture_value1=0,capture_value2=0;
unsigned int difference_value_period=0;
unsigned int difference_value_dutycycle=0;
unsigned int Freq_InputCapture[32];
unsigned int DutyCycle_InputCapture[32];
unsigned int Measure_Counts =0;
/******************************************************************************************
Name:
Function:中断函数
Description:
******************************************************************************************/
void TIM3_IRQHandler(void)
{
//上升沿,算频率
if(TIM_GetITStatus(TIM3,TIM_IT_CC4)!= RESET)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC4);
if(flag_capture == 0)
{
capture_value0 = (uint32_t)(TIM3->CCR4);
flag_capture =1;
}
else if(flag_capture == 1)
{
flag_capture =0;
capture_value2 = (uint32_t)(TIM3->CCR4);
//计算
if(capture_value2 > capture_value0)
{
difference_value_period = (capture_value2-capture_value0);
difference_value_dutycycle =capture_value1 - capture_value0;
}
else if(capture_value2 < capture_value0)
{
difference_value_period = ((0xffffffff-capture_value0)+capture_value2);
if(capture_value0 <= capture_value1)
difference_value_dutycycle =capture_value1 - capture_value0;
else
difference_value_dutycycle = ((0xffffffff-capture_value0)+capture_value1);
}
else
{
difference_value_period =0;
difference_value_dutycycle = 0;
}
Freq_InputCapture[Measure_Counts] = (uint32_t)SystemCoreClock/4/difference_value_period;
DutyCycle_InputCapture[Measure_Counts] = difference_value_dutycycle*100/difference_value_period;
Measure_Counts++;
if(Measure_Counts >=32) Measure_Counts =0;
}
}
//捕获下降沿,占空比
if(TIM_GetITStatus(TIM3,TIM_IT_CC3)!= RESET)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC3);
if(flag_capture == 1)
{
capture_value1 = (uint32_t)(TIM3->CCR3);
}
}
}
一周热门 更多>