Just like the millis()
function in Arduino, this function returns
the time in milliseconds since the program started.
This function has only been tested on the atmega328p, but may work on many other AVRs as well.
Implementing this function is easy - take a look at the example:
-
First you must initiate the clock with
init_millis()
-
After calling
init_millis()
, callsei()
to enable global interrupts -
Use
millis()
whenever you like to get the time in milliseconds since the program started
Complications that may arise: A problem I faced was that I got this error messsage when compiling:
/usr/local/CrossPack-AVR-20131216/avr/include/millis.c: In function 'millis':
/usr/local/CrossPack-AVR-20131216/avr/include/millis.c:49:3: error: 'for' loop initial declarations are only allowed in C99 mode
ATOMIC_BLOCK(ATOMIC_FORCEON) {
^
/usr/local/CrossPack-AVR-20131216/avr/include/millis.c:49:3: note: use option -std=c99 or -std=gnu99 to compile your code
make: *** [main.o] Error 1
The error is nicely explained here and can be fixed by
adding -std=c99
or -std=gnu99
to the command avr-gcc
when compiling your code.
For any questions write me an email at: [email protected]