AnsweredAssumed Answered

Generating 1Mhz CLK signal using TIMER

Question asked by EYIGEL.Levent on Mar 14, 2012
Latest reply on Mar 14, 2012 by Clive One
Hello I want to try generate 1Mhz Clock signal , to use it with another board.I made configuration the TIM3 but i see 130khz ~ 140khz signal on the oscilloscope screen

the configuration code is:
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"

/* Private variables ---------------------------------------------------------*/
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
__IO uint16_t CCR1_Val = 145545;
__IO uint16_t CCR2_Val = 16384;
__IO uint16_t CCR3_Val = 8192;
__IO uint16_t CCR4_Val = 4096;
ErrorStatus HSEStartUpStatus;


/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);


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


/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* System Clocks Configuration */
  RCC_Configuration();


  /* NVIC Configuration */
  NVIC_Configuration();


  /* GPIO Configuration */
  GPIO_Configuration();


  /* ---------------------------------------------------------------------------
    TIM3 Configuration: Output Compare Toggle Mode:
    TIM3CLK = 36 MHz, Prescaler = 0x2, TIM3 counter clock = 12 MHz
    CC1 update rate = TIM3 counter clock / CCR1_Val = xxHz
    CC2 update rate = TIM3 counter clock / CCR2_Val = xxx Hz
    CC3 update rate = TIM3 counter clock / CCR3_Val = xxx Hz
    CC4 update rate = TIM3 counter clock / CCR4_Val =  xxx Hz
  ----------------------------------------------------------------------------*/


  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 1455;
  TIM_TimeBaseStructure.TIM_Prescaler = 1;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;


  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);


  /* Output Compare Toggle Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OC1Init(TIM3, &TIM_OCInitStructure);


  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);


  /* Output Compare Toggle Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;


  TIM_OC2Init(TIM3, &TIM_OCInitStructure);


  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);


  /* Output Compare Toggle Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;


  TIM_OC3Init(TIM3, &TIM_OCInitStructure);


  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);


  /* Output Compare Toggle Mode configuration: Channel4 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;


  TIM_OC4Init(TIM3, &TIM_OCInitStructure);


  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);


  /* TIM enable counter */
  TIM_Cmd(TIM3, ENABLE);


  /* TIM IT enable */
  TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);


  while (1)
  {}
}
void RCC_Configuration(void)
{
  /* Setup the microcontroller system. Initialize the Embedded Flash Interface,  
     initialize the PLL and update the SystemFrequency variable. */
  SystemInit();


  /* PCLK1 = HCLK/4 */
  RCC_PCLK1Config(RCC_HCLK_Div4);


  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);


  /* GPIOA clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE|
                         RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
}
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;


#ifdef STM32F10X_CL
  /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;


  GPIO_Init(GPIOC, &GPIO_InitStructure);


  GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);     


#else
  /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;


  GPIO_Init(GPIOA, &GPIO_InitStructure);


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
#endif
}
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;


  /* Enable the TIM3 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
And the TIM3_IRQHanlder() function is:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TIM3_IRQHandler(void)

 static  unsigned int i;
  if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
  {
    
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 );
    capture = TIM_GetCapture1(TIM3);
    TIM_SetCompare1(TIM3, capture + CCR1_Val );
    if(i==1)
    {
    GPIO_SetBits(GPIOE,GPIO_Pin_7);
    i=0;
    }
    else
    {
    GPIO_ResetBits(GPIOE,GPIO_Pin_7);
    i=1;
    }
  }
I want to see from PE7 pin this clock signal..

who can help me about this function ? where do i making wrong on this functios ? I see again: I want to make 1 Mhz clk signal but i can't do this..

Outcomes