⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stm32f10x_encoder.lst

📁 STM32利用正交编码器实现电机的控制
💻 LST
📖 第 1 页 / 共 2 页
字号:
    176                haux = ENCODER_TIMER->CNT; 
    177                hEncoder_Timer_Overflow = 0;            
    178              }
    179               
    180              if (hEnc_Timer_Overflow_sample_one != hEnc_Timer_Overflow_sample_two)
    181              { //Compare sample 1 & 2 and check if an overflow has been generated right 
    182                //after the reading of encoder timer. If yes, copy sample 2 result in 
    183                //sample 1 for next process 
    184                hCurrent_angle_sample_one = hCurrent_angle_sample_two;
    185                hEnc_Timer_Overflow_sample_one = hEnc_Timer_Overflow_sample_two;
    186              }
    187              
    188              if ( (ENCODER_TIMER->CR1 & TIM_CounterMode_Down) == TIM_CounterMode_Down)  
    189              {// encoder timer down-counting
    190                wDelta_angle = (s32)(hCurrent_angle_sample_one - hPrevious_angle - 
    191                              (hEnc_Timer_Overflow_sample_one) * (4*ENCODER_PPR));
    192              }
    193              else  
    194              {//encoder timer up-counting
    195                wDelta_angle = (s32)(hCurrent_angle_sample_one - hPrevious_angle + 
    196                              (hEnc_Timer_Overflow_sample_one) * (4*ENCODER_PPR));
    197              }
    198              
    199              // speed computation as delta angle * 1/(speed sempling time)
    200              temp = (signed long long)(wDelta_angle * SPEED_SAMPLING_FREQ);
    201              temp *= 10;  // 0.1 Hz resolution
    202              temp /= (4*ENCODER_PPR);
    203                  
    204            } //is first measurement, discard it
    205            else
    206            {
    207              bIs_First_Measurement = FALSE;
    208              temp = 0;
    209              hEncoder_Timer_Overflow = 0;
    210              haux = ENCODER_TIMER->CNT;       
    211              // Check if Encoder_Timer_Overflow is still zero. In case an overflow IT 
    212              // occured it resets overflow counter and wPWM_Counter_Angular_Velocity
    213              if (hEncoder_Timer_Overflow != 0) 
    214              {
    215                haux = ENCODER_TIMER->CNT; 
    216                hEncoder_Timer_Overflow = 0;            
    217              }
    218            }
    219            
    220            hPrevious_angle = haux;  
    221           
    222            return((s16) temp);
    223          }
    224          
    225          /*******************************************************************************
    226          * Function Name  : ENC_Calc_Average_Speed
    227          * Description    : Compute smoothed motor speed based on last SPEED_BUFFER_SIZE
    228                             informations and store it variable  
    229          * Input          : None
    230          * Output         : s16
    231          * Return         : Return rotor speed in 0.1 Hz resolution. This routine 
    232                             will return the average mechanical speed of the motor.
    233          *******************************************************************************/
    234          void ENC_Calc_Average_Speed(void)
    235          {   
    236            s32 wtemp;
    237            u32 i;
    238            
    239            wtemp = ENC_Calc_Rot_Speed();
    240                  
    241          /* Compute the average of the read speeds */  
    242            hSpeed_Buffer[bSpeed_Buffer_Index] = (s16)wtemp;
    243            bSpeed_Buffer_Index++;
    244            
    245            if (bSpeed_Buffer_Index == SPEED_BUFFER_SIZE) 
    246            {
    247              bSpeed_Buffer_Index = 0;
    248            }
    249          
    250            wtemp=0;
    251          
    252            for (i=0;i<SPEED_BUFFER_SIZE;i++)
    253            {
    254              wtemp += hSpeed_Buffer[i];
    255            }
    256            wtemp /= SPEED_BUFFER_SIZE;
    257            
    258            hRot_Speed = ((s16)(wtemp));
    259          }
    260          
    261          /*******************************************************************************
    262          * Function Name  : LCD_Display
    263          * Description    : This function handles the display of timer counter, theta and
    264                              electronical frequency:
    265                              theta --- resolution: 1 degree;
    266                              electronical frequency --- resolution: 0.1Hz.
    267          * Input          : None
    268          * Output         : None
    269          * Return         : None
    270          *******************************************************************************/
    271          void LCD_Display(DisplayType DisplayStatus)
    272          {
    273            u16 hValue;
    274            s16 Theta;
    275            s16 hSpeed;
    276            char *pstr;
    277            
    278            switch (DisplayStatus)
    279            {
    280              case DISPLAY_TIMCNT: 
    281                hValue = TIM_GetCounter(ENCODER_TIMER);
    282                write_string(int2char(hValue));      
    283              break;
    284              
    285              case DISPLAY_THETA:      
    286                Theta = ENC_Get_Electrical_Angle()*360/U16_MAX;
    287                if (Theta < 0) 
    288                {
    289                  hValue = (u16)(-Theta);
    290                  pstr = int2char(hValue);
    291                  *pstr = '-';
    292                }
    293                else
    294                {
    295                  hValue = (u16)Theta;
    296                  pstr = int2char(hValue);
    297                  if (hValue != 0) *pstr = '+';  
    298                }
    299                write_string(pstr);
    300              break;
    301              
    302              default:
    303                hSpeed = hRot_Speed;
    304                if (hSpeed < 0) 
    305                {
    306                  hValue = (u16)(-hSpeed);
    307                  pstr = int2char(hValue);
    308                  *pstr = '-';
    309                }
    310                else
    311                {
    312                  hValue = (u16)hSpeed;
    313                  pstr = int2char(hValue);
    314                  if (hValue != 0) *pstr = '+';  
    315                }
    316                write_string(pstr);
    317              break;
    318            }
    319          }
    320          
    321          /*******************************************************************************
    322          * Function Name  : TIM2_IRQHandler
    323          * Description    : This function handles TIMx Update interrupt request.
    324                             Encoder unit connected to TIM2
    325          * Input          : None
    326          * Output         : None
    327          * Return         : None
    328          *******************************************************************************/
    329          void TIM3_IRQHandler(void)
    330          {  
    331            /* Clear the interrupt pending flag */
    332            TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);
    333            
    334            if (hEncoder_Timer_Overflow != U16_MAX)  
    335            {
    336             hEncoder_Timer_Overflow++;
    337            }
    338          }
    339          
    340          /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/

   Maximum stack usage in bytes:

     Function                 CSTACK
     --------                 ------
     ENC_Calc_Average_Speed       4
     ENC_Calc_Rot_Speed          16
     ENC_Clear_Speed_Buffer       0
     ENC_Get_Electrical_Angle     4
     ENC_Init                    32
     LCD_Display                  8
     TIM3_IRQHandler              4


   Segment part sizes:

     Function/Label                 Bytes
     --------------                 -----
     hPrevious_angle                   2
     hSpeed_Buffer                    16
     hRot_Speed                        2
     bSpeed_Buffer_Index               1
     hEncoder_Timer_Overflow           2
     bIs_First_Measurement             1
     ENC_Init                        224
     ENC_Get_Electrical_Angle         32
     ENC_Clear_Speed_Buffer           28
     ENC_Calc_Rot_Speed              220
     ENC_Calc_Average_Speed           80
     LCD_Display                     172
     TIM3_IRQHandler                  34
     ??DataTable21                     4
     ??DataTable28                     4
     ??DataTable32                     4
     ??DataTable33                     4
     ??DataTable35                     4
     ??DataTable37                     4
     ?<Initializer for bIs_First_Measurement>
                                       1
      Others                         200

 
 990 bytes in segment CODE
   1 byte  in segment DATA_I
   1 byte  in segment DATA_ID
  23 bytes in segment DATA_Z
  24 bytes in segment INITTAB
 
 814 bytes of CODE  memory (+ 200 bytes shared)
   1 byte  of CONST memory
  24 bytes of DATA  memory

Errors: none
Warnings: none

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -