timer0
Dependents: 09_PT1000 10_PT1000 11_PT1000 18_PT1000
timer0.cpp@0:bbd867fd30d1, 2014-07-26 (annotated)
- Committer:
- rs27
- Date:
- Sat Jul 26 07:27:37 2014 +0000
- Revision:
- 0:bbd867fd30d1
- Child:
- 1:45063f72267b
timer
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:bbd867fd30d1 | 15 | } |
rs27 | 0:bbd867fd30d1 | 16 | |
rs27 | 0:bbd867fd30d1 | 17 | //-------------------------------------------------------- |
rs27 | 0:bbd867fd30d1 | 18 | // Interruptroutine wird jede ms aufgerufen |
rs27 | 0:bbd867fd30d1 | 19 | void timer0::func(void) |
rs27 | 0:bbd867fd30d1 | 20 | { |
rs27 | 0:bbd867fd30d1 | 21 | uint8_t i; |
rs27 | 0:bbd867fd30d1 | 22 | |
rs27 | 0:bbd867fd30d1 | 23 | if(counter != 0) counter--; |
rs27 | 0:bbd867fd30d1 | 24 | |
rs27 | 0:bbd867fd30d1 | 25 | // ----- count down timers in ms ------------------------------------------------- |
rs27 | 0:bbd867fd30d1 | 26 | for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++) |
rs27 | 0:bbd867fd30d1 | 27 | { |
rs27 | 0:bbd867fd30d1 | 28 | if (CountDownTimers[i].status == 1) |
rs27 | 0:bbd867fd30d1 | 29 | { // 10 ms |
rs27 | 0:bbd867fd30d1 | 30 | if (CountDownTimers[i].count_timer > 0) |
rs27 | 0:bbd867fd30d1 | 31 | CountDownTimers[i].count_timer -- ; |
rs27 | 0:bbd867fd30d1 | 32 | if (CountDownTimers[i].count_timer == 0) |
rs27 | 0:bbd867fd30d1 | 33 | CountDownTimers[i].status = 0; |
rs27 | 0:bbd867fd30d1 | 34 | } |
rs27 | 0:bbd867fd30d1 | 35 | } |
rs27 | 0:bbd867fd30d1 | 36 | |
rs27 | 0:bbd867fd30d1 | 37 | } |
rs27 | 0:bbd867fd30d1 | 38 | |
rs27 | 0:bbd867fd30d1 | 39 | //-------------------------------------------------------- |
rs27 | 0:bbd867fd30d1 | 40 | // Abfrage nach freiem Timer |
rs27 | 0:bbd867fd30d1 | 41 | // |
rs27 | 0:bbd867fd30d1 | 42 | // wenn alle Timer belegt sind wird 0xFF zurückgegebne |
rs27 | 0:bbd867fd30d1 | 43 | |
rs27 | 0:bbd867fd30d1 | 44 | uint8_t timer0::AllocateCountdownTimer (void) |
rs27 | 0:bbd867fd30d1 | 45 | { |
rs27 | 0:bbd867fd30d1 | 46 | uint8_t i; |
rs27 | 0:bbd867fd30d1 | 47 | |
rs27 | 0:bbd867fd30d1 | 48 | for (i=0; i<TIMER0_NUM_COUNTDOWNTIMERS; i++) |
rs27 | 0:bbd867fd30d1 | 49 | if (CountDownTimers[i].status == 0xFF) |
rs27 | 0:bbd867fd30d1 | 50 | { |
rs27 | 0:bbd867fd30d1 | 51 | CountDownTimers[i].status = 0x00; // Timer reserviert, nicht gestartet |
rs27 | 0:bbd867fd30d1 | 52 | // printf_P(PSTR("\rallocate timer [%03d] %d\n"),i,ListPointer); |
rs27 | 0:bbd867fd30d1 | 53 | |
rs27 | 0:bbd867fd30d1 | 54 | return i; |
rs27 | 0:bbd867fd30d1 | 55 | } |
rs27 | 0:bbd867fd30d1 | 56 | |
rs27 | 0:bbd867fd30d1 | 57 | return 0xFF; |
rs27 | 0:bbd867fd30d1 | 58 | } |
rs27 | 0:bbd867fd30d1 | 59 | |
rs27 | 0:bbd867fd30d1 | 60 | //-------------------------------------------------------- |
rs27 | 0:bbd867fd30d1 | 61 | // Timer wieder freigeben |
rs27 | 0:bbd867fd30d1 | 62 | void timer0::RemoveCountdownTimer(uint8_t timer) |
rs27 | 0:bbd867fd30d1 | 63 | { |
rs27 | 0:bbd867fd30d1 | 64 | CountDownTimers[timer].status = 0xFF; |
rs27 | 0:bbd867fd30d1 | 65 | } |
rs27 | 0:bbd867fd30d1 | 66 | |
rs27 | 0:bbd867fd30d1 | 67 | //-------------------------------------------------------- |
rs27 | 0:bbd867fd30d1 | 68 | // Abfrage ob Timer 0 erreicht hat |
rs27 | 0:bbd867fd30d1 | 69 | uint8_t timer0::GetTimerStatus(uint8_t timer) |
rs27 | 0:bbd867fd30d1 | 70 | { |
rs27 | 0:bbd867fd30d1 | 71 | return CountDownTimers[timer].status; |
rs27 | 0:bbd867fd30d1 | 72 | } |
rs27 | 0:bbd867fd30d1 | 73 | |
rs27 | 0:bbd867fd30d1 | 74 | //-------------------------------------------------------- |
rs27 | 0:bbd867fd30d1 | 75 | // Abfrage der verbleibenden Zeit |
rs27 | 0:bbd867fd30d1 | 76 | uint16_t timer0::GetTimerZeit(uint8_t timer) |
rs27 | 0:bbd867fd30d1 | 77 | { |
rs27 | 0:bbd867fd30d1 | 78 | return CountDownTimers[timer].count_timer; |
rs27 | 0:bbd867fd30d1 | 79 | } |
rs27 | 0:bbd867fd30d1 | 80 | |
rs27 | 0:bbd867fd30d1 | 81 | //-------------------------------------------------------- |
rs27 | 0:bbd867fd30d1 | 82 | // Timer aktivieren |
rs27 | 0:bbd867fd30d1 | 83 | void timer0::SetCountdownTimer(uint8_t timer, uint8_t status, uint16_t value) |
rs27 | 0:bbd867fd30d1 | 84 | { |
rs27 | 0:bbd867fd30d1 | 85 | CountDownTimers[timer].count_timer = value; |
rs27 | 0:bbd867fd30d1 | 86 | CountDownTimers[timer].status = status; |
rs27 | 0:bbd867fd30d1 | 87 | } |