/
tiny1_timer_a.hpp
76 lines (66 loc) · 1.85 KB
/
tiny1_timer_a.hpp
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
71
72
73
74
75
76
#ifndef AVRLIB_TIYN1_TIMER_A_HPP
#define AVRLIB_TIYN1_TIMER_A_HPP
#include <avr/io.h>
#include "timer_base.hpp"
namespace avrlib
{
class timer_a_t
{
public:
typedef uint16_t value_type;
typedef value_type time_type;
timer_a_t(TCA_t& timer)
:value_bits(16), m_timer(timer.SINGLE)
{}
value_type value() { return m_timer.CNT; }
void value(const value_type& v) { m_timer.CNT = v; }
value_type top() { return m_timer.PER; }
void top(const value_type& v)
{
m_timer.PER = v;
switch (v)
{
case 0: value_bits = 16; break;
case 1: value_bits = 0; break;
case 2: value_bits = 1; break;
case 4: value_bits = 2; break;
case 8: value_bits = 3; break;
case 16: value_bits = 4; break;
case 32: value_bits = 5; break;
case 64: value_bits = 6; break;
case 128: value_bits = 7; break;
case 256: value_bits = 8; break;
case 512: value_bits = 9; break;
case 1024: value_bits = 10; break;
case 2048: value_bits = 11; break;
case 4096: value_bits = 12; break;
case 8192: value_bits = 13; break;
case 16384: value_bits = 14; break;
case 32768: value_bits = 15; break;
}
}
void clock_source(const avrlib::timer_clock_source& prescaler)
{
if (prescaler == timer_no_clock)
m_timer.CTRLA = 0;
else
m_timer.CTRLA = (((prescaler - 1) & 7)<<1) | TCA_SINGLE_ENABLE_bm;
}
uint8_t overflow() { return m_timer.INTFLAGS & TCA_SINGLE_OVF_bm; }
void clear_overflow() { m_timer.INTFLAGS = TCA_SINGLE_OVF_bm; }
void tov_interrupt(uint8_t v)
{
if(v)
{
m_timer.INTFLAGS = TCA_SINGLE_OVF_bm;
m_timer.INTCTRL |= TCA_SINGLE_OVF_bm;
}
else
m_timer.INTCTRL &= ~TCA_SINGLE_OVF_bm;
}
volatile uint8_t value_bits;
private:
TCA_SINGLE_t& m_timer;
};
} // namespace avrlib
#endif // AVRLIB_TIYN1_TIMER_A