Zeitsteuerung

Fork of timer0 by V09

Files at this revision

API Documentation at this revision

Comitter:
rs27
Date:
Sat Jan 03 15:26:23 2015 +0000
Commit message:
123;

Changed in this revision

timer0.cpp Show annotated file Show diff for this revision Revisions of this file
timer0.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 8d3e2b74d1d5 timer0.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/timer0.cpp	Sat Jan 03 15:26:23 2015 +0000
@@ -0,0 +1,147 @@
+#include "mbed.h"
+#include "timer0.h"
+
+
+//--------------------------------------------------------
+// Construktor initialisiert den Timer
+timer0::timer0()
+{
+    uint8_t i;
+    
+    ms = 0;
+    sec = 0;
+    
+    // Initialize countdown timers
+    for (i=0; i < TIMER0_NUM_COUNTDOWNTIMERS; i++)
+      CountDownTimers[i].status = 0xFF;
+  
+    ticker.attach_us(this, &timer0::func, 1000);
+}
+
+//--------------------------------------------------------
+// Interruptroutine wird jede ms aufgerufen
+void timer0::func(void)
+{
+    uint8_t i;
+    
+    if(counter != 0) counter--;
+
+    // Zeitbasis für Systemzeit
+    // ----- count down timers in ms -------------------------------------------------
+    for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++) 
+    {
+        if (CountDownTimers[i].status == 1) 
+        {       // ms
+            if (CountDownTimers[i].count_timer > 0)
+                CountDownTimers[i].count_timer -- ;
+            if (CountDownTimers[i].count_timer == 0)
+                CountDownTimers[i].status = 0;
+        }
+    }
+    
+    if (ms < 1000) 
+    {
+        ms++;  
+    }
+    else
+    {
+        ms = 0;
+       
+        seconds++;
+        set_time(seconds);
+       
+        // ----- count down timers in s -------------------------------------------------
+        for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++) 
+        {
+            if (CountDownTimers[i].status == 2) 
+            {   // sekunden
+                if (CountDownTimers[i].count_timer > 0)
+                    CountDownTimers[i].count_timer -- ;
+                if (CountDownTimers[i].count_timer == 0)
+                    CountDownTimers[i].status = 0;
+            }
+        }
+        
+        if (sec < 60) 
+        {
+            sec++;
+        }
+        else
+        {
+            sec = 0; 
+            
+            // hier folgt der Minutenzähler falls erforderlich           
+        }   
+    }
+    
+}
+
+//--------------------------------------------------------
+// Abfrage nach freiem Timer
+//
+// wenn alle Timer belegt sind wird 0xFF zurückgegebne
+
+uint8_t timer0::AllocateCountdownTimer (void)
+{
+    uint8_t i;
+    
+    for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++)
+        if (CountDownTimers[i].status == 0xFF)
+        {
+            CountDownTimers[i].status = 0x00;   // Timer reserviert, nicht gestartet
+            // printf_P(PSTR("\rallocate timer [%03d] %d\n"),i,ListPointer);            
+            
+            return i;
+        }
+
+    return 0xFF;
+}
+
+//--------------------------------------------------------
+// Timer wieder freigeben
+void timer0::RemoveCountdownTimer(uint8_t timer)
+{
+    CountDownTimers[timer].status = 0xFF;
+}
+
+//--------------------------------------------------------
+// Abfrage ob Timer 0 erreicht hat                  
+uint8_t timer0::GetTimerStatus(uint8_t timer)
+{
+    return CountDownTimers[timer].status;
+}
+
+//--------------------------------------------------------
+// Abfrage der verbleibenden Zeit               
+uint16_t timer0::GetTimerZeit(uint8_t timer)
+{
+    return CountDownTimers[timer].count_timer;
+}
+
+//--------------------------------------------------------
+// Timer aktivieren
+void timer0::SetCountdownTimer(unsigned char timer, unsigned char status, unsigned short value)
+{
+    CountDownTimers[timer].count_timer = value;
+    CountDownTimers[timer].status = status;
+}
+
+//--------------------------------------------------------
+// Zeitstempel setzen
+void timer0::Set_t(uint8_t * data)
+{
+    char buffer[40];    
+    
+    t.tm_sec  = data[0];     // Sekunden 0-59
+    t.tm_min  = data[1];     // Minuten 0-59
+    t.tm_hour = data[2];     // Stunden 0-23
+    t.tm_mday = data[3];     // Tag 1-31
+    t.tm_mon  = data[4] - 1; // Monat 0-11 0 = Januar
+    t.tm_year = data[5];     // Jahr year since 1900   
+    
+    seconds = mktime(&t);
+    set_time(seconds);
+    
+    strftime(buffer, 40, "%a,%d.%m.%Y %H:%M:%S", localtime(&seconds));
+    // pc.printf("\ntimer0::set_t = %s", buffer);   
+}
diff -r 000000000000 -r 8d3e2b74d1d5 timer0.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/timer0.h	Sat Jan 03 15:26:23 2015 +0000
@@ -0,0 +1,43 @@
+
+#include "mbed.h"
+
+#define TIMER0_NUM_COUNTDOWNTIMERS  25
+
+// status definition:
+// 0            the timer has expired
+// 1            10 ms Timer
+// 2            Sekunden Timer
+// 3            Minuten Timer
+// 4            Stunden Timer
+// 0xFF         Free timer
+
+
+class timer0 {
+  private:
+    
+    struct timerStruct
+    {
+      unsigned char status;           // siehe Beschreibung
+      unsigned int count_timer;       // count down Zähler
+    } CountDownTimers[TIMER0_NUM_COUNTDOWNTIMERS];    
+    
+    struct tm t;        // Struktur Datum & Zeit
+    
+    Ticker ticker;
+  
+  public:
+    uint16_t counter;
+    
+    uint16_t ms;        // Zähler für milli Sekunden
+    uint8_t sec;        // Zähler für die Sekunden
+    time_t seconds;     // Sekunden
+        
+    timer0();
+    void func(void);
+    uint8_t AllocateCountdownTimer (void);
+    void RemoveCountdownTimer(uint8_t timer);
+    uint8_t GetTimerStatus(uint8_t timer);
+    uint16_t GetTimerZeit(uint8_t timer);
+    void SetCountdownTimer(unsigned char timer, unsigned char status, unsigned short value);
+    void Set_t(uint8_t * data);
+};
\ No newline at end of file