1 | #include "TimerThree.h"
|
2 |
|
3 | TimerThree Timer3; // preinstatiate
|
4 |
|
5 | ISR(TIMER3_OVF_vect) // interrupt service routine that wraps a user defined function supplied by attachInterrupt
|
6 | {
|
7 | Timer3.isrCallback();
|
8 | }
|
9 |
|
10 | void TimerThree::initialize(long microseconds)
|
11 | {
|
12 | TCCR3A = 0; // clear control register A
|
13 | TCCR3B = _BV(WGM13); // set mode as phase and frequency correct pwm, stop the timer
|
14 | setPeriod(microseconds);
|
15 | }
|
16 |
|
17 | void TimerThree::setPeriod(long microseconds)
|
18 | {
|
19 | long cycles = (F_CPU * microseconds) / 2000000; // the counter runs backwards after TOP, interrupt is at BOTTOM so divide microseconds by 2
|
20 | if(cycles < RESOLUTION) clockSelectBits = _BV(CS10); // no prescale, full xtal
|
21 | else if((cycles >>= 3) < RESOLUTION) clockSelectBits = _BV(CS11); // prescale by /8
|
22 | else if((cycles >>= 3) < RESOLUTION) clockSelectBits = _BV(CS11) | _BV(CS10); // prescale by /64
|
23 | else if((cycles >>= 2) < RESOLUTION) clockSelectBits = _BV(CS12); // prescale by /256
|
24 | else if((cycles >>= 2) < RESOLUTION) clockSelectBits = _BV(CS12) | _BV(CS10); // prescale by /1024
|
25 | else cycles = RESOLUTION - 1, clockSelectBits = _BV(CS12) | _BV(CS10); // request was out of bounds, set as maximum
|
26 | ICR3 = pwmPeriod = cycles; // ICR1 is TOP in p & f correct pwm mode
|
27 | TCCR3B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
|
28 | TCCR3B |= clockSelectBits; // reset clock select register
|
29 | }
|
30 |
|
31 | void TimerThree::setPwmDuty(char pin, int duty)
|
32 | {
|
33 | unsigned long dutyCycle = pwmPeriod;
|
34 | dutyCycle *= duty;
|
35 | dutyCycle >>= 10;
|
36 | if(pin == 5) OCR3A = dutyCycle;
|
37 | if(pin == 2) OCR3B = dutyCycle;
|
38 | if(pin == 3) OCR3C = dutyCycle;
|
39 | }
|
40 |
|
41 | void TimerThree::pwm(char pin, int duty, long microseconds) // expects duty cycle to be 10 bit (1024)
|
42 | {
|
43 | if(microseconds > 0) setPeriod(microseconds);
|
44 |
|
45 | // sets data direction register for pwm output pin
|
46 | // activates the output pin
|
47 | if(pin == 5) { DDRE |= _BV(PORTE3); TCCR3A |= _BV(COM3A1); }
|
48 | if(pin == 2) { DDRE |= _BV(PORTE4); TCCR3A |= _BV(COM3B1); }
|
49 | if(pin == 3) { DDRE |= _BV(PORTE5); TCCR3A |= _BV(COM3C1); }
|
50 | setPwmDuty(pin, duty);
|
51 | start();
|
52 | }
|
53 |
|
54 | void TimerThree::disablePwm(char pin)
|
55 | {
|
56 | if(pin == 5) TCCR3A &= ~_BV(COM3A1); // clear the bit that enables pwm on PE3
|
57 | if(pin == 2) TCCR3A &= ~_BV(COM3B1); // clear the bit that enables pwm on PE4
|
58 | if(pin == 3) TCCR3A &= ~_BV(COM3C1); // clear the bit that enables pwm on PE5
|
59 | }
|
60 |
|
61 | void TimerThree::attachInterrupt(void (*isr)(), long microseconds)
|
62 | {
|
63 | if(microseconds > 0) setPeriod(microseconds);
|
64 | isrCallback = isr; // register the user's callback with the real ISR
|
65 | TIMSK3 = _BV(TOIE1); // sets the timer overflow interrupt enable bit
|
66 | sei(); // ensures that interrupts are globally enabled
|
67 | start();
|
68 | }
|
69 |
|
70 | void TimerThree::detachInterrupt()
|
71 | {
|
72 | TIMSK3 &= ~_BV(TOIE1); // clears the timer overflow interrupt enable bit
|
73 | }
|
74 |
|
75 | void TimerThree::start()
|
76 | {
|
77 | TCCR3B |= clockSelectBits;
|
78 | }
|
79 |
|
80 | void TimerThree::stop()
|
81 | {
|
82 | TCCR3B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12)); // clears all clock selects bits
|
83 | }
|
84 |
|
85 | void TimerThree::restart()
|
86 | {
|
87 | TCNT3 = 0;
|
88 | }
|