AnsweredAssumed Answered

STM32F407VG Basic Timer 6 Magic value

Question asked by dziundzia.illia on Sep 19, 2016
Latest reply on Sep 23, 2016 by dziundzia.illia
Hello! I use a basic timer 6 to make a simple delay. Here is my code:

#define DELAY_TIME       10
#define PB2_ON()         GPIOB->BSRRL = GPIO_Pin_2
#define PB2_OFF()        GPIOB->BSRRH = GPIO_Pin_2

//Config PortB_Pin2 as output
void PortB_Init(void)
{
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

   GPIO_InitTypeDef PortB_Pin2_init;

   PortB_Pin2_init.GPIO_Mode = GPIO_Mode_OUT;
   PortB_Pin2_init.GPIO_Pin = GPIO_Pin_2;
   PortB_Pin2_init.GPIO_Speed = GPIO_Speed_100MHz;
   PortB_Pin2_init.GPIO_OType = GPIO_OType_PP;
   PortB_Pin2_init.GPIO_PuPd = GPIO_PuPd_NOPULL;

   GPIO_Init(GPIOB, &PortB_Pin2_init);
}

//Timer 6 config
void Timer6_Init(void)
{
   RCC->APB1ENR |= RCC_APB1Periph_TIM6; // Enable Timer 6 clock

   TIM6->PSC = 83;  // Clock frequency: 1MHz
   TIM6->ARR = 0xFFFF; // Auto-reload register value  
                       // After reaching this value appears Update Interrupt and Counter Register will be reset 
   TIM6->CR1 |= TIM_CR1_CEN; //TIM6 Enable
}

//Delay-func. Count µs
void ddelay_micros(uint16_t delay_time)
{
   TIM6->CNT = 0;
   while (TIM6->CNT != delay_time) ;
   return;
}

void main(void)
{
   SystemInit();  
   PortB_Init();
   Timer6_Init();

   RCC_ClocksTypeDef RCC_Clocks;
   RCC_GetClocksFreq(&RCC_Clocks);

    while(1)
    {
       PB2_ON();
       ddelay_micros(DELAY_TIME);
       PB2_OFF();
       ddelay_micros(DELAY_TIME);
    }
}

It works pretty well, the achieved delay is about 10,6 µs.
With DELAY_TIME values: 15, 20, 1000 i obtain the correct time intervals.
But! If i set DELAY_TIME to 12, the generated delay ist about 420 ns!
I do not understand what am i missing out.

Thanks for advice! :)

 

Outcomes