anthony harivel / Simple_DCF77_KL25Z

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * ----------------------------------------------------------------------------
00003  * "THE BEER-WARE LICENSE" (Revision 42):
00004  * <anthony.harivel@gmail.com> wrote this file. As long as you retain this notice you
00005  * can do whatever you want with this stuff. If we meet some day, and you think
00006  * this stuff is worth it, you can buy me a beer in return 
00007  * ----------------------------------------------------------------------------
00008  */
00009  
00010 #include "mbed.h"
00011 
00012 Serial pc(USBTX,USBRX); //enable serial connection
00013 
00014 InterruptIn dcfPinIn (PTD7); //connection to non inverting output of dcf module
00015 
00016 Timer r,f; //create two timers
00017 
00018 int count; //count the received info per minutes
00019 
00020 int dcf_array[60]; //array of the dcf77 info
00021 
00022 // boolean for Rising and falling edge
00023 volatile bool bRise = false;
00024 volatile bool bFall = false;
00025 
00026 // when we got a rising edge...
00027 void riseInt()
00028 {
00029     f.stop();
00030     r.reset();
00031     r.start();
00032     bFall = true;
00033 }
00034 
00035 // when we got a falling edge...
00036 void fallInt()
00037 {
00038     r.stop();
00039     f.reset();
00040     f.start();
00041     bRise = true;
00042 }
00043 
00044 // simple dcf77 information print function.
00045 void print_time()
00046 {   
00047     int hour, min, day, month, year;
00048     
00049     //calculate hour--------------------------------------------------------------------------------------
00050     hour = dcf_array[34] * 20 + dcf_array[33] * 10 + dcf_array[32] * 8 + dcf_array[31] * 4 + dcf_array[30] * 2 + dcf_array[29] * 1;
00051     
00052     //calculate minutes------------------------------------------------------------------------------------
00053     min = dcf_array[24] * 8 + dcf_array[23] * 4 + dcf_array[22] * 2 + dcf_array[21] * 1 + dcf_array[27] * 40 + dcf_array[26] * 20 +dcf_array[25] * 10;
00054     
00055     //calculate day----------------------------------------------------------------------------------------
00056     day = dcf_array[39] * 8 + dcf_array[38] * 4 + dcf_array[37] * 2 + dcf_array[36] * 1 + dcf_array[41] * 20 + dcf_array[40] * 10;
00057     
00058     //calculate month--------------------------------------------------------------------------------------
00059     month = dcf_array[49] * 10 + dcf_array[48] * 8 + dcf_array[47] * 4 + dcf_array[46] * 2 + dcf_array[45] * 1;
00060     
00061     //calculate year---------------------------------------------------------------------------------------
00062     year = dcf_array[57] * 80 + dcf_array[56] * 40 + dcf_array[55] * 20 + dcf_array[54] * 10 + dcf_array[53] * 8 +dcf_array[52] * 4 + dcf_array[51] * 2 + dcf_array[50] * 1;
00063     
00064     pc.printf("%dh%dmin %d/%d/%d \r\n", hour, min, day, month, year);
00065 }
00066 
00067 // main //
00068 int main()
00069 {
00070     // attach two functions:
00071     // 1 for the falling edge
00072     // 1 for the rising edge
00073     dcfPinIn.rise(&riseInt);
00074     dcfPinIn.fall(&fallInt);
00075     
00076     // endless loop
00077     while(1)
00078     {
00079         ///////////////////////////
00080         if( bFall == true )
00081         {
00082             // did we pass the reset time ?
00083             if( f.read_ms() > 950 )
00084             {
00085                 //pc.printf("reset");
00086                 print_time();
00087                 count = 0;
00088             }
00089             else
00090             {
00091                 pc.printf(".");
00092             }
00093             
00094             bFall = false;
00095         } //if bFall
00096         
00097         //////////////////////////    
00098         if( bRise == true )
00099         {
00100             // 100ms = '0' and 200ms = '1' 
00101             if( r.read_ms() > 150 )
00102             {
00103               //pc.printf("1");
00104               dcf_array[count] = 1;
00105             }
00106             else
00107             {
00108               //pc.printf("0");
00109               dcf_array[count] = 0;
00110             }
00111             
00112             count ++;
00113             bRise = false;
00114         }//if bRise
00115             
00116     } // while
00117 } // main
00118 
00119