aboutsummaryrefslogtreecommitdiffstats
path: root/notes/timers.txt
blob: 3e6bb9c9bd99e37f3c6104222307d58a2e59709c (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
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
77

Each timer (1-4) has 4 capture/compare channels (1-4). These are directly used
by PWM but have a ton of other possible functionality. The STM32 implementation
is particularly complicated with, eg, the ability to chain together timers

Timer1 is an "advanced timer" with many more features. I think if we use just
the "Capture and compare interrupt", and enable MOE during initialization
everything will be ok. There are seperate Break, Update, and Trigger interrupts
as well that we will ignore for now.

Timer2,Ch 3+4 are D0 and D1, which conflict with Serial2. USART should work
fine as long as pins aren't in output mode? and timers should work fine if
Serial2 isn't in use?

Misc Notes
------------------------------------------------------------------------------
implementation with case/switch in the interrupt handlers doesn't work; a lot
of the time multiple interrupt flags are active at the same time (or become
active?)

TODO
------------------------------------------------------------------------------
- function to read out CCR registers
- allow comparison output to the pin (a la PWM)
- additional modes and configuration (up, down, up/down, etc)
- Wirish (C++) higher level implementation
- implement the update interrupt as a "5th channel"
- track down and handle all pin conflicts
- document

Possible Wirish implementation
------------------------------------------------------------------------------
This recent implementation seems pretty clean:
http://arduino.cc/pipermail/developers_arduino.cc/2010-June/002845.html

        class Timer1Class {
        public:
            static void (*isrCompareA)(void);
            static void (*isrCompareB)(void);

            static const uint8_t PRESCALE1 = 1;
            static const uint8_t PRESCALE8 = 2;
            static const uint8_t PRESCALE64 = 3;
            static const uint8_t PRESCALE256 = 4;
            static const uint8_t PRESCALE1024 = 5;

            const static uint8_t NORMAL = 0;
            const static uint8_t CTC = 4;

            void setPrescaleFactor(uint8_t factor);
            void setMode(uint8_t mode);
            uint16_t read();
            void write(uint16_t val);
            void writeCompareA(uint16_t val);
            void writeCompareB(uint16_t val);
            void attachCompareAInterrupt(void (*f)(void));
            void attachCompareBInterrupt(void (*f)(void));
            void detachCompareAInterrupt();
            void detachCompareBInterrupt();
        };

        extern Timer1Class Timer1;

Here's one of the more standard libaries out there:
http://www.arduino.cc/playground/Code/Timer1

        void initialize(long microseconds=1000000);
        void start();
        void stop();
        void restart();
        void setPeriod(long microseconds);
        void pwm(char pin, int duty, long microseconds=-1);
        void setPwmDuty(char pin, int duty);
        void disablePwm(char pin);
        void attachInterrupt(void (*isr)(), long microseconds=-1);
        void detachInterrupt();