-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtdelay.c
46 lines (35 loc) · 1 KB
/
tdelay.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
///\file
///\brief An attempt to make a usable delay routine
///
/// Regular util/delay.h is weird, it's defined as __always_inline__ and
/// it seems to make the text segment overflow too soon.
///
/// This is a fairly poor attempt at a delay routine. It uses Timer2 to
/// measure intervals. Not tested at all.
#include <inttypes.h>
#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
void tdelay(uint16_t ms) {
uint16_t i;
if (ms == 0) return;
ms = ms * 12;
uint8_t remainder = ms % 256;
if (remainder) {
TCCR2 = 0;
TCNT2 = 0;
OCR2 = remainder;
TIFR |= _BV(OCF2);
// prescaler = 1024, 0.128ms per cycle
for (TCCR2 = _BV(CS22)|_BV(CS21)|_BV(CS20); (TIFR & _BV(OCF2)) == 0;);
}
OCR2 = 0;
TCNT2 = 0;
for (i = ms/256; i > 0; i--) {
TCCR2 = 0;
TIFR |= _BV(TOV2);
for (TCCR2 = _BV(CS22)|_BV(CS21)|_BV(CS20); (TIFR & _BV(TOV2)) == 0;);
}
TCCR2 = 0;
}
//$Id$