关于F4的编码器测速

2019-07-20 22:03发布

本人正在学小车编码器测速 求哪位大神给个F4板子上能用的完整的编码器测速例程 不胜感激
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
3条回答
正点原子
1楼-- · 2019-07-21 03:50
论坛搜索下,参考下别人的例程吧
pjhyx
2楼-- · 2019-07-21 06:17
void TIM3_Int_Init(u16 arr,u16 psc)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        NVIC_InitTypeDef NVIC_InitStructure;

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ʱÖÓʹÄÜ
       
        //¶¨Ê±Æ÷TIM3³õʼ»¯
        TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ       
        TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòÉϼÆÊýģʽ
        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»       
// TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);//Ñ¡ÔñTIMx´Óģʽ
        TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //ʹÄÜÖ¸¶¨µÄTIM3ÖжÏ,ÔÊÐí¸üÐÂÖжÏ

        //ÖжÏÓÅÏȼ¶NVICÉèÖÃ
        NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3ÖжÏ
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //ÏÈÕ¼ÓÅÏȼ¶3¼¶
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //´ÓÓÅÏȼ¶3¼¶
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀ±»Ê¹ÄÜ
        NVIC_Init(&NVIC_InitStructure);  //³õʼ»¯NVIC¼Ä´æÆ÷


       
        TIM_Cmd(TIM3, ENABLE);  //ʹÄÜTIMx                                         
}


void TIM2_Encoder_Init(u16 arr,u16 psc)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        TIM_ICInitTypeDef TIM_ICInitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
       
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //ʱÖÓʹÄÜ
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
       
        //GPIO_StructInit(&GPIO_InitStructure);
  /* Configure PC.06,07 as encoder input */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);


  GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_TIM2);
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM2);
       
        //¶¨Ê±Æ÷TIM2³õʼ»¯
        TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ       
        TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòÉϼÆÊýģʽ
        TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
        //TIM_SetCounter(TIM3, 0xFF88);//ÉèÖÃTIMx×Ô¶¯ÖØ×°ÔؼĴæÆ÷Öµ

        TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12,
                                TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);
        TIM_ICStructInit(&TIM_ICInitStructure);
        TIM_ICInitStructure.TIM_ICFilter = 6;
        //TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;        //ÉÏÉýÑز¶»ñ
        //TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;         //ÅäÖÃÊäÈë·ÖƵ,²»·ÖƵ
        TIM_ICInit(TIM2, &TIM_ICInitStructure);
  //TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);//Ñ¡ÔñTIMx´Óģʽ
  TIM_ClearFlag(TIM2, TIM_FLAG_Update);
        TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //ʹÄÜÖ¸¶¨µÄTIM3ÖжÏ,ÔÊÐí¸üÐÂÖжÏ
  //TIM2->CNT = 120;
        //ÖжÏÓÅÏȼ¶NVICÉèÖÃ
        NVIC_InitStructure.NVIC_IRQChannel = TIM_Channel_1|TIM_Channel_2;  //TIM3ÖжÏ
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //ÏÈÕ¼ÓÅÏȼ¶3¼¶
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //´ÓÓÅÏȼ¶3¼¶
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀ±»Ê¹ÄÜ
  NVIC_Init(&NVIC_InitStructure);  //³õʼ»¯NVIC¼Ä´æÆ÷

  //TIM3_Encoder_Write(65416) ;
       
        TIM_Cmd(TIM2, ENABLE);  //ʹÄÜTIMx                                         
}
pjhyx
3楼-- · 2019-07-21 07:25
pjhyx 发表于 2016-5-25 21:28
void TIM3_Int_Init(u16 arr,u16 psc)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

这是我代码 求各位大神帮忙看看
我在main里用LCD_ShowxNum(250,200,TIM_GetCounter(TIM2),5,16,0);
转轮子发现一直是0啊

一周热门 更多>