#include "encode.h"
#include "sys.h"
#include "delay.h"
#include "lcd.h"
#include "string.h"
#include "stdio.h"
#include "usart.h"
#include "led.h"
//编码器
//定时器3通道1和2输入捕获配置
void Encoder_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//使能TIM3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能GPIOA时钟
//配置IO口PA6和PA7
GPIO_StructInit(&GPIO_InitStructure);//复位GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置定时器3
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; //不分频
TIM_TimeBaseStructure.TIM_Period = 500*4; //计数装载值 编码器线数500
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//配置编码器模式
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);
TIM3->CNT = 0;//计数器归零
TIM_Cmd(TIM3, ENABLE);//使能定时器
}
/*********************************************************/
//输出角度
u8 Show_Data[30]={0};
void Printf_encode_angle(void)
{
// float Theta=0;
TIM3->CNT = 0;
delay_ms(500);
// Theta=(s16)(TIM3->CNT)*0.18;//计算转过的角度0.18=360/500/4
sprintf((char*)Show_Data, "ROTARY ANGLE:%d",(s16)(TIM3->CNT) );
LCD_ShowString(25, 260, 320, 16, 16, Show_Data);
}
编码器放着不去转动它,计数器的数都在不断变化,想知道到底是哪里出了问题
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
终于解决,下面是可以运行的程序,编码器为差分输出,500线。
#include "stm32f10x.h"
#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++;
}
不用中断吧,计数器最大值我可以设为0XFFFF,照理说0.5s的时间也不可能产生更新中断,关键是编码器不动的时候,计数器的值也在不断的变换,应该一直为0吧。
一周热门 更多>