AnsweredAssumed Answered

Measuring time on STM32f401

Question asked by oliviero.gennaro on Sep 29, 2015
Latest reply on Sep 29, 2015 by Clive One
hi, I'm trying to turn ON and OFF the LEDs of my board with 10-second intervals, but I get results in about 8 seconds.
This is my code, I did not understand the frequencies of the processor card.
I apologize, I'm just a student.

#include "stm32f4xx.h"
 
 
void sleep(float, int);
 
//accensione di un led
int main (void){
 
  RCC->AHB1ENR |=0X8;  //Enable port D
  RCC->APB1ENR |=1; // Enable TIM2  32 bit
   
  GPIOD->MODER |=0x55<<24; // Enable LEDs
 
   
   TIM2->PSC = 0;
   TIM2->ARR = 48000000;
   TIM2->CR1 |= 1;  //Enable  timer putting 1 on CEN register
   TIM2->CNT = 0; //start counting
   while(1){
     if((TIM2->SR & 1) != 0){
     
        if((GPIOD->ODR & (1<<15)) == 0)
          GPIOD->ODR |= 1<<15;
  
        sleep(10,1);
  
        if((GPIOD->ODR & (1<<15)) != 0)
          GPIOD->ODR &= ~(1<<15);
  
        sleep(10,1);
     }
     
   }
}
 
 
 
void sleep(float mhz, int sec){
    float hz = mhz*1000000;
    float t = 1/hz;
    int i = 0;
    for(i = 0; i < (int)sec/t; i++);
}

Outcomes