cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 runın at 16 mhz

FaintSmile
Associate II
Posted on December 14, 2013 at 09:32

I wrote 1 sn delay code for stm32 and ı used 168 mhz system clock.My delay cycle ıs 16000 for each ms and ıt work led blınk 1 sn.It dıdn't work when ıt was 168000 shouldn't it have worked at 168000 cycle.my code ıs

**

**  File        : main.c

**

**  Abstract    : main function.

**

**  Functions   : main

**

**  Environment : Atollic TrueSTUDIO(R)

**                STMicroelectronics STM32F4xx Standard Peripherals Library

**

**  Distribution: The file is distributed “as is,�? without any warranty

**                of any kind.

**

**  (c)Copyright Atollic AB.

**  You may use this file as-is or modify it according to the needs of your

**  project. This file may only be built (assembled or compiled and linked)

**  using the Atollic TrueSTUDIO(R) product. The use of this file together

**  with other tools than Atollic TrueSTUDIO(R) is not permitted.

**

*****************************************************************************

*/

/* Includes */

#include ''stm32f4xx.h''

/* Private macro */

/* Private variables */

/* Private function prototypes */

/* Private functions */

/**

**===========================================================================

**

**  Abstract: main program

**

**===========================================================================

*/

void delay_ms(int delay1)

{

delay1 = delay1*16000;

while (--delay1)

{

}

}

int main(void)

{

GPIO_InitTypeDef GPIO_InitStruct;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14 | GPIO_Pin_13 | GPIO_Pin_12; // we want to configure all LED GPIO pins

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; // we want the pins to be an output

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz; // this sets the GPIO modules clock speed

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this sets the pin type to push / pull (as opposed to open drain)

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; // this sets the pullup / pulldown resistors to be inactive

GPIO_Init(GPIOD, &GPIO_InitStruct);  // this passes the configuration to the Init function which takes care of the low level stuff

  /* Infinite loop */

  while (1)

  {

// if(1 == GPIO_ReadInputDataBit( GPIOA,GPIO_Pin_0))

// {

// while(1 == GPIO_ReadInputDataBit( GPIOA,GPIO_Pin_0));

 GPIO_SetBits ( GPIOD, GPIO_Pin_14) ;

 delay_ms(1000);

 GPIO_ResetBits (GPIOD, GPIO_Pin_14) ;

 delay_ms(1000);

// }

  }

}

/*

 * Callback used by stm32f4_discovery_audio_codec.c.

 * Refer to stm32f4_discovery_audio_codec.h for more info.

 */

void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size){

  /* TODO, implement your code here */

  return;

}

/*

 * Callback used by stm324xg_eval_audio_codec.c.

 * Refer to stm324xg_eval_audio_codec.h for more info.

 */

uint16_t EVAL_AUDIO_GetSampleCallBack(void){

  /* TODO, implement your code here */

  return -1;

}

4 REPLIES 4
Posted on December 14, 2013 at 13:16

If you are concerned about the processor speed, use the MCOx pins and output internal clocks, and measure them on a scope.

Observe that the processor in fact starts for a 16 MHz HSI clock, and has to be switched to something else, typically via SystemInit() in system_stm32f4xx.c

Finally look at the assembler code generated by the compiler, and the optimization level selected. Unless you're writing code in assembler you probably have little control of cycle level performance.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
FaintSmile
Associate II
Posted on December 15, 2013 at 09:11

Clive you are right but why ıs ıt happening for  exapmle when ı use systemclick for delays lle these 

 if (SysTick_Config(168000000 / 1000))

 {

   /* Capture error */

   while (1);

 }

Delay(1000);

it does really waits for 1 second but when ı use my 1 second delay code lke these

void delay_s

 

(int delay2)

{

delay2 = delay2*168

0

0

0;

while (delay2--)

{

}

}

Delay(1000);

it waits more than 10 second ı dıdn't understant why it waits more.ısn't ıt te same thing its like my main fuction has a diffrent clock speed from systemclock and it is like 8mhz or smtn like that

Posted on December 15, 2013 at 15:00

The processor doesn't natively run C code, so assuming it's going to take a single cycle per loop iteration probably isn't going to hold water. To understand where the 10 cycles go you'll want to review the code output by the compiler.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 16, 2013 at 01:16

''The processor doesn't natively run C code, so assuming it's going to take a single cycle per loop iteration probably (sic?) isn't going to hold water''

I'd say it is certainly  not going to hold water!

In fact, any assumption - unless fully  qualified by compiler, version, and complete settings - about a direct correspondence from 'C' source lines to machine code is unlikely to hold water.

http://www.8052.com/forum/read/162556