[Hỏi] Cảm biến siêu âm srf05 dùng stm32f4 bị tràn timer

Collapse
X
 
  • Giờ
  • Show
Clear All
new posts
  • locthanh3005
    Thành viên mới
    • Oct 2009
    • 13

    #1

    [Hỏi] Cảm biến siêu âm srf05 dùng stm32f4 bị tràn timer

    mình đang làm đo khoảng cách bằng cảm biến siêu âm srf05 nhưng bị tràn timer nên chỉ đếm đc tới 65535 thôi... ae nào rành giúp mình đi... hjxhjx

    main.c
    PHP Code:
    #include "stm32f4_discovery.h"
    
    TIM_ICInitTypeDef  TIM_ICInitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    void TIM_Config(void);
    
    void Delay(__IO uint32_t time)
    {
     /*Delay is in milliseconds*/
        time=time*80000;  
      while(time--)
      {
      }
    }
    int main(void)
    {
    
      /* TIM Configuration */
      TIM_Config();
    
      /* TIM4 configuration: PWM Input mode ------------------------
         The external signal is connected to TIM4 CH2 pin (PB.07), 
         The Rising edge is used as active edge,
         The TIM4 CCR2 is used to compute the frequency value 
         The TIM4 CCR1 is used to compute the duty cycle value
      ------------------------------------------------------------ */
    
      TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
      TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
      TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
      TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
      TIM_ICInitStructure.TIM_ICFilter = 0x0;
    
      TIM_PWMIConfig(TIM4, &TIM_ICInitStructure);
    
      /* Select the TIM4 Input Trigger: TI2FP2 */
      TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);
    
      /* Select the slave Mode: Reset Mode */
      TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
      TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);
    
     
      /* Enable the CC2 Interrupt Request */
      TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);
    
      STM_EVAL_LEDInit(LED3);
        STM_EVAL_LEDInit(LED4);        
    }
    
    
    void TIM_Config(void)
    {
      GPIO_InitTypeDef GPIO_InitStructure;
      NVIC_InitTypeDef NVIC_InitStructure;
    
      /* TIM4 clock enable */
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
    
      /* GPIOB clock enable */
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
      
      /* TIM4 chennel2 configuration : PB.07 */
      GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;
      GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
      GPIO_Init(GPIOB, &GPIO_InitStructure);
        
      TIM_TimeBaseStructure.TIM_Period = 4200 - 1;
        
      /* Connect TIM pin to AF2 */
      GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
    
      /* Enable the TIM4 global Interrupt */
      NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
      NVIC_Init(&NVIC_InitStructure);
        
        TIM_TimeBaseStructure.TIM_Period = 4;
        TIM_TimeBaseStructure.TIM_Prescaler = 360;
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
        TIM_Cmd(TIM4, DISABLE);
    }
    
    #ifdef  USE_FULL_ASSERT
    
    void assert_failed(uint8_t* file, uint32_t line)
    {
      while (1)
      {}
    }
    #endif 
    
    stm32f4xx_it.c
    PHP Code:
    #include "stm32f4xx_it.h"
    #include "stm32f4_discovery.h"
    
    __IO uint16_t IC2Value = 0;
    __IO uint16_t DutyCycle = 0;
    __IO uint32_t Frequency = 0;
    
    __IO uint16_t range;
    uint16_t cm;
    uint16_t time=0;
    void NMI_Handler(void)
    {
    }
    
    void HardFault_Handler(void)
    {
      while (1)
      {
        }
    }
    
    
    void MemManage_Handler(void)
    {
      while (1)
      {
        }
    }
    
    void BusFault_Handler(void)
    {
      while (1)
      {
        }
    }
    void UsageFault_Handler(void)
    {
      while (1)
      {}
    }
    
    
    void DebugMon_Handler(void)
    {
        
    }
    
    void SVC_Handler(void)
    {
        
    }
    
    
    void PendSV_Handler(void)
    {
        
    }
    
    void SysTick_Handler(void)
    {}
    
    uint16_t TIMERANGE1=100;
    uint16_t TIMERANGE=100;
    void TIM4_IRQHandler(void)
    {
      RCC_ClocksTypeDef RCC_Clocks;
      RCC_GetClocksFreq(&RCC_Clocks);
        
      /* Clear TIM4 Capture compare interrupt pending bit */
      TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
    
      STM_EVAL_LEDOn(LED3);        
      while (1)
        {
                STM_EVAL_LEDOn(LED4);
                Delay(5);
                STM_EVAL_LEDOff(LED4);
                while (!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7));
                TIM_Cmd(TIM4, ENABLE);
                while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7))
                {
                    time++;
                }
                TIM_Cmd(TIM4, DISABLE);
                range = TIM_GetCounter(TIM4);
                time = TIM4->CNT;
                range;
                cm = range/47;
                TIM_SetCounter(TIM4, 0);
                Delay(50);    
                time = 0;
        }
    } 
    
  • zerotohero
    Thành viên chính thức
    • Jun 2010
    • 82

    #2
    Nguyên văn bởi locthanh3005 Xem bài viết
    mình đang làm đo khoảng cách bằng cảm biến siêu âm srf05 nhưng bị tràn timer nên chỉ đếm đc tới 65535 thôi... ae nào rành giúp mình đi... hjxhjx

    main.c
    PHP Code:
    #include "stm32f4_discovery.h"
    
    TIM_ICInitTypeDef  TIM_ICInitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    void TIM_Config(void);
    
    void Delay(__IO uint32_t time)
    {
     /*Delay is in milliseconds*/
        time=time*80000;  
      while(time--)
      {
      }
    }
    int main(void)
    {
    
      /* TIM Configuration */
      TIM_Config();
    
      /* TIM4 configuration: PWM Input mode ------------------------
         The external signal is connected to TIM4 CH2 pin (PB.07), 
         The Rising edge is used as active edge,
         The TIM4 CCR2 is used to compute the frequency value 
         The TIM4 CCR1 is used to compute the duty cycle value
      ------------------------------------------------------------ */
    
      TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
      TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
      TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
      TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
      TIM_ICInitStructure.TIM_ICFilter = 0x0;
    
      TIM_PWMIConfig(TIM4, &TIM_ICInitStructure);
    
      /* Select the TIM4 Input Trigger: TI2FP2 */
      TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);
    
      /* Select the slave Mode: Reset Mode */
      TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
      TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);
    
     
      /* Enable the CC2 Interrupt Request */
      TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);
    
      STM_EVAL_LEDInit(LED3);
        STM_EVAL_LEDInit(LED4);        
    }
    
    
    void TIM_Config(void)
    {
      GPIO_InitTypeDef GPIO_InitStructure;
      NVIC_InitTypeDef NVIC_InitStructure;
    
      /* TIM4 clock enable */
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
    
      /* GPIOB clock enable */
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
      
      /* TIM4 chennel2 configuration : PB.07 */
      GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;
      GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
      GPIO_Init(GPIOB, &GPIO_InitStructure);
        
      TIM_TimeBaseStructure.TIM_Period = 4200 - 1;
        
      /* Connect TIM pin to AF2 */
      GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
    
      /* Enable the TIM4 global Interrupt */
      NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
      NVIC_Init(&NVIC_InitStructure);
        
        TIM_TimeBaseStructure.TIM_Period = 4;
        TIM_TimeBaseStructure.TIM_Prescaler = 360;
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
        TIM_Cmd(TIM4, DISABLE);
    }
    
    #ifdef  USE_FULL_ASSERT
    
    void assert_failed(uint8_t* file, uint32_t line)
    {
      while (1)
      {}
    }
    #endif 
    
    stm32f4xx_it.c
    PHP Code:
    #include "stm32f4xx_it.h"
    #include "stm32f4_discovery.h"
    
    __IO uint16_t IC2Value = 0;
    __IO uint16_t DutyCycle = 0;
    __IO uint32_t Frequency = 0;
    
    __IO uint16_t range;
    uint16_t cm;
    uint16_t time=0;
    void NMI_Handler(void)
    {
    }
    
    void HardFault_Handler(void)
    {
      while (1)
      {
        }
    }
    
    
    void MemManage_Handler(void)
    {
      while (1)
      {
        }
    }
    
    void BusFault_Handler(void)
    {
      while (1)
      {
        }
    }
    void UsageFault_Handler(void)
    {
      while (1)
      {}
    }
    
    
    void DebugMon_Handler(void)
    {
        
    }
    
    void SVC_Handler(void)
    {
        
    }
    
    
    void PendSV_Handler(void)
    {
        
    }
    
    void SysTick_Handler(void)
    {}
    
    uint16_t TIMERANGE1=100;
    uint16_t TIMERANGE=100;
    void TIM4_IRQHandler(void)
    {
      RCC_ClocksTypeDef RCC_Clocks;
      RCC_GetClocksFreq(&RCC_Clocks);
        
      /* Clear TIM4 Capture compare interrupt pending bit */
      TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
    
      STM_EVAL_LEDOn(LED3);        
      while (1)
        {
                STM_EVAL_LEDOn(LED4);
                Delay(5);
                STM_EVAL_LEDOff(LED4);
                while (!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7));
                TIM_Cmd(TIM4, ENABLE);
                while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7))
                {
                    time++;
                }
                TIM_Cmd(TIM4, DISABLE);
                range = TIM_GetCounter(TIM4);
                time = TIM4->CNT;
                range;
                cm = range/47;
                TIM_SetCounter(TIM4, 0);
                Delay(50);    
                time = 0;
        }
    } 
    
    Mình không biết cậu đo bị tràn khi khoảng cách là bao nhiêu.nếu mạch hoạt động đúng thì tăng prescaler lên và sửa lại công thức tính khoảng cách chắc là ok.

    Comment

    • locthanh3005
      Thành viên mới
      • Oct 2009
      • 13

      #3
      mình chỉ đo đang ở giới hạn là 14cm ak... mình mới học làm nên gà lắm

      Comment

      • zerotohero
        Thành viên chính thức
        • Jun 2010
        • 82

        #4
        Nguyên văn bởi locthanh3005 Xem bài viết
        mình chỉ đo đang ở giới hạn là 14cm ak... mình mới học làm nên gà lắm
        thế là ok nhé. Lí do là tại cái bàn và mode nạp không chuẩn lắm+code viết không đúng chỗ. Thích dùng ngắt thì đó là ngắt ngoài nhé không phải ngắt của timer4 đâu.Nạp lại Prescaler cho nó chuẩn là ok rồi

        Comment

        • locthanh3005
          Thành viên mới
          • Oct 2009
          • 13

          #5
          tks bạn nha... mà giờ thì có vấn đề xẩy ra rồi nè... lúc chân echo không nhận đc tính hiệu về thì chương trình nó chạy mãi mãi luôn... hjx

          Comment

          • zerotohero
            Thành viên chính thức
            • Jun 2010
            • 82

            #6
            thêm phần time out.đơn giản thôi mà.Có thể thêm một biến nữa để check time out hoặc kiểm tra luôn CNT của timer4 nếu vượt quá giá trị nào đó(giá trị này >CNT max lúc không gặp vật cản )để dừng vòng while

            Comment

            • hongmta
              Thành viên mới
              • Jan 2010
              • 28

              #7
              srf05 tốt nhất cứ test trên oscilloscop đã, mấy module này sai số nhiều lăm

              Comment

              Về tác giả

              Collapse

              locthanh3005 Tìm hiểu thêm về locthanh3005

              Bài viết mới nhất

              Collapse

              Đang tải...