summaryrefslogtreecommitdiff
path: root/include/arch/arduino-nano/driver/counter.h
blob: 9105219f9377735dc6920c751221dee1c0b13420 (plain)
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
#include <avr/io.h>
#include <avr/interrupt.h>

class Counter {
	private:
		Counter(const Counter &copy);

	public:
		uint16_t value;
		volatile uint8_t overflowed;

		Counter() : overflowed(0) {}

		inline void start() {
			overflowed = 0;
			TCNT1 = 0;
			TCCR1A = 0;
			TCCR1B = _BV(CS10); // no prescaler
			TIMSK1 = _BV(TOIE1);
		}

		inline void stop() {
			TCCR1B = 0;
			value = TCNT1;
		}
};

extern Counter counter;