中级会员
 
- 积分
- 461
- 金钱
- 461
- 注册时间
- 2015-11-29
- 在线时间
- 45 小时
|
请教大家一个问题,我这个电机编码器用串口打印出来的数据为什么为0;请大家指点一下,帮帮忙。
下面是相应的代码:
#include "timer.h"
#include "dianji.h"
#include "sys.h"
u32 temp;
u32 count=0;
u32 predir;
u32 upcount=0;
u32 temp_mileage;
void EncodeInit(u8 none1,u32 period)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = none1; // No prescaling
TIM_TimeBaseStructure.TIM_Period = period;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
//Reset counter
TIM_SetCounter(TIM4,0);
TIM_Cmd(TIM4, ENABLE);
// return 0;
}
void TIM4_IRQHandler(void)
{
temp=(TIM_GetCounter(TIM4)&0xffff);
if(TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
{
if(temp==9999)
{
count--;
if(predir==0)
{
upcount--;
}
else
{
predir=0;
}
}
else if(temp==0)
{
count++;
if(predir==1)
{
upcount++;
}
else
{
predir=1;
}
}
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
}
}
void EncodeGetMileage(void)
{
u8 i=0;
temp=(TIM_GetCounter(TIM4)&0xffff);
if(count<0)
{
temp_mileage=(abs(count)-1)*1000 +(1000-temp);
}
else
{
temp_mileage=count*1000 +temp;
}
// return temp_mileage;
}
|
|