参考的正点原子的代码
测试平台
stm32f429i-disco
配了一个gpio
时钟
gpio
/* USER CODE BEGIN 0 */ typedef uint8_t u8; typedef uint32_t u32; u8 fac_us; void delay_init(u8 SYSCLK) { #if SYSTEM_SUPPORT_OS //?????? OS. u32 reload; #endif HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); //SysTick ??? HCLK fac_us=SYSCLK; //?????? OS,fac_us ????? #if SYSTEM_SUPPORT_OS //?????? OS. reload=SYSCLK; //???????? ??? K reload*=1000000/delay_ostickspersec; //?? delay_ostickspersec ?????? //reload ? 24 ????,???:16777216,? 180M ?,?? 0.745s ?? fac_ms=1000/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 } void delay_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 ??????????. else tcnt+=reload-tnow+told; told=tnow; if(tcnt>=ticks)break; //????/????????,???. } }; } /* USER CODE END 0 */
int main(void) { /* 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(50); HAL_GPIO_WritePin(GPIOF, GPIO_PIN_7, GPIO_PIN_RESET); delay_us(50); /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }
实际测试, 函数调用能带来几百ns的误差.
时间: 2024-10-29 12:24:13