S
S
Slavka2016-04-17 02:17:17
Microcontrollers
Slavka, 2016-04-17 02:17:17

How to make stm32 smart delay before execution?

There are such cases, some devices require a delay between different commands, for example, wait 300ms, arduino people are used to simple while(cnt--) loops, but this does not suit me, so the question is how to organize a delay with a timer?
Here is the code I posted:

#include "Delay.h"

static uint8_t flag = 1;

void delay(uint32_t _delay){

  initTIM6();
  TIM6->ARR = _delay;
  TIM6->CR1 |= TIM_CR1_CEN;
  flag = 1;
  while(flag);

}


void TIM6_IRQHandler(void){
  flag = 0;
  TIM6->CR1 &= ~TIM_CR1_CEN;
  TIM6->SR &= ~TIM_SR_UIF;
}

void initTIM6(){
  RCC->APB1ENR |= RCC_APB1ENR_TIM6EN;
  TIM6->PSC = SystemCoreClock / 1000 - 1;
  TIM6->DIER |= TIM_DIER_UIE;
  NVIC_EnableIRQ(TIM6_IRQn);
}

in the right place just call delay(300); but for some reason the delay occurs for a shorter time, but all other functions are working at that moment, that is, it does not seem to take up CPU time. But there is still a problem in that for a delay of 1s I need to enter not 1000 but 10,000, although the limiter is set so that the CNT register is increased every 1000ms

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Gusev, 2016-04-17
@Sanchogus

Hmm, prescaler 1000, does it work at 1 MHz in total?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question