اطلاعیه

Collapse
No announcement yet.

توضیح کد delay برای stm32f103c8

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    توضیح کد delay برای stm32f103c8

    میشه این کد دوستان توضیح بدن ممنون میشم
    این کد یک us delay برای stm32f103 نوشته شده اون قسمت های مربوط به کتابخانه hal اصلا متوجه نمیشم
    trc دقیقا چیه تو اینترنت سرچ زدم فهمیدم مربوط به timer
    فیلمشو تو یوتیوب دیدم
    تو نرم افزار stm32cubemx منبع کلاک رو حالت کریستال خارجی گذاشت
    تو قسمت sys هم تو فیلد Debug مقدار رو روی serial wire قرار داد میشه علت این کار توضیح بدین برای استفاده از این کد حتما منبع کلاک باید خارجی باشه
    ببخشید سوالام زیاد شد
    کد:
    #include "dwt_stm32_delay.h"
    
    
    
    
    /**
     * @brief  Initializes DWT_Clock_Cycle_Count for DWT_Delay_us function
     * @return Error DWT counter
     *         1: clock cycle counter not started
     *         0: clock cycle counter works
     */
    uint32_t DWT_Delay_Init(void) {
      /* Disable TRC */
      CoreDebug->DEMCR &= ~CoreDebug_DEMCR_TRCENA_Msk; // ~0x01000000;
      /* Enable TRC */
      CoreDebug->DEMCR |=  CoreDebug_DEMCR_TRCENA_Msk; // 0x01000000;
    
    
      /* Disable clock cycle counter */
      DWT->CTRL &= ~DWT_CTRL_CYCCNTENA_Msk; //~0x00000001;
      /* Enable  clock cycle counter */
      DWT->CTRL |=  DWT_CTRL_CYCCNTENA_Msk; //0x00000001;
    
    
      /* Reset the clock cycle counter value */
      DWT->CYCCNT = 0;
    
    
         /* 3 NO OPERATION instructions */
         __ASM volatile ("NOP");
         __ASM volatile ("NOP");
      __ASM volatile ("NOP");
    
    
      /* Check if clock cycle counter has started */
         if(DWT->CYCCNT)
         {
           return 0; /*clock cycle counter started*/
         }
         else
      {
        return 1; /*clock cycle counter not started*/
      }
    }
    کد هدر:
    کد:
    #ifndef DWT_STM32_DELAY_H
    #define DWT_STM32_DELAY_H
    
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    
    
    
    #include "stm32f1xx_hal.h"
    
    
    /**
     * @brief  Initializes DWT_Cycle_Count for DWT_Delay_us function
     * @return Error DWT counter
     *         1: DWT counter Error
     *         0: DWT counter works
     */
    uint32_t DWT_Delay_Init(void);
    
    
    
    
    
    
    /**
     * @brief  This function provides a delay (in microseconds)
     * [MENTION=17545]para[/MENTION]m  microseconds: delay in microseconds
     */
    __STATIC_INLINE void DWT_Delay_us(volatile uint32_t microseconds)
    {
      uint32_t clk_cycle_start = DWT->CYCCNT;
    
    
      /* Go to number of cycles for system */
      microseconds *= (HAL_RCC_GetHCLKFreq() / 1000000);
    
    
      /* Delay till end */
      while ((DWT->CYCCNT - clk_cycle_start) < microseconds);
    }
    
    
    
    
    #ifdef __cplusplus
    }
    #endif
    
    
    #endif
    سقراط : دانش من بدانجایی رسید که فهمیدم هیچ نمیدانم.
لطفا صبر کنید...
X