打印

软件测试

[复制链接]
1326|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wanda216|  楼主 | 2013-2-15 21:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/*0001*/  void ENC_Init(void)
/*0002*/  {
/*0003*/          TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/*0004*/          TIM_ICInitTypeDef TIM_ICInitStructure;
/*0005*/         
/*0006*/          /* Encoder unit connected to TIM3, 4X mode */   
/*0007*/          GPIO_InitTypeDef GPIO_InitStructure;
/*0008*/          NVIC_InitTypeDef NVIC_InitStructure;
/*0009*/         
/*0010*/          /* TIM3 clock source enable */
/*0011*/          RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/*0012*/          /* Enable GPIOA, clock */
/*0013*/          RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/*0014*/         
/*0015*/          GPIO_StructInit(&GPIO_InitStructure);
/*0016*/          /* Configure PA.06,07 as encoder input */
/*0017*/          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
/*0018*/          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
/*0019*/          GPIO_Init(GPIOA, &GPIO_InitStructure);
/*0020*/         
/*0021*/          /* Enable the TIM3 Update Interrupt */
/*0022*/          NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
/*0023*/          NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIMx_PRE_EMPTION_PRIORITY;
/*0024*/          NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIMx_SUB_PRIORITY;
/*0025*/          NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/*0026*/          NVIC_Init(&NVIC_InitStructure);
/*0027*/         
/*0028*/          /* Timer configuration in Encoder mode */
/*0029*/          TIM_DeInit(ENCODER_TIMER);
/*0030*/          TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
/*0031*/         
/*0032*/          TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling
/*0033*/          TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD;
/*0034*/          TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
/*0035*/          TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   
/*0036*/          TIM_TimeBaseInit(ENCODER_TIMER, &TIM_TimeBaseStructure);
/*0037*/         
/*0038*/          TIM_EncoderInterfaceConfig(ENCODER_TIMER, TIM_EncoderMode_TI12,
/*0039*/                                       TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
/*0040*/          TIM_ICStructInit(&TIM_ICInitStructure);
/*0041*/          TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER;
/*0042*/          TIM_ICInit(ENCODER_TIMER, &TIM_ICInitStructure);
/*0043*/         
/*0044*/          // Clear all pending interrupts
/*0045*/          TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);
/*0046*/          TIM_ITConfig(ENCODER_TIMER, TIM_IT_Update, ENABLE);
/*0047*/          //Reset counter
/*0048*/          TIM2->CNT = COUNTER_RESET;
/*0049*/         
/*0050*/          // ENC_Clear_Speed_Buffer();
/*0051*/         
/*0052*/          TIM_Cmd(ENCODER_TIMER, ENABLE);
/*0053*/  }
/*0054*/  
/*0055*/  以下为获取一次计数值,此算法来自lxyppc,可以规避超过16位的情况,具体细节见http://bbs.21ic.com/viewthread.php?tid=110623的讨论
/*0056*/  
/*0057*/  s16 ENC_Get_Electrical_Angle(void)
/*0058*/  {
/*0059*/          static u16   lastCount = 0;
/*0060*/         
/*0061*/          u16 curCount = ENCODER_TIMER->CNT;
/*0062*/          s32 dAngle = curCount - lastCount;
/*0063*/         
/*0064*/          if(dAngle >= MAX_COUNT){
/*0065*/              dAngle -= ENCODER_TIM_PERIOD;
/*0066*/          }else if(dAngle < -MAX_COUNT){
/*0067*/              dAngle += ENCODER_TIM_PERIOD;
/*0068*/          }
/*0069*/          lastCount = curCount;
/*0070*/          return (s16)dAngle;
/*0071*/  }
/*0072*/  
/*0073*/  以下为系统滴答的初始化和中断函数
/*0074*/  
/*0075*/  void TB_Init(void)
/*0076*/  {   
/*0077*/          /* Setup SysTick Timer for 10 msec interrupts */
/*0078*/          if (SysTick_Config(SystemFrequency / 100))
/*0079*/          {
/*0080*/              /* Capture error */
/*0081*/              while (1);
/*0082*/          }
/*0083*/  }
/*0084*/  
/*0085*/  void SysTick_Handler(void)
/*0086*/  {   
/*0087*/          /*if (hTimebase_display_500us != 0)
|*0088*|          {
|*0089*|              hTimebase_display_500us --;
|*0090*|          }
|*0091*|             */

/*0092*/          if (hSpeedMeas_Timebase_500us !=0)
/*0093*/          {
/*0094*/              hSpeedMeas_Timebase_500us--;
/*0095*/          }
/*0096*/          else
/*0097*/          {
/*0098*/              hSpeedMeas_Timebase_500us = SPEED_SAMPLING_TIME;
/*0099*/         
/*0100*/              CurrentCount += ENC_Get_Electrical_Angle();
/*0101*/                  
/*0102*/              //ENC_Calc_Average_Speed must be called ONLY every SPEED_MEAS_TIMEBASE ms
/*0103*/              //ENC_Calc_Average_Speed();   
/*0104*/          }
/*0105*/  }
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

8

主题

13

帖子

0

粉丝