Hallo,
ich habe gestern angefangen mich mit dem Code von Peter Dannegger zu
befassen:
http://www.mikrocontroller.net/articles/AVR_-_Die_genaue_Sekunde_/_RTC
Mein Problem ist jetzt allerdings, dass meine Sekunde zu schnell gezählt
wird! Das heißt in meinem Programm vergehen in einer realen Sekunde ca.
4-5min!
Das zweite Mysterium ist der XTAL Wert. Ich kann ihn beliebig ändern,
ohne das er Auswirkungen auf meinen Timer hat.
Mein Code:
1 | #include <avr/io.h>
|
2 | #include <avr/interrupt.h>
|
3 | #include <avr/signal.h>
|
4 |
|
5 | #define XTAL 16000000L // after measuring deviation: 1.5s/d
|
6 |
|
7 | #define DEBOUNCE 256L // debounce clock (256Hz = 4msec)
|
8 |
|
9 | #define uchar unsigned char
|
10 | #define uint unsigned int
|
11 |
|
12 | uchar prescaler;
|
13 | uchar volatile second; // count seconds
|
14 |
|
15 |
|
16 | SIGNAL (SIG_OUTPUT_COMPARE1A)
|
17 | {
|
18 | #if XTAL % DEBOUNCE
|
19 | OCR1A = XTAL / DEBOUNCE - 1; // compare DEBOUNCE - 1 times
|
20 | #endif
|
21 | if( --prescaler == 0 ){
|
22 | prescaler = (uchar)DEBOUNCE;
|
23 | second++; // exact one second over
|
24 | #if XTAL % DEBOUNCE // handle remainder
|
25 | OCR1A = XTAL / DEBOUNCE + XTAL % DEBOUNCE - 1; // compare once per second
|
26 | #endif
|
27 | }
|
28 | }
|
29 |
|
30 | void setup()
|
31 | {
|
32 | Serial.begin(115200);
|
33 |
|
34 | TCCR1B = 1<<WGM12^1<<CS10; // divide by 1
|
35 | // clear on compare
|
36 | OCR1A = XTAL / DEBOUNCE - 1;
|
37 | TCNT1 = 0;
|
38 | second = 0;
|
39 | prescaler = (uchar)DEBOUNCE;
|
40 |
|
41 | TIMSK1 = 1<<OCIE1A;
|
42 | sei();
|
43 | }
|
44 |
|
45 | void loop()
|
46 | {
|
47 | if( second == 60 )
|
48 | {
|
49 | second = 0;
|
50 | Serial.println(second, DEC);
|
51 | }
|
52 | }
|
Meine Hardware ist ein Arduino-Board mit einem Mega168 und 16MHz Clock.
1 | Serial.println(second, DEC);
|
Sollte jede Minute eine 0 dem Code nach ausgeben. Ausgabe kommt jedoch,
wie oben schon erwähnt, ca. 4-5mal pro Sekunde.
Hoffe mir kann jemand helfen.