cancel
Showing results for 
Search instead for 
Did you mean: 

i have written a led blinking program with as per my knowledge but i couldn't reach output so guys who can clear that? MCU- STM32f401 clock input -25MHz External clock output -80 MHz with pll configuration

Msams.1
Associate II

#include "stm32f4xx.h"

#define PLLM 25

#define PLLN 320

#define PLLP 1//divide 4

void SysConfigure()

{

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

}

void GpioConfigure()

{

  RCC->AHB1ENR|=RCC_AHB1ENR_GPIOCEN;

  GPIOC->MODER|=(1<<26);

  GPIOC->OTYPER|=0;

  GPIOC->OSPEEDR|=0;

}

void Delay(int x)

{

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

}

int main()

{

   SysConfigure();

   GpioConfigure();

   while(1)

   {

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

    Delay(1000);

    GPIOC->BSRR=(1<<29);

    Delay(1000);

   }

   return 0;

}

7 REPLIES 7
  1. avoid the clock setup for the initial experiment, leave it initially at the default HSI clock
  2. define variable i in Delay() as volatile
  3. use a debugger to toggle the given pin manually
  4. what hardware are you using, is this a "known-good" board such as a Nucleo?

JW

Msams.1
Associate II

@waclawek.jan (Community Member)

The board is stm32f401cc black pill .

sir i already worked with hal gpio firmware and that was fine. i need to work with register level programming so i should learn reference manual.

why did you say "avoid initial clock setup"?

wright now i am working with stm32cube ide.

Delay unlikely to be long enough to be human visible. Check signal toggle with a scope.

Double check register settings with debugger.

ORing with zero seems pointless.

Watch for hazard related to enabling APB clocks and touching just enabled peripheral. ​

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

Avoid clock setup so you can see if the code there is problematic, ie stuck in loop or whatever.

If you skip that function part will simply continue clocking from 16 MHz HSI​

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

@Tesla DeLorean (Community Member),waclawek.jan (Community Member)

hey brothers,

i have gotten a led blink program with internal oscillator. it has given below and it's fine.

But we know the internal oscillator's accuracy is a bit low than the external frequency .

so could you pls give me a little bit about configuration of external oscillator .

could you pls give me a sample pll configuration code because i have tried many times on the internet but i couldn't reach correct tutorials.

#include "stm32f4xx.h"

#define DELAY_COUNT    10000000

static void delay( void )

{

   uint32_t i = 0;

   for( i=0; i<=DELAY_COUNT; i++ );

}

int main(void){

 RCC->AHB1ENR|= RCC_AHB1ENR_GPIOCEN;

 GPIOC->MODER = 0x55555555;

 while(1){

   GPIOC->BSRR = 0x0000FFFF;

   delay();

   GPIOC->BSRR = 0xFFFF0000;

   delay();

 }

}

Msams.1
Associate II

@Tesla DeLorean (Community Member),waclawek.jan (Community Member)

hey brothers,

now i have gotten the correct output .that was the delay issues.@Tesla DeLorean (Community Member) ,your assumption is absolutely true.i couldn't see the difference with my delay .

anyway a special thanks to everyone to spent some more times for me.

now i have another doubt could you clear that ?

doubt

can i read the current clock speed from stm32 mcu ,thus which topic should i learn?

>>can i read the current clock speed from stm32 mcu ,thus which topic should i learn?

You can unpack the settings for the sources and PLL at the register level.

The IC doesn't know what the HSE clock source frequency is, this is conveyed via HSE_VALUE define which you provide at a project level to describe the hardware you're using.

Examples where the HAL unpack the speed, look at the UART baud rate computation code, it works out what the APB clock is for the given UART, and then divides that down.

 printf("APB1=%d\n", HAL_RCC_GetPCLK1Freq());

 printf("APB2=%d\n", HAL_RCC_GetPCLK2Freq());

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