MCPcopy Create free account
hub / github.com/PolySync/oscc / timer2_init

Function timer2_init

firmware/common/libs/timer/oscc_timer.cpp:109–194  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

107
108#ifndef __AVR_ATmega32U4__
109void timer2_init( float frequency, void (*isr)(void) )
110{
111 // disable interrupts temporarily
112 cli();
113
114 // clear existing config
115 TCCR2A = 0;
116 TCCR2B = 0;
117
118 // initialize counter value to 0
119 TCNT2 = 0;
120
121
122 unsigned long prescaler = F_CPU / ((TIMER2_SIZE+1) * frequency);
123
124 if ( prescaler > 256 )
125 {
126 prescaler = 1024;
127
128 TCCR2B |= TIMER2_PRESCALER_1024;
129 }
130 else if ( prescaler > 128 )
131 {
132 prescaler = 256;
133
134 TCCR2B |= TIMER2_PRESCALER_256;
135 }
136 else if ( prescaler > 64 )
137 {
138 prescaler = 128;
139
140 TCCR2B |= TIMER2_PRESCALER_128;
141 }
142 else if ( prescaler > 32 )
143 {
144 prescaler = 64;
145
146 TCCR2B |= TIMER2_PRESCALER_64;
147 }
148 else if ( prescaler > 8 )
149 {
150 prescaler = 32;
151
152 TCCR2B |= TIMER2_PRESCALER_32;
153 }
154 else if ( prescaler > 1 )
155 {
156 prescaler = 8;
157
158 TCCR2B |= TIMER2_PRESCALER_8;
159 }
160 else
161 {
162 prescaler = 1;
163
164 TCCR2B |= TIMER2_PRESCALER_1;
165 }
166

Callers 1

start_timersFunction · 0.85

Calls 2

cliFunction · 0.85
seiFunction · 0.85

Tested by

no test coverage detected