Problem in Avr GCC, kehrt nicht aus Prozedur zurück.

Gast #2766529
Lesenswert?

Hallo liebe Forianer

Ich habe ein Problem, bei welchem ich nicht mehr weiter komme.

In einem STK500 steckt ein Atmega 328P.
PB0 und PB1 sind mit LED0 und LED1 verbunden.

Folgenden Code habe ich im AVR Studio 6 übersetzt und auf den Chip 
geladen:
1
#include <avr/io.h>
2

3
void test(void)
4
{
5
    DDRB |= (1<<DDB1);
6
}
7

8
int main(void)
9
{
10
    test();
11
    DDRB |= (1<<DDB0);
12
  
13
    while(1)
14
    {
15
    }
16
}

Es leuchtet nun LED1. Erwarten würde ich aber, dass beide LED leuchten.

Zum Test habe ich folgenden Code compiliert und übertragen: Es leuchten 
beide LED.
1
#include <avr/io.h>
2

3
int main(void)
4
{
5
  
6
    DDRB |= (1<<DDB1);
7
    DDRB |= (1<<DDB0);
8
  
9
    while(1)
10
    {
11
    }
12
}

Was mache ich falsch?
Gast #2766562
Lesenswert?

Peter II schrieb:
> zeig uns mal bitte den ASM code (*.lst) von beiden versionen.
>
> hast du auch den richtigen Prozessor angeben?

Der Inhalt der LST Files folgt:
Es ist die gleiche Reihenfolge wie oben.

Ich habe den Richtigen Prozessor ausgewählt, ja. Ich habe auch das 
Projekt gelöscht und neu angelegt. Ausserdem habe ich letztendlich noch 
einen zweiten Mega88PA (es waren beide neu) eingesetzt.

Mit Funktion:
1
test.elf:     file format elf32-avr
2

3
Sections:
4
Idx Name          Size      VMA       LMA       File off  Algn
5
  0 .text         00000090  00000000  00000000  00000054  2**1
6
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
7
  1 .stab         000006cc  00000000  00000000  000000e4  2**2
8
                  CONTENTS, READONLY, DEBUGGING
9
  2 .stabstr      00000090  00000000  00000000  000007b0  2**0
10
                  CONTENTS, READONLY, DEBUGGING
11
  3 .debug_aranges 00000020  00000000  00000000  00000840  2**0
12
                  CONTENTS, READONLY, DEBUGGING
13
  4 .debug_pubnames 00000024  00000000  00000000  00000860  2**0
14
                  CONTENTS, READONLY, DEBUGGING
15
  5 .debug_info   00000097  00000000  00000000  00000884  2**0
16
                  CONTENTS, READONLY, DEBUGGING
17
  6 .debug_abbrev 00000063  00000000  00000000  0000091b  2**0
18
                  CONTENTS, READONLY, DEBUGGING
19
  7 .debug_line   00000114  00000000  00000000  0000097e  2**0
20
                  CONTENTS, READONLY, DEBUGGING
21
  8 .debug_frame  00000030  00000000  00000000  00000a94  2**2
22
                  CONTENTS, READONLY, DEBUGGING
23
  9 .debug_str    00000077  00000000  00000000  00000ac4  2**0
24
                  CONTENTS, READONLY, DEBUGGING
25
 10 .debug_pubtypes 0000001e  00000000  00000000  00000b3b  2**0
26
                  CONTENTS, READONLY, DEBUGGING
27

28
Disassembly of section .text:
29

30
00000000 <__vectors>:
31
   0:  0c 94 34 00   jmp  0x68  ; 0x68 <__ctors_end>
32
   4:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
33
   8:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
34
   c:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
35
  10:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
36
  14:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
37
  18:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
38
  1c:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
39
  20:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
40
  24:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
41
  28:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
42
  2c:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
43
  30:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
44
  34:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
45
  38:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
46
  3c:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
47
  40:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
48
  44:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
49
  48:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
50
  4c:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
51
  50:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
52
  54:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
53
  58:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
54
  5c:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
55
  60:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
56
  64:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
57

58
00000068 <__ctors_end>:
59
  68:  11 24         eor  r1, r1
60
  6a:  1f be         out  0x3f, r1  ; 63
61
  6c:  cf ef         ldi  r28, 0xFF  ; 255
62
  6e:  d8 e0         ldi  r29, 0x08  ; 8
63
  70:  de bf         out  0x3e, r29  ; 62
64
  72:  cd bf         out  0x3d, r28  ; 61
65
  74:  0e 94 42 00   call  0x84  ; 0x84 <main>
66
  78:  0c 94 46 00   jmp  0x8c  ; 0x8c <_exit>
67

68
0000007c <__bad_interrupt>:
69
  7c:  0c 94 00 00   jmp  0  ; 0x0 <__vectors>
70

71
00000080 <test>:
72

73
#include <avr/io.h>
74

75
void test(void)
76
{
77
  DDRB |= (1<<DDB1);
78
  80:  21 9a         sbi  0x04, 1  ; 4
79
}
80
  82:  08 95         ret
81

82
00000084 <main>:
83

84
int main(void)
85
{
86
  
87
  test();
88
  84:  0e 94 40 00   call  0x80  ; 0x80 <test>
89
  DDRB |= (1<<DDB0);
90
  88:  20 9a         sbi  0x04, 0  ; 4
91
  8a:  ff cf         rjmp  .-2        ; 0x8a <main+0x6>
92

93
0000008c <_exit>:
94
  8c:  f8 94         cli
95

96
0000008e <__stop_program>:
97
  8e:  ff cf         rjmp  .-2        ; 0x8e <__stop_program>

Ohne Funktion:
1
test.elf:     file format elf32-avr
2

3
Sections:
4
Idx Name          Size      VMA       LMA       File off  Algn
5
  0 .text         0000008a  00000000  00000000  00000054  2**1
6
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
7
  1 .stab         000006cc  00000000  00000000  000000e0  2**2
8
                  CONTENTS, READONLY, DEBUGGING
9
  2 .stabstr      00000090  00000000  00000000  000007ac  2**0
10
                  CONTENTS, READONLY, DEBUGGING
11
  3 .debug_aranges 00000020  00000000  00000000  0000083c  2**0
12
                  CONTENTS, READONLY, DEBUGGING
13
  4 .debug_pubnames 0000001b  00000000  00000000  0000085c  2**0
14
                  CONTENTS, READONLY, DEBUGGING
15
  5 .debug_info   00000082  00000000  00000000  00000877  2**0
16
                  CONTENTS, READONLY, DEBUGGING
17
  6 .debug_abbrev 0000004e  00000000  00000000  000008f9  2**0
18
                  CONTENTS, READONLY, DEBUGGING
19
  7 .debug_line   000000fc  00000000  00000000  00000947  2**0
20
                  CONTENTS, READONLY, DEBUGGING
21
  8 .debug_frame  00000020  00000000  00000000  00000a44  2**2
22
                  CONTENTS, READONLY, DEBUGGING
23
  9 .debug_str    00000072  00000000  00000000  00000a64  2**0
24
                  CONTENTS, READONLY, DEBUGGING
25
 10 .debug_pubtypes 0000001e  00000000  00000000  00000ad6  2**0
26
                  CONTENTS, READONLY, DEBUGGING
27

28
Disassembly of section .text:
29

30
00000000 <__vectors>:
31
   0:  0c 94 34 00   jmp  0x68  ; 0x68 <__ctors_end>
32
   4:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
33
   8:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
34
   c:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
35
  10:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
36
  14:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
37
  18:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
38
  1c:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
39
  20:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
40
  24:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
41
  28:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
42
  2c:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
43
  30:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
44
  34:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
45
  38:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
46
  3c:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
47
  40:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
48
  44:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
49
  48:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
50
  4c:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
51
  50:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
52
  54:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
53
  58:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
54
  5c:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
55
  60:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
56
  64:  0c 94 3e 00   jmp  0x7c  ; 0x7c <__bad_interrupt>
57

58
00000068 <__ctors_end>:
59
  68:  11 24         eor  r1, r1
60
  6a:  1f be         out  0x3f, r1  ; 63
61
  6c:  cf ef         ldi  r28, 0xFF  ; 255
62
  6e:  d8 e0         ldi  r29, 0x08  ; 8
63
  70:  de bf         out  0x3e, r29  ; 62
64
  72:  cd bf         out  0x3d, r28  ; 61
65
  74:  0e 94 40 00   call  0x80  ; 0x80 <main>
66
  78:  0c 94 43 00   jmp  0x86  ; 0x86 <_exit>
67

68
0000007c <__bad_interrupt>:
69
  7c:  0c 94 00 00   jmp  0  ; 0x0 <__vectors>
70

71
00000080 <main>:
72

73

74
int main(void)
75
{
76
  
77
  DDRB |= (1<<DDB1);
78
  80:  21 9a         sbi  0x04, 1  ; 4
79
  DDRB |= (1<<DDB0);
80
  82:  20 9a         sbi  0x04, 0  ; 4
81
  84:  ff cf         rjmp  .-2        ; 0x84 <main+0x4>
82

83
00000086 <_exit>:
84
  86:  f8 94         cli
85

86
00000088 <__stop_program>:
87
  88:  ff cf         rjmp  .-2        ; 0x88 <__stop_program>
Gast #2766565
Lesenswert?

Peter II schrieb:
> Kan asta schrieb:
>> Willst du ihm nicht zwischenzeitlich erstmal den Unterschied zwischen
>> DDR und PORT erklären?
>
Der Unterschied ist mir bekannt.

> ist doch egal, auch mit einem PullUp leuchtet die LED. Das verhalten
> sollte zumindest immer gleich sein.
>

>> Vielleicht auch mal PORTB mit den richtigen Werten initialisieren ?
> auch egal, es ist definiert das es am anfang 0 ist.

Richtig, daher sollte die Led leuchten, wenn der Port als Ausgang 
gesetzt ist (und auf 0 initialisiert), weil beim STK500 die Leds 
low-aktiv sind.
Gast #2766694
Lesenswert?

Herzlichen Dank an JojoS und Jörg Wunsch.

Ihr habt beide Recht: Der ret funktioniert nicht, und der Prozessor ist 
kein 328P.

Das ist wohl der peinlichste Fehler, der mir je unterlaufen ist (auch 
wenn ich das nur als Hobby mache). Es ist ein Atmega88 (beide, die ich 
getestet habe). Jetzt wo auch das AVR-Studio den richtigen Prozessor 
kennt, geht es natürlich auch.

Noch einmal vielen Dank dafür, dass ihr mich in die richtige Richtung 
gelenkt habt.

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