Forum: Mikrocontroller und Digitale Elektronik STM32 und Timer Interrupt-Periode umstellen


von Klaus (Gast)


Lesenswert?

Hallo, ich möchte innerhalb des Timer Interrupts die Periode umstellen, 
also dass der nächste Interrupt langsamer/schneller kommt. Geht sowas?

Ich habe es mit folgenden Funktionen versucht. Das funktioniert nicht:

1
int main(void)
2
{
3
   TI_Init(200, 1800); //5ms
4
   TI_Start();
5
6
   while(1)
7
   {
8
   }
9
}
10
11
void TI_Init(uint16_t period, uint16_t prescaler)
12
{
13
   TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure;
14
   NVIC_InitTypeDef NVIC_InitStructure;
15
16
   //Initialize Timer 2 and interrupt.
17
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
18
   TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
19
   TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
20
   TIM_TimeBase_InitStructure.TIM_Prescaler = prescaler - 1;  
21
   TIM_TimeBase_InitStructure.TIM_Period = period - 1;      
22
   TIM_TimeBaseInit(TIM2, &TIM_TimeBase_InitStructure);
23
   TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
24
   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
25
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
26
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
27
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
28
   NVIC_Init(&NVIC_InitStructure);
29
}
30
31
void TI_Start(void)
32
{
33
   TIM_Cmd(TIM2, ENABLE);
34
}
35
36
void TI_Stop(void)
37
{
38
   TIM_Cmd(TIM2, DISABLE);
39
}
40
41
void TIM2_IRQHandler(void)
42
{
43
   TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
44
   
45
   TI_Stop();
46
   TI_Init(40000, 1800); //1000ms
47
   TI_Start();
48
  
49
   Led2_Toggle();
50
}

Die LED sollte also im Sekundentakt blinken. Tut Sie aber nicht. Sie 
geht an (blinkt im 5ms Takt) und das wars.

Timer und LED funktionieren. Wenn ich nämlich in der main() den Timer 
mit 40000 initialisiere und die Stop()-Init()-Start()-Sequenz im 
Interrupt weglasse, dann blinkt die LED im Sekundentakt.

Danke, Klaus

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.