Microsecond delay Delay_us in Stm32 cubemx HAL Project

Source: Internet
Author: User

Reference to the code of the punctual atom

Test Platform

Stm32f429i-disco

With a gpio.

Clock

Gpio


/*USER CODE BEGIN 0*/typedef uint8_t U8;TYPEDEF uint32_t u32;u8 fac_us;voiddelay_init (U8 sysclk) {#ifSystem_support_os//?????? OS.u32 Reload; #endifHal_systick_clksourceconfig (SYSTICK_CLKSOURCE_HCLK); //SysTick??? HCLKFAC_US=SYSCLK;//?????? Os,fac_us????? #ifSystem_support_os//?????? OS.Reload=SYSCLK;//???????? ??? Kreload*=1000000/delay_ostickspersec;//?? delay_ostickspersec?????? //Reload????,??? : 16777216,? 180M?,?? 0.745s??fac_ms= +/delay_ostickspersec;//?? OS?????????systick->ctrl|=systick_ctrl_tickint_msk;//?? SYSTICK??systick->load=reload;//? 1/os_ticks_per_sec?????systick->ctrl|=systick_ctrl_enable_msk;//?? SYSTICK #else #endif}voidDelay_us (u32 nus) {u32 ticks; U32 told,tnow,tcnt=0; U32 Reload=systick->load;//LOAD??Ticks=nus*fac_us;//??????told=systick->val;//????????? while(1) {Tnow=systick->VAL; if(tnow!=told) { if(Tnow<told) Tcnt+=told-tnow;//???? SYSTICK??????????. Elsetcnt+=reload-tnow+told; Told=Tnow; if(tcnt>=ticks) Break;//???? /????????,???. } };}/*USER CODE END 0*/
intMainvoid){  /*USER CODE BEGIN 1*/  /*USER CODE END 1*/  /*MCU Configuration----------------------------------------------------------*/  /*Reset of all peripherals, initializes the Flash interface and the Systick.*/Hal_init (); /*Configure the system clock*/Systemclock_config (); /*Initialize all configured Peripherals*/Mx_gpio_init (); /*USER CODE BEGIN 2*/Delay_init (168); /*USER CODE END 2*/  /*Infinite Loop*/  /*USER CODE BEGIN while*/   while(1)  {  /*USER CODE END while*/Hal_gpio_writepin (Gpiof, gpio_pin_7, Gpio_pin_set); Delay_us ( -);    Hal_gpio_writepin (Gpiof, gpio_pin_7, Gpio_pin_reset); Delay_us ( -); /*USER CODE BEGIN 3*/  }  /*USER CODE END 3*/}

The actual test, the function call can bring hundreds of NS error.

Microsecond delay Delay_us in Stm32 cubemx HAL Project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.