We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello, I configured a timer to generate a signal every 1 ms.
See below my code about the timer:
TIM_DeInit(TIMx); // TIM1 Deinitialization * TIM_InitStructure.TIM_Mode = TIM_OCM_CHANNEL_1; TIM_InitStructure.TIM_OC1_Modes = TIM_TIMING; TIM_InitStructure.TIM_Clock_Source = TIM_CLK_APB; TIM_InitStructure.TIM_Clock_Edge = TIM_CLK_EDGE_FALLING; TIM_InitStructure.TIM_Prescaler = 0xEF; // period = 5us TIM_InitStructure.TIM_Pulse_Length_1 = 0xC8; // cycle of 200 period => cycle = 1ms TIM_Init (TIMx, &TIM_InitStructure);
And the statements inside timer's interrupt:
void TIM1_IRQHandler(void) { // port 3 pin 7 activated P37_1; // ACK interrupt TIM_ClearFlag(TIM1, TIM_FLAG_OC1); // clear Output Compare 1 flag TIM_CounterCmd(TIM1, TIM_CLEAR); // Reset TIM1 Counter VIC0->VAR = 0xFF; // write any value to VIC0 VAR }
BUT my oscilloscope measured a period of 1.03 ms instead of the 1ms expected. Normally to generate a pulse every 1.03 ms the TIM_Pulse_Length_1 should be set to 0xCE. As you can see, the difference between 0xCE and 0xC8 is big.
What can be the cause of this timer's inaccuracy ? > Problem in PLL configuration ? > Problem of crystal ? > Problem in timer configuration ?
Has someone the same problem ? In advance, thank you
What can be the cause of this timer's inaccuracy ?
It would be helpful to know what chip you're using and how it is configured (PLL, etc).
Are you running an RC oscillator?
We use: > STR912FAW44
for the PLL configuration : > In the main application loaded by the bootloader, the PLL is configured as followed inside main.c (there is no file with the extension .s):
// Initialisation SCU_MCLKSourceConfig(SCU_MCLK_OSC); /* Default configuration */ /*wait state insertion :This function should be executed from SRAM when*/ /*booting from bank1 to avoid Read-While-Write from the Same Bank.*/ FMI_Config(FMI_READ_WAIT_STATE_2, FMI_WRITE_WAIT_STATE_0, FMI_PWD_ENABLE,\ FMI_LVD_ENABLE, FMI_FREQ_HIGH);/*Insert 2 Wait States for read*/ SCU_PLLFactorsConfig(192, 25, 2); /* PLL factors Configuration based on*/ /* a OSC/Crystal value = 25Mhz*/ SCU_PLLCmd(ENABLE); /* PLL Enable and wait for Locking*/ SCU_RCLKDivisorConfig(SCU_RCLK_Div1); /* RCLK @96Mhz */ SCU_HCLKDivisorConfig(SCU_HCLK_Div1); /* AHB @96Mhz */ SCU_FMICLKDivisorConfig(SCU_FMICLK_Div1);/* FMI @96Mhz */ SCU_PCLKDivisorConfig(SCU_PCLK_Div2); /* APB @48Mhz */ SCU_MCLKSourceConfig(SCU_MCLK_PLL); /* MCLK @96Mhz */
I give you also how I calculated my counter and prescaler: > APB = 48Mhz > prescaler = 0xEF because we choosed to count 5us per 5us <= 1/(48/240) = 5 us > counter = 0xC8 because 0xC8 = 200 and 200 * 5us = 1 ms
Are you running an RC oscillator? >> I used the MCBSTR9 eval board. They used a 25MHz oscillator
How did it measure that? And is your oscilloscope calibrated to better than 3%...?
Oscillo: > We tried with another oscillo (a new one, so normally it is calibrated) and we measured (with the cursor) the same period as with the another one.
Test of Timer accuracy > We configured the clock to be at 25MHz (oscillator frequency) and we did two tests: 1st) we used a prescaler of 25 and we counted to 1000. we measured 1.009 ms 2nd) We used a prescaler of 1 and we counted to 25 000. We measured 1.005 ms
It seems to us that the STR9 is not accurate but ... we don't believe it. The mistake must come from us ?
SCU_MCLKSourceConfig(SCU_MCLK_PLL); /* MCLK @96Mhz */
Whats the exact value? Is it really 96000000Hz? For me its 99328000 instead of 99MHz.
Helo.
the reload value in the timer of STR9 for u is 0xFFFC i think/?
do yo're calcs know it?
always yo're freind
Zeusti.
(now As approved by Trevor Smythe)
Hi Stefan, I'm sorry but I didn't understand your question : "SCU_MCLKSourceConfig(SCU_MCLK_PLL); /* MCLK @96Mhz */ Whats the exact value? Is it really 96000000Hz?"
How can I measure that value ? this PLL clock is generated inside the micro, so you can't access it ?
Hi Approved Zeusti, You are right, the counter is reset to 0xFFFC. So you think that I should change the value of my counter to my previous value + 6? I will try and replace my counter value with my counter value + 6 (to take into account the starting point 0xFFFC). (If you are right, I don't understand why ST didn't explain it in its datasheet)
I keep you inform.
Zeusti is right on this.
I briefly got caught out by this unexpected reload value when I first started using the STR9.
It is mentioned in the ST documentation, but I never found an explanation for why this is the reload value.
I guess that someone far cleverer than me decided that this was a good thing to do.
As far as I can remember, the STR9 can also generate an interrupt when the count rolls over from 0xFFFF to 0x0000. Is this tied in with the reload value? Anyone know?
I'm sorry but I didn't understand your question :
Sometimes it says e.g. 96MHz but the real frequency is a little bit higher or lower. It depends on the base frequency, which is used for clock generation, by up-/downscaling. So if the frequency of your OSC/Crystal is not exactly 25Mhz the resulting frequencies e.g. MCLK will not have the frequency you expect.
SCU_PLLFactorsConfig(192, 25, 2); /* PLL factors Configuration based on*/ /* a OSC/Crystal value = 25Mhz*/
If this were my project, I'd be seriously worried wether an output frequency of 192 MHz for the PLL might be impossible to achieve with an input of 25 MHz. PLLs like this usually only allow to scale up the input frequency by an integer factor. 192/25 isn't integer.
In the case at hand, I'd expect a result of 8*25 MHz, i.e. 200 MHz. That would throw off all other frequencies and timings by a factor of 200/192, which just so happens to be 1.04. Sound familiar? ;-)
I was wondering ... Who is this Trevor Smythe?
Is he some sort of guru?
(no idea who is Trevor Smythe -> en.wikipedia.org/.../Trevor_Smith)
I did a mistake. To take into account that the counter starts at 0xFFFC and not at 0x0000, I used counter(0xFFFC) = counter(0x0000) - 4
@Stefan: For the oscillator, I used the MCB-STR9. Normally the oscillator is well chose.
@all: Now we have 1.006 ms instead of 1.03ms
Here is the guru:
www.facebook.com/.../1406569736