Forum: Mikrocontroller und Digitale Elektronik Problem mit Timer Atmega8515


von Sebastian (Gast)


Lesenswert?

Hi Leute,

ich habe eine Problem mit dem Timer beim Atmega8515.

Hab mich so gut es ging durch das Datenblatt gewurstelt, doch irgendwo 
klemmt es noch.

Leider finde ich nirgends einen Beispiel Code und mit dem Tutorium komme 
ich auch nicht recht weiter.

Hier mein Code.
1
/* Test Timer Atmega8515 STK 500 */
2
3
 
4
#include <avr/io.h>          // (1)
5
#include <inttypes.h>
6
#include <avr/interrupt.h>
7
#include <stdlib.h>
8
#define F_CPU 8000000UL
9
10
uint16_t count_overflow=0;
11
int timer;
12
13
int main (void) {            // (2)
14
   DDRB  = 0xff;             // (3)
15
   PORTB = 0xff;
16
   //DDRA  = 0x0;
17
   
18
   int i;
19
   i = 1;
20
   
21
22
23
 // Timer0 (8Bit): Normal-Mode(0), Prescaler 1/1024
24
  TCCR0 = (0<<FOC0)|(0<<WGM00)|(1<<COM01)|(0<<COM00)|(0<<WGM01)|(1<<CS02)|(0<<CS01)|(1<<CS00);
25
  TIMSK = (0<<OCIE1B)|(0<<OCIE1A)|(1<<TOIE1); // Overflow ON
26
27
  sei(); // interupts global on
28
29
30
   while(1) {
31
   
32
   if(i == 0 && timer == 1) {
33
   PORTB = 0xf0;
34
   i=1;
35
   timer = 0;
36
   }
37
   
38
   if(i == 1 && timer == 1) {
39
   PORTB = 0x0f;
40
   i=0;
41
   timer = 0;
42
   }
43
   
44
45
46
   } 
47
48
49
}
50
51
52
// Timer0 Overflow Interrupt ------------------------------
53
ISR (TIMER0_OVF_vect)
54
{
55
  count_overflow++; 
56
  if (count_overflow >= 4000)
57
  {
58
    timer = 1;
59
    count_overflow = 0; // reset counter
60
  }
61
} //-------------------------------------- Ende TOV0_INT */

Kann es sein das für die Verwendung des Timers noch etwas an den Fuse 
Bits eingestellt werden muss?

FUSE_BITS = -u -U lfuse:w:0xff:m -U hfuse:w:0xd9:m

Danke für eure Hilfe wünsch schon mal einen guten Rutsch

Gruß Sebastian

von Stefan E. (sternst)


Lesenswert?


von Stefan B. (stefan) Benutzerseite


Lesenswert?

Erster Versuchsballon: Die Variable timer muss auch mit volatile 
gekennzeichnet werden. count_overflow auch, wenn es in Zukunft 
ausserhalb ISR (TIMER0_OVF_vect) benutzt wird.

Lesetipps:
http://www.mikrocontroller.net/articles/Spezial:Suche?search=volatile&fulltext=Suche
http://www.mikrocontroller.net/forum/gcc?filter=volatile

von anfaenger (Gast)


Lesenswert?

du enabelst den timeroverflowinterrupt für timer1 obwohl du eigentlich 
mit timer0 arbeitest.

von Sebastian (Gast)


Lesenswert?

Zunächst mal Danke für deine Antwort

ich nehme an du wolltest auf das volatile vor dem int timer hinaus.

Hab ich übernommen! Funktioniert aber leider immer noch nicht!

Gruß Sebastian

von Sebastian (Gast)


Lesenswert?

Super danke das war es!

Für alle nach mir, hier nochmal mein Funktionsfähiger Code!
1
/* Test Timer Atmega8515 STK 500 */
2
3
 
4
#include <avr/io.h>          // (1)
5
#include <inttypes.h>
6
#include <avr/interrupt.h>
7
#include <stdlib.h>
8
#define F_CPU 8000000UL     /* Takt STK 500 mit  */
9
10
volatile uint16_t count_overflow=0;
11
volatile int timer;
12
13
int main (void) {            // (2)
14
   DDRB  = 0xff;             // (3)
15
   PORTB = 0xff;
16
   //DDRA  = 0x0;
17
   
18
   int i;
19
   i = 1;
20
   
21
22
23
 // Timer0 (8Bit): Normal-Mode(0), Prescaler 1/1024
24
  TCCR0 = (0<<FOC0)|(0<<WGM00)|(1<<COM01)|(0<<COM00)|(0<<WGM01)|(0<<CS02)|(0<<CS01)|(1<<CS00);
25
  TIMSK = (0<<OCIE1B)|(0<<OCIE1A)|(1<<TOIE0); // Overflow ON
26
27
  sei(); // interupts global on
28
29
30
   while(1) {
31
   
32
   if(i == 0 && timer == 1) {
33
   PORTB = 0xf0;
34
   i=1;
35
   timer = 0;
36
   }
37
   
38
   if(i == 1 && timer == 1) {
39
   PORTB = 0x0f;
40
   i=0;
41
   timer = 0;
42
   }
43
   
44
45
46
   } 
47
48
49
}
50
51
52
// Timer0 Overflow Interrupt ------------------------------
53
ISR (TIMER0_OVF_vect)
54
{
55
  count_overflow++; 
56
  if (count_overflow >= 4000)
57
  {
58
    timer = 1;
59
    count_overflow = 0; // reset counter
60
  }
61
} //-------------------------------------- Ende TOV0_INT */

Danke guten Rutsch

Sebastian

von Sebastian (Gast)


Lesenswert?

Super danke das war es!

Für alle nach mir, hier nochmal mein Funktionsfähiger Code!
1
/* Test Timer Atmega8515 STK 500 */
2
3
 
4
#include <avr/io.h>          // (1)
5
#include <inttypes.h>
6
#include <avr/interrupt.h>
7
#include <stdlib.h>
8
#define F_CPU 8000000UL    
9
10
volatile uint16_t count_overflow=0;
11
volatile int timer;
12
13
int main (void) {            // (2)
14
   DDRB  = 0xff;             // (3)
15
   PORTB = 0xff;
16
   //DDRA  = 0x0;
17
   
18
   int i;
19
   i = 1;
20
   
21
22
23
 // Timer0 (8Bit): Normal-Mode(0), Prescaler 1/1024
24
  TCCR0 = (0<<FOC0)|(0<<WGM00)|(1<<COM01)|(0<<COM00)|(0<<WGM01)|(0<<CS02)|(0<<CS01)|(1<<CS00);
25
  TIMSK = (0<<OCIE1B)|(0<<OCIE1A)|(1<<TOIE0); // Overflow ON
26
27
  sei(); // interupts global on
28
29
30
   while(1) {
31
   
32
   if(i == 0 && timer == 1) {
33
   PORTB = 0xf0;
34
   i=1;
35
   timer = 0;
36
   }
37
   
38
   if(i == 1 && timer == 1) {
39
   PORTB = 0x0f;
40
   i=0;
41
   timer = 0;
42
   }
43
   
44
45
46
   } 
47
48
49
}
50
51
52
// Timer0 Overflow Interrupt ------------------------------
53
ISR (TIMER0_OVF_vect)
54
{
55
  count_overflow++; 
56
  if (count_overflow >= 4000)
57
  {
58
    timer = 1;
59
    count_overflow = 0; // reset counter
60
  }
61
} //-------------------------------------- Ende TOV0_INT */

Danke guten Rutsch

Sebastian

von Gordon (Gast)


Lesenswert?

Kommentar überflüssig!

von Gordon (Gast)


Lesenswert?

>Stefan Ernst

gordon

von Stefan E. (sternst)


Lesenswert?

Gordon wrote:
>>Stefan Ernst
>
> gordon

Muss mir das jetzt irgendetwas sagen?

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.