Forum: Mikrocontroller und Digitale Elektronik Interrupt springt nicht mehr zurück


von Michael S. (michi88)


Lesenswert?

Hi,

ich versuche gerade mit dem Timer1 vom atmega8 in C++ einen Takt von 
einer Sekunde zu erzeugen.
Mein Problem dabei ist, dass er nicht mehr aus den Interrupt 
rausspringt.

Außerdem haut meine Berechnung für den Compare-Wert nicht ganz hin.
Seht ihr da zufällig einen Fehler?


Hier mal der Code:
1
#ifndef F_CPU
2
#define F_CPU 8000000
3
#endif
4
5
#include <avr/io.h>
6
#include <avr/interrupt.h>
7
#include "schieberegister.h"
8
9
volatile int t0overf;
10
volatile int t1_comp_counter;
11
volatile int time_sec;
12
volatile int time_min;
13
volatile int time_hour;
14
char bcd[4];
15
16
ISR(TIMER1_COMPB_vect) {
17
  t1_comp_counter++;
18
  
19
  if (t1_comp_counter == 200) {
20
    t1_comp_counter = 0;
21
    time_sec++;
22
    if (time_sec > 59) {
23
      time_sec = 0;
24
      time_min++;
25
    }
26
    
27
    if (time_min > 59) {
28
      time_min = 0;
29
      time_hour++;
30
    }
31
    
32
    if (time_hour > 23)
33
      time_hour = 0;
34
  }
35
}
36
37
void deztobcd(int dez)
38
{
39
  int i = 0;
40
  if (dez > 9)
41
    return;
42
43
  bcd[0] = 0;
44
  bcd[1] = 0;
45
  bcd[2] = 0;
46
  bcd[3] = 0;
47
  while (dez) {
48
    bcd[i++] = dez % 2;
49
    dez /= 2;
50
    }
51
}
52
53
int main (void) {
54
  init_move_out(&PORTD);
55
  int reset;
56
  int move_data[24];
57
    
58
  TCCR1B  |= (1 << CS10) | (1 << WGM12);
59
  OCR1B  |= 63999;
60
  TIMSK  |= (1 << OCIE1A);
61
62
  sei();
63
  
64
  while (1) {
65
66
    deztobcd(2);
67
    move_data[0] = bcd[0];
68
    move_data[1] = bcd[1];
69
    move_data[2] = bcd[2];
70
    move_data[3] = bcd[3];
71
72
    move_out(&PORTD,move_data);
73
  }
74
}

gruß michael

von holger (Gast)


Lesenswert?

Fällt dir hier was auf?

ISR(TIMER1_COMPB_vect) {
               ^
               |

TIMSK  |= (1 << OCIE1A);
                     ^
                     |

von Michael S. (michi88)


Lesenswert?

alles klar danke!

Ich hab vorhin beim Vektor schon son Brett vorm Kopf gehabt.
Aber warum muss man denn das Register OCR1A und nicht OCR1B mit nem Wert 
befüllen, damit das funktioniert?

von Michael U. (amiga)


Lesenswert?

Hallo,

weil Atmel das für diesen CTC-Mode so eingebaut hat.
Datenblatt Seite 88 oder so.

Gruß aus Berlin
Michael

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.