This is a simple way but yet efficient to get DCF77 information out of the FRDM_KL25Z. It simply outputs the time and date every minutes. With that, you can easily change and modify the code to your conveniance and add a display, a RTC...
main.cpp@0:e2c2d9435339, 2013-11-11 (annotated)
- Committer:
- auxtony78
- Date:
- Mon Nov 11 11:18:17 2013 +0000
- Revision:
- 0:e2c2d9435339
initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
auxtony78 | 0:e2c2d9435339 | 1 | /* |
auxtony78 | 0:e2c2d9435339 | 2 | * ---------------------------------------------------------------------------- |
auxtony78 | 0:e2c2d9435339 | 3 | * "THE BEER-WARE LICENSE" (Revision 42): |
auxtony78 | 0:e2c2d9435339 | 4 | * <anthony.harivel@gmail.com> wrote this file. As long as you retain this notice you |
auxtony78 | 0:e2c2d9435339 | 5 | * can do whatever you want with this stuff. If we meet some day, and you think |
auxtony78 | 0:e2c2d9435339 | 6 | * this stuff is worth it, you can buy me a beer in return |
auxtony78 | 0:e2c2d9435339 | 7 | * ---------------------------------------------------------------------------- |
auxtony78 | 0:e2c2d9435339 | 8 | */ |
auxtony78 | 0:e2c2d9435339 | 9 | |
auxtony78 | 0:e2c2d9435339 | 10 | #include "mbed.h" |
auxtony78 | 0:e2c2d9435339 | 11 | |
auxtony78 | 0:e2c2d9435339 | 12 | Serial pc(USBTX,USBRX); //enable serial connection |
auxtony78 | 0:e2c2d9435339 | 13 | |
auxtony78 | 0:e2c2d9435339 | 14 | InterruptIn dcfPinIn (PTD7); //connection to non inverting output of dcf module |
auxtony78 | 0:e2c2d9435339 | 15 | |
auxtony78 | 0:e2c2d9435339 | 16 | Timer r,f; //create two timers |
auxtony78 | 0:e2c2d9435339 | 17 | |
auxtony78 | 0:e2c2d9435339 | 18 | int count; //count the received info per minutes |
auxtony78 | 0:e2c2d9435339 | 19 | |
auxtony78 | 0:e2c2d9435339 | 20 | int dcf_array[60]; //array of the dcf77 info |
auxtony78 | 0:e2c2d9435339 | 21 | |
auxtony78 | 0:e2c2d9435339 | 22 | // boolean for Rising and falling edge |
auxtony78 | 0:e2c2d9435339 | 23 | volatile bool bRise = false; |
auxtony78 | 0:e2c2d9435339 | 24 | volatile bool bFall = false; |
auxtony78 | 0:e2c2d9435339 | 25 | |
auxtony78 | 0:e2c2d9435339 | 26 | // when we got a rising edge... |
auxtony78 | 0:e2c2d9435339 | 27 | void riseInt() |
auxtony78 | 0:e2c2d9435339 | 28 | { |
auxtony78 | 0:e2c2d9435339 | 29 | f.stop(); |
auxtony78 | 0:e2c2d9435339 | 30 | r.reset(); |
auxtony78 | 0:e2c2d9435339 | 31 | r.start(); |
auxtony78 | 0:e2c2d9435339 | 32 | bFall = true; |
auxtony78 | 0:e2c2d9435339 | 33 | } |
auxtony78 | 0:e2c2d9435339 | 34 | |
auxtony78 | 0:e2c2d9435339 | 35 | // when we got a falling edge... |
auxtony78 | 0:e2c2d9435339 | 36 | void fallInt() |
auxtony78 | 0:e2c2d9435339 | 37 | { |
auxtony78 | 0:e2c2d9435339 | 38 | r.stop(); |
auxtony78 | 0:e2c2d9435339 | 39 | f.reset(); |
auxtony78 | 0:e2c2d9435339 | 40 | f.start(); |
auxtony78 | 0:e2c2d9435339 | 41 | bRise = true; |
auxtony78 | 0:e2c2d9435339 | 42 | } |
auxtony78 | 0:e2c2d9435339 | 43 | |
auxtony78 | 0:e2c2d9435339 | 44 | // simple dcf77 information print function. |
auxtony78 | 0:e2c2d9435339 | 45 | void print_time() |
auxtony78 | 0:e2c2d9435339 | 46 | { |
auxtony78 | 0:e2c2d9435339 | 47 | int hour, min, day, month, year; |
auxtony78 | 0:e2c2d9435339 | 48 | |
auxtony78 | 0:e2c2d9435339 | 49 | //calculate hour-------------------------------------------------------------------------------------- |
auxtony78 | 0:e2c2d9435339 | 50 | 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; |
auxtony78 | 0:e2c2d9435339 | 51 | |
auxtony78 | 0:e2c2d9435339 | 52 | //calculate minutes------------------------------------------------------------------------------------ |
auxtony78 | 0:e2c2d9435339 | 53 | 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; |
auxtony78 | 0:e2c2d9435339 | 54 | |
auxtony78 | 0:e2c2d9435339 | 55 | //calculate day---------------------------------------------------------------------------------------- |
auxtony78 | 0:e2c2d9435339 | 56 | 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; |
auxtony78 | 0:e2c2d9435339 | 57 | |
auxtony78 | 0:e2c2d9435339 | 58 | //calculate month-------------------------------------------------------------------------------------- |
auxtony78 | 0:e2c2d9435339 | 59 | month = dcf_array[49] * 10 + dcf_array[48] * 8 + dcf_array[47] * 4 + dcf_array[46] * 2 + dcf_array[45] * 1; |
auxtony78 | 0:e2c2d9435339 | 60 | |
auxtony78 | 0:e2c2d9435339 | 61 | //calculate year--------------------------------------------------------------------------------------- |
auxtony78 | 0:e2c2d9435339 | 62 | 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; |
auxtony78 | 0:e2c2d9435339 | 63 | |
auxtony78 | 0:e2c2d9435339 | 64 | pc.printf("%dh%dmin %d/%d/%d \r\n", hour, min, day, month, year); |
auxtony78 | 0:e2c2d9435339 | 65 | } |
auxtony78 | 0:e2c2d9435339 | 66 | |
auxtony78 | 0:e2c2d9435339 | 67 | // main // |
auxtony78 | 0:e2c2d9435339 | 68 | int main() |
auxtony78 | 0:e2c2d9435339 | 69 | { |
auxtony78 | 0:e2c2d9435339 | 70 | // attach two functions: |
auxtony78 | 0:e2c2d9435339 | 71 | // 1 for the falling edge |
auxtony78 | 0:e2c2d9435339 | 72 | // 1 for the rising edge |
auxtony78 | 0:e2c2d9435339 | 73 | dcfPinIn.rise(&riseInt); |
auxtony78 | 0:e2c2d9435339 | 74 | dcfPinIn.fall(&fallInt); |
auxtony78 | 0:e2c2d9435339 | 75 | |
auxtony78 | 0:e2c2d9435339 | 76 | // endless loop |
auxtony78 | 0:e2c2d9435339 | 77 | while(1) |
auxtony78 | 0:e2c2d9435339 | 78 | { |
auxtony78 | 0:e2c2d9435339 | 79 | /////////////////////////// |
auxtony78 | 0:e2c2d9435339 | 80 | if( bFall == true ) |
auxtony78 | 0:e2c2d9435339 | 81 | { |
auxtony78 | 0:e2c2d9435339 | 82 | // did we pass the reset time ? |
auxtony78 | 0:e2c2d9435339 | 83 | if( f.read_ms() > 950 ) |
auxtony78 | 0:e2c2d9435339 | 84 | { |
auxtony78 | 0:e2c2d9435339 | 85 | //pc.printf("reset"); |
auxtony78 | 0:e2c2d9435339 | 86 | print_time(); |
auxtony78 | 0:e2c2d9435339 | 87 | count = 0; |
auxtony78 | 0:e2c2d9435339 | 88 | } |
auxtony78 | 0:e2c2d9435339 | 89 | else |
auxtony78 | 0:e2c2d9435339 | 90 | { |
auxtony78 | 0:e2c2d9435339 | 91 | pc.printf("."); |
auxtony78 | 0:e2c2d9435339 | 92 | } |
auxtony78 | 0:e2c2d9435339 | 93 | |
auxtony78 | 0:e2c2d9435339 | 94 | bFall = false; |
auxtony78 | 0:e2c2d9435339 | 95 | } //if bFall |
auxtony78 | 0:e2c2d9435339 | 96 | |
auxtony78 | 0:e2c2d9435339 | 97 | ////////////////////////// |
auxtony78 | 0:e2c2d9435339 | 98 | if( bRise == true ) |
auxtony78 | 0:e2c2d9435339 | 99 | { |
auxtony78 | 0:e2c2d9435339 | 100 | // 100ms = '0' and 200ms = '1' |
auxtony78 | 0:e2c2d9435339 | 101 | if( r.read_ms() > 150 ) |
auxtony78 | 0:e2c2d9435339 | 102 | { |
auxtony78 | 0:e2c2d9435339 | 103 | //pc.printf("1"); |
auxtony78 | 0:e2c2d9435339 | 104 | dcf_array[count] = 1; |
auxtony78 | 0:e2c2d9435339 | 105 | } |
auxtony78 | 0:e2c2d9435339 | 106 | else |
auxtony78 | 0:e2c2d9435339 | 107 | { |
auxtony78 | 0:e2c2d9435339 | 108 | //pc.printf("0"); |
auxtony78 | 0:e2c2d9435339 | 109 | dcf_array[count] = 0; |
auxtony78 | 0:e2c2d9435339 | 110 | } |
auxtony78 | 0:e2c2d9435339 | 111 | |
auxtony78 | 0:e2c2d9435339 | 112 | count ++; |
auxtony78 | 0:e2c2d9435339 | 113 | bRise = false; |
auxtony78 | 0:e2c2d9435339 | 114 | }//if bRise |
auxtony78 | 0:e2c2d9435339 | 115 | |
auxtony78 | 0:e2c2d9435339 | 116 | } // while |
auxtony78 | 0:e2c2d9435339 | 117 | } // main |
auxtony78 | 0:e2c2d9435339 | 118 | |
auxtony78 | 0:e2c2d9435339 | 119 |