I've tried to create an output clock using the timer 3 of the board STM32F429I-DISCO but the resulting output frequency doesn't match my calculations.
I'm using the file "system_stm32f4xx.c" with this configuration.
*============================================================================= * Supported STM32F42xxx/43xxx devices *----------------------------------------------------------------------------- * System Clock source | PLL (HSE) *----------------------------------------------------------------------------- * SYSCLK(Hz) | 180000000 *----------------------------------------------------------------------------- * HCLK(Hz) | 180000000 *----------------------------------------------------------------------------- * AHB Prescaler | 1 *----------------------------------------------------------------------------- * APB1 Prescaler | 4 *----------------------------------------------------------------------------- * APB2 Prescaler | 2 *----------------------------------------------------------------------------- * HSE Frequency(Hz) | 25000000 *----------------------------------------------------------------------------- * PLL_M | 25 *----------------------------------------------------------------------------- * PLL_N | 360 *----------------------------------------------------------------------------- * PLL_P | 2 *----------------------------------------------------------------------------- * PLL_Q | 7 *----------------------------------------------------------------------------- * PLLI2S_N | NA *----------------------------------------------------------------------------- * PLLI2S_R | NA *----------------------------------------------------------------------------- * I2S input clock | NA *----------------------------------------------------------------------------- * VDD(V) | 3.3 *----------------------------------------------------------------------------- * Main regulator output voltage | Scale1 mode *----------------------------------------------------------------------------- * Flash Latency(WS) | 5 *----------------------------------------------------------------------------- * Prefetch Buffer | ON *----------------------------------------------------------------------------- * Instruction cache | ON *----------------------------------------------------------------------------- * Data cache | ON *----------------------------------------------------------------------------- * Require 48MHz for USB OTG FS, | Disabled * SDIO and RNG clock | *----------------------------------------------------------------------------- *=============================================================================
This configuration should give me a input clock for the timer 3 of 22.5 MHz, Am i right?
I used this piece of code to toggle a led with diferent values for the configuration of the timer 3.
void TIM3_IRQHandler(void) { if(TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET) { TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); GPIO_ToggleBits(GPIOG, GPIO_Pin_14); } }
Timer 3 configuration:
void TIM_Config(void) { /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Time base configuration */ timerInitStructure.TIM_Prescaler = 343; timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up; timerInitStructure.TIM_Period =65535; timerInitStructure.TIM_ClockDivision = 0; timerInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM3, &timerInitStructure); /* TIM Interrupts enable */ TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE); /* TIM2 enable counter */ TIM_Cmd(TIM3, ENABLE); }
I used an oscilloscope to measure the led frequency and this is what i got:
Preescaler = 1 Period = 65535 ----->Led freq = 109.873 Hz ---> 109.873*65535*2(togling dives period by 2)* 1 =14.4Mhz of tim3 Input Freq
Preescaler = 2 Period = 45525 ----->Led freq = 105.449 Hz --->Tim3Inputfreq = 105.449*45525*2*2 = 19.202MHz
Preescaler = 4 Period = 45525 ----->Led freq = 63.2663 = 23.041MHz
Preescaler = 6 Period = 45525 ----->Led freq = 45.1902 = 24.687MHz
I don't know if i'm missing something but i don't understand why the Input frequency of the timer varies so much and why is it not constant at 22.5MHz?
Could someone clarify what's going on?
Thanks.
Full code:
http://pastebin.com/Ntg5j9dP --> main.c
http://pastebin.com/p7Bui9Db --> system_stm32f4xx.c
I forgot to say i got the same results with and without the funciion "SystemInit()".
SystemInit(); TIM_Config(); EnableTimer3Interrupt(); LED_Init();
The STM32F429I-DISCO has an 8 MHz crystal (clock source), you'd probably want to adjust the PLL properly. Then the TIMCLK on APB2 timers would be 180 MHz, and 90 MHz on the APB1 timers.