forked from Candas1/Arduino-FOC
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime_utils.cpp
More file actions
31 lines (28 loc) · 1.03 KB
/
Copy pathtime_utils.cpp
File metadata and controls
31 lines (28 loc) · 1.03 KB
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
#include "time_utils.h"
// function buffering delay()
// arduino uno function doesn't work well with interrupts
void _delay(unsigned long ms){
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega328PB__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__)
// if arduino uno and other atmega328p chips
// use while instad of delay,
// due to wrong measurement based on changed timer0
unsigned long t = _micros() + ms*1000;
while( _micros() < t ){};
#else
// regular micros
delay(ms);
#endif
}
// function buffering _micros()
// arduino function doesn't work well with interrupts
unsigned long _micros(){
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega328PB__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__)
// if arduino uno and other atmega328p chips
//return the value based on the prescaler
if((TCCR0B & 0b00000111) == 0x01) return (micros()/32);
else return (micros());
#else
// regular micros
return micros();
#endif
}