DCF77 Funkuhr

Arduino DCF77 Funkuhr

Die DCF77 Funkuhr ist ein Projekt für eine Funkuhr mit dem Arduino. Derzeit habe ich das DCF77 Funkmodul mit dem 7-Segment-Board kombiniert. In dem aktuellen Code werden die Sekunden noch über den internen Interrupt des Microcontrollers gezählt. Da das aber sehr ungenau ist (über Nacht 5min Abweichung ..) wird als nächstes die RTC mit angeklemmt.
Der Code basiert auf dem Beispielcode von J.M. Lietaer. Nachteil an dem Code ist jedoch, dass das DCF-Signal nicht überprüft wird. Deswegen habe ich eine Funktion geschrieben, die die Paritätsbits auswertet:

boolean validateDCFsignal(){
  // Check bit 20 - should be 1
  if(DCF77signal[20] != 1){
    #ifdef DEBUG
      Serial.println("VALIDATE: Bit 20 NOT 1");
      Serial.println("");
    #endif
    return false;
  }
 
  // Check parity bit 27 - parity for minutes
  int parity = DCF77signal[21] ^ DCF77signal[22] ^ DCF77signal[23] ^ DCF77signal[24] ^ DCF77signal[25] ^ DCF77signal[26] ^ DCF77signal[27];
  if(parity != DCF77signal[28]){
    #ifdef DEBUG
      Serial.print("VALIDATE: Parity Bit 27 (minutes) should be ");
      Serial.print(parity);
      Serial.print(" NOT ");
      Serial.println(DCF77signal[28]);
      Serial.println("");
    #endif
    return false;
  }
 
  // Check parity bit 35 - parity for hours
  parity = DCF77signal[29] ^ DCF77signal[30] ^ DCF77signal[31] ^ DCF77signal[32] ^ DCF77signal[33] ^ DCF77signal[34];
  if(parity != DCF77signal[35]){
    #ifdef DEBUG
      Serial.print("VALIDATE: Parity Bit 35 (hours) should be ");
      Serial.print(parity);
      Serial.print(" NOT ");
      Serial.println(DCF77signal[35]);
      Serial.println("");
    #endif
    return false;
  }
 
  // Check parity bit 58 - parity for date
  parity = DCF77signal[36] ^ DCF77signal[37] ^ DCF77signal[38] ^ DCF77signal[39] ^ DCF77signal[40] ^ DCF77signal[41] ^ DCF77signal[42] ^ DCF77signal[43] ^ DCF77signal[44] ^ DCF77signal[45] ^ DCF77signal[46] ^ DCF77signal[47] ^ DCF77signal[48] ^ DCF77signal[49] ^ DCF77signal[50] ^ DCF77signal[51] ^ DCF77signal[52] ^ DCF77signal[53] ^ DCF77signal[54] ^ DCF77signal[55] ^ DCF77signal[56] ^ DCF77signal[57];
  if(parity != DCF77signal[58]){
    #ifdef DEBUG
      Serial.print("VALIDATE: Parity Bit 58 (date) should be ");
      Serial.print(parity);
      Serial.print(" NOT ");
      Serial.println(DCF77signal[58]);
      Serial.println("");
    #endif
    return false;
  }
 
  #ifdef DEBUG
    Serial.println("VALIDATE: OK");
    Serial.println("");
  #endif
  return true;
}

Das ganze ist derzeit noch als Bastel-Code zu sehen. Das ist noch nicht fertig und nicht einsatzbereit.

Quellcode

    /*
     * Torsten Amshove <torsten@amshove.net>
     *
     * DCF77 Uhr mit Ausgabe über 7-Segmentanzeigen
     *
     * Die Uhr wird mit einem Timer2-Interrupt betrieben.
     * Das DCF77 Modul muss an Port 2 betrieben werden, damit der externe Interrupt ausgelöst wird.
     *
     * Die decodierung des DCF77-Signals kommt von J.M. Lietaer
     *  - http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1266858603
     *
     * Der Timer Interrupt kommt von Mathias Dalheimer
     *  - http://gonium.net/md/2007/04/18/tweaking-the-code/index.html
     *
     * Weitere Links:
     *  - https://secure.wikimedia.org/wikipedia/en/wiki/DCF77#Time_code_interpretation
     *  - https://secure.wikimedia.org/wikipedia/de/wiki/DCF77#Zeitinformation
     *
     *
     * ToDo:
     *  - DayOfWeek speichern
     *  - DCF77 automatisch aktivieren (alle 8 std?)
     *  - Datum weiterzählen?!
     */
     
     
    #define DEBUG 1        // Turn debugging on/off
     
    #define BLINKLED 13      // Blink every second
    #define SWITCH 10        // Pin to switch to trigger DCF77 receiver
    #define DCF77 3          // digital in - Pin 3 = external interrupt 1 - DCF77 module - if you change this, you have to change the interrupt in setup()
    #define DCF77pon 12      // Pin to enable/disable DCF77 module (PON)
    boolean DCF77value = LOW;// digital value from DCF77 module
    int DCF77data = 0;       // 0 = low / 1 = high
    int DCF77start = 0;      // start high in millis
    int DCF77tick = 0;       // most recent in millis 
    int DCF77signal[60];     // array of DCF77 values (http://en.wikipedia.org/wiki/DCF77#Time_code_interpretation) 
    int DCF77count = 0;      // count variable for array manipulation
    int DCF77dw = 0;         // day of week translation (e.g. 1 = Monday)
     
    // Time to save for internal clock
    volatile int hour = 0;
    volatile int minute = 0;
    volatile int second = 0;
    volatile int day = 0;
    volatile int month = 0;
    volatile int year = 0;
    volatile boolean status = HIGH;  // Switches every second
     
    /**
     * Definitions for the timer interrupt 2 handler:
     * The Arduino runs at 16 Mhz, we use a prescaler of 64 -> We need to 
     * initialize the counter with 6. This way, we have 1000 interrupts per second.
     * We use tick_counter to count the interrupts.
     */
    #define INIT_TIMER_COUNT 6
    #define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT
    int tick_counter = 0;
     
    // 7-Segment-Anzeige
    const int latchPin = 4;        // ST_CP Pin des 74HC595 (Schieberegister)
    const int clockPin = 5;        // SH_CP Pin des 74HC595 (Schieberegister)
    const int dataPin = 6;         // DS Pin des 74HC595 (Schieberegister)
     
    volatile boolean initialSetup = 1; // Zeige Spielerei solang kein Signal empfangen wurde
    long previousMillis = 0;           // Der loop-"delay" der Spielerei
    int posCounter = 0;                // Der Positions-Counter des Spielerei-Arrays
     
    const uint8_t num[10] = {      // Binaere Codierung der Ziffern 0-9 der 7-Segmentanzeigen
      0b10000010, // 0
      0b11011011, // 1
      0b00000111, // 2
      0b00010011, // 3
      0b01011010, // 4
      0b00110010, // 5
      0b00100010, // 6 
      0b10011011, // 7
      0b00000010, // 8
      0b00010010  // 9
    };
     
    const uint8_t initial[] = {    // Initialisierungs-Spielerei ..
      0b11111011,
      0b11110011,
      0b11100011,
      0b11100010,
      0b10100010,
      0b10000010,
      0b10000110,
      0b10001110,
      0b10011110,
      0b10011111,
      0b11011111,
      0b11111111
    };
     
    ////////////////////////////////////////////////////////////////////////
     
    // Setup
    void setup() {
      pinMode(SWITCH, INPUT);
      pinMode(DCF77, INPUT);
      pinMode(DCF77pon, OUTPUT);
     
      digitalWrite(SWITCH, HIGH);
      digitalWrite(DCF77pon, LOW);
     
      attachInterrupt(1,interruptDCF77,CHANGE); // External Interrupt 1 = Pin 3
      SetupTimer2(); 
     
      // Schieberegister (7-Segmentanzeigen)
      pinMode(latchPin, OUTPUT);
      pinMode(clockPin, OUTPUT);
      pinMode(dataPin, OUTPUT);
     
      #ifdef DEBUG 
        Serial.begin(9600);
      #endif
    }
     
    // Loop
    void loop() {  
      // If SWITCH is pressed, toggle DCF77 module on
      if(digitalRead(SWITCH) == LOW){
        digitalWrite(DCF77pon, LOW);
      }
     
      // If there is no signal on power-on just play a bit
      if(initialSetup == 1){
        writeInitialSetup();
      }
    }
     
     
    //////////// Timer Interrupt ////////////
     
    // Timer-Interrupt (count Seconds)
    ISR(TIMER2_OVF_vect){
      RESET_TIMER2;
      tick_counter += 1;
      if (tick_counter == 1000) {
        second++;
        if(second == 60){
          second = 0;
          minute++;
          if(minute == 60){
            minute = 0;
            hour++;
            if(hour == 24){
              hour = 0;
            }
          }
          writeClock();
        }
        status = !status;
        digitalWrite(BLINKLED,status);
        tick_counter = 0;
     
        #ifdef DEBUG
          int h2 = hour % 10;
          int h1 = (hour - h2) / 10;
          Serial.print(h1);
          Serial.print(h2);
          Serial.print(":");
          int m2 = minute % 10;
          int m1 = (minute - m2) / 10;
          Serial.print(m1);
          Serial.print(m2);
          Serial.print(":");
          int s2 = second % 10;
          int s1 = (second - s2) / 10;
          Serial.print(s1);
          Serial.print(s2);
          Serial.println(" Uhr");
        #endif
      }
    }
     
    // Initialize Timer2 Interrupt
    unsigned char SetupTimer2(){
      // Set prescaler to 64 (16Mhz / 64)
      TCCR2B |= (1<<CS22);                   // turn on CS22 bit
      TCCR2B &= ~((1<<CS21) | (1<<CS20));    // turn off CS21 and CS20 bits   
      // Normal Mode
      TCCR2A &= ~((1<<WGM21) | (1<<WGM20));  // turn off WGM21 and WGM20 bits 
      TCCR2B &= ~(1<<WGM22);                 // turn off WGM22
      //Timer2 Overflow Interrupt Enable  
      TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A);    
      RESET_TIMER2;
    }
     
     
    //////////// DCF77 ////////////
     
    // External Interrupt for DCF77 module - Read the DCF77 Signal
    void interruptDCF77() {
      DCF77value = digitalRead(DCF77);
      if(DCF77value == HIGH){
        if (DCF77data == 0) {
          DCF77start = millis();
          if (DCF77start - DCF77tick > 1200) {
            // Signal complete - Save and reset
            saveReceivedSignal();
            for (DCF77count = 0; DCF77count < 60; DCF77count = DCF77count + 1) {
              DCF77signal[DCF77count] = 0;
            }
            DCF77count = 0;
          }
          else {
            if (DCF77start - DCF77tick > 850) {
              DCF77signal[DCF77count] = 0;
            }
            else {
              if (DCF77start - DCF77tick < 850) {
                if (DCF77start - DCF77tick > 650) {
                  DCF77signal[DCF77count] = 1;
                }
              }
            }
            if (DCF77start - DCF77tick > 650) {
              DCF77count = DCF77count + 1;
            }
          }
        }
        DCF77data = 1;
        DCF77tick = millis();
      }
      else {
        DCF77data = 0;
      }
    }
     
    // Validate the DCF77 signal (parity bits, etc)
    boolean validateDCFsignal(){
      // Check bit 20 - should be 1
      if(DCF77signal[20] != 1){
        #ifdef DEBUG
          Serial.println("VALIDATE: Bit 20 NOT 1");
          Serial.println("");
        #endif
        return false;
      }
     
      // Check parity bit 27 - parity for minutes
      int parity = DCF77signal[21] ^ DCF77signal[22] ^ DCF77signal[23] ^ DCF77signal[24] ^ DCF77signal[25] ^ DCF77signal[26] ^ DCF77signal[27];
      if(parity != DCF77signal[28]){
        #ifdef DEBUG
          Serial.print("VALIDATE: Parity Bit 27 (minutes) should be ");
          Serial.print(parity);
          Serial.print(" NOT ");
          Serial.println(DCF77signal[28]);
          Serial.println("");
        #endif
        return false;
      }
     
      // Check parity bit 35 - parity for hours
      parity = DCF77signal[29] ^ DCF77signal[30] ^ DCF77signal[31] ^ DCF77signal[32] ^ DCF77signal[33] ^ DCF77signal[34];
      if(parity != DCF77signal[35]){
        #ifdef DEBUG
          Serial.print("VALIDATE: Parity Bit 35 (hours) should be ");
          Serial.print(parity);
          Serial.print(" NOT ");
          Serial.println(DCF77signal[35]);
          Serial.println("");
        #endif
        return false;
      }
     
      // Check parity bit 58 - parity for date
      parity = DCF77signal[36] ^ DCF77signal[37] ^ DCF77signal[38] ^ DCF77signal[39] ^ DCF77signal[40] ^ DCF77signal[41] ^ DCF77signal[42] ^ DCF77signal[43] ^ DCF77signal[44] ^ DCF77signal[45] ^ DCF77signal[46] ^ DCF77signal[47] ^ DCF77signal[48] ^ DCF77signal[49] ^ DCF77signal[50] ^ DCF77signal[51] ^ DCF77signal[52] ^ DCF77signal[53] ^ DCF77signal[54] ^ DCF77signal[55] ^ DCF77signal[56] ^ DCF77signal[57];
      if(parity != DCF77signal[58]){
        #ifdef DEBUG
          Serial.print("VALIDATE: Parity Bit 58 (date) should be ");
          Serial.print(parity);
          Serial.print(" NOT ");
          Serial.println(DCF77signal[58]);
          Serial.println("");
        #endif
        return false;
      }
     
      #ifdef DEBUG
        Serial.println("VALIDATE: OK");
        Serial.println("");
      #endif
      return true;
    }
     
    // Set the global time/date variables
    void saveReceivedSignal(){
      #ifdef DEBUG
        showReceivedSignal();
      #endif
     
      if(validateDCFsignal()){
        initialSetup = 0; // Disable initial Setup
     
        hour = DCF77signal[29] * 1 + DCF77signal[30] * 2 + DCF77signal[31] * 4 + DCF77signal[32] * 8 + DCF77signal[33] * 10 + DCF77signal[34] * 20;
        minute = DCF77signal[21] * 1 + DCF77signal[22] * 2 + DCF77signal[23] * 4 + DCF77signal[24] * 8 + DCF77signal[25] * 10 + DCF77signal[26] * 20 + DCF77signal[27] * 40;
        second = 0;
        day = DCF77signal[36] * 1 + DCF77signal[37] * 2 + DCF77signal[38] * 4 + DCF77signal[39] * 8 + DCF77signal[40] * 10 + DCF77signal[41] * 20;
        month = DCF77signal[45] * 1 + DCF77signal[46] * 2 + DCF77signal[47] * 4 + DCF77signal[48] * 8 + DCF77signal[49] * 10;
        year = DCF77signal[50] * 1 + DCF77signal[51] * 2 + DCF77signal[52] * 4 + DCF77signal[53] * 8 + DCF77signal[54] * 10 + DCF77signal[55] * 20 + DCF77signal[56] * 40 + DCF77signal[57] * 80;
     
        RESET_TIMER2;
     
        digitalWrite(DCF77pon, HIGH); // Disable DCF77 module
     
        writeClock();
      }
    }
     
    // Dump received DCF77 signal
    void showReceivedSignal() {
      Serial.println("");
      Serial.println("---CYCLE---");
      for (DCF77count = 0; DCF77count < 60; DCF77count = DCF77count + 1) {
        Serial.print(DCF77signal[DCF77count]);
      }
      Serial.println("");
      Serial.print("M = ");
      Serial.println(DCF77signal[0]);
      Serial.print("R = ");
      Serial.println(DCF77signal[15]);
      Serial.print("A1 = ");
      Serial.println(DCF77signal[16]);
      Serial.print("Z1 = ");
      Serial.println(DCF77signal[17]);
      Serial.print("Z2 = ");
      Serial.println(DCF77signal[18]);
      Serial.print("A2 = ");
      Serial.println(DCF77signal[19]);
      Serial.print("S = ");
      Serial.println(DCF77signal[20]);
      DCF77dw = DCF77signal[42] * 1 + DCF77signal[43] * 2 + DCF77signal[44] * 4;
      Serial.print("DW = ");
      Serial.print(DCF77dw);
     
      switch (DCF77dw) {
      case 1:
        Serial.println(" (Monday)");
        break;
      case 2:
        Serial.println(" (Tuesday)");
        break;
      case 3:
        Serial.println(" (Wednesday)");
        break;
      case 4:
        Serial.println(" (Thursday)");
        break;
      case 5:
        Serial.println(" (Friday)");
        break;
      case 6:
        Serial.println(" (Saturday)");
        break;
      case 7:
        Serial.println(" (Sunday)");
        break;
      default: 
        Serial.println(" (?)");
      }
      Serial.print("DD = ");
      Serial.println(DCF77signal[36] * 1 + DCF77signal[37] * 2 + DCF77signal[38] * 4 + DCF77signal[39] * 8 + DCF77signal[40] * 10 + DCF77signal[41] * 20);
      Serial.print("MM = ");
      Serial.println(DCF77signal[45] * 1 + DCF77signal[46] * 2 + DCF77signal[47] * 4 + DCF77signal[48] * 8 + DCF77signal[49] * 10);
      Serial.print("YY = ");
      Serial.println(DCF77signal[50] * 1 + DCF77signal[51] * 2 + DCF77signal[52] * 4 + DCF77signal[53] * 8 + DCF77signal[54] * 10 + DCF77signal[55] * 20 + DCF77signal[56] * 40 + DCF77signal[57] * 80);
      Serial.print("HH = ");
      Serial.println(DCF77signal[29] * 1 + DCF77signal[30] * 2 + DCF77signal[31] * 4 + DCF77signal[32] * 8 + DCF77signal[33] * 10 + DCF77signal[34] * 20);
      Serial.print("MM = ");
      Serial.println(DCF77signal[21] * 1 + DCF77signal[22] * 2 + DCF77signal[23] * 4 + DCF77signal[24] * 8 + DCF77signal[25] * 10 + DCF77signal[26] * 20 + DCF77signal[27] * 40);
      Serial.println("SS = 0");
      if (DCF77signal[17] == 1) {
        Serial.println("CEST");  
     
      }
      if (DCF77signal[18] == 1) {
        Serial.println("CET");  
      }
     
      Serial.println("----------");
    }
     
     
    //////////// Write Display ////////////
     
    // Show clock on display
    void writeClock(){
      if(initialSetup == 0){
        int m2 = minute % 10;
        int m1 = (minute - m2)/10;
        int h2 = hour % 10;
        int h1 = (hour - h2)/10;
     
        digitalWrite(latchPin, LOW);
        shiftOut(dataPin, clockPin, MSBFIRST, num[h1]);
        shiftOut(dataPin, clockPin, MSBFIRST, num[h2]);
        shiftOut(dataPin, clockPin, MSBFIRST, num[m1]);
        shiftOut(dataPin, clockPin, MSBFIRST, num[m2]);
        digitalWrite(latchPin, HIGH); 
      }
    }
     
    // Show initial Setup (Spielerei)
    void writeInitialSetup() {
      unsigned long currentMillis = millis();
      if(currentMillis - previousMillis > 50) {
        previousMillis = currentMillis;
     
        digitalWrite(latchPin, LOW);
        shiftOut(dataPin, clockPin, MSBFIRST, initial[posCounter]);
        shiftOut(dataPin, clockPin, MSBFIRST, initial[posCounter]);
        shiftOut(dataPin, clockPin, MSBFIRST, initial[posCounter]);
        shiftOut(dataPin, clockPin, MSBFIRST, initial[posCounter]);
        digitalWrite(latchPin, HIGH);
     
        posCounter++;
        if(posCounter == 12){
          posCounter = 0;
        }
      }
    }

 

Kommentare sind abgeschaltet.