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