Zeitsteuerung
Fork of timer0 by
timer0.cpp@0:8d3e2b74d1d5, 2015-01-03 (annotated)
- Committer:
- rs27
- Date:
- Sat Jan 03 15:26:23 2015 +0000
- Revision:
- 0:8d3e2b74d1d5
123;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rs27 | 0:8d3e2b74d1d5 | 1 | #include "mbed.h" |
rs27 | 0:8d3e2b74d1d5 | 2 | #include "timer0.h" |
rs27 | 0:8d3e2b74d1d5 | 3 | |
rs27 | 0:8d3e2b74d1d5 | 4 | |
rs27 | 0:8d3e2b74d1d5 | 5 | //-------------------------------------------------------- |
rs27 | 0:8d3e2b74d1d5 | 6 | // Construktor initialisiert den Timer |
rs27 | 0:8d3e2b74d1d5 | 7 | timer0::timer0() |
rs27 | 0:8d3e2b74d1d5 | 8 | { |
rs27 | 0:8d3e2b74d1d5 | 9 | uint8_t i; |
rs27 | 0:8d3e2b74d1d5 | 10 | |
rs27 | 0:8d3e2b74d1d5 | 11 | ms = 0; |
rs27 | 0:8d3e2b74d1d5 | 12 | sec = 0; |
rs27 | 0:8d3e2b74d1d5 | 13 | |
rs27 | 0:8d3e2b74d1d5 | 14 | // Initialize countdown timers |
rs27 | 0:8d3e2b74d1d5 | 15 | for (i=0; i < TIMER0_NUM_COUNTDOWNTIMERS; i++) |
rs27 | 0:8d3e2b74d1d5 | 16 | CountDownTimers[i].status = 0xFF; |
rs27 | 0:8d3e2b74d1d5 | 17 | |
rs27 | 0:8d3e2b74d1d5 | 18 | ticker.attach_us(this, &timer0::func, 1000); |
rs27 | 0:8d3e2b74d1d5 | 19 | } |
rs27 | 0:8d3e2b74d1d5 | 20 | |
rs27 | 0:8d3e2b74d1d5 | 21 | //-------------------------------------------------------- |
rs27 | 0:8d3e2b74d1d5 | 22 | // Interruptroutine wird jede ms aufgerufen |
rs27 | 0:8d3e2b74d1d5 | 23 | void timer0::func(void) |
rs27 | 0:8d3e2b74d1d5 | 24 | { |
rs27 | 0:8d3e2b74d1d5 | 25 | uint8_t i; |
rs27 | 0:8d3e2b74d1d5 | 26 | |
rs27 | 0:8d3e2b74d1d5 | 27 | if(counter != 0) counter--; |
rs27 | 0:8d3e2b74d1d5 | 28 | |
rs27 | 0:8d3e2b74d1d5 | 29 | // Zeitbasis für Systemzeit |
rs27 | 0:8d3e2b74d1d5 | 30 | // ----- count down timers in ms ------------------------------------------------- |
rs27 | 0:8d3e2b74d1d5 | 31 | for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++) |
rs27 | 0:8d3e2b74d1d5 | 32 | { |
rs27 | 0:8d3e2b74d1d5 | 33 | if (CountDownTimers[i].status == 1) |
rs27 | 0:8d3e2b74d1d5 | 34 | { // ms |
rs27 | 0:8d3e2b74d1d5 | 35 | if (CountDownTimers[i].count_timer > 0) |
rs27 | 0:8d3e2b74d1d5 | 36 | CountDownTimers[i].count_timer -- ; |
rs27 | 0:8d3e2b74d1d5 | 37 | if (CountDownTimers[i].count_timer == 0) |
rs27 | 0:8d3e2b74d1d5 | 38 | CountDownTimers[i].status = 0; |
rs27 | 0:8d3e2b74d1d5 | 39 | } |
rs27 | 0:8d3e2b74d1d5 | 40 | } |
rs27 | 0:8d3e2b74d1d5 | 41 | |
rs27 | 0:8d3e2b74d1d5 | 42 | if (ms < 1000) |
rs27 | 0:8d3e2b74d1d5 | 43 | { |
rs27 | 0:8d3e2b74d1d5 | 44 | ms++; |
rs27 | 0:8d3e2b74d1d5 | 45 | } |
rs27 | 0:8d3e2b74d1d5 | 46 | else |
rs27 | 0:8d3e2b74d1d5 | 47 | { |
rs27 | 0:8d3e2b74d1d5 | 48 | ms = 0; |
rs27 | 0:8d3e2b74d1d5 | 49 | |
rs27 | 0:8d3e2b74d1d5 | 50 | seconds++; |
rs27 | 0:8d3e2b74d1d5 | 51 | set_time(seconds); |
rs27 | 0:8d3e2b74d1d5 | 52 | |
rs27 | 0:8d3e2b74d1d5 | 53 | // ----- count down timers in s ------------------------------------------------- |
rs27 | 0:8d3e2b74d1d5 | 54 | for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++) |
rs27 | 0:8d3e2b74d1d5 | 55 | { |
rs27 | 0:8d3e2b74d1d5 | 56 | if (CountDownTimers[i].status == 2) |
rs27 | 0:8d3e2b74d1d5 | 57 | { // sekunden |
rs27 | 0:8d3e2b74d1d5 | 58 | if (CountDownTimers[i].count_timer > 0) |
rs27 | 0:8d3e2b74d1d5 | 59 | CountDownTimers[i].count_timer -- ; |
rs27 | 0:8d3e2b74d1d5 | 60 | if (CountDownTimers[i].count_timer == 0) |
rs27 | 0:8d3e2b74d1d5 | 61 | CountDownTimers[i].status = 0; |
rs27 | 0:8d3e2b74d1d5 | 62 | } |
rs27 | 0:8d3e2b74d1d5 | 63 | } |
rs27 | 0:8d3e2b74d1d5 | 64 | |
rs27 | 0:8d3e2b74d1d5 | 65 | if (sec < 60) |
rs27 | 0:8d3e2b74d1d5 | 66 | { |
rs27 | 0:8d3e2b74d1d5 | 67 | sec++; |
rs27 | 0:8d3e2b74d1d5 | 68 | } |
rs27 | 0:8d3e2b74d1d5 | 69 | else |
rs27 | 0:8d3e2b74d1d5 | 70 | { |
rs27 | 0:8d3e2b74d1d5 | 71 | sec = 0; |
rs27 | 0:8d3e2b74d1d5 | 72 | |
rs27 | 0:8d3e2b74d1d5 | 73 | // hier folgt der Minutenzähler falls erforderlich |
rs27 | 0:8d3e2b74d1d5 | 74 | } |
rs27 | 0:8d3e2b74d1d5 | 75 | } |
rs27 | 0:8d3e2b74d1d5 | 76 | |
rs27 | 0:8d3e2b74d1d5 | 77 | } |
rs27 | 0:8d3e2b74d1d5 | 78 | |
rs27 | 0:8d3e2b74d1d5 | 79 | //-------------------------------------------------------- |
rs27 | 0:8d3e2b74d1d5 | 80 | // Abfrage nach freiem Timer |
rs27 | 0:8d3e2b74d1d5 | 81 | // |
rs27 | 0:8d3e2b74d1d5 | 82 | // wenn alle Timer belegt sind wird 0xFF zurückgegebne |
rs27 | 0:8d3e2b74d1d5 | 83 | |
rs27 | 0:8d3e2b74d1d5 | 84 | uint8_t timer0::AllocateCountdownTimer (void) |
rs27 | 0:8d3e2b74d1d5 | 85 | { |
rs27 | 0:8d3e2b74d1d5 | 86 | uint8_t i; |
rs27 | 0:8d3e2b74d1d5 | 87 | |
rs27 | 0:8d3e2b74d1d5 | 88 | for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++) |
rs27 | 0:8d3e2b74d1d5 | 89 | if (CountDownTimers[i].status == 0xFF) |
rs27 | 0:8d3e2b74d1d5 | 90 | { |
rs27 | 0:8d3e2b74d1d5 | 91 | CountDownTimers[i].status = 0x00; // Timer reserviert, nicht gestartet |
rs27 | 0:8d3e2b74d1d5 | 92 | // printf_P(PSTR("\rallocate timer [%03d] %d\n"),i,ListPointer); |
rs27 | 0:8d3e2b74d1d5 | 93 | |
rs27 | 0:8d3e2b74d1d5 | 94 | return i; |
rs27 | 0:8d3e2b74d1d5 | 95 | } |
rs27 | 0:8d3e2b74d1d5 | 96 | |
rs27 | 0:8d3e2b74d1d5 | 97 | return 0xFF; |
rs27 | 0:8d3e2b74d1d5 | 98 | } |
rs27 | 0:8d3e2b74d1d5 | 99 | |
rs27 | 0:8d3e2b74d1d5 | 100 | //-------------------------------------------------------- |
rs27 | 0:8d3e2b74d1d5 | 101 | // Timer wieder freigeben |
rs27 | 0:8d3e2b74d1d5 | 102 | void timer0::RemoveCountdownTimer(uint8_t timer) |
rs27 | 0:8d3e2b74d1d5 | 103 | { |
rs27 | 0:8d3e2b74d1d5 | 104 | CountDownTimers[timer].status = 0xFF; |
rs27 | 0:8d3e2b74d1d5 | 105 | } |
rs27 | 0:8d3e2b74d1d5 | 106 | |
rs27 | 0:8d3e2b74d1d5 | 107 | //-------------------------------------------------------- |
rs27 | 0:8d3e2b74d1d5 | 108 | // Abfrage ob Timer 0 erreicht hat |
rs27 | 0:8d3e2b74d1d5 | 109 | uint8_t timer0::GetTimerStatus(uint8_t timer) |
rs27 | 0:8d3e2b74d1d5 | 110 | { |
rs27 | 0:8d3e2b74d1d5 | 111 | return CountDownTimers[timer].status; |
rs27 | 0:8d3e2b74d1d5 | 112 | } |
rs27 | 0:8d3e2b74d1d5 | 113 | |
rs27 | 0:8d3e2b74d1d5 | 114 | //-------------------------------------------------------- |
rs27 | 0:8d3e2b74d1d5 | 115 | // Abfrage der verbleibenden Zeit |
rs27 | 0:8d3e2b74d1d5 | 116 | uint16_t timer0::GetTimerZeit(uint8_t timer) |
rs27 | 0:8d3e2b74d1d5 | 117 | { |
rs27 | 0:8d3e2b74d1d5 | 118 | return CountDownTimers[timer].count_timer; |
rs27 | 0:8d3e2b74d1d5 | 119 | } |
rs27 | 0:8d3e2b74d1d5 | 120 | |
rs27 | 0:8d3e2b74d1d5 | 121 | //-------------------------------------------------------- |
rs27 | 0:8d3e2b74d1d5 | 122 | // Timer aktivieren |
rs27 | 0:8d3e2b74d1d5 | 123 | void timer0::SetCountdownTimer(unsigned char timer, unsigned char status, unsigned short value) |
rs27 | 0:8d3e2b74d1d5 | 124 | { |
rs27 | 0:8d3e2b74d1d5 | 125 | CountDownTimers[timer].count_timer = value; |
rs27 | 0:8d3e2b74d1d5 | 126 | CountDownTimers[timer].status = status; |
rs27 | 0:8d3e2b74d1d5 | 127 | } |
rs27 | 0:8d3e2b74d1d5 | 128 | |
rs27 | 0:8d3e2b74d1d5 | 129 | //-------------------------------------------------------- |
rs27 | 0:8d3e2b74d1d5 | 130 | // Zeitstempel setzen |
rs27 | 0:8d3e2b74d1d5 | 131 | void timer0::Set_t(uint8_t * data) |
rs27 | 0:8d3e2b74d1d5 | 132 | { |
rs27 | 0:8d3e2b74d1d5 | 133 | char buffer[40]; |
rs27 | 0:8d3e2b74d1d5 | 134 | |
rs27 | 0:8d3e2b74d1d5 | 135 | t.tm_sec = data[0]; // Sekunden 0-59 |
rs27 | 0:8d3e2b74d1d5 | 136 | t.tm_min = data[1]; // Minuten 0-59 |
rs27 | 0:8d3e2b74d1d5 | 137 | t.tm_hour = data[2]; // Stunden 0-23 |
rs27 | 0:8d3e2b74d1d5 | 138 | t.tm_mday = data[3]; // Tag 1-31 |
rs27 | 0:8d3e2b74d1d5 | 139 | t.tm_mon = data[4] - 1; // Monat 0-11 0 = Januar |
rs27 | 0:8d3e2b74d1d5 | 140 | t.tm_year = data[5]; // Jahr year since 1900 |
rs27 | 0:8d3e2b74d1d5 | 141 | |
rs27 | 0:8d3e2b74d1d5 | 142 | seconds = mktime(&t); |
rs27 | 0:8d3e2b74d1d5 | 143 | set_time(seconds); |
rs27 | 0:8d3e2b74d1d5 | 144 | |
rs27 | 0:8d3e2b74d1d5 | 145 | strftime(buffer, 40, "%a,%d.%m.%Y %H:%M:%S", localtime(&seconds)); |
rs27 | 0:8d3e2b74d1d5 | 146 | // pc.printf("\ntimer0::set_t = %s", buffer); |
rs27 | 0:8d3e2b74d1d5 | 147 | } |