cancel
Showing results for 
Search instead for 
Did you mean: 

Microsecond delay using Timer in stm32l0

umesh_patil
Associate II

I have tried below code for generating the microsecond delay for the stm32l0, but its not working.

int myTicks = 0;

********

//Enabling The timerr

RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

      TIM2->PSC = 0;

      TIM2->ARR = 72;

      TIM2->CR1 |= TIM_CR1_URS;

      TIM2->DIER |= TIM_DIER_UIE;

      TIM2->EGR |= TIM_EGR_UG;

//      TIM2->CR1 |= TIM_CR1_CEN;

//      NVIC_SetPriority(TIM2_IRQn,0x03);

      NVIC_EnableIRQ(TIM2_IRQn);

********

void dUs(int us)

{

   TIM2->CR1 |= TIM_CR1_CEN;

   myTicks = 0;

   while(myTicks<us);

   TIM2->CR1 &= ~TIM_CR1_CEN;

}

*******

void TIM2_IRQ_handler(void)

{

    if (TIM2->SR & TIM_SR_UIF) {

      TIM2->SR &= ~(TIM_SR_UIF);

      myTicks++;

      }

}

2 REPLIES 2

Don't use 1 MHz interrupts. Watch the counter count instead.

Set ARR to 0xFFFF and PSC = CPUMHZ-1

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
umesh_patil
Associate II

my system clock is 8Mhz and I want to generate the 1 microsecond delay.

What should be ARR and PSC ?