cancel
Showing results for 
Search instead for 
Did you mean: 

Hi all, i would like to make a my own bare metal code for stm32f401ccu. Here i have given my code. But it's not showing correct clock frequency. Anyone could you pls check it for anything missed or etc...

Msams.1
Associate II

#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(10000);

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

Delay_ms(10000);

}

return 0;

}

this is a register level code as per my research. the program is working but my expected output is 80 mhz

but i'm not sure it is getting .

external clock frequency 25 mhz

output of pll 80 mhz.

Input of pll 25mhz.

I have created a timer program.if the cpu frequency is 80 mhz .​

Could you pls check it?​

6 REPLIES 6
MM..1
Chief II

Every lines in your init is

xxxx |= bbb

maybe no previous state in register need manage , but i mean some required

*** = bbb
 
or
 
*** &= bbb

Msams.1
Associate II

Any other difference did you get​.

When i use or operation it will not over write previous state. I thought this like​.

TDK
Guru

> my expected output is 80 mhz but i'm not sure it is getting .

What output are you getting instead?

It would be more efficient and more accurate to use the 32-bit DWT->CYCCNT as a counter rather than rolling your own.

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

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

Both of these operations set ALL bits in that register, not just the one for the PC13. Probably not what you want.

If you feel a post has answered your question, please click "Accept as Solution".

> What output are you getting instead?

As MM said above, the root of the problem is in ORing to nonzero registers. Here, probably the one with most impact is

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

I'd recommend to read out content of the changed registers and check.

JW

Msams.1
Associate II

Thanks all of you. Spent ​some more time for my question.

First of all i will check it once with your instructions.

I have only one option to view output.first of all create the clock frequency and run a timer then toggle a gpio pin. The toggling delay assigned through timer.It Will view from a logic analyzer.

​What output are you getting instead?

I have only getting 10 ms instead of 1sec each high and low state of gpio.​

MM..1
Chief II

Maybe you need som mind use. Your code is 10s not 1s and seems optimizer and RISC architecture for you instead 80M now is 80GHz for 10ms.

Simply your code is bad .

Use CubeMX generate normal code for PWM output on pin