2019物联网开发者大会 12.06 北京北辰洲际酒店→ 报名参会 ←
电子产品世界 » 论坛首页 » 嵌入式开发 » STM32 » [问答] stm32 TIM1 TIM3 编码器模式?

共2条 1/1 1 跳转至

[问答] stm32 TIM1 TIM3 编码器模式?

助工
2019-10-12 12:47:51    评分

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  脉冲数





关键词: stm32     编码器     模式    

专家
2019-10-12 13:09:25    评分
2楼

贴的这是个啥啊



共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]