forked from nicholaskell/Teacup_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelay.c
48 lines (41 loc) · 1.04 KB
/
delay.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
#include "delay.h"
/** \file
\brief Delay routines
*/
//#include <util/delay_basic.h>
//#include "watchdog.h"
/// interruptable microsecond delay
/// does NOT call wd_reset
/// \param delay time in microseconds
void delay_us( uint16_t delay )
{
while (delay > (65536UL / (F_CPU / 4000000UL))) {
_delay_loop_2(65534); //
delay -= (65536L / (F_CPU / 4000000L));
}
_delay_loop_2( delay * (F_CPU / 4000000UL));
}
/// delay microseconds
/// \param delay time to wait in microseconds
void _delay(uint32_t delay) {
wd_reset();
while (delay > (65536L / (F_CPU / 4000000L))) {
_delay_loop_2(65534); // we use 65534 here to compensate for the time that the surrounding loop takes. TODO: exact figure needs tuning
delay -= (65536L / (F_CPU / 4000000L));
wd_reset();
}
_delay_loop_2(delay * (F_CPU / 4000000L));
wd_reset();
}
/// delay milliseconds
/// \param delay time to wait in milliseconds
void _delay_ms(uint32_t delay) {
wd_reset();
while (delay > 65) {
delay_us(64999);
delay -= 65;
wd_reset();
}
delay_us(delay * 1000);
wd_reset();
}