forked from mtakala/megaprinter_tempdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoftPWM_timer.h
57 lines (47 loc) · 1.97 KB
/
SoftPWM_timer.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
/* $Id: SoftPWM_timer.h 116 2010-06-28 23:31:02Z [email protected] $
A Software PWM Library
Simple timer abstractions by Paul Stoffregen (paul at pjrc dot com)
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
// allow certain chips to use different timers
#if defined(__AVR_ATmega32U4__)
#define USE_TIMER4_HS // Teensy 2.0 lacks timer2, but has high speed timer4 :-)
#else
#define USE_TIMER2
#endif
// for each timer, these macros define how to actually use it
#if defined(USE_TIMER2)
#define SOFTPWM_TIMER_INTERRUPT TIMER2_COMPA_vect
#define SOFTPWM_TIMER_SET(val) (TCNT2 = (val))
#define SOFTPWM_TIMER_INIT(ocr) ({\
TIFR2 = (1 << TOV2); /* clear interrupt flag */ \
TCCR2B = (1 << CS21); /* start timer (ck/8 prescalar) */ \
TCCR2A = (1 << WGM21); /* CTC mode */ \
OCR2A = (ocr); /* We want to have at least 30Hz or else it gets choppy */ \
TIMSK2 = (1 << OCIE2A); /* enable timer2 output compare match interrupt */ \
})
#elif defined(USE_TIMER4_HS)
#define SOFTPWM_TIMER_INTERRUPT TIMER4_COMPA_vect
#define SOFTPWM_TIMER_SET(val) (TCNT4 = (val))
#define SOFTPWM_TIMER_INIT(ocr) ({\
TCCR4A = 0; \
TCCR4B = 0x04; /* CTC Mode */\
TCCR4C = 0; \
TCCR4D = 0; \
TCCR4E = 0; \
OCR4C = 0; \
OCR4C = (ocr); \
TIMSK4 = (1 << OCIE4A); \
})
#endif