timer 0 mit Sekunden

Dependents:   16_PT1000

Fork of timer0 by Reinhold Schaefer

Committer:
rs27
Date:
Sun Aug 03 19:12:56 2014 +0000
Revision:
1:cd53b287f41b
Parent:
0:bbd867fd30d1
Sekunden Timer integriert

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rs27 0:bbd867fd30d1 1
rs27 0:bbd867fd30d1 2 #include "timer0.h"
rs27 0:bbd867fd30d1 3
rs27 0:bbd867fd30d1 4 //--------------------------------------------------------
rs27 0:bbd867fd30d1 5 // Construktor initialisiert den Timer
rs27 0:bbd867fd30d1 6 timer0::timer0()
rs27 0:bbd867fd30d1 7 {
rs27 0:bbd867fd30d1 8 uint8_t i;
rs27 0:bbd867fd30d1 9
rs27 0:bbd867fd30d1 10 // Initialize countdown timers
rs27 0:bbd867fd30d1 11 for (i=0; i < TIMER0_NUM_COUNTDOWNTIMERS; i++)
rs27 0:bbd867fd30d1 12 CountDownTimers[i].status = 0xFF;
rs27 0:bbd867fd30d1 13
rs27 0:bbd867fd30d1 14 ticker.attach_us(this, &timer0::func, 1000);
rs27 1:cd53b287f41b 15
rs27 1:cd53b287f41b 16 countMillisecond = 0;
rs27 0:bbd867fd30d1 17 }
rs27 0:bbd867fd30d1 18
rs27 0:bbd867fd30d1 19 //--------------------------------------------------------
rs27 0:bbd867fd30d1 20 // Interruptroutine wird jede ms aufgerufen
rs27 0:bbd867fd30d1 21 void timer0::func(void)
rs27 0:bbd867fd30d1 22 {
rs27 0:bbd867fd30d1 23 uint8_t i;
rs27 0:bbd867fd30d1 24
rs27 1:cd53b287f41b 25 countMillisecond++;
rs27 1:cd53b287f41b 26
rs27 0:bbd867fd30d1 27 if(counter != 0) counter--;
rs27 0:bbd867fd30d1 28
rs27 0:bbd867fd30d1 29 // ----- count down timers in ms -------------------------------------------------
rs27 1:cd53b287f41b 30
rs27 0:bbd867fd30d1 31 for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++)
rs27 0:bbd867fd30d1 32 {
rs27 0:bbd867fd30d1 33 if (CountDownTimers[i].status == 1)
rs27 0:bbd867fd30d1 34 { // 10 ms
rs27 0:bbd867fd30d1 35 if (CountDownTimers[i].count_timer > 0)
rs27 0:bbd867fd30d1 36 CountDownTimers[i].count_timer -- ;
rs27 0:bbd867fd30d1 37 if (CountDownTimers[i].count_timer == 0)
rs27 0:bbd867fd30d1 38 CountDownTimers[i].status = 0;
rs27 0:bbd867fd30d1 39 }
rs27 0:bbd867fd30d1 40 }
rs27 0:bbd867fd30d1 41
rs27 1:cd53b287f41b 42 //---------------------------------------------------------
rs27 1:cd53b287f41b 43 // count down timer für Sekunden
rs27 1:cd53b287f41b 44
rs27 1:cd53b287f41b 45 if (countMillisecond >= 1000)
rs27 1:cd53b287f41b 46 {
rs27 1:cd53b287f41b 47 countMillisecond = 0;
rs27 1:cd53b287f41b 48
rs27 1:cd53b287f41b 49 // ----- count down timers in Sekunden -------------------------------------------------
rs27 1:cd53b287f41b 50
rs27 1:cd53b287f41b 51 for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++)
rs27 1:cd53b287f41b 52 {
rs27 1:cd53b287f41b 53 if (CountDownTimers[i].status == 2)
rs27 1:cd53b287f41b 54 {
rs27 1:cd53b287f41b 55 if (CountDownTimers[i].count_timer > 0)
rs27 1:cd53b287f41b 56 CountDownTimers[i].count_timer -- ;
rs27 1:cd53b287f41b 57 if (CountDownTimers[i].count_timer == 0)
rs27 1:cd53b287f41b 58 CountDownTimers[i].status = 0;
rs27 1:cd53b287f41b 59 }
rs27 1:cd53b287f41b 60 }
rs27 1:cd53b287f41b 61 }
rs27 0:bbd867fd30d1 62 }
rs27 0:bbd867fd30d1 63
rs27 0:bbd867fd30d1 64 //--------------------------------------------------------
rs27 0:bbd867fd30d1 65 // Abfrage nach freiem Timer
rs27 0:bbd867fd30d1 66 //
rs27 0:bbd867fd30d1 67 // wenn alle Timer belegt sind wird 0xFF zurückgegebne
rs27 0:bbd867fd30d1 68
rs27 0:bbd867fd30d1 69 uint8_t timer0::AllocateCountdownTimer (void)
rs27 0:bbd867fd30d1 70 {
rs27 0:bbd867fd30d1 71 uint8_t i;
rs27 0:bbd867fd30d1 72
rs27 0:bbd867fd30d1 73 for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++)
rs27 0:bbd867fd30d1 74 if (CountDownTimers[i].status == 0xFF)
rs27 0:bbd867fd30d1 75 {
rs27 0:bbd867fd30d1 76 CountDownTimers[i].status = 0x00; // Timer reserviert, nicht gestartet
rs27 0:bbd867fd30d1 77 // printf_P(PSTR("\rallocate timer [%03d] %d\n"),i,ListPointer);
rs27 0:bbd867fd30d1 78
rs27 0:bbd867fd30d1 79 return i;
rs27 0:bbd867fd30d1 80 }
rs27 0:bbd867fd30d1 81
rs27 0:bbd867fd30d1 82 return 0xFF;
rs27 0:bbd867fd30d1 83 }
rs27 0:bbd867fd30d1 84
rs27 0:bbd867fd30d1 85 //--------------------------------------------------------
rs27 0:bbd867fd30d1 86 // Timer wieder freigeben
rs27 0:bbd867fd30d1 87 void timer0::RemoveCountdownTimer(uint8_t timer)
rs27 0:bbd867fd30d1 88 {
rs27 0:bbd867fd30d1 89 CountDownTimers[timer].status = 0xFF;
rs27 0:bbd867fd30d1 90 }
rs27 0:bbd867fd30d1 91
rs27 0:bbd867fd30d1 92 //--------------------------------------------------------
rs27 0:bbd867fd30d1 93 // Abfrage ob Timer 0 erreicht hat
rs27 0:bbd867fd30d1 94 uint8_t timer0::GetTimerStatus(uint8_t timer)
rs27 0:bbd867fd30d1 95 {
rs27 0:bbd867fd30d1 96 return CountDownTimers[timer].status;
rs27 0:bbd867fd30d1 97 }
rs27 0:bbd867fd30d1 98
rs27 0:bbd867fd30d1 99 //--------------------------------------------------------
rs27 0:bbd867fd30d1 100 // Abfrage der verbleibenden Zeit
rs27 0:bbd867fd30d1 101 uint16_t timer0::GetTimerZeit(uint8_t timer)
rs27 0:bbd867fd30d1 102 {
rs27 0:bbd867fd30d1 103 return CountDownTimers[timer].count_timer;
rs27 0:bbd867fd30d1 104 }
rs27 0:bbd867fd30d1 105
rs27 0:bbd867fd30d1 106 //--------------------------------------------------------
rs27 0:bbd867fd30d1 107 // Timer aktivieren
rs27 0:bbd867fd30d1 108 void timer0::SetCountdownTimer(uint8_t timer, uint8_t status, uint16_t value)
rs27 0:bbd867fd30d1 109 {
rs27 0:bbd867fd30d1 110 CountDownTimers[timer].count_timer = value;
rs27 0:bbd867fd30d1 111 CountDownTimers[timer].status = status;
rs27 0:bbd867fd30d1 112 }