cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 timer (Prescaler Value)

kazafwing
Associate II
Posted on January 24, 2013 at 03:46

Hi, I have just saw a program about a interrupt time. The program can let the LED on and off in a specified period. But I have some question about the prescaler value, can anyone help me to understand the prescale value of this case, thanks a lot. My problem is that, I don't know why the period value is 10000-1 for 1 ms delay, is there any equation for this case and why the prescaler value is 336-1 for 1MHz. Also, why the period and prescaler value is alway -1?

Thanks.

The program is as follow:

/**

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

  * @file    USART/main.c 

  * @author  Andrew Markham

  * @version V1.0.0

  * @date    27-April-2012

  * @brief   Main program body

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

  */ 

/* Includes ------------------------------------------------------------------*/

&sharpinclude ''stm32f4xx.h''

&sharpinclude <stdio.h>

/* Private typedef -----------------------------------------------------------*/

GPIO_InitTypeDef  GPIO_InitStructure;

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

void INTTIM_Config(void);

void LED_Config(void);

/* Private functions ---------------------------------------------------------*/

void TIM2_IRQHandler(void)

{

  if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

  {

    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

GPIO_ToggleBits(GPIOD, GPIO_Pin_14);

    GPIO_ToggleBits(GPIOD, GPIO_Pin_13);

GPIO_ToggleBits(GPIOD, GPIO_Pin_12);

  }

}

/**

  * @brief  Main program

  * @param  None

  * @retval None

  */

int main(void)

{

  INTTIM_Config();

  /* LED Configuration */

  LED_Config();

  while (1)

  {  

  }

}

/**

  * @brief  Setup an interval timer

  * @param  None

  * @retval None

  */

void INTTIM_Config(void)

{

  NVIC_InitTypeDef NVIC_InitStructure;

  

  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;            /* Enable the TIM2 gloabal Interrupt */

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  /* value = 0 - 15*/

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;         /* value = 0 - 15*/

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  /* TIM2 clock enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  /* Time base configuration */

  TIM_TimeBaseStructure.TIM_Period = 10000 - 1;  // 1 MHz down to 1 KHz (1 ms)

  TIM_TimeBaseStructure.TIM_Prescaler = 336 - 1; // 24 MHz Clock down to 1 MHz (adjust per your clock)

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  /* TIM IT enable */

  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

  /* TIM2 enable counter */

  TIM_Cmd(TIM2, ENABLE);

}

void LED_Config(void)

{

  /* GPIOD Periph clock enable */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

  /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

}

&sharpifdef  USE_FULL_ASSERT

/**

  * @brief  Reports the name of the source file and the source line number

  *         where the assert_param error has occurred.

  * @param  file: pointer to the source file name

  * @param  line: assert_param error line source number

  * @retval None

  */

void assert_failed(uint8_t* file, uint32_t line)

{

  /* User can add his own implementation to report the file name and line number,

     ex: printf(''Wrong parameters value: file %s on line %d\r\n'', file, line) */

  while (1)

  {}

}

&sharpendif

/**

  * @}

  */ 

/**

  * @}

  */ 

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

#calculate-presc #stm32f2 #set-time-base #bxcan #filter
13 REPLIES 13
abuchholz91
Associate II
Posted on October 01, 2014 at 08:07

I have some very basic code working (MOSTY)

@Clive - I wonder if you can please help me out?

I have a problem, above 1282Hz I get normal and expected readings.

Below 1282Hz I get random un-expected readings that can vary fro 2x Clock to 5x Clock...

My Code:

/// <summary>

/// Need more work here...

/// </summary>

/// <param name=''TIMx_BASE''>The timer BASE to configure.</param>

/// <returns>bool true of false.</returns>

private bool CalculateFrequency(UInt32 TIMx_BASE)

{

UInt32 SYSTEM_APB1_CLOCK_HZ = 42000000; // 42MHz

UInt32 SYSTEM_APB2_CLOCK_HZ = 84000000; // 84MHz

if (TIMx_BASE == STM32F4xx.TIM1_BASE | TIMx_BASE == STM32F4xx.TIM8_BASE)

{

debug = true;

// Calculate the Number needed to get the Prime Factors...

UInt32 PxQ = (SYSTEM_APB1_CLOCK_HZ / this.Frequency);

// Get the list of Prime Factors...

List = GetFactors(PxQ);

for (int i = 0; i < (List.Count - 1); i++)

{

if (debug) Debug.Print(''i = '' + i);

if ((Convert.ToInt32(List[i].ToString())) >= (65535 / 2))

{

// Factor is too large...

}

else

{

if (0 != (i % 2))

{

this.Prescalar = (UInt16)Convert.ToInt16(List[i - 1].ToString());

this.Period = (UInt32)Convert.ToInt32(List[i].ToString());

break;

}

else if (List.Count == 2)

{

this.Prescalar = (UInt16)Convert.ToInt16(List[0].ToString());

this.Period = (UInt16)Convert.ToInt16(List[1].ToString());

}

}

}

if (debug)

{

Debug.Print(''PxQ = '' + PxQ);

Debug.Print(''List Length = '' + List.Count);

Debug.Print(''Period = '' + Period);

Debug.Print(''Prescalar = '' + Prescalar);

Debug.Print(''Frequency = '' + (SYSTEM_APB1_CLOCK_HZ / (Prescalar * Period)));

}

}

else

{

}

// Calculate the Duty Cycle...

this.DutyCycle = (this.Period * this.DutyCycle);

debug = false;

return true;

}

/// <summary>

/// Thanks to those involved in getting this excellent Function together!!!

/// See:

http://stackoverflow.com/questions/239865/best-way-to-find-all-factors-of-a-given-number-in-c-sharp

/// This Function has been slightly modified by myself to suit the purposes of this application.

/// </summary>

/// <param name=''number''>Number to get the Factored Numbers for. E.G: (3 * 2) = 6. 6 Being the Number.</param>

/// <returns>List<uint></returns>

public static ArrayList GetFactors(UInt32 number)

{

ArrayList factors = new ArrayList();

// Round down the Number to check...

UInt32 max = (UInt32)System.Math.Sqrt(number);

for (UInt32 factor = 1; factor <= max; ++factor)

{

// Test from 1 to the square root, or the int below it, inclusive...

if (number % factor == 0)

{

factors.Add(factor);

if (factor != number / factor)

{

// Don't add the square root twice! Thanks Jon

factors.Add(number / factor);

}

}

}

return factors;

}

I am not sure what it is that I am missing. The Clock frequency appears to be correct for all frequencies above 1282Hz.

EDIT:

PxQ = 32786

List Length = 12

Period = 16393

Prescalar = 2

Frequency = 1281

42000000 / 2 * 16393 = 1281

but instead at this calculated frequency (1.281KHz) I get: 1.708KHz

All the best

  Chris

abuchholz91
Associate II
Posted on October 05, 2014 at 23:20

I have carried this code on here: [DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32%20Series%20-%20Calculations%20of%20Timer%20Variables&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/AllItems.aspx&currentviews=0]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fcortex%5fmx%5fstm32%2fSTM32%20Series%20%2d%20Calculations%20of%20Timer%20Variables&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&TopicsView=https%3A%2F%2Fmy%2Est%2Ecom%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex%5Fmx%5Fstm32%2FAllItems%2Easpx&currentviews=0

rm239955
Associate II
Posted on November 27, 2014 at 13:51

Hallo

How to set the value of APB1 Clock of STM32F429 to 42Mhz.

Posted on November 27, 2014 at 14:09

> How to set the value of APB1 Clock of STM32F429 to 42Mhz.

Don't hijack threads - start your own.

JW