需要做一个编码器的程序,编码器为欧姆龙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
劳烦路过的大神帮忙瞧瞧
#include "extcoder.h"
#include "stdio.h"
#include "stm32f10x_it.h"
static int count_negative=0;
static int count_positive=0;
void TIM3_coder_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//定时器初始化结构体
TIM_ICInitTypeDef TIM_ICInitStructure;//通道输入比较结构体
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // TIM3 时钟源使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // GPIOA时钟使能
GPIO_StructInit(&GPIO_InitStructure); //将PA6,PA7配置为编码器输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//使能TIM3中断
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* 定时器配置在编码器模式 */
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);//填入缺省值
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;// 没有预引比例因子 ,预分频值
TIM_TimeBaseStructure.TIM_Period = (2000)-1;//计数值,编码器线数为500
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//TIMx 使用的定时器
//TIM_EncoderMode 模式,单向计数(只反映速度);两相计数(速度和方向)
//TIM_IC1Polarity 通道1的捕捉极性
//TIM_IC2Polarity 通道2的捕捉极性
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//配置定时器编码器接口函数
TIM_ICStructInit(&TIM_ICInitStructure);//填入缺省值
TIM_ICInitStructure.TIM_ICFilter = 6; // 设置使用滤波器的长度
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);//清除定时器3挂起的中断
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);//使能定时器3中断
TIM3->CNT = 0;//计数器归0
//ENC_Clear_Speed_Buffer();
TIM_Cmd(TIM3, ENABLE);//开启定时器3
}
float ENC_Get_Electrical_Angle(void)
{
float temp;
if((count_negative+count_positive)>=0)
temp = (TIM_GetCounter(TIM3) * 1.0) * ( 360.0 / (2000)) + (count_negative+count_positive) * 360.0;
else
temp = (TIM_GetCounter(TIM3)*1.0 - 2000) * ( 360.0/ (2000)) + (count_negative+count_positive + 1) * 360.0;
return(temp);
}
//定时器3中断函数
void TIM3_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM3, TIM_FLAG_Update);//清除中断标志位
if((TIM3->CR1 & 0x0010)==0x0010)//如果计数器是向下计数
count_negative--;
else//计数器是向上计数
count_positive++;
}
///////////////////////////////////////////////////////////////
可以使用的程序,编码器线数为500,AB相计数
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);
}
}
}
可以产生中断了,但是我只是想要计算脉冲的个数。但是程序在编码器快速转的时候,LCD上没有显示增加,只有在转的特别慢的时候才会增加。
#include "time.h"
#include "sys.h"
#include "led.h"
#include "delay.h"
#include "lcd.h"
#include "string.h"
#include "stdio.h"
//编码器
//定时器3通道1和2输入捕获配置
TIM_ICInitTypeDef TIM3_ICInitStructure;
void TIM3_Cap_Init(void)
{
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 = 999; //设定计数器自动重装值 999
TIM_TimeBaseStructure.TIM_Prescaler = 0; //不分频 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; //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);
TIM3_ICInitStructure.TIM_Channel = 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 = 0; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_ITConfig(TIM3, TIM_IT_CC1|TIM_IT_CC2,ENABLE);//使能定时器3输入捕获中断
TIM_Cmd(TIM3, ENABLE);//开启定时器3
}
//定时器3中断服务程序
//extern vs16 Count;
u8 Show_Data[30] = { 0 };
vs16 Count_pulse=0;
u8 TIM3CH1_CAPTURE_STA_A=0; //A相电平状态
u8 TIM3CH2_CAPTURE_STA_B=0; //B相电平状态
void TIM3_IRQHandler(void)
{
//捕获1发生捕获事件
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); //清除中断标志位
if(TIM3CH1_CAPTURE_STA_A==1)//A↓
{
LED1=1,TIM3CH1_CAPTURE_STA_A=0;
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising);//CC1P=0 设置为上升沿捕获
if(TIM3CH2_CAPTURE_STA_B==1)//A脉冲结束时,B相电平为高电平时
{
Count_pulse++;
sprintf((char*)Show_Data, "ROTARY ANGLE:%d D", Count_pulse),LCD_ShowString(25,260,320,16,16,Show_Data);
}
else
{
Count_pulse--;
sprintf((char*)Show_Data, "ROTARY ANGLE:%d D", Count_pulse),LCD_ShowString(25,260,320,16,16,Show_Data);
}
}
else//A↑
{
TIM3CH1_CAPTURE_STA_A=0;//清空状态
LED1=0,TIM3CH1_CAPTURE_STA_A=1;
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Falling);//CC1P=1 设置为下降沿捕获
}
}
//捕获2发生捕获事件
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
if(TIM3CH2_CAPTURE_STA_B == 1)// B↓
{
TIM3CH2_CAPTURE_STA_B = 0;
TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Rising);
}
else// B↑
{
TIM3CH2_CAPTURE_STA_B = 0;
TIM3CH2_CAPTURE_STA_B=1;
TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Falling);
}
}
}
一周热门 更多>