diff options
Diffstat (limited to 'timer.h')
| -rw-r--r-- | timer.h | 21 |
1 files changed, 20 insertions, 1 deletions
| @@ -20,7 +20,26 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 20 | 20 | ||
| 21 | #include <stdint.h> | 21 | #include <stdint.h> |
| 22 | 22 | ||
| 23 | #define TIMER_DIFF(a, b) ((a) >= (b) ? (a) - (b) : UINT16_MAX - (b) + (a)) | 23 | #ifndef TIMER_PRESCALER |
| 24 | # if F_CPU > 16000000 | ||
| 25 | # define TIMER_PRESCALER 256 | ||
| 26 | # elif F_CPU >= 4000000 | ||
| 27 | # define TIMER_PRESCALER 64 | ||
| 28 | # else | ||
| 29 | # define TIMER_PRESCALER 8 | ||
| 30 | # endif | ||
| 31 | #endif | ||
| 32 | #define TIMER_RAW_FREQ (F_CPU/TIMER_PRESCALER) | ||
| 33 | #define TIMER_RAW TCNT0 | ||
| 34 | #define TIMER_RAW_TOP (TIMER_RAW_FREQ/1000) | ||
| 35 | |||
| 36 | #if (TIMER_RAW_TOP > 255) | ||
| 37 | # error "Timer0 can't count 1ms at this clock freq. Use larger prescaler." | ||
| 38 | #endif | ||
| 39 | |||
| 40 | #define TIMER_DIFF(a, b, max) ((a) >= (b) ? (a) - (b) : (max) - (b) + (a)) | ||
| 41 | #define TIMER_DIFF_RAW(a, b) TIMER_DIFF(a, b, UINT8_MAX) | ||
| 42 | #define TIMER_DIFF_MS(a, b) TIMER_DIFF(a, b, UINT16_MAX) | ||
| 24 | 43 | ||
| 25 | 44 | ||
| 26 | extern volatile uint16_t timer_count; | 45 | extern volatile uint16_t timer_count; |
