编码器程序。运行后TIM_Counter(TIM3)得不到数值是为什么?求指教!谢谢

2019-07-20 23:06发布

下面是我按网上的例程改写的旋转编码器角度的计算程序,用定时器计数值来计算角度,利用TIM_Counter(TIM3)来得到TIM3的计数值,然后用来算旋转角。编译没有出错,但是单步运行时会发现TIMX为0x40000400,但是jishu,temp,angle都是0,这是为什么?
求指教!谢谢

 #include "timer.h"
#include "usart.h"
#include "stm32f10x_tim.h"
#include "sys.h"

void TIM_Init(void)   
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 TIM_ICInitTypeDef TIM_ICInitStructure; 
 NVIC_InitTypeDef NVIC_InitStructure;   
 GPIO_InitTypeDef GPIO_InitStructure;  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); 
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); 
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
  GPIO_Init(GPIOC, &GPIO_InitStructure);     
 
 NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  
 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
 NVIC_Init(&NVIC_InitStructure);   TIM_DeInit(TIM3);

 TIM_TimeBaseStructure.TIM_Period = 999; 
 TIM_TimeBaseStructure.TIM_Prescaler =0x00; 
 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=0x03;
  TIM_ICInit(TIM3,&TIM_ICInitStructure);
 
 TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); 
 TIM_ClearFlag(TIM3,TIM_FLAG_Update);  TIM3->CNT=0; 
 
 TIM_Cmd(TIM3, ENABLE);  
 } s32 Get_Angle(void)
{
 s32 temp;
 s32 angle;
 s32 jishu;
 jishu=TIM_GetCounter(TIM3);
 temp=(jishu)*360/1000;
 angle=temp/65536;
 return angle;
} void TIM3_IRQHandler(void)
{
 if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
 {
  TIM_ClearFlag(TIM3,TIM_FLAG_Update); 
  TIM_ClearITPendingBit(TIM3, TIM_IT_Update); 
 }
}  
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。