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