cancel
Showing results for 
Search instead for 
Did you mean: 

Measuring time on STM32f401

olivierognn
Associate
Posted on September 29, 2015 at 11:52

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++);
}

#stm32f401
1 REPLY 1
Posted on September 29, 2015 at 14:44

Is the processor configured to run at 84 MHz? review the system_stm32f4xx.c file

Values programmed into the Period (ARR) are in the form N-1 

The Update status bit in SR needs clearing if you're pacing on that.

TIM2->SR = ~1; // not RMW form

Your software delay is of some what arbitrary, as it's hard to establish a C line to processor instruction, and the optimizer might establish your loop is pointless and remove it. Consider a volatile iterator.

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