-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
131 lines (106 loc) · 3.81 KB
/
timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*----------------------------------------------------------------------------
Copyright: Radig Ulrich mailto: mail@ulrichradig.de
Author: Radig Ulrich
Remarks:
known Problems: none
Version: 24.10.2007
Description: Timer Routinen
Dieses Programm ist freie Software. Sie können es unter den Bedingungen der
GNU General Public License, wie von der Free Software Foundation veröffentlicht,
weitergeben und/oder modifizieren, entweder gemäß Version 2 der Lizenz oder
(nach Ihrer Option) jeder späteren Version.
Die Veröffentlichung dieses Programms erfolgt in der Hoffnung,
daß es Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE,
sogar ohne die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT
FÜR EINEN BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
Programm erhalten haben.
Falls nicht, schreiben Sie an die Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
------------------------------------------------------------------------------*/
#include "config.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include "timer.h"
#if USE_ENOCEAN
#include "enocean.h"
#endif //USE_ENOCEAN
volatile unsigned long time;
volatile unsigned long time_watchdog = 0;
volatile uint8_t key_state = 0; // debounced and inverted key state:
volatile uint8_t key_press = 0; // key press detect
//----------------------------------------------------------------------------
//Diese Routine startet und inizialisiert den Timer
void timer_init (void)
{
#if EXTCLOCK==1
#if defined (__AVR_ATmega644__)
//Asynchroner Modus ein, Oszillator an TOSC1 und TOSC2 aktiv
ASSR |= (1<<AS2);
TCCR2B = 0x05;
while(ASSR & 0x11);
//Capture/Compare-Interrupt aktiv
TIMSK2 |= (1<<OCIE2A);
#else
//Asynchroner Modus ein, Oszillator an TOSC1 und TOSC2 aktiv
ASSR = (1<<AS2);
//CTC-Modus an (Clear Timer on Compare Match)
TCCR2 = (1<<WGM21);
//dieser Wert ergibt eine Sekunde Periodendauer
OCR2 = 31;
//lösche Prescaler 2
SFIE = (1<<PSR2);
//Starte Timer 2 mit Prescaler gleich 1/1024
TCCR2 |= (1<<CS22)|(1<<CS21)|(1<<CS20);
while(ASSR & 0x07);
//Capture/Compare-Interrupt aktiv
TIMSK = (1<<OCIE2);
#endif
#else
#if defined (__AVR_ATmega328P__)
TCCR1B |= (1<<WGM12) | (1<<CS10 | 0<<CS11 | 1<<CS12); //1024
TCNT1 = 0;
OCR1A = ((F_CPU / 1024) / 200) - 1;
TIMSK1 = (1 << OCIE1A);
#else
TCCR1B |= (1<<WGM12) | (1<<CS10 | 0<<CS11 | 1<<CS12); //1024
TCNT1 = 0;
OCR1A = ((F_CPU / 1024) / 200) - 1;
TIMSK |= (1 << OCIE1A);
#endif
#endif
return;
};
//----------------------------------------------------------------------------
//Timer Interrupt
#if EXTCLOCK==1
#if defined (__AVR_ATmega644__)
ISR (TIMER2_COMPA_vect)
#else
ISR (TIMER2_COMP_vect)
#endif
#else
ISR (TIMER1_COMPA_vect)
#endif
{
static uint16_t prescaler_10ms = 2;
if(!prescaler_10ms--)
{
prescaler_10ms = 1;
static uint8_t ct0, ct1;
uint8_t i;
i = key_state ^ ~PIND; // key changed ?
ct0 = ~( ct0 & i ); // reset or count ct0
ct1 = ct0 ^ (ct1 & i); // reset or count ct1
i &= ct0 & ct1; // count until roll over ?
key_state ^= i; // then toggle debounced state
key_press |= key_state & i; // 0->1: key press detect
}
static uint16_t prescaler_s = 200;
if(!prescaler_s--)
{
prescaler_s = 199;
merker = 1;
enocean_tick();
}
}