Problem mit 2 Interrupts

Gast #238595
Lesenswert?
• ▲
▼
Hallo,
ich verwende in einem Programm 2 Timer, die jeweils mit einer ISR
verknüpft sind. Mit Timer2 wird eine Zeitverzögerung gemacht, indem der
Timer so eingestellt wird, dass er eine ms braucht bis er überläuft und
dann wird in der Timer2-Overflow-Routine jedesmal eine Variable erhöht,
bis die gewünschte Verzögerungsdauer erreicht ist.
Mit Timer0 will ich einem Servo die benötigten Impulse geben um eine
Position anzufahren, das geschieht mit Hilfe der
Timer0-Overflow-Routine. Einzeln funktioniert beides, wenn ich jedoch
will dass ein der Endlosschleife im Hauptprogramm erst 1000ms gewartet
wird und dann eine Position angefahren werden soll, wird zwar um 1000ms
verzögert, aber der Servo reagiert nicht. Kann es sein, dass sich da die
beiden ISRs in die Quere kommen, und wenn ja, wie kann ich das Problem
lösen?
Gruß
Martin
Gast #238597
Lesenswert?
• ▲
▼
Hallo,
hier der Code:
1
#define F_CPU 16000000
2

3
#include <avr/io.h>
4
#include <avr/signal.h>
5
#include <avr/interrupt.h>
6
#include <inttypes.h>
7
#include <math.h>
8
#include <avr/delay.h>
9
#include <stdlib.h>
10

11
int16_t sollwert=(0.0023/0.000004); 
12
int16_t wert_alt;
13
int16_t time_20ms;
14
int8_t timerstufe;
15
int8_t t_pos, zaehler;
16
int8_t temp;
17
volatile int16_t waitms_, time, pos;
18

19
void init_usart(void)
20
{
21
 UBRRL |= 0b01100111;
22
 UCSRB = (1<<TXEN) | (1<<RXEN);
23
 UCSRC = (1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0);
24
}
25

26
void send_char(char c)
27
{
28
 while (!(UCSRA & (1<<UDRE))) {}
29
   UDR = c;
30
}
31

32
void send_string(char *s)
33
{
34
 while(*s != '\0')
35
  {
36
   send_char(*s);
37
   s++;
38
  }
39
}
40

41
void waitms(int16_t time_waitms)
42
{
43
 time = time_waitms;
44
 waitms_ = 0;
45
 TCCR2 = (1<<CS22);
46
 TIMSK = (1<<TOIE2);
47
 TCNT2 = (255-250); 
48
 while (waitms_ < time_waitms)
49
  {
50
  }
51
  TIMSK = (0<<TOIE2);
52
  return;
53
}
54

55

56
int main(void)
57
{
58
 
59
 DDRB = (1<<DDB0);
60
 PORTB = (0<<PB0);
61
 TCCR0 = (1<<CS02) | (0<<CS01) | (0<<CS00); //Vorteiler 256
62
 TIMSK = (1<<TOIE0);  //Interrupt enablensei();
63
 init_usart();
64
 sei();
65
 for(;;)
66
 {
67
  char *test;
68
  test = "erfolg!!!";
69
  waitms(1000);
70
  send_string(test);
71
  }
72
} 
73

74

75
SIGNAL (SIG_OVERFLOW0)
76
{
77
 if (timerstufe < 9) 
78
  {
79
     TCNT0 == 0;
80
     timerstufe = timerstufe + 1;
81
     PORTB = (0<<PB0);
82
  }
83
 else
84
  { 
85
   if (timerstufe == 9)
86
    {
87
     TCNT0 = (255 - 98); 
88
     timerstufe = 10; 
89
   PORTB = (0<<PB0);
90
    }
91
   if (timerstufe == 10)
92
    {
93
   PORTB = (1<<PB0);
94
   TCNT0 = (255 - 93);
95
   timerstufe = 0;
96
  }
97
  }
98
} 
99

100
SIGNAL (SIG_OVERFLOW2)
101
{
102
 waitms_ ++;
103
 if(waitms_ < time)
104
  {
105
   TCNT2 = (255-250);
106
  }  
107
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren