测试环境:主频168M

#include "delay.h"

void delay_init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); TIM_TimeBaseStructure.TIM_Period = ;
TIM_TimeBaseStructure.TIM_Prescaler = ;
TIM_TimeBaseStructure.TIM_ClockDivision = ;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
} void delay_ms(u16 nms)
{
u32 cnt;
cnt = nms*;
TIM_Cmd(TIM4, ENABLE);
while(cnt--)
{
while(TIM_GetFlagStatus(TIM4, TIM_FLAG_Update) == RESET);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
}
TIM_Cmd(TIM4, DISABLE);
}
//延时nus
//nus为要延时的us数.
void delay_us(u32 nus)
{
u32 cnt;
cnt = nus;
TIM_Cmd(TIM4, ENABLE);
while(cnt--)
{
while(TIM_GetFlagStatus(TIM4, TIM_FLAG_Update) == RESET);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
}
TIM_Cmd(TIM4, DISABLE);
}
#ifndef __DELAY_H
#define __DELAY_H #include "stm32f4xx_conf.h" void delay_init();
void delay_ms(u16 nms);
void delay_us(u32 nus); #endif
05-06 15:32