发 帖  
原厂入驻New
[问答] stm32 TIM1 TIM3 编码器模式?
263 stm32
分享
tiM1脉冲数可以TIM1ITCount 获取    TIM3获取脉冲数TIM3->CN   TIM3->只能获取一圈200个脉冲 很奇怪? 求大神解惑
void TIM1_Size_Encoder_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  
TIM_ICInitTypeDef TIM_ICInitStructure;  
GPIO_InitTypeDef GPIO_InitStructure;
   NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphclockCmd(RCC_APB2Periph_TIM1, ENABLE);//?????4???
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//??PA????

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);      
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // ????
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//??????:???
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIM????  
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;//TIM1高级定时器特有
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_EncoderInteRFaceConfig(TIM1, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
   NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);
TIM_ClearFlag(TIM1, TIM_FLAG_Update);//??TIM??????
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
//TIM_ITConfig(TIM1, TIM_IT_Update | TIM_IT_trigger, ENABLE);
//Reset counter
TIM_SetCounter(TIM1,0);
TIM_Cmd(TIM1, ENABLE);     
}
uint16_t TIM1ITCount=0;//脉冲数
void TIM1_UP_IRQHandler(void)
{   
      IF(TIM1->SR&0x0001)//
      {
       TIM1ITCount++;  
      }   
      TIM1->SR&=~(1<<0);//   
}


void TIM3_Angle_Encode_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  
TIM_ICInitTypeDef TIM_ICInitStructure;  
GPIO_InitTypeDef GPIO_InitStructure;
   NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//?????4???
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//??PA????

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);      
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // ????
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//??????:???
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIM????  
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI11, TIM_ICPolarity_Falling, TIM_ICPolarity_Rising); //TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);  
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
   NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);   
TIM_ClearFlag(TIM3, TIM_FLAG_Update);//??TIM??????
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
//Reset counter
TIM_SetCounter(TIM3,0);
TIM_Cmd(TIM3, ENABLE);   
}
TIM3->CN  脉冲数
0
5 天前   评论 分享淘帖 邀请回答
2个回答

撰写答案

你正在撰写答案

如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。

高级模式
您需要登录后才可以回帖 登录 | 注册

我要提问
课程
    关闭

    站长推荐 上一条 /10 下一条

    快速回复 返回顶部 返回列表