Zeitsteuerung

Fork of timer0 by V09

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers timer0.cpp Source File

timer0.cpp

00001 #include "mbed.h"
00002 #include "timer0.h"
00003 
00004 
00005 //--------------------------------------------------------
00006 // Construktor initialisiert den Timer
00007 timer0::timer0()
00008 {
00009     uint8_t i;
00010     
00011     ms = 0;
00012     sec = 0;
00013     
00014     // Initialize countdown timers
00015     for (i=0; i < TIMER0_NUM_COUNTDOWNTIMERS; i++)
00016       CountDownTimers[i].status = 0xFF;
00017   
00018     ticker.attach_us(this, &timer0::func, 1000);
00019 }
00020 
00021 //--------------------------------------------------------
00022 // Interruptroutine wird jede ms aufgerufen
00023 void timer0::func(void)
00024 {
00025     uint8_t i;
00026     
00027     if(counter != 0) counter--;
00028 
00029     // Zeitbasis für Systemzeit
00030     // ----- count down timers in ms -------------------------------------------------
00031     for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++) 
00032     {
00033         if (CountDownTimers[i].status == 1) 
00034         {       // ms
00035             if (CountDownTimers[i].count_timer > 0)
00036                 CountDownTimers[i].count_timer -- ;
00037             if (CountDownTimers[i].count_timer == 0)
00038                 CountDownTimers[i].status = 0;
00039         }
00040     }
00041     
00042     if (ms < 1000) 
00043     {
00044         ms++;  
00045     }
00046     else
00047     {
00048         ms = 0;
00049        
00050         seconds++;
00051         set_time(seconds);
00052        
00053         // ----- count down timers in s -------------------------------------------------
00054         for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++) 
00055         {
00056             if (CountDownTimers[i].status == 2) 
00057             {   // sekunden
00058                 if (CountDownTimers[i].count_timer > 0)
00059                     CountDownTimers[i].count_timer -- ;
00060                 if (CountDownTimers[i].count_timer == 0)
00061                     CountDownTimers[i].status = 0;
00062             }
00063         }
00064         
00065         if (sec < 60) 
00066         {
00067             sec++;
00068         }
00069         else
00070         {
00071             sec = 0; 
00072             
00073             // hier folgt der Minutenzähler falls erforderlich           
00074         }   
00075     }
00076     
00077 }
00078 
00079 //--------------------------------------------------------
00080 // Abfrage nach freiem Timer
00081 //
00082 // wenn alle Timer belegt sind wird 0xFF zurückgegebne
00083 
00084 uint8_t timer0::AllocateCountdownTimer (void)
00085 {
00086     uint8_t i;
00087     
00088     for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++)
00089         if (CountDownTimers[i].status == 0xFF)
00090         {
00091             CountDownTimers[i].status = 0x00;   // Timer reserviert, nicht gestartet
00092             // printf_P(PSTR("\rallocate timer [%03d] %d\n"),i,ListPointer);            
00093             
00094             return i;
00095         }
00096 
00097     return 0xFF;
00098 }
00099 
00100 //--------------------------------------------------------
00101 // Timer wieder freigeben
00102 void timer0::RemoveCountdownTimer(uint8_t timer)
00103 {
00104     CountDownTimers[timer].status = 0xFF;
00105 }
00106 
00107 //--------------------------------------------------------
00108 // Abfrage ob Timer 0 erreicht hat                  
00109 uint8_t timer0::GetTimerStatus(uint8_t timer)
00110 {
00111     return CountDownTimers[timer].status;
00112 }
00113 
00114 //--------------------------------------------------------
00115 // Abfrage der verbleibenden Zeit               
00116 uint16_t timer0::GetTimerZeit(uint8_t timer)
00117 {
00118     return CountDownTimers[timer].count_timer;
00119 }
00120 
00121 //--------------------------------------------------------
00122 // Timer aktivieren
00123 void timer0::SetCountdownTimer(unsigned char timer, unsigned char status, unsigned short value)
00124 {
00125     CountDownTimers[timer].count_timer = value;
00126     CountDownTimers[timer].status = status;
00127 }
00128 
00129 //--------------------------------------------------------
00130 // Zeitstempel setzen
00131 void timer0::Set_t(uint8_t * data)
00132 {
00133     char buffer[40];    
00134     
00135     t.tm_sec  = data[0];     // Sekunden 0-59
00136     t.tm_min  = data[1];     // Minuten 0-59
00137     t.tm_hour = data[2];     // Stunden 0-23
00138     t.tm_mday = data[3];     // Tag 1-31
00139     t.tm_mon  = data[4] - 1; // Monat 0-11 0 = Januar
00140     t.tm_year = data[5];     // Jahr year since 1900   
00141     
00142     seconds = mktime(&t);
00143     set_time(seconds);
00144     
00145     strftime(buffer, 40, "%a,%d.%m.%Y %H:%M:%S", localtime(&seconds));
00146     // pc.printf("\ntimer0::set_t = %s", buffer);   
00147 }