cancel
Showing results for 
Search instead for 
Did you mean: 

hi guys, I have created a simple led+tim2's program.when debugging that code i get fine delay but unfortunately won't get correct delay after reset or power on.

Msams.1
Associate II
 
2 REPLIES 2

Is your code actually running?

BOOT0 Pulled Low?

Stuck in some loop waiting for clocks and PLLs to start?

What happens if you just continue with the HSI or MSI clock source the MCU started with?

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

//This is my code

// could you please check it

#include "stm32f4xx.h"

#define PLLM 25

#define PLLN 320

#define PLLP 1//divide 4

int clock_80MHz_Ex_pll();

int pinMode();

void InitTIM2();

void Delay_us();

void Delay_ms(int x);

int clock_80MHz_Ex_pll()

{

      RCC->CR|=RCC_CR_HSEON;//External Crystal Oscillator

      while(!(RCC->CR & RCC_CR_HSERDY));// External Crystal oscillator status flag checking

      RCC->APB1ENR|=RCC_APB1ENR_PWREN;// power Enable

      PWR->CR|=PWR_CR_VOS;// voltage scaling

      FLASH->ACR|=(FLASH_ACR_DCEN|FLASH_ACR_ICEN|FLASH_ACR_PRFTEN|FLASH_ACR_LATENCY_2WS);

      RCC->CFGR|=(RCC_CFGR_PPRE2_DIV1|RCC_CFGR_PPRE1_DIV2|RCC_CFGR_HPRE_DIV1);

      RCC->PLLCFGR|=(PLLM<<0|PLLN<<6|PLLP<<16|1<<22);

      RCC->CR|=RCC_CR_PLLON;

      while(!(RCC->CR & RCC_CR_PLLRDY));

      RCC->CFGR|=RCC_CFGR_SW_PLL;

      while(!(RCC->CFGR & RCC_CFGR_SWS_PLL));

       return 1;

}

int pinMode()

{

  RCC->AHB1ENR|=RCC_AHB1ENR_GPIOCEN;

  GPIOC->MODER|=0x55555555;

  GPIOC->OTYPER=0;

  GPIOC->OSPEEDR=0;

  return 0;

}

void InitTIM2()

{

   RCC->APB1ENR|=(1<<0);//Enable timer 2

   TIM2->PSC =79;

   TIM2->ARR=0xFFFF;

   TIM2->CR1 |=(1<<0);

}

void Delay_us()

{

   TIM2->CNT=0;

   while(TIM2->CNT < 1000);

}

void Delay_ms(int x)

{

   for(int i=0;i<x;i++){

   Delay_us();}

}

int main()

{

clock_80MHz_Ex_pll();

pinMode();

InitTIM2();

while(1){

   GPIOC->ODR=(1<<13);

   Delay_ms(1000);

   GPIOC->ODR=~(1<<13);

   Delay_ms(1000);

   }

return 0;

}