STM32编码测速问题

[复制链接]
13506|23
 楼主| wenwenyuanyuan 发表于 2013-10-24 09:36 | 显示全部楼层 |阅读模式
请问有谁做过相关的STM32F103XXX的正交编码测速?
问题是这样的:要求定时器配置为正交编码模式,在TI1和TI2两个通道同时对A,B两路增量式的360线码盘的上升沿输入捕获,没10ms产生一次中断后计算出此时的电机转速。
有哪个高手做过的,请共享一下例程好吧?再次非常感谢。
 楼主| wenwenyuanyuan 发表于 2013-10-24 12:18 | 显示全部楼层
我的这个问题很难吗?为什么每次我发表问题都看不到高手回帖呢??
huzi2099 发表于 2013-10-24 14:09 | 显示全部楼层
wenwenyuanyuan 发表于 2013-10-24 12:18
我的这个问题很难吗?为什么每次我发表问题都看不到高手回帖呢??

取个时间差就是转速了.
 楼主| wenwenyuanyuan 发表于 2013-10-24 14:44 | 显示全部楼层
huzi2099 发表于 2013-10-24 14:09
取个时间差就是转速了.

那样测出来的转速不精确
huzi2099 发表于 2013-10-24 14:45 | 显示全部楼层
wenwenyuanyuan 发表于 2013-10-24 14:44
那样测出来的转速不精确

为何?那你又想怎?
 楼主| wenwenyuanyuan 发表于 2013-10-24 15:26 | 显示全部楼层
huzi2099 发表于 2013-10-24 14:45
为何?那你又想怎?

我想要利用M/T来测速的,不知道你有没有ST官方的正交编码程序和解释?我这里下载了官方的例程,就是有几点不明白
trumpxp 发表于 2013-10-24 16:06 | 显示全部楼层
兄弟   这一块   我不是很了解   帮你顶一个吧   呼唤一下版主   他应该能够给你一点指导意见
weifengdq 发表于 2013-10-24 16:36 | 显示全部楼层
貌似是200线的能读出来800 1000线的编码器能读出来4000 测的是一个编码器脉冲的时钟周期 STM32 72M 所以测的很准确 这是传统定时器没有的 与传统的M/T法有所差别 比较准确
weifengdq 发表于 2013-10-24 16:37 | 显示全部楼层
  1. /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
  2. * File Name          : stm32f10x_encoder.c
  3. * Author             : IMS Systems Lab  
  4. * Date First Issued  : 21/11/07
  5. * Description        : This file contains the software implementation for the
  6. *                      encoder unit
  7. ********************************************************************************
  8. * History:
  9. * 21/11/07 v1.0
  10. ********************************************************************************
  11. * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  13. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  14. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  15. * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  16. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *******************************************************************************/
  18. /* Includes ------------------------------------------------------------------*/
  19. //#include <stm32f10x_lib.h>
  20. #include "encoder.h"
  21. #include "lcd1602.h"
  22. #include "usart.h"

  23. /* Private variables ---------------------------------------------------------*/
  24. s16 hPrevious_angle, hSpeed_Buffer[SPEED_BUFFER_SIZE], hRot_Speed;
  25. //static s16 hPrevious_angle, hSpeed_Buffer[SPEED_BUFFER_SIZE];
  26. static u8 bSpeed_Buffer_Index = 0;
  27. static volatile u16 hEncoder_Timer_Overflow;

  28. #define TRUE 1
  29. #define FALSE 0
  30. static unsigned char bIs_First_Measurement = TRUE;

  31. /*******************************************************************************
  32. * Function Name  : ENC_Init
  33. * Description    : General Purpose Timer x set-up for encoder speed/position
  34. *                  sensors
  35. * Input          : None
  36. * Output         : None
  37. * Return         : None
  38. *******************************************************************************/
  39. void ENC_Init(void)
  40. {
  41.   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  42.   TIM_ICInitTypeDef TIM_ICInitStructure;
  43.   
  44. /* Encoder unit connected to TIM3, 4X mode */   
  45.   GPIO_InitTypeDef GPIO_InitStructure;
  46.   NVIC_InitTypeDef NVIC_InitStructure;
  47.   
  48.   /* TIM3 clock source enable */
  49.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  50.   /* Enable GPIOA, clock */
  51.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  52.   
  53.   GPIO_StructInit(&GPIO_InitStructure);
  54.   /* Configure PA.06,07 as encoder input */
  55.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  56.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  57.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  58.   
  59.   /* Enable the TIM3 Update Interrupt */
  60.   NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  61.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIMx_PRE_EMPTION_PRIORITY;
  62.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIMx_SUB_PRIORITY;
  63.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  64.   NVIC_Init(&NVIC_InitStructure);

  65.   /* Timer configuration in Encoder mode */
  66.   TIM_DeInit(ENCODER_TIMER);
  67.   TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  68.   
  69.   TIM_TimeBaseStructure.TIM_Prescaler = 0x0;  // No prescaling         //72M捕获?
  70.   TIM_TimeBaseStructure.TIM_Period = (4*ENCODER_PPR)-1;  
  71.   TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  72.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   
  73.   TIM_TimeBaseInit(ENCODER_TIMER, &TIM_TimeBaseStructure);

  74.   TIM_EncoderInterfaceConfig(ENCODER_TIMER, TIM_EncoderMode_TI12,
  75.                              TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
  76.   TIM_ICStructInit(&TIM_ICInitStructure);
  77.   TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER;
  78.   TIM_ICInit(ENCODER_TIMER, &TIM_ICInitStructure);
  79.   
  80. // Clear all pending interrupts
  81.   TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);
  82.   TIM_ITConfig(ENCODER_TIMER, TIM_IT_Update, ENABLE);
  83.   //Reset counter
  84.   TIM2->CNT = COUNTER_RESET;
  85.   
  86.   ENC_Clear_Speed_Buffer();
  87.   
  88.   TIM_Cmd(ENCODER_TIMER, ENABLE);  
  89. }

  90. /*******************************************************************************
  91. * Function Name  : ENC_Get_Electrical_Angle
  92. * Description    : Returns the absolute electrical Rotor angle
  93. * Input          : None
  94. * Output         : None
  95. * Return         : Rotor electrical angle: 0 -> 0 degrees,
  96. *                                          S16_MAX-> 180 degrees,
  97. *                                          S16_MIN-> -180 degrees                  
  98. *******************************************************************************/
  99. s16 ENC_Get_Electrical_Angle(void)
  100. {
  101.   s32 temp;
  102.   
  103.   temp = (s32)(TIM_GetCounter(ENCODER_TIMER)) * (s32)(4294967295 / (4*ENCODER_PPR));
  104.   return((s16)(temp/65536)); // s16 result
  105. }

  106. /*******************************************************************************
  107. * Function Name  : ENC_Clear_Speed_Buffer
  108. * Description    : Clear speed buffer used for average speed calculation  
  109. * Input          : None
  110. * Output         : None
  111. * Return         : None
  112. *******************************************************************************/
  113. void ENC_Clear_Speed_Buffer(void)
  114. {   
  115.   u32 i;

  116.   for (i=0;i<SPEED_BUFFER_SIZE;i++)
  117.   {
  118.     hSpeed_Buffer[i] = 0;
  119.   }
  120.   bIs_First_Measurement = TRUE;
  121. }

  122. /*******************************************************************************
  123. * Function Name  : ENC_Calc_Rot_Speed
  124. * Description    : Compute return latest speed measurement
  125. * Input          : None
  126. * Output         : s16
  127. * Return         : Return the speed in 0.1 Hz resolution.                    
  128. *******************************************************************************/
  129. s16 ENC_Calc_Rot_Speed(void)
  130. {   
  131.   s32 wDelta_angle;
  132.   u16 hEnc_Timer_Overflow_sample_one, hEnc_Timer_Overflow_sample_two;
  133.   u16 hCurrent_angle_sample_one, hCurrent_angle_sample_two;
  134.   signed long long temp;
  135.   s16 haux;
  136.   
  137.   if (!bIs_First_Measurement)
  138.   {
  139.     // 1st reading of overflow counter   
  140.     hEnc_Timer_Overflow_sample_one = hEncoder_Timer_Overflow;
  141.     // 1st reading of encoder timer counter
  142.     hCurrent_angle_sample_one = ENCODER_TIMER->CNT;
  143.     // 2nd reading of overflow counter
  144.     hEnc_Timer_Overflow_sample_two = hEncoder_Timer_Overflow;  
  145.     // 2nd reading of encoder timer counter
  146.     hCurrent_angle_sample_two = ENCODER_TIMER->CNT;      

  147.     // Reset hEncoder_Timer_Overflow and read the counter value for the next
  148.     // measurement
  149.     hEncoder_Timer_Overflow = 0;
  150.     haux = ENCODER_TIMER->CNT;   
  151.    
  152.     if (hEncoder_Timer_Overflow != 0)
  153.     {
  154.       haux = ENCODER_TIMER->CNT;
  155.       hEncoder_Timer_Overflow = 0;            
  156.     }
  157.      
  158.     if (hEnc_Timer_Overflow_sample_one != hEnc_Timer_Overflow_sample_two)
  159.     { //Compare sample 1 & 2 and check if an overflow has been generated right
  160.       //after the reading of encoder timer. If yes, copy sample 2 result in
  161.       //sample 1 for next process
  162.       hCurrent_angle_sample_one = hCurrent_angle_sample_two;
  163.       hEnc_Timer_Overflow_sample_one = hEnc_Timer_Overflow_sample_two;
  164.     }
  165.    
  166.     if ( (ENCODER_TIMER->CR1 & TIM_CounterMode_Down) == TIM_CounterMode_Down)  
  167.     {// encoder timer down-counting
  168.       wDelta_angle = (s32)(hCurrent_angle_sample_one - hPrevious_angle -
  169.                     (hEnc_Timer_Overflow_sample_one) * (4*ENCODER_PPR));
  170.     }
  171.     else  
  172.     {//encoder timer up-counting
  173.       wDelta_angle = (s32)(hCurrent_angle_sample_one - hPrevious_angle +
  174.                     (hEnc_Timer_Overflow_sample_one) * (4*ENCODER_PPR));
  175.     }
  176.    
  177.     // speed computation as delta angle * 1/(speed sempling time)
  178.     temp = (signed long long)(wDelta_angle * SPEED_SAMPLING_FREQ);
  179.     temp *= 10;  // 0.1 Hz resolution
  180.     temp /= (4*ENCODER_PPR);
  181.         
  182.   } //is first measurement, discard it
  183.   else
  184.   {
  185.     bIs_First_Measurement = FALSE;
  186.     temp = 0;
  187.     hEncoder_Timer_Overflow = 0;
  188.     haux = ENCODER_TIMER->CNT;      
  189.     // Check if Encoder_Timer_Overflow is still zero. In case an overflow IT
  190.     // occured it resets overflow counter and wPWM_Counter_Angular_Velocity
  191.     if (hEncoder_Timer_Overflow != 0)
  192.     {
  193.       haux = ENCODER_TIMER->CNT;
  194.       hEncoder_Timer_Overflow = 0;            
  195.     }
  196.   }
  197.   
  198.   hPrevious_angle = haux;  

  199.   return((s16) temp);
  200. }

  201. /*******************************************************************************
  202. * Function Name  : ENC_Calc_Average_Speed
  203. * Description    : Compute smoothed motor speed based on last SPEED_BUFFER_SIZE
  204.                    informations and store it variable  
  205. * Input          : None
  206. * Output         : s16
  207. * Return         : Return rotor speed in 0.1 Hz resolution. This routine
  208.                    will return the average mechanical speed of the motor.
  209. *******************************************************************************/
  210. void ENC_Calc_Average_Speed(void)
  211. {   
  212.   s32 wtemp,wtemp_1=0;
  213.   u32 i;
  214.   
  215. //  wtemp = ENC_Calc_Rot_Speed();
  216. //        
  217. ///* Compute the average of the read speeds */  
  218. //  hSpeed_Buffer[bSpeed_Buffer_Index] = (s16)wtemp;
  219. //  bSpeed_Buffer_Index++;
  220. //  
  221. //  if (bSpeed_Buffer_Index == SPEED_BUFFER_SIZE)
  222. //  {
  223. //    bSpeed_Buffer_Index = 0;
  224. //  }

  225. //  wtemp=0;

  226. //  for (i=0;i<SPEED_BUFFER_SIZE;i++)
  227. //  {
  228. //    wtemp += hSpeed_Buffer[i];
  229. //  }
  230. //  wtemp /= SPEED_BUFFER_SIZE;


  231.                 for (i=0;i<SPEED_BUFFER_SIZE;i++)
  232.                 {
  233.                   if (bSpeed_Buffer_Index == SPEED_BUFFER_SIZE)
  234.                         {
  235.                                 bSpeed_Buffer_Index = 0;
  236.                         }

  237.                         wtemp = ENC_Calc_Rot_Speed();
  238.                         hSpeed_Buffer[bSpeed_Buffer_Index] = (s16)wtemp;
  239.                         bSpeed_Buffer_Index++;
  240.                         wtemp_1 += wtemp;       
  241.                 }       
  242.   wtemp_1 /= SPEED_BUFFER_SIZE;
  243.   
  244.   hRot_Speed = ((s16)(wtemp_1));
  245. }

  246. /*******************************************************************************
  247. * Function Name  : LCD_Display
  248. * Description    : This function handles the display of timer counter, theta and
  249.                     electronical frequency:
  250.                     theta --- resolution: 1 degree;
  251.                     electronical frequency --- resolution: 0.1Hz.
  252. * Input          : None
  253. * Output         : None
  254. * Return         : None
  255. *******************************************************************************/
  256. //void LCD_Display(DisplayType DisplayStatus)
  257. //{
  258. //  u16 hValue;
  259. //  s16 Theta;
  260. //  s16 hSpeed;
  261. //  char *pstr;
  262. //  
  263. //  switch (DisplayStatus)
  264. //  {
  265. //    case DISPLAY_TIMCNT:
  266. //      hValue = TIM_GetCounter(ENCODER_TIMER);
  267. //      LCD_ShowNum(0,0,(hValue),5);  
  268. //                        printf("%d,",hValue);
  269. //    break;
  270. //   
  271. //    case DISPLAY_THETA:      
  272. //      Theta = ENC_Get_Electrical_Angle()*360/65535;        //U16_MAX;
  273. //      if (Theta < 0)
  274. //      {
  275. //        hValue = (u16)(-Theta);
  276. ////        pstr = int2char(hValue);
  277. //        *pstr = '-';
  278. //      }
  279. //      else
  280. //      {
  281. //        hValue = (u16)Theta;
  282. ////        pstr = int2char(hValue);
  283. //        if (hValue != 0) *pstr = '+';  
  284. //      }
  285. //     LCD_ShowChar(0,8,*pstr);
  286. //                LCD_ShowNum(0,9,hValue,5);
  287. //    break;
  288. //   
  289. //    default:
  290. //      hSpeed = hRot_Speed;
  291. //      if (hSpeed < 0)
  292. //      {
  293. //        hValue = (u16)(-hSpeed);
  294. ////        pstr = int2char(hValue);
  295. //        *pstr = '-';
  296. //      }
  297. //      else
  298. //      {
  299. //        hValue = (u16)hSpeed;
  300. ////        pstr = int2char(hValue);
  301. //        if (hValue != 0) *pstr = '+';  
  302. //      }
  303. //                        LCD_ShowChar(0,8,*pstr);
  304. //      LCD_ShowNum(1,0,*pstr,5);
  305. //    break;
  306. //  }
  307. //}

  308. /*******************************************************************************
  309. * Function Name  : TIM2_IRQHandler
  310. * Description    : This function handles TIMx Update interrupt request.
  311.                    Encoder unit connected to TIM2
  312. * Input          : None
  313. * Output         : None
  314. * Return         : None
  315. *******************************************************************************/
  316. void TIM3_IRQHandler(void)
  317. {  
  318.   /* Clear the interrupt pending flag */
  319.   TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);
  320.   
  321.   if (hEncoder_Timer_Overflow != 65535)  
  322.   {
  323.    hEncoder_Timer_Overflow++;
  324.   }
  325. }





  326. //寄存器版本
  327. /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/


  328. //#include "encoder.h"

  329. //void Encoder_Init(void)
  330. //{
  331. //        /* TIM3 clock source enable */
  332. //        RCC->APB1ENR|=1<<1;       //TIM3时钟使能
  333. //        /* Enable GPIOA, clock */
  334. //        RCC->APB2ENR|=1<<2;    //使能PORTA时钟

  335. //        /* Configure PA.06,07 as encoder input */
  336. //        GPIOA->CRL&=0XF0FFFFFF;//PA6
  337. //        GPIOA->CRL|=0X04000000;//浮空输入
  338. //        GPIOA->CRL&=0X0FFFFFFF;//PA7
  339. //        GPIOA->CRL|=0X40000000;//浮空输入

  340. //        /* Enable the TIM3 Update Interrupt */
  341. //        //这两个东东要同时设置才可以使用中断
  342. //        TIM3->DIER|=1<<0;   //允许更新中断                               
  343. //        TIM3->DIER|=1<<6;   //允许触发中断
  344. //        MY_NVIC_Init(1,3,TIM3_IRQChannel,2);

  345. //        /* Timer configuration in Encoder mode */
  346. //        TIM3->PSC = 0x0;//预分频器
  347. //        TIM3->ARR = ENCODER_TIM_PERIOD-1;//设定计数器自动重装值
  348. //        TIM3->CR1 &=~(3<<8);// 选择时钟分频:不分频
  349. //        TIM3->CR1 &=~(3<<5);// 选择计数模式:边沿对齐模式
  350. //               
  351. //        TIM3->CCMR1 |= 1<<0; //CC1S='01' IC1FP1映射到TI1
  352. //        TIM3->CCMR1 |= 1<<8; //CC2S='01' IC2FP2映射到TI2
  353. //        TIM3->CCER &= ~(1<<1);         //CC1P='0'         IC1FP1不反相,IC1FP1=TI1
  354. //        TIM3->CCER &= ~(1<<5);         //CC2P='0'         IC2FP2不反相,IC2FP2=TI2
  355. //        TIM3->CCMR1 |= 3<<4; //        IC1F='1000' 输入捕获1滤波器
  356. //        TIM3->SMCR |= 3<<0;         //SMS='011' 所有的输入均在上升沿和下降沿有效
  357. //        TIM3->CNT = COUNTER_RESET;
  358. //        TIM3->CR1 |= 0x01;    //CEN=1,使能定时器
  359. //}

weifengdq 发表于 2013-10-24 16:38 | 显示全部楼层
  1. #ifndef __ENCODER_H__
  2. #define __ENCODER_H__

  3. #include "sys.h"

  4. //void Encoder_Init(void);

  5. //#endif

  6. /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
  7. * File Name          : stm32f10x_encoder.h
  8. * Author             : IMS Systems Lab
  9. * Date First Issued  : 21/11/07
  10. * Description        : This file contains the software implementation for the
  11. *                      encoder position and speed reading.
  12. ********************************************************************************
  13. * History:
  14. * 21/11/07 v1.0
  15. ********************************************************************************
  16. * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  17. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  18. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  19. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  20. * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  21. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  22. *******************************************************************************/

  23. /* Define to prevent recursive inclusion -------------------------------------*/


  24. /* Private typedef -----------------------------------------------------------*/
  25. typedef enum {DISPLAY_TIMCNT = 0,DISPLAY_THETA,DISPLAY_W} DisplayType;

  26. /* Private typedef -----------------------------------------------------------*/
  27. /* Private define ------------------------------------------------------------*/
  28. #define ENCODER_TIMER   TIM3  // Encoder unit connected to TIM3
  29. #define ENCODER_PPR           (u16)(200)   // number of pulses per revolution
  30. #define SPEED_BUFFER_SIZE 2//8

  31. #define COUNTER_RESET   (u16)0
  32. #define ICx_FILTER      (u8) 6 // 6<-> 670nsec

  33. #define TIMx_PRE_EMPTION_PRIORITY 1
  34. #define TIMx_SUB_PRIORITY 0

  35. #define SPEED_SAMPLING_FREQ (u16)(2000/(SPEED_SAMPLING_TIME+1))

  36. extern s16 hRot_Speed;


  37. /* Private functions ---------------------------------------------------------*/
  38. s16 ENC_Calc_Rot_Speed(void);


  39. /* Includes ------------------------------------------------------------------*/
  40. /* Private define ------------------------------------------------------------*/
  41. #define SPEED_SAMPLING_TIME  9     // (9+1)*500usec = 5msec

  42. /* Exported functions ------------------------------------------------------- */
  43. void ENC_Init(void);
  44. s16 ENC_Get_Electrical_Angle(void);
  45. void ENC_Clear_Speed_Buffer(void);
  46. void ENC_Calc_Average_Speed(void);
  47. //void LCD_Display(DisplayType DisplayStatus);
  48. s16 ENC_Get_Speed(void);
  49. void TIM3_IRQHandler(void);;

  50. #endif  
  51. /*__STM32F10x_ENCODER_H*/
  52. /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
weifengdq 发表于 2013-10-24 16:40 | 显示全部楼层
这是暑假参加电赛时(做的倒立摆)山寨的代码 当时用着很好用(放了放编译不通过了 某些地方有问题 你参考参考吧) 一个enconder.c 一个是encoder.h
huzi2099 发表于 2013-10-24 18:07 | 显示全部楼层
wenwenyuanyuan 发表于 2013-10-24 15:26
我想要利用M/T来测速的,不知道你有没有ST官方的正交编码程序和解释?我这里下载了官方的例程,就是有几 ...

哪里不明白?
huzi2099 发表于 2013-10-24 18:14 | 显示全部楼层
weifengdq 发表于 2013-10-24 16:36
貌似是200线的能读出来800 1000线的编码器能读出来4000 测的是一个编码器脉冲的时钟周期 STM32 72M 所以测 ...

上下沿都计数的模式是四倍
MCUWYL 发表于 2013-10-24 18:55 | 显示全部楼层
围观、、、
 楼主| wenwenyuanyuan 发表于 2013-10-25 07:52 | 显示全部楼层
有知道的请帮忙一下,不知道的请把它顶起来,让高手们看到!
 楼主| wenwenyuanyuan 发表于 2013-10-25 07:59 | 显示全部楼层
weifengdq 发表于 2013-10-24 16:37

你好!很感谢你的分享,但有一点我不太懂,就是void ENC_Init(void)这个函数中的倒数第三行程序“TIM2->CNT = COUNTER_REST”我想知道的是,我们的正交编码配置不是配置的是TIM3吗,为什么计数器清零清的是TIM2呢?应该是写成“TIM3->CNT = COUNTER_REST”才对吧?求指导
leebai_001 发表于 2013-10-25 08:19 | 显示全部楼层
STM32单片机原厂提供的代码,只能实现M法测速,测速闸门时间取决于查询周期,这在高速运行时没问题。M法不适合低速,如果需要控制电机在低速或极低速下运行,会有问题。

原厂单片机提供的测速方式是在TIM2上实现的,如果你需要更换定时器,自己需要把对应的定时器设计进行修改。
weifengdq 发表于 2013-10-25 12:49 | 显示全部楼层
wenwenyuanyuan 发表于 2013-10-25 07:59
你好!很感谢你的分享,但有一点我不太懂,就是void ENC_Init(void)这个函数中的倒数第三行程序“TIM2- ...

嗯 应该是我改错了 是TIM3 源代码给你 用的是MDK 4.72a编译的 或许某些地方还有类似的错误 仅供参考
http://pan.baidu.com/s/1ABZJD

评分

参与人数 1威望 +3 收起 理由
wenwenyuanyuan + 3 谢谢了!你的程序对我帮助很大。.

查看全部评分

weifengdq 发表于 2013-10-25 12:57 | 显示全部楼层
 楼主| wenwenyuanyuan 发表于 2013-10-25 13:24 | 显示全部楼层
顺便问一句,为什么我在结贴的时候,总是弹出说“没有结贴权限”呢?求吧主解释下!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

8

主题

56

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部