|
1 | 1 | #include "hardwareTimer.h" |
2 | 2 |
|
3 | | -void start_hardware_timer(TIME unTime){ |
4 | | - /* |
5 | | - * Seteo el timer en multiplo de 1seg. |
6 | | - * unTime esta en ms. |
| 3 | +void start_hardware_timer(TIME unTime) |
| 4 | +{ |
| 5 | + /* |
| 6 | + * Set timer on 1sec. |
| 7 | + * unTime in ms. |
7 | 8 | * prescaler 8 max_ms : 32 ms |
8 | 9 | * prescaler 64 max_ms : 262ms |
9 | 10 | * prescaler 256 max_ms : 1048 ms |
10 | 11 | * prescaler 1024 max_ms : 4164 ms |
11 | 12 | */ |
12 | 13 |
|
13 | | - |
14 | | - TIME ticks=0; |
15 | | - float prescaler=1; |
| 14 | + TIME ticks = 0; |
| 15 | + float prescaler = 1; |
16 | 16 | cli(); |
17 | | - |
18 | | - if ((unTime>=0) & (unTime <= 32)){ |
| 17 | + if ((unTime >= 0) & (unTime <= 32)) |
| 18 | + { |
19 | 19 | prescaler = 8.0; |
20 | | - }else if((unTime>=33) & (unTime <= 262)){ |
| 20 | + } |
| 21 | + else if ((unTime >= 33) & (unTime <= 262)) |
| 22 | + { |
21 | 23 | prescaler = 64.0; |
22 | | - }else if ((unTime>=263) & (unTime <= 1048)){ |
| 24 | + } |
| 25 | + else if ((unTime >= 263) & (unTime <= 1048)) |
| 26 | + { |
23 | 27 | prescaler = 256.0; |
24 | | - }else if((unTime>=1049) & (unTime <= 4164)){ |
| 28 | + } |
| 29 | + else if ((unTime >= 1049) & (unTime <= 4164)) |
| 30 | + { |
25 | 31 | prescaler = 1024.0; |
26 | | - }else{ |
27 | | - prescaler = 1024.0; //TODO control de error |
28 | 32 | } |
29 | | -// ticks = round((unsigned long)((float)unTime*15624)/1000); //1s 15624 ticks max value 65535 |
30 | | - //Serial.println(ticks); |
31 | | - // disable global interrupts |
32 | | - TCCR1A = 0; // set entire TCCR1A register to 0 |
33 | | - TCCR1B = 0; // same for TCCR1B |
34 | | - //OCR1A = ticks; |
35 | | - OCR1A = round((16000.0f/prescaler)*unTime - 1); |
36 | | - // turn on CTC mode: |
| 33 | + else |
| 34 | + { |
| 35 | + prescaler = 1024.0; //TODO: control de error |
| 36 | + } |
| 37 | + |
| 38 | + TCCR1A = 0; // set entire TCCR1A register to 0 |
| 39 | + TCCR1B = 0; // same for TCCR1B |
| 40 | + TCNT1 = 0; |
| 41 | + |
| 42 | + OCR1A = round((16000.0f / prescaler) * unTime - 1); |
| 43 | + TIFR1 = 0xFF; // reset flags |
| 44 | + // turn on CTC mode: |
37 | 45 | TCCR1B |= (1 << WGM12); |
38 | | - // Set CS10 and CS12 bits for 1024 prescaler: |
39 | | - switch ((int)prescaler) { |
40 | | - case 8: |
41 | | - TCCR1B |= (1 << CS11); |
42 | | - break; |
43 | | - case 64: |
44 | | - TCCR1B |= (1 << CS10); |
45 | | - TCCR1B |= (1 << CS11); |
46 | | - break; |
47 | | - case 256: |
48 | | - TCCR1B |= (1 << CS12); |
49 | | - break; |
50 | | - case 1024: |
51 | | - default: |
52 | | - TCCR1B |= (1 << CS10); |
53 | | - TCCR1B |= (1 << CS12); |
54 | | - break; |
55 | | - } |
56 | | - // enable timer compare interrupt: |
| 46 | + // Set CS10 and CS12 bits for 1024 prescaler: |
| 47 | + switch ((int)prescaler) |
| 48 | + { |
| 49 | + case 8: |
| 50 | + TCCR1B |= (1 << CS11); |
| 51 | + break; |
| 52 | + case 64: |
| 53 | + TCCR1B |= (1 << CS10); |
| 54 | + TCCR1B |= (1 << CS11); |
| 55 | + break; |
| 56 | + case 256: |
| 57 | + TCCR1B |= (1 << CS12); |
| 58 | + break; |
| 59 | + case 1024: |
| 60 | + default: |
| 61 | + TCCR1B |= (1 << CS10); |
| 62 | + TCCR1B |= (1 << CS12); |
| 63 | + break; |
| 64 | + } |
| 65 | + // enable timer compare interrupt: |
57 | 66 | TIMSK1 |= (1 << OCIE1A); |
58 | | - // enable global interrupts: |
59 | 67 | sei(); |
60 | 68 | } |
61 | 69 |
|
62 | | -ISR(TIMER1_COMPA_vect) { |
63 | | - /* |
64 | | - * En la interrupcion hago un update con el timepo que expiro y seteo un nuevo timer con el valor de timer_next( si es que hay uno) |
65 | | - */ |
| 70 | +ISR(TIMER1_COMPA_vect) |
| 71 | +{ |
| 72 | + |
66 | 73 | TCCR1A = 0; |
67 | | - TCCR1B = 0; //apago el timer sino queda ciclico por defecto |
| 74 | + TCCR1B = 0; //stop timer |
68 | 75 | time_now = update_time_now(); |
69 | | - timers_update(time_now - time_timer_set); // actualizo el nuevo timer_next |
70 | | - /* start physical timer for next shortest time if one exists */ |
71 | | - if (timer_next) { |
72 | | - time_timer_set = time_now; |
73 | | - start_hardware_timer(timer_next->time); |
74 | | - |
| 76 | + timers_update(time_now - time_timer_set); // update new timer_next |
| 77 | + /* start physical timer for next shortest time if one exists */ |
| 78 | + if (timer_next) |
| 79 | + { |
| 80 | + time_timer_set = time_now; |
| 81 | + start_hardware_timer(timer_next->time); |
75 | 82 | } |
76 | 83 | } |
77 | 84 |
|
78 | | -TIME update_time_now(){ |
| 85 | +TIME update_time_now() |
| 86 | +{ |
79 | 87 | return millis(); |
80 | 88 | } |
0 commit comments