سؤال

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?

هل كانت مفيدة؟

المحلول

You can get the apb clock frequency at run time by using the function RCC_GetClocksFreq which is provided in the STM32 peripheral library. For this function to return correct values, if you are using an external crystal which has a frequency other than the default value (8 MHz or 25 MHz for the STM32F1xx, check for your microcontroller) you need to define the macro HSE_VALUE and set it to the crystal frequency used in your project.

You didn't say which microcontroller you are using (there are a lot of different stm32 micros), but the different parameters are documented in the reference manual corresponding to your microcontroller family, for example for the STM32F1XX this would be section 15.3 of the reference manual, this document:

http://www.st.com/web/en/resource/technical/document/reference_manual/CD00171190.pdf

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top