forked from benlaurie/arduino--
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock16.h
70 lines (53 loc) · 1.79 KB
/
clock16.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// -*- mode: c++; indent-tabs-mode: nil; -*-
#ifndef ARDUINO_MINUS_MINUS_CLOCK16_H
#define ARDUINO_MINUS_MINUS_CLOCK16_H
#ifdef ARDUINO_MINUS_MINUS_CLOCK32_H
#error "Only one clock resolution can be used"
#endif
/* This file should be included - once - in the main C++ file of the
application.
Other headers should - if at all possible - not include this file and
use a template instead.
*/
/** This is a Clock with 16bit clock resolution.
The value from Clock16::millis() will wrap around after about 65 seconds.
The recommended way to use a clock value in user code is:
typename Clock16::time_res_t now = Arduino16::millis();
Motivation: With Clock16 instead of Clock32, Lars has seen a code size
reduction of 238 bytes with avr-gcc 4.6.1.
*/
// Define this for a slower clock (for low power modes). Note that you should
// also set TIMER0_MICRO_SCALE if you don't use the default here
// See the Makefile and timerscale.py for details
#ifndef TIMER0_PRESCALE
# define TIMER0_PRESCALE 64
#endif
typedef _Clock<uint16_t,Timer0> Clock16;
Clock16 clock;
static void clock16_isr()
{
// copy these to local variables so they can be stored in registers
// (volatile variables must be read from memory on every access)
typename Clock16::time_res_t m = Clock16::timer_millis;
uint16_t f = Clock16::timer_fract;
m += (TIMER0_PRESCALE * (256 / (F_CPU / 1000000))) / 1000;
f += (TIMER0_PRESCALE * (256 / (F_CPU / 1000000))) % 1000;
if (f >= 1000)
{
f -= 1000;
m += 1;
}
Clock16::timer_fract = f;
Clock16::timer_millis = m;
Clock16::timer_overflow_count++;
}
/*
* Implementation of the clock ISR for 16 bits resolution
*/
#ifndef CLOCK_NO_ISR
ISR(TIMER0_OVF_vect)
{
clock16_isr();
}
#endif
#endif