diff options
author | Karel Kočí <cynerd@email.cz> | 2016-03-08 16:10:33 +0100 |
---|---|---|
committer | Karel Kočí <cynerd@email.cz> | 2016-03-08 16:10:33 +0100 |
commit | 5d29fe79d30f430ae326d9dc57ccfaed6fe61328 (patch) | |
tree | 8341804d561c0060176cbebc3f9a57c7c07f7816 /include/utils/timers_div.h | |
parent | 4e773191d447ac434536262a6f204dd991d4ad77 (diff) | |
download | avr-ioe-5d29fe79d30f430ae326d9dc57ccfaed6fe61328.tar.gz avr-ioe-5d29fe79d30f430ae326d9dc57ccfaed6fe61328.tar.bz2 avr-ioe-5d29fe79d30f430ae326d9dc57ccfaed6fe61328.zip |
Another full update of current work
Diffstat (limited to 'include/utils/timers_div.h')
-rw-r--r-- | include/utils/timers_div.h | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/include/utils/timers_div.h b/include/utils/timers_div.h new file mode 100644 index 0000000..9077aac --- /dev/null +++ b/include/utils/timers_div.h @@ -0,0 +1,81 @@ +//#include "../timer.h" +enum timerDivider { + TIMER_DIVIDER_1, + TIMER_DIVIDER_8, + TIMER_DIVIDER_64, + TIMER_DIVIDER_256, + TIMER_DIVIDER_1024 +}; + +#ifndef TIMER_DIV_RESTIME +#error Please define minimal time (us) before include timers_div.h in TIMER_DIV_RESTIME +#endif + +// Calculate optimal division +#define _TD_OPTIMAL (TIMER_DIV_RESTIME * F_CPU/1000000L) + +// Set minimal and maximal division closest to optimal +#if _TD_OPTIMAL < 8 +#define _TD_MIN 1 +#define _TD_MAX 8 +#elif _TD_OPTIMAL < 64 +#define _TD_MIN 8 +#define _TD_MAX 64 +#elif _TD_OPTIMAL < 256 +#define _TD_MIN 64 +#define _TD_MAX 256 +#elif _TD_OPTIMAL < 1024 +#define _TD_MIN 256 +#define _TD_MAX 1024 +#else +#define _TD_MIN 1024 +#define _TD_MAX 1024 +#endif + +// Calculate time difference between required time and got time resolution +#define _TD_MIN_TIME (TIMER_DIV_RESTIME - (_TD_MIN * 1000000L / F_CPU)) +#define _TD_MAX_TIME (TIMER_DIV_RESTIME - (_TD_MAX * 1000000L / F_CPU)) + +// Do absolute value of calculated numbers +#if _TD_MIN_TIME < 0 +#define _TD_MIN_TIMEF (_TD_MIN_TIME * -1) +#else +#define _TD_MIN_TIMEF _TD_MIN_TIME +#endif +#if _TD_MAX_TIME < 0 +#define _TD_MAX_TIMEF (_TD_MAX_TIME * -1) +#else +#define _TD_MAX_TIMEF _TD_MAX_TIME +#endif + +// Select closest one +#if _TD_MIN_TIMEF < _TD_MAX_TIMEF +#define _TD_TIMER_DIV _TD_MIN +#else +#define _TD_TIMER_DIV _TD_MAX +#endif + +// Set macro to enum value +#if _TD_TIMER_DIV == 1 +#define TIMER_DIV TIMER_DIVIDER_1 +#elif _TD_TIMER_DIV == 8 +#define TIMER_DIV TIMER_DIVIDER_8 +#elif _TD_TIMER_DIV == 64 +#define TIMER_DIV TIMER_DIVIDER_64 +#elif _TD_TIMER_DIV == 256 +#define TIMER_DIV TIMER_DIVIDER_256 +#elif _TD_TIMER_DIV == 1024 +#define TIMER_DIV TIMER_DIVIDER_1024 +#else +#error Generated unknown timer division. Something is wrong with timers_div.h +#endif + +// Undefine all used variables +#undef _TD_OPTIMAL +#undef _TD_MIN +#undef _TD_MAX +#undef _TD_MIN_TIME +#undef _TD_MAX_TIME +#undef _TD_MIN_TIMEF +#undef _TD_MAX_TIMEF +#undef _TD_TIMER_DIV |