I'm trying to generate 1us delay use stm32, I know I'll need a timer. I find out some code on timer, but dont know how to configure the setting to make it 1us. because the setting will depend on apb1 clock frequency or else. below is my code:
void TIM5_Init_Query(void)
{
TIM_TimeBaseInitTypeDef Tim5;
TIM_DeInit(TIM5);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
Tim5.TIM_Period=1;
Tim5.TIM_Prescaler=80-1;
Tim5.TIM_ClockDivision=1;
Tim5.TIM_CounterMode=TIM_CounterMode_Down;
TIM_TimeBaseInit(TIM5,&Tim5);
}
void usDelay(u32 nTime)
{
u16 counter=nTime&0xffff;
TIM_Cmd(TIM5,ENABLE);
TIM_SetCounter(TIM5,counter);
while(counter>1)
{
counter=TIM_GetCounter(TIM5);
}
TIM_Cmd(TIM5,DISABLE);
}
My question is ,how to set the TIM_Prescaler,TIM_Period,and Tim5.TIM_ClockDivision, what is the relation between those value and my delay. how to check apb1 clock frequency in my code?