-1

I'm a beginner in using an STM32F4 discovery board for a project and am wondering if I'm approaching the problem correctly. wonna test my ms delay function but no vain ..

#include "stm32f4x.h"
#define LED_BLUE_GPIO   GPIOC
#define LED_BLUE_PIN     8

void Delay_ms(uint16_t ms){

TIM3->ARR = ms;         //timer 3
TIM3->CNT = 0;
while((TIM3->SR & TIM_SR_UIF)==0){}
TIM3->SR &= ~TIM_SR_UIF;
}


int main(void)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN;
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;

//initialization

TIM3->PSC = 23999;      // f timer = fclk / 24000 => 1kHz
TIM3->ARR = 0xFFFF;
TIM3->CR1 = TIM_CR1_CEN;    
DBGMCU->CR = DBGMCU_CR_DBG_TIM3_STOP;

while(1){
GPIOC->ODR = GPIO_ODR_ODR8;
Delay_ms(1000);     //delay 1 sec
GPIOC->ODR = GPIO_ODR_ODR8;
Delay_ms(1000);     //delay 1 sec
}

} 
Ensaf Atef
  • 1
  • 1
  • 3

1 Answers1

1

I use something like that

void delay_ms(int ms)
    {
        int c = 0;

        //set prescaler
        TIM7 -> PSC = 1000;

        //value autoreload
        TIM7 -> ARR = tim_clk - 1 ;

        //counter enable
        TIM7 ->CR1 |= TIM_CR1_CEN;

        while(c < ms)
        {
            while(!(TIM7->SR & TIM_SR_UIF));
            c++;
        }

      //counter disable 
        TIM7 ->CR1 &= ~TIM_CR1_CEN;
    }

and I recommend to use HAL Driver from ST, because for every microcontroler from ST code will be the same and you are saving your time.

opetany93
  • 51
  • 2