Forum: Mikrocontroller und Digitale Elektronik STM32F103 USART1 affects TIM1? TIM1 interrupt frequenz varying


von Noah (Gast)


Angehängte Dateien:

Lesenswert?

Hi,


I am relatively new to STM32 but already did a project with STMs Timers 
DMA...
About the project: this STM32F103C8 chould handle 3 UART (not USART) 
ports with communication on 20Hz basis. Furthermore control a gear motor 
with a relay for direction and FET and PWM for speed. A PID should 
control the speed based on velocity from encoder.
To compute the speed I want to get and 40Hz interrupt from Timer1 as all 
the other timers are busy. So as the clock is 72Mhz I set prescalar to 
1800 and period to 1000. But when I look at the Interrupt calls with my 
Ozi, i get a much lower speed about 0,26 Hz.
So I searched the net how this could be and eventually encovered, that 
there might be interference with UART1.
So the odd part comes now:
When I out comment the UART1 initialization (USART_Cmd) the frequency 
doubles to about 0,5Hz
After adding comments to USART_DMACmd the frequency jumps to 0,18Hz...
After 3 hours of searching and debugging I don't know what to do.
Can anybody tell me what I am doing wrong here?

Thanks in advance
Noah

Source code (PID not finished yet)

von pegel (Gast)


Lesenswert?

...Priority = 0x0F;
Sets the lowest Priority for the Timers.

von Noah (Gast)


Lesenswert?

Hi,
me once again.
After another 2 hours of testing the UART :( I eventually figured out 
what the problem was.
So for any other newbie how runs in this issue:
After reading the datasheet another time I figured out, that TIM1 in 
comparision to TIM2 - 4 has a RepetitionCounter - which I didn't set.
So the right initialization would be
1
TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
2
TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
3
TIM_TimeBase_InitStructure.TIM_Period = 999;
4
TIM_TimeBase_InitStructure.TIM_Prescaler = 1799;
5
TIM_TimeBase_InitStructure.TIM_RepetitionCounter = 0;
6
TIM_TimeBaseInit(TIM1, &TIM_TimeBase_InitStructure);

My explanation for the strange behaviour with UART1 is that 
TIM_TimeBase_InitStructure is local and therefore on the stack or so. As 
RepetitionCounter never gets assigned it is what was at the stack 
before. This varys from the before function calls and their variables. 
So it has nothing to do with UART1, except that its initialization 
function was the last I called before Timer initialization...

Hope I can prevent future confussions.

Happy coding
Noah

PS.:
@pegel you are right. But I'm not into this priority stuff right know. 
And therefore every interrupt gets the same priority... the lowest.
My Motor is running smoothly now, though. And interrupt is very exact at 
25ms so I don't care yet.

von W.S. (Gast)


Lesenswert?

Noah schrieb:
> I am relatively new to STM32 but already did a project with STMs Timers
> DMA...

So you are new to STM32 or to ARM-CORTEX in general?
But the very first thing you do is trying to use DMA.

And when I see the following:
1
void USARTIRQHandler(void)
2
{
3
    /* RXNE handler */
4
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
5
    {
6
        /* If received 't', toggle LED and transmit 'T' */
7
        if((char)USART_ReceiveData(USART1) == 't')
8
        {
9
            USART_SendData(USART1, 'T');
10
            /* Wait until Tx data register is empty, not really
11
             * required for this example but put in here anyway.

I become angry. You really wait inside a interrupt handler?
A rather long time ago I posted here a little project with this 
STM32F103 and there you can see, how to handle all UART's and the 
USB-VCP without any DMA. And how to write a firmware, which does not 
wait inside of interrupt handlers.

W.S.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.