Hi,
aus dem main()-Teil rufe ich eine sleep(uint milisec)-Fkt. auf.
Allerdings tutst diese sleep()-Fkt. nicht. Also habe ich statt des
Aufrufs im main()-Teil die sleep-Funktion da direkt reingebaut und dann
funktioniert das. Warum geht das als Subfunktion nicht? Der Code dazu,
der NICHT funktioniert:
1 | int main(void)
|
2 | {
|
3 | // Initialisierung der Ports..
|
4 | sei();
|
5 |
|
6 | while(1) // Warteschleifentest, Parameter in ms
|
7 | {
|
8 | sleep(5);
|
9 | }
|
10 | return 0;
|
11 | }
|
12 |
|
13 | void sleep(unsigned int waittime)
|
14 | {
|
15 | // Timer 0, 8-Bit initialisieren
|
16 | w = 256-29;
|
17 | TCNT0 = w; // 29 Inkrementierungen bis Interrupt -> 29 x 34.722us =
|
18 | 1.00694 ms
|
19 | TCCR0B = (1<<CS02); // Timer 0, Prescaler = 256 f. Wartezeiten von
|
20 | 34.722 us pro TCNT0 Inkermentierung
|
21 | //TIFR0 = (1<<TOV0);
|
22 |
|
23 | do
|
24 | {
|
25 | TIMSK0 = (1<<TOIE0); // Timer Overflow Interrupt Enable 0
|
26 | einschalten
|
27 | asm volatile("NOP"); // tu_was_gescheites();
|
28 | PORTD = ~PORTD; // Kontrolle mit Scope
|
29 | }while(waittime > 0);
|
30 | TIMSK0 = 0;
|
31 | PORTB = ~PORTB; // Kontrolle mit Scope
|
32 | }
|
33 |
|
34 | ISR(SIG_OVERFLOW0)
|
35 | {
|
36 | waittime--;
|
37 | TCNT0 = w;
|
38 | }
|
Funktionieren tut's aber, wenn ich anstatt des sleep(5)-Aufrufs die
komplette sleep-Routine da reinbaue, und vor der do...while Schleife
die Variable waittime auf 5 setzte (waittime = 5;).
Versteh ich nicht, warum das so ist.
Ichb habe mir auch mal das Listing dazu angekukkt (*.lss), darin wird
die do...while Schleife anders uebersetzt, wenn die in einer
Subfunktion aufgerufen wird. Da heisst es naemlich (Zeilen mit >)
---
PORTD = ~PORTD;
84: 8b b1 in r24, 0x0b ; 11
86: 80 95 com r24
88: 8b b9 out 0x0b, r24 ; 11
//PORTB = ~waittime;
}while(waittime > 0);
> 8a: 21 15 cp r18, r1
> 8c: 31 05 cpc r19, r1
> 8e: b9 f7 brne .-18 ; 0x7e
TIMSK0 = 0;
90: 10 92 6e 00 sts 0x006E, r1
---
(funzt nicht!)
Das funzt aber:
---
PORTD = ~PORTD;
a2: 8b b1 in r24, 0x0b ; 11
a4: 80 95 com r24
a6: 8b b9 out 0x0b, r24 ; 11
//PORTB = ~waittime;
}while(waittime > 0);
> a8: 80 91 00 01 lds r24, 0x0100
> ac: 90 91 01 01 lds r25, 0x0101
> b0: 89 2b or r24, r25
> b2: a1 f7 brne .-24 ; 0x9c
TIMSK0 = 0;
b4: 10 92 6e 00 sts 0x006E, r1
---
Gibt es dafuer ne Erklaerung? Ist uebrigens mit dem GCC uebersetzt
worden. Ist da evtl. ein Parameter dran Schuld?
Compiling: t.c
avr-gcc -c -mmcu=atmega48 -I. -g -DF_CPU=7372800UL
-DUART_RX_BUFFER_SIZE=128 -DUART_TX_BUFFER_SIZE=128 -Os
-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall
-Wstrict-prototypes -Wa,-adhlns=t.lst -std=gnu99 -MD -MP -MF
.dep/t.o.d t.c -o t.o
Linking: t.elf
avr-gcc -mmcu=atmega48 -I. -g -DF_CPU=7372800UL
-DUART_RX_BUFFER_SIZE=128 -DUART_TX_BUFFER_SIZE=128 -Os
-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall
-Wstrict-prototypes -Wa,-adhlns=t.o -std=gnu99 -MD -MP -MF
.dep/t.elf.d t.o --output t.elf -Wl,-Map=t.map,--cref -lm