cancel
Showing results for 
Search instead for 
Did you mean: 

TIM4 how to get Peak-To-Peak 4 Mhz clock

killgi
Associate II
Posted on April 14, 2009 at 11:21

TIM4 how to get Peak-To-Peak 4 Mhz clock

3 REPLIES 3
killgi
Associate II
Posted on May 17, 2011 at 13:09

Hi I'm newbie with STM32 and I need to learn quickly for a new project. :-[

I need to generate Peak-To-Peak 4 Mhz clock for external device as an oscillator(camera). I have an example, it is used TIM4 and generate clock pulse with 18MHz.

Code:

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;

// Time base configuration

TIM_TimeBaseStructure.TIM_Period = 1;

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

// Output Compare Active Mode configuration: Channel3

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = 1;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OC3Init(TIM4, &TIM_OCInitStructure);

TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Disable);

// TIM4 enable counter

TIM_Cmd(TIM4, ENABLE);

Can you, STM32 experts, help me to fix this code to obtain 4MHz

killgi
Associate II
Posted on May 17, 2011 at 13:09

My RCC config

Code:

RCC_DeInit();

/* Enable HSE */

RCC_HSEConfig(RCC_HSE_ON);

/* Wait till HSE is ready */

HSEStartUpStatus = RCC_WaitForHSEStartUp();

if (HSEStartUpStatus == SUCCESS){

/* Enable Prefetch Buffer */

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/* Flash 2 wait state */

FLASH_SetLatency(FLASH_Latency_2);

/* HCLK = SYSCLK */

RCC_HCLKConfig(RCC_SYSCLK_Div1);

/* PCLK2 = HCLK */

RCC_PCLK2Config(RCC_HCLK_Div1);

/* PCLK1 = HCLK/2 */

RCC_PCLK1Config(RCC_HCLK_Div2);

/* PLLCLK = 8MHz * 9 = 72 MHz */

RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

/* Enable PLL */

RCC_PLLCmd(ENABLE);

/* Wait till PLL is ready */

while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){}

/* Select PLL as system clock source */

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

/* Wait till PLL is used as system clock source */

while (RCC_GetSYSCLKSource() != 0x08){}

}

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 |

RCC_APB1Periph_SPI2 |

RCC_APB1Periph_I2C1,

ENABLE);

Posted on May 17, 2011 at 13:09

U've configured the RCC to run the system clock at 72 MHz and the peripheral clock would be 36 Mhz. The timer clock prescaler value used is zero (no prescaling) and auto reload value (period) is 1. That is, the timer counts to 1 at every tick of the system clock, at the next tick it resets to zero and toggles the output pin (because a compare match with zero occurs). So in effect, you get a square wave with frequency 36/2, that is 18Mhz. So, by my reckoning, if you change the period to 8 instead of 1, the timer should count to 8 and then reset at the next tick. It should give you 36/9 = 4Mhz wave.

TIM_TimeBaseStructure.TIM_Period = 8;

try it, might work 😉

[ This message was edited by: felix.varghese on 14-04-2009 16:20 ]