AnsweredAssumed Answered

STM32f4 Discovery Timer Clock Problem

Question asked by Stratoraider on Mar 6, 2014
Latest reply on Mar 6, 2014 by Clive One
Hi you all,

I am trying to produce a low jittered sharp clock signal at 96kHz freq. for my I2S application. The purpose is obtaining a interrupt at every 96kHz cycle (10.4us) and with each interrupt, sending one sample of the I2S data (a sine wave).

I calculate the clock divider values as (7-1) for PSC, (250-1) for ARR and Period, 0 for clockDivision for TIM1 I assume that work with 84MHz clock frequency according to referance manual and app. notes.  But, I can obtain 96kHz clock freq. with (7-1) for PSC and (125-1) for ARR and Period, CLockDivision is same as 0. Moreover, if I configure all divider values as 0 (PSC) and 1(ARR and Period), I see 1.8us clock frequency on ToggleBit() with my Tektronix oscilloscope and it don't change with small divider values like (7-1) for PSC and 1 for ARR. Even though, I see the correct values as 168MHz, 42Mhz with RCC_GetClocksFreq() function, I see different thing on scope. Can you help me or any idea why I am facing that weird problem?

Thanks a lot!

You can see my code in below.

#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_usart.h"
#include "misc.h"
#include "stdio.h"

void RCC_Config();
void TIM_NVIC_Config();
void GPIO_Config();
void Delay(__IO uint32_t nCount);
void USART2_Config();
void TIM1_CC_IRQHandler(void);

int main(void)

    RCC_ClocksTypeDef RCC_Clocks;

    GPIO_SetBits(GPIOD, GPIO_Pin_15);
    GPIO_ResetBits(GPIOD, GPIO_Pin_15);


    return 0;


void RCC_Config()
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
                            |RCC_AHB1Periph_GPIOD, ENABLE);


void GPIO_Config()
    GPIO_InitTypeDef GPIOStruct;

    GPIOStruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIOStruct.GPIO_Speed = GPIO_Speed_100MHz;
    GPIOStruct.GPIO_OType = GPIO_OType_PP;
    GPIOStruct.GPIO_Pin = GPIO_Pin_7;
    GPIOStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIOStruct.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_8|GPIO_Pin_9
    GPIOStruct.GPIO_Speed = GPIO_Speed_100MHz;
    GPIOStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_Init(GPIOA, &GPIOStruct);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_TIM1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_TIM1);

    //Alternate Func. Configurations for USART2
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);    //USART2_TX
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);     //USART2_RX

void TIM_NVIC_Config()
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
    NVIC_InitTypeDef NVICStruct;

    /*TIM Configurations*/
    TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStruct.TIM_Prescaler = 0;
    TIM_TimeBaseStruct.TIM_Period = 1;
    TIM_TimeBaseStruct.TIM_ClockDivision = 0;
    TIM_TimeBaseStruct.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStruct);
    TIM_SetAutoreload(TIM1, 1);

    /*NVIC configurations*/
    NVICStruct.NVIC_IRQChannel = TIM1_CC_IRQn;
    NVICStruct.NVIC_IRQChannelPreemptionPriority = 0;
    NVICStruct.NVIC_IRQChannelSubPriority = 1;
    NVICStruct.NVIC_IRQChannelCmd = ENABLE;

    TIM_Cmd(TIM1, ENABLE);

    TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);

/*----------------------------Delay Function--------------------------------------*/
void Delay(__IO uint32_t nCount)
/*---------------------------USART Configurations---------------------------------*/
void USART2_Config(void){

    USART_InitTypeDef USART_InitStructure;

    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    USART_Init(USART2, &USART_InitStructure);


void TIM1_CC_IRQHandler(void)
    if(TIM_GetITStatus(TIM1,TIM_IT_Update) != RESET)
        GPIO_ToggleBits(GPIOB, GPIO_Pin_7);