timer0

Dependents:   09_PT1000 10_PT1000 11_PT1000 18_PT1000

Committer:
rs27
Date:
Fri May 08 07:47:04 2015 +0000
Revision:
2:d8e4c9bfd51d
Parent:
1:45063f72267b
080515;

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:45063f72267b 15
rs27 1:45063f72267b 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:45063f72267b 25 countMillisecond++;
rs27 1:45063f72267b 26
rs27 0:bbd867fd30d1 27 if(counter != 0) counter--;
rs27 0:bbd867fd30d1 28
rs27 0:bbd867fd30d1 29 // ----- count down timers in ms -------------------------------------------------
rs27 1:45063f72267b 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 1:45063f72267b 34 {
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:45063f72267b 42 //---------------------------------------------------------
rs27 1:45063f72267b 43 // count down timer für Sekunden
rs27 1:45063f72267b 44
rs27 1:45063f72267b 45 if (countMillisecond >= 1000)
rs27 1:45063f72267b 46 {
rs27 1:45063f72267b 47 countMillisecond = 0; // ----- count down timers in Sekunden -------------------------------------------------
rs27 1:45063f72267b 48
rs27 1:45063f72267b 49 for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++)
rs27 1:45063f72267b 50 {
rs27 1:45063f72267b 51 if (CountDownTimers[i].status == 2)
rs27 1:45063f72267b 52 {
rs27 1:45063f72267b 53 if (CountDownTimers[i].count_timer > 0)
rs27 1:45063f72267b 54 CountDownTimers[i].count_timer -- ;
rs27 1:45063f72267b 55 if (CountDownTimers[i].count_timer == 0)
rs27 1:45063f72267b 56 CountDownTimers[i].status = 0;
rs27 1:45063f72267b 57 }
rs27 1:45063f72267b 58 }
rs27 1:45063f72267b 59 }
rs27 1:45063f72267b 60
rs27 0:bbd867fd30d1 61 }
rs27 0:bbd867fd30d1 62
rs27 0:bbd867fd30d1 63 //--------------------------------------------------------
rs27 0:bbd867fd30d1 64 // Abfrage nach freiem Timer
rs27 0:bbd867fd30d1 65 //
rs27 0:bbd867fd30d1 66 // wenn alle Timer belegt sind wird 0xFF zurückgegebne
rs27 0:bbd867fd30d1 67
rs27 0:bbd867fd30d1 68 uint8_t timer0::AllocateCountdownTimer (void)
rs27 0:bbd867fd30d1 69 {
rs27 0:bbd867fd30d1 70 uint8_t i;
rs27 0:bbd867fd30d1 71
rs27 0:bbd867fd30d1 72 for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++)
rs27 0:bbd867fd30d1 73 if (CountDownTimers[i].status == 0xFF)
rs27 0:bbd867fd30d1 74 {
rs27 0:bbd867fd30d1 75 CountDownTimers[i].status = 0x00; // Timer reserviert, nicht gestartet
rs27 0:bbd867fd30d1 76 // printf_P(PSTR("\rallocate timer [%03d] %d\n"),i,ListPointer);
rs27 0:bbd867fd30d1 77
rs27 0:bbd867fd30d1 78 return i;
rs27 0:bbd867fd30d1 79 }
rs27 0:bbd867fd30d1 80
rs27 0:bbd867fd30d1 81 return 0xFF;
rs27 0:bbd867fd30d1 82 }
rs27 0:bbd867fd30d1 83
rs27 0:bbd867fd30d1 84 //--------------------------------------------------------
rs27 0:bbd867fd30d1 85 // Timer wieder freigeben
rs27 0:bbd867fd30d1 86 void timer0::RemoveCountdownTimer(uint8_t timer)
rs27 0:bbd867fd30d1 87 {
rs27 0:bbd867fd30d1 88 CountDownTimers[timer].status = 0xFF;
rs27 0:bbd867fd30d1 89 }
rs27 0:bbd867fd30d1 90
rs27 0:bbd867fd30d1 91 //--------------------------------------------------------
rs27 0:bbd867fd30d1 92 // Abfrage ob Timer 0 erreicht hat
rs27 0:bbd867fd30d1 93 uint8_t timer0::GetTimerStatus(uint8_t timer)
rs27 0:bbd867fd30d1 94 {
rs27 0:bbd867fd30d1 95 return CountDownTimers[timer].status;
rs27 0:bbd867fd30d1 96 }
rs27 0:bbd867fd30d1 97
rs27 0:bbd867fd30d1 98 //--------------------------------------------------------
rs27 0:bbd867fd30d1 99 // Abfrage der verbleibenden Zeit
rs27 0:bbd867fd30d1 100 uint16_t timer0::GetTimerZeit(uint8_t timer)
rs27 0:bbd867fd30d1 101 {
rs27 0:bbd867fd30d1 102 return CountDownTimers[timer].count_timer;
rs27 0:bbd867fd30d1 103 }
rs27 0:bbd867fd30d1 104
rs27 0:bbd867fd30d1 105 //--------------------------------------------------------
rs27 0:bbd867fd30d1 106 // Timer aktivieren
rs27 0:bbd867fd30d1 107 void timer0::SetCountdownTimer(uint8_t timer, uint8_t status, uint16_t value)
rs27 0:bbd867fd30d1 108 {
rs27 0:bbd867fd30d1 109 CountDownTimers[timer].count_timer = value;
rs27 0:bbd867fd30d1 110 CountDownTimers[timer].status = status;
rs27 0:bbd867fd30d1 111 }