Infrared remote library for Arduino: send and receive infrared signals with multiple protocols Port from Arduino-IRremote https://github.com/z3t0/Arduino-IRremote
Dependents: Lilnija_29012017 NucleoF042K6_IRReceiver
IRremoteInt.h@0:70c8e56bac45, 2016-01-23 (annotated)
- Committer:
- yuhki50
- Date:
- Sat Jan 23 06:16:48 2016 +0000
- Revision:
- 0:70c8e56bac45
- Child:
- 3:17440cf7ab90
import https://github.com/z3t0/Arduino-IRremote e3ec11d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yuhki50 | 0:70c8e56bac45 | 1 | //****************************************************************************** |
yuhki50 | 0:70c8e56bac45 | 2 | // IRremote |
yuhki50 | 0:70c8e56bac45 | 3 | // Version 2.0.1 June, 2015 |
yuhki50 | 0:70c8e56bac45 | 4 | // Copyright 2009 Ken Shirriff |
yuhki50 | 0:70c8e56bac45 | 5 | // For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html |
yuhki50 | 0:70c8e56bac45 | 6 | // |
yuhki50 | 0:70c8e56bac45 | 7 | // Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers |
yuhki50 | 0:70c8e56bac45 | 8 | // |
yuhki50 | 0:70c8e56bac45 | 9 | // Interrupt code based on NECIRrcv by Joe Knapp |
yuhki50 | 0:70c8e56bac45 | 10 | // http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 |
yuhki50 | 0:70c8e56bac45 | 11 | // Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ |
yuhki50 | 0:70c8e56bac45 | 12 | // |
yuhki50 | 0:70c8e56bac45 | 13 | // JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post) |
yuhki50 | 0:70c8e56bac45 | 14 | // Whynter A/C ARC-110WD added by Francesco Meschia |
yuhki50 | 0:70c8e56bac45 | 15 | //****************************************************************************** |
yuhki50 | 0:70c8e56bac45 | 16 | |
yuhki50 | 0:70c8e56bac45 | 17 | #ifndef IRremoteint_h |
yuhki50 | 0:70c8e56bac45 | 18 | #define IRremoteint_h |
yuhki50 | 0:70c8e56bac45 | 19 | |
yuhki50 | 0:70c8e56bac45 | 20 | //------------------------------------------------------------------------------ |
yuhki50 | 0:70c8e56bac45 | 21 | // Include the right Arduino header |
yuhki50 | 0:70c8e56bac45 | 22 | // |
yuhki50 | 0:70c8e56bac45 | 23 | #if defined(ARDUINO) && (ARDUINO >= 100) |
yuhki50 | 0:70c8e56bac45 | 24 | # include <Arduino.h> |
yuhki50 | 0:70c8e56bac45 | 25 | #else |
yuhki50 | 0:70c8e56bac45 | 26 | # if !defined(IRPRONTO) |
yuhki50 | 0:70c8e56bac45 | 27 | # include <WProgram.h> |
yuhki50 | 0:70c8e56bac45 | 28 | # endif |
yuhki50 | 0:70c8e56bac45 | 29 | #endif |
yuhki50 | 0:70c8e56bac45 | 30 | |
yuhki50 | 0:70c8e56bac45 | 31 | //------------------------------------------------------------------------------ |
yuhki50 | 0:70c8e56bac45 | 32 | // This handles definition and access to global variables |
yuhki50 | 0:70c8e56bac45 | 33 | // |
yuhki50 | 0:70c8e56bac45 | 34 | #ifdef IR_GLOBAL |
yuhki50 | 0:70c8e56bac45 | 35 | # define EXTERN |
yuhki50 | 0:70c8e56bac45 | 36 | #else |
yuhki50 | 0:70c8e56bac45 | 37 | # define EXTERN extern |
yuhki50 | 0:70c8e56bac45 | 38 | #endif |
yuhki50 | 0:70c8e56bac45 | 39 | |
yuhki50 | 0:70c8e56bac45 | 40 | //------------------------------------------------------------------------------ |
yuhki50 | 0:70c8e56bac45 | 41 | // Information for the Interrupt Service Routine |
yuhki50 | 0:70c8e56bac45 | 42 | // |
yuhki50 | 0:70c8e56bac45 | 43 | #define RAWBUF 101 // Maximum length of raw duration buffer |
yuhki50 | 0:70c8e56bac45 | 44 | |
yuhki50 | 0:70c8e56bac45 | 45 | typedef |
yuhki50 | 0:70c8e56bac45 | 46 | struct { |
yuhki50 | 0:70c8e56bac45 | 47 | // The fields are ordered to reduce memory over caused by struct-padding |
yuhki50 | 0:70c8e56bac45 | 48 | uint8_t rcvstate; // State Machine state |
yuhki50 | 0:70c8e56bac45 | 49 | uint8_t recvpin; // Pin connected to IR data from detector |
yuhki50 | 0:70c8e56bac45 | 50 | uint8_t blinkpin; |
yuhki50 | 0:70c8e56bac45 | 51 | uint8_t blinkflag; // true -> enable blinking of pin on IR processing |
yuhki50 | 0:70c8e56bac45 | 52 | uint8_t rawlen; // counter of entries in rawbuf |
yuhki50 | 0:70c8e56bac45 | 53 | unsigned int timer; // State timer, counts 50uS ticks. |
yuhki50 | 0:70c8e56bac45 | 54 | unsigned int rawbuf[RAWBUF]; // raw data |
yuhki50 | 0:70c8e56bac45 | 55 | uint8_t overflow; // Raw buffer overflow occurred |
yuhki50 | 0:70c8e56bac45 | 56 | } |
yuhki50 | 0:70c8e56bac45 | 57 | irparams_t; |
yuhki50 | 0:70c8e56bac45 | 58 | |
yuhki50 | 0:70c8e56bac45 | 59 | // ISR State-Machine : Receiver States |
yuhki50 | 0:70c8e56bac45 | 60 | #define STATE_IDLE 2 |
yuhki50 | 0:70c8e56bac45 | 61 | #define STATE_MARK 3 |
yuhki50 | 0:70c8e56bac45 | 62 | #define STATE_SPACE 4 |
yuhki50 | 0:70c8e56bac45 | 63 | #define STATE_STOP 5 |
yuhki50 | 0:70c8e56bac45 | 64 | #define STATE_OVERFLOW 6 |
yuhki50 | 0:70c8e56bac45 | 65 | |
yuhki50 | 0:70c8e56bac45 | 66 | // Allow all parts of the code access to the ISR data |
yuhki50 | 0:70c8e56bac45 | 67 | // NB. The data can be changed by the ISR at any time, even mid-function |
yuhki50 | 0:70c8e56bac45 | 68 | // Therefore we declare it as "volatile" to stop the compiler/CPU caching it |
yuhki50 | 0:70c8e56bac45 | 69 | EXTERN volatile irparams_t irparams; |
yuhki50 | 0:70c8e56bac45 | 70 | |
yuhki50 | 0:70c8e56bac45 | 71 | //------------------------------------------------------------------------------ |
yuhki50 | 0:70c8e56bac45 | 72 | // Defines for blinking the LED |
yuhki50 | 0:70c8e56bac45 | 73 | // |
yuhki50 | 0:70c8e56bac45 | 74 | |
yuhki50 | 0:70c8e56bac45 | 75 | #if defined(CORE_LED0_PIN) |
yuhki50 | 0:70c8e56bac45 | 76 | # define BLINKLED CORE_LED0_PIN |
yuhki50 | 0:70c8e56bac45 | 77 | # define BLINKLED_ON() (digitalWrite(CORE_LED0_PIN, HIGH)) |
yuhki50 | 0:70c8e56bac45 | 78 | # define BLINKLED_OFF() (digitalWrite(CORE_LED0_PIN, LOW)) |
yuhki50 | 0:70c8e56bac45 | 79 | |
yuhki50 | 0:70c8e56bac45 | 80 | #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) |
yuhki50 | 0:70c8e56bac45 | 81 | # define BLINKLED 13 |
yuhki50 | 0:70c8e56bac45 | 82 | # define BLINKLED_ON() (PORTB |= B10000000) |
yuhki50 | 0:70c8e56bac45 | 83 | # define BLINKLED_OFF() (PORTB &= B01111111) |
yuhki50 | 0:70c8e56bac45 | 84 | |
yuhki50 | 0:70c8e56bac45 | 85 | #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) |
yuhki50 | 0:70c8e56bac45 | 86 | # define BLINKLED 0 |
yuhki50 | 0:70c8e56bac45 | 87 | # define BLINKLED_ON() (PORTD |= B00000001) |
yuhki50 | 0:70c8e56bac45 | 88 | # define BLINKLED_OFF() (PORTD &= B11111110) |
yuhki50 | 0:70c8e56bac45 | 89 | |
yuhki50 | 0:70c8e56bac45 | 90 | #else |
yuhki50 | 0:70c8e56bac45 | 91 | # define BLINKLED 13 |
yuhki50 | 0:70c8e56bac45 | 92 | #define BLINKLED_ON() (PORTB |= B00100000) |
yuhki50 | 0:70c8e56bac45 | 93 | # define BLINKLED_OFF() (PORTB &= B11011111) |
yuhki50 | 0:70c8e56bac45 | 94 | #endif |
yuhki50 | 0:70c8e56bac45 | 95 | |
yuhki50 | 0:70c8e56bac45 | 96 | //------------------------------------------------------------------------------ |
yuhki50 | 0:70c8e56bac45 | 97 | // CPU Frequency |
yuhki50 | 0:70c8e56bac45 | 98 | // |
yuhki50 | 0:70c8e56bac45 | 99 | #ifdef F_CPU |
yuhki50 | 0:70c8e56bac45 | 100 | # define SYSCLOCK F_CPU // main Arduino clock |
yuhki50 | 0:70c8e56bac45 | 101 | #else |
yuhki50 | 0:70c8e56bac45 | 102 | # define SYSCLOCK 16000000 // main Arduino clock |
yuhki50 | 0:70c8e56bac45 | 103 | #endif |
yuhki50 | 0:70c8e56bac45 | 104 | |
yuhki50 | 0:70c8e56bac45 | 105 | //------------------------------------------------------------------------------ |
yuhki50 | 0:70c8e56bac45 | 106 | // Defines for setting and clearing register bits |
yuhki50 | 0:70c8e56bac45 | 107 | // |
yuhki50 | 0:70c8e56bac45 | 108 | #ifndef cbi |
yuhki50 | 0:70c8e56bac45 | 109 | # define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) |
yuhki50 | 0:70c8e56bac45 | 110 | #endif |
yuhki50 | 0:70c8e56bac45 | 111 | |
yuhki50 | 0:70c8e56bac45 | 112 | #ifndef sbi |
yuhki50 | 0:70c8e56bac45 | 113 | # define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) |
yuhki50 | 0:70c8e56bac45 | 114 | #endif |
yuhki50 | 0:70c8e56bac45 | 115 | |
yuhki50 | 0:70c8e56bac45 | 116 | //------------------------------------------------------------------------------ |
yuhki50 | 0:70c8e56bac45 | 117 | // Pulse parms are ((X*50)-100) for the Mark and ((X*50)+100) for the Space. |
yuhki50 | 0:70c8e56bac45 | 118 | // First MARK is the one after the long gap |
yuhki50 | 0:70c8e56bac45 | 119 | // Pulse parameters in uSec |
yuhki50 | 0:70c8e56bac45 | 120 | // |
yuhki50 | 0:70c8e56bac45 | 121 | |
yuhki50 | 0:70c8e56bac45 | 122 | // Due to sensor lag, when received, Marks tend to be 100us too long and |
yuhki50 | 0:70c8e56bac45 | 123 | // Spaces tend to be 100us too short |
yuhki50 | 0:70c8e56bac45 | 124 | #define MARK_EXCESS 100 |
yuhki50 | 0:70c8e56bac45 | 125 | |
yuhki50 | 0:70c8e56bac45 | 126 | // microseconds per clock interrupt tick |
yuhki50 | 0:70c8e56bac45 | 127 | #define USECPERTICK 50 |
yuhki50 | 0:70c8e56bac45 | 128 | |
yuhki50 | 0:70c8e56bac45 | 129 | // Upper and Lower percentage tolerances in measurements |
yuhki50 | 0:70c8e56bac45 | 130 | #define TOLERANCE 25 |
yuhki50 | 0:70c8e56bac45 | 131 | #define LTOL (1.0 - (TOLERANCE/100.)) |
yuhki50 | 0:70c8e56bac45 | 132 | #define UTOL (1.0 + (TOLERANCE/100.)) |
yuhki50 | 0:70c8e56bac45 | 133 | |
yuhki50 | 0:70c8e56bac45 | 134 | // Minimum gap between IR transmissions |
yuhki50 | 0:70c8e56bac45 | 135 | #define _GAP 5000 |
yuhki50 | 0:70c8e56bac45 | 136 | #define GAP_TICKS (_GAP/USECPERTICK) |
yuhki50 | 0:70c8e56bac45 | 137 | |
yuhki50 | 0:70c8e56bac45 | 138 | #define TICKS_LOW(us) ((int)(((us)*LTOL/USECPERTICK))) |
yuhki50 | 0:70c8e56bac45 | 139 | #define TICKS_HIGH(us) ((int)(((us)*UTOL/USECPERTICK + 1))) |
yuhki50 | 0:70c8e56bac45 | 140 | |
yuhki50 | 0:70c8e56bac45 | 141 | //------------------------------------------------------------------------------ |
yuhki50 | 0:70c8e56bac45 | 142 | // IR detector output is active low |
yuhki50 | 0:70c8e56bac45 | 143 | // |
yuhki50 | 0:70c8e56bac45 | 144 | #define MARK 0 |
yuhki50 | 0:70c8e56bac45 | 145 | #define SPACE 1 |
yuhki50 | 0:70c8e56bac45 | 146 | |
yuhki50 | 0:70c8e56bac45 | 147 | //------------------------------------------------------------------------------ |
yuhki50 | 0:70c8e56bac45 | 148 | // Define which timer to use |
yuhki50 | 0:70c8e56bac45 | 149 | // |
yuhki50 | 0:70c8e56bac45 | 150 | // Uncomment the timer you wish to use on your board. |
yuhki50 | 0:70c8e56bac45 | 151 | // If you are using another library which uses timer2, you have options to |
yuhki50 | 0:70c8e56bac45 | 152 | // switch IRremote to use a different timer. |
yuhki50 | 0:70c8e56bac45 | 153 | // |
yuhki50 | 0:70c8e56bac45 | 154 | |
yuhki50 | 0:70c8e56bac45 | 155 | // Arduino Mega |
yuhki50 | 0:70c8e56bac45 | 156 | #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) |
yuhki50 | 0:70c8e56bac45 | 157 | //#define IR_USE_TIMER1 // tx = pin 11 |
yuhki50 | 0:70c8e56bac45 | 158 | #define IR_USE_TIMER2 // tx = pin 9 |
yuhki50 | 0:70c8e56bac45 | 159 | //#define IR_USE_TIMER3 // tx = pin 5 |
yuhki50 | 0:70c8e56bac45 | 160 | //#define IR_USE_TIMER4 // tx = pin 6 |
yuhki50 | 0:70c8e56bac45 | 161 | //#define IR_USE_TIMER5 // tx = pin 46 |
yuhki50 | 0:70c8e56bac45 | 162 | |
yuhki50 | 0:70c8e56bac45 | 163 | // Teensy 1.0 |
yuhki50 | 0:70c8e56bac45 | 164 | #elif defined(__AVR_AT90USB162__) |
yuhki50 | 0:70c8e56bac45 | 165 | #define IR_USE_TIMER1 // tx = pin 17 |
yuhki50 | 0:70c8e56bac45 | 166 | |
yuhki50 | 0:70c8e56bac45 | 167 | // Teensy 2.0 |
yuhki50 | 0:70c8e56bac45 | 168 | #elif defined(__AVR_ATmega32U4__) |
yuhki50 | 0:70c8e56bac45 | 169 | //#define IR_USE_TIMER1 // tx = pin 14 |
yuhki50 | 0:70c8e56bac45 | 170 | //#define IR_USE_TIMER3 // tx = pin 9 |
yuhki50 | 0:70c8e56bac45 | 171 | #define IR_USE_TIMER4_HS // tx = pin 10 |
yuhki50 | 0:70c8e56bac45 | 172 | |
yuhki50 | 0:70c8e56bac45 | 173 | // Teensy 3.0 / Teensy 3.1 |
yuhki50 | 0:70c8e56bac45 | 174 | #elif defined(__MK20DX128__) || defined(__MK20DX256__) |
yuhki50 | 0:70c8e56bac45 | 175 | #define IR_USE_TIMER_CMT // tx = pin 5 |
yuhki50 | 0:70c8e56bac45 | 176 | |
yuhki50 | 0:70c8e56bac45 | 177 | // Teensy-LC |
yuhki50 | 0:70c8e56bac45 | 178 | #elif defined(__MKL26Z64__) |
yuhki50 | 0:70c8e56bac45 | 179 | #define IR_USE_TIMER_TPM1 // tx = pin 16 |
yuhki50 | 0:70c8e56bac45 | 180 | |
yuhki50 | 0:70c8e56bac45 | 181 | // Teensy++ 1.0 & 2.0 |
yuhki50 | 0:70c8e56bac45 | 182 | #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) |
yuhki50 | 0:70c8e56bac45 | 183 | //#define IR_USE_TIMER1 // tx = pin 25 |
yuhki50 | 0:70c8e56bac45 | 184 | #define IR_USE_TIMER2 // tx = pin 1 |
yuhki50 | 0:70c8e56bac45 | 185 | //#define IR_USE_TIMER3 // tx = pin 16 |
yuhki50 | 0:70c8e56bac45 | 186 | |
yuhki50 | 0:70c8e56bac45 | 187 | // Sanguino |
yuhki50 | 0:70c8e56bac45 | 188 | #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) |
yuhki50 | 0:70c8e56bac45 | 189 | //#define IR_USE_TIMER1 // tx = pin 13 |
yuhki50 | 0:70c8e56bac45 | 190 | #define IR_USE_TIMER2 // tx = pin 14 |
yuhki50 | 0:70c8e56bac45 | 191 | |
yuhki50 | 0:70c8e56bac45 | 192 | // Atmega8 |
yuhki50 | 0:70c8e56bac45 | 193 | #elif defined(__AVR_ATmega8P__) || defined(__AVR_ATmega8__) |
yuhki50 | 0:70c8e56bac45 | 194 | #define IR_USE_TIMER1 // tx = pin 9 |
yuhki50 | 0:70c8e56bac45 | 195 | |
yuhki50 | 0:70c8e56bac45 | 196 | // ATtiny84 |
yuhki50 | 0:70c8e56bac45 | 197 | #elif defined(__AVR_ATtiny84__) |
yuhki50 | 0:70c8e56bac45 | 198 | #define IR_USE_TIMER1 // tx = pin 6 |
yuhki50 | 0:70c8e56bac45 | 199 | |
yuhki50 | 0:70c8e56bac45 | 200 | //ATtiny85 |
yuhki50 | 0:70c8e56bac45 | 201 | #elif defined(__AVR_ATtiny85__) |
yuhki50 | 0:70c8e56bac45 | 202 | #define IR_USE_TIMER_TINY0 // tx = pin 1 |
yuhki50 | 0:70c8e56bac45 | 203 | |
yuhki50 | 0:70c8e56bac45 | 204 | // Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc |
yuhki50 | 0:70c8e56bac45 | 205 | #else |
yuhki50 | 0:70c8e56bac45 | 206 | //#define IR_USE_TIMER1 // tx = pin 9 |
yuhki50 | 0:70c8e56bac45 | 207 | #define IR_USE_TIMER2 // tx = pin 3 |
yuhki50 | 0:70c8e56bac45 | 208 | |
yuhki50 | 0:70c8e56bac45 | 209 | #endif |
yuhki50 | 0:70c8e56bac45 | 210 | |
yuhki50 | 0:70c8e56bac45 | 211 | //------------------------------------------------------------------------------ |
yuhki50 | 0:70c8e56bac45 | 212 | // Defines for Timer |
yuhki50 | 0:70c8e56bac45 | 213 | |
yuhki50 | 0:70c8e56bac45 | 214 | //--------------------------------------------------------- |
yuhki50 | 0:70c8e56bac45 | 215 | // Timer2 (8 bits) |
yuhki50 | 0:70c8e56bac45 | 216 | // |
yuhki50 | 0:70c8e56bac45 | 217 | #if defined(IR_USE_TIMER2) |
yuhki50 | 0:70c8e56bac45 | 218 | |
yuhki50 | 0:70c8e56bac45 | 219 | #define TIMER_RESET |
yuhki50 | 0:70c8e56bac45 | 220 | #define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1)) |
yuhki50 | 0:70c8e56bac45 | 221 | #define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1))) |
yuhki50 | 0:70c8e56bac45 | 222 | #define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A)) |
yuhki50 | 0:70c8e56bac45 | 223 | #define TIMER_DISABLE_INTR (TIMSK2 = 0) |
yuhki50 | 0:70c8e56bac45 | 224 | #define TIMER_INTR_NAME TIMER2_COMPA_vect |
yuhki50 | 0:70c8e56bac45 | 225 | |
yuhki50 | 0:70c8e56bac45 | 226 | #define TIMER_CONFIG_KHZ(val) ({ \ |
yuhki50 | 0:70c8e56bac45 | 227 | const uint8_t pwmval = SYSCLOCK / 2000 / (val); \ |
yuhki50 | 0:70c8e56bac45 | 228 | TCCR2A = _BV(WGM20); \ |
yuhki50 | 0:70c8e56bac45 | 229 | TCCR2B = _BV(WGM22) | _BV(CS20); \ |
yuhki50 | 0:70c8e56bac45 | 230 | OCR2A = pwmval; \ |
yuhki50 | 0:70c8e56bac45 | 231 | OCR2B = pwmval / 3; \ |
yuhki50 | 0:70c8e56bac45 | 232 | }) |
yuhki50 | 0:70c8e56bac45 | 233 | |
yuhki50 | 0:70c8e56bac45 | 234 | #define TIMER_COUNT_TOP (SYSCLOCK * USECPERTICK / 1000000) |
yuhki50 | 0:70c8e56bac45 | 235 | |
yuhki50 | 0:70c8e56bac45 | 236 | //----------------- |
yuhki50 | 0:70c8e56bac45 | 237 | #if (TIMER_COUNT_TOP < 256) |
yuhki50 | 0:70c8e56bac45 | 238 | # define TIMER_CONFIG_NORMAL() ({ \ |
yuhki50 | 0:70c8e56bac45 | 239 | TCCR2A = _BV(WGM21); \ |
yuhki50 | 0:70c8e56bac45 | 240 | TCCR2B = _BV(CS20); \ |
yuhki50 | 0:70c8e56bac45 | 241 | OCR2A = TIMER_COUNT_TOP; \ |
yuhki50 | 0:70c8e56bac45 | 242 | TCNT2 = 0; \ |
yuhki50 | 0:70c8e56bac45 | 243 | }) |
yuhki50 | 0:70c8e56bac45 | 244 | #else |
yuhki50 | 0:70c8e56bac45 | 245 | # define TIMER_CONFIG_NORMAL() ({ \ |
yuhki50 | 0:70c8e56bac45 | 246 | TCCR2A = _BV(WGM21); \ |
yuhki50 | 0:70c8e56bac45 | 247 | TCCR2B = _BV(CS21); \ |
yuhki50 | 0:70c8e56bac45 | 248 | OCR2A = TIMER_COUNT_TOP / 8; \ |
yuhki50 | 0:70c8e56bac45 | 249 | TCNT2 = 0; \ |
yuhki50 | 0:70c8e56bac45 | 250 | }) |
yuhki50 | 0:70c8e56bac45 | 251 | #endif |
yuhki50 | 0:70c8e56bac45 | 252 | |
yuhki50 | 0:70c8e56bac45 | 253 | //----------------- |
yuhki50 | 0:70c8e56bac45 | 254 | #if defined(CORE_OC2B_PIN) |
yuhki50 | 0:70c8e56bac45 | 255 | # define TIMER_PWM_PIN CORE_OC2B_PIN // Teensy |
yuhki50 | 0:70c8e56bac45 | 256 | #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) |
yuhki50 | 0:70c8e56bac45 | 257 | # define TIMER_PWM_PIN 9 // Arduino Mega |
yuhki50 | 0:70c8e56bac45 | 258 | #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) |
yuhki50 | 0:70c8e56bac45 | 259 | # define TIMER_PWM_PIN 14 // Sanguino |
yuhki50 | 0:70c8e56bac45 | 260 | #else |
yuhki50 | 0:70c8e56bac45 | 261 | # define TIMER_PWM_PIN 3 // Arduino Duemilanove, Diecimila, LilyPad, etc |
yuhki50 | 0:70c8e56bac45 | 262 | #endif |
yuhki50 | 0:70c8e56bac45 | 263 | |
yuhki50 | 0:70c8e56bac45 | 264 | //--------------------------------------------------------- |
yuhki50 | 0:70c8e56bac45 | 265 | // Timer1 (16 bits) |
yuhki50 | 0:70c8e56bac45 | 266 | // |
yuhki50 | 0:70c8e56bac45 | 267 | #elif defined(IR_USE_TIMER1) |
yuhki50 | 0:70c8e56bac45 | 268 | |
yuhki50 | 0:70c8e56bac45 | 269 | #define TIMER_RESET |
yuhki50 | 0:70c8e56bac45 | 270 | #define TIMER_ENABLE_PWM (TCCR1A |= _BV(COM1A1)) |
yuhki50 | 0:70c8e56bac45 | 271 | #define TIMER_DISABLE_PWM (TCCR1A &= ~(_BV(COM1A1))) |
yuhki50 | 0:70c8e56bac45 | 272 | |
yuhki50 | 0:70c8e56bac45 | 273 | //----------------- |
yuhki50 | 0:70c8e56bac45 | 274 | #if defined(__AVR_ATmega8P__) || defined(__AVR_ATmega8__) |
yuhki50 | 0:70c8e56bac45 | 275 | # define TIMER_ENABLE_INTR (TIMSK |= _BV(OCIE1A)) |
yuhki50 | 0:70c8e56bac45 | 276 | # define TIMER_DISABLE_INTR (TIMSK &= ~_BV(OCIE1A)) |
yuhki50 | 0:70c8e56bac45 | 277 | #else |
yuhki50 | 0:70c8e56bac45 | 278 | # define TIMER_ENABLE_INTR (TIMSK1 = _BV(OCIE1A)) |
yuhki50 | 0:70c8e56bac45 | 279 | # define TIMER_DISABLE_INTR (TIMSK1 = 0) |
yuhki50 | 0:70c8e56bac45 | 280 | #endif |
yuhki50 | 0:70c8e56bac45 | 281 | |
yuhki50 | 0:70c8e56bac45 | 282 | //----------------- |
yuhki50 | 0:70c8e56bac45 | 283 | #define TIMER_INTR_NAME TIMER1_COMPA_vect |
yuhki50 | 0:70c8e56bac45 | 284 | |
yuhki50 | 0:70c8e56bac45 | 285 | #define TIMER_CONFIG_KHZ(val) ({ \ |
yuhki50 | 0:70c8e56bac45 | 286 | const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ |
yuhki50 | 0:70c8e56bac45 | 287 | TCCR1A = _BV(WGM11); \ |
yuhki50 | 0:70c8e56bac45 | 288 | TCCR1B = _BV(WGM13) | _BV(CS10); \ |
yuhki50 | 0:70c8e56bac45 | 289 | ICR1 = pwmval; \ |
yuhki50 | 0:70c8e56bac45 | 290 | OCR1A = pwmval / 3; \ |
yuhki50 | 0:70c8e56bac45 | 291 | }) |
yuhki50 | 0:70c8e56bac45 | 292 | |
yuhki50 | 0:70c8e56bac45 | 293 | #define TIMER_CONFIG_NORMAL() ({ \ |
yuhki50 | 0:70c8e56bac45 | 294 | TCCR1A = 0; \ |
yuhki50 | 0:70c8e56bac45 | 295 | TCCR1B = _BV(WGM12) | _BV(CS10); \ |
yuhki50 | 0:70c8e56bac45 | 296 | OCR1A = SYSCLOCK * USECPERTICK / 1000000; \ |
yuhki50 | 0:70c8e56bac45 | 297 | TCNT1 = 0; \ |
yuhki50 | 0:70c8e56bac45 | 298 | }) |
yuhki50 | 0:70c8e56bac45 | 299 | |
yuhki50 | 0:70c8e56bac45 | 300 | //----------------- |
yuhki50 | 0:70c8e56bac45 | 301 | #if defined(CORE_OC1A_PIN) |
yuhki50 | 0:70c8e56bac45 | 302 | # define TIMER_PWM_PIN CORE_OC1A_PIN // Teensy |
yuhki50 | 0:70c8e56bac45 | 303 | #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) |
yuhki50 | 0:70c8e56bac45 | 304 | # define TIMER_PWM_PIN 11 // Arduino Mega |
yuhki50 | 0:70c8e56bac45 | 305 | #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) |
yuhki50 | 0:70c8e56bac45 | 306 | # define TIMER_PWM_PIN 13 // Sanguino |
yuhki50 | 0:70c8e56bac45 | 307 | #elif defined(__AVR_ATtiny84__) |
yuhki50 | 0:70c8e56bac45 | 308 | # define TIMER_PWM_PIN 6 |
yuhki50 | 0:70c8e56bac45 | 309 | #else |
yuhki50 | 0:70c8e56bac45 | 310 | # define TIMER_PWM_PIN 9 // Arduino Duemilanove, Diecimila, LilyPad, etc |
yuhki50 | 0:70c8e56bac45 | 311 | #endif |
yuhki50 | 0:70c8e56bac45 | 312 | |
yuhki50 | 0:70c8e56bac45 | 313 | //--------------------------------------------------------- |
yuhki50 | 0:70c8e56bac45 | 314 | // Timer3 (16 bits) |
yuhki50 | 0:70c8e56bac45 | 315 | // |
yuhki50 | 0:70c8e56bac45 | 316 | #elif defined(IR_USE_TIMER3) |
yuhki50 | 0:70c8e56bac45 | 317 | |
yuhki50 | 0:70c8e56bac45 | 318 | #define TIMER_RESET |
yuhki50 | 0:70c8e56bac45 | 319 | #define TIMER_ENABLE_PWM (TCCR3A |= _BV(COM3A1)) |
yuhki50 | 0:70c8e56bac45 | 320 | #define TIMER_DISABLE_PWM (TCCR3A &= ~(_BV(COM3A1))) |
yuhki50 | 0:70c8e56bac45 | 321 | #define TIMER_ENABLE_INTR (TIMSK3 = _BV(OCIE3A)) |
yuhki50 | 0:70c8e56bac45 | 322 | #define TIMER_DISABLE_INTR (TIMSK3 = 0) |
yuhki50 | 0:70c8e56bac45 | 323 | #define TIMER_INTR_NAME TIMER3_COMPA_vect |
yuhki50 | 0:70c8e56bac45 | 324 | |
yuhki50 | 0:70c8e56bac45 | 325 | #define TIMER_CONFIG_KHZ(val) ({ \ |
yuhki50 | 0:70c8e56bac45 | 326 | const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ |
yuhki50 | 0:70c8e56bac45 | 327 | TCCR3A = _BV(WGM31); \ |
yuhki50 | 0:70c8e56bac45 | 328 | TCCR3B = _BV(WGM33) | _BV(CS30); \ |
yuhki50 | 0:70c8e56bac45 | 329 | ICR3 = pwmval; \ |
yuhki50 | 0:70c8e56bac45 | 330 | OCR3A = pwmval / 3; \ |
yuhki50 | 0:70c8e56bac45 | 331 | }) |
yuhki50 | 0:70c8e56bac45 | 332 | |
yuhki50 | 0:70c8e56bac45 | 333 | #define TIMER_CONFIG_NORMAL() ({ \ |
yuhki50 | 0:70c8e56bac45 | 334 | TCCR3A = 0; \ |
yuhki50 | 0:70c8e56bac45 | 335 | TCCR3B = _BV(WGM32) | _BV(CS30); \ |
yuhki50 | 0:70c8e56bac45 | 336 | OCR3A = SYSCLOCK * USECPERTICK / 1000000; \ |
yuhki50 | 0:70c8e56bac45 | 337 | TCNT3 = 0; \ |
yuhki50 | 0:70c8e56bac45 | 338 | }) |
yuhki50 | 0:70c8e56bac45 | 339 | |
yuhki50 | 0:70c8e56bac45 | 340 | //----------------- |
yuhki50 | 0:70c8e56bac45 | 341 | #if defined(CORE_OC3A_PIN) |
yuhki50 | 0:70c8e56bac45 | 342 | # define TIMER_PWM_PIN CORE_OC3A_PIN // Teensy |
yuhki50 | 0:70c8e56bac45 | 343 | #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) |
yuhki50 | 0:70c8e56bac45 | 344 | # define TIMER_PWM_PIN 5 // Arduino Mega |
yuhki50 | 0:70c8e56bac45 | 345 | #else |
yuhki50 | 0:70c8e56bac45 | 346 | # error "Please add OC3A pin number here\n" |
yuhki50 | 0:70c8e56bac45 | 347 | #endif |
yuhki50 | 0:70c8e56bac45 | 348 | |
yuhki50 | 0:70c8e56bac45 | 349 | //--------------------------------------------------------- |
yuhki50 | 0:70c8e56bac45 | 350 | // Timer4 (10 bits, high speed option) |
yuhki50 | 0:70c8e56bac45 | 351 | // |
yuhki50 | 0:70c8e56bac45 | 352 | #elif defined(IR_USE_TIMER4_HS) |
yuhki50 | 0:70c8e56bac45 | 353 | |
yuhki50 | 0:70c8e56bac45 | 354 | #define TIMER_RESET |
yuhki50 | 0:70c8e56bac45 | 355 | #define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1)) |
yuhki50 | 0:70c8e56bac45 | 356 | #define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1))) |
yuhki50 | 0:70c8e56bac45 | 357 | #define TIMER_ENABLE_INTR (TIMSK4 = _BV(TOIE4)) |
yuhki50 | 0:70c8e56bac45 | 358 | #define TIMER_DISABLE_INTR (TIMSK4 = 0) |
yuhki50 | 0:70c8e56bac45 | 359 | #define TIMER_INTR_NAME TIMER4_OVF_vect |
yuhki50 | 0:70c8e56bac45 | 360 | |
yuhki50 | 0:70c8e56bac45 | 361 | #define TIMER_CONFIG_KHZ(val) ({ \ |
yuhki50 | 0:70c8e56bac45 | 362 | const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ |
yuhki50 | 0:70c8e56bac45 | 363 | TCCR4A = (1<<PWM4A); \ |
yuhki50 | 0:70c8e56bac45 | 364 | TCCR4B = _BV(CS40); \ |
yuhki50 | 0:70c8e56bac45 | 365 | TCCR4C = 0; \ |
yuhki50 | 0:70c8e56bac45 | 366 | TCCR4D = (1<<WGM40); \ |
yuhki50 | 0:70c8e56bac45 | 367 | TCCR4E = 0; \ |
yuhki50 | 0:70c8e56bac45 | 368 | TC4H = pwmval >> 8; \ |
yuhki50 | 0:70c8e56bac45 | 369 | OCR4C = pwmval; \ |
yuhki50 | 0:70c8e56bac45 | 370 | TC4H = (pwmval / 3) >> 8; \ |
yuhki50 | 0:70c8e56bac45 | 371 | OCR4A = (pwmval / 3) & 255; \ |
yuhki50 | 0:70c8e56bac45 | 372 | }) |
yuhki50 | 0:70c8e56bac45 | 373 | |
yuhki50 | 0:70c8e56bac45 | 374 | #define TIMER_CONFIG_NORMAL() ({ \ |
yuhki50 | 0:70c8e56bac45 | 375 | TCCR4A = 0; \ |
yuhki50 | 0:70c8e56bac45 | 376 | TCCR4B = _BV(CS40); \ |
yuhki50 | 0:70c8e56bac45 | 377 | TCCR4C = 0; \ |
yuhki50 | 0:70c8e56bac45 | 378 | TCCR4D = 0; \ |
yuhki50 | 0:70c8e56bac45 | 379 | TCCR4E = 0; \ |
yuhki50 | 0:70c8e56bac45 | 380 | TC4H = (SYSCLOCK * USECPERTICK / 1000000) >> 8; \ |
yuhki50 | 0:70c8e56bac45 | 381 | OCR4C = (SYSCLOCK * USECPERTICK / 1000000) & 255; \ |
yuhki50 | 0:70c8e56bac45 | 382 | TC4H = 0; \ |
yuhki50 | 0:70c8e56bac45 | 383 | TCNT4 = 0; \ |
yuhki50 | 0:70c8e56bac45 | 384 | }) |
yuhki50 | 0:70c8e56bac45 | 385 | |
yuhki50 | 0:70c8e56bac45 | 386 | //----------------- |
yuhki50 | 0:70c8e56bac45 | 387 | #if defined(CORE_OC4A_PIN) |
yuhki50 | 0:70c8e56bac45 | 388 | # define TIMER_PWM_PIN CORE_OC4A_PIN // Teensy |
yuhki50 | 0:70c8e56bac45 | 389 | #elif defined(__AVR_ATmega32U4__) |
yuhki50 | 0:70c8e56bac45 | 390 | # define TIMER_PWM_PIN 13 // Leonardo |
yuhki50 | 0:70c8e56bac45 | 391 | #else |
yuhki50 | 0:70c8e56bac45 | 392 | # error "Please add OC4A pin number here\n" |
yuhki50 | 0:70c8e56bac45 | 393 | #endif |
yuhki50 | 0:70c8e56bac45 | 394 | |
yuhki50 | 0:70c8e56bac45 | 395 | //--------------------------------------------------------- |
yuhki50 | 0:70c8e56bac45 | 396 | // Timer4 (16 bits) |
yuhki50 | 0:70c8e56bac45 | 397 | // |
yuhki50 | 0:70c8e56bac45 | 398 | #elif defined(IR_USE_TIMER4) |
yuhki50 | 0:70c8e56bac45 | 399 | |
yuhki50 | 0:70c8e56bac45 | 400 | #define TIMER_RESET |
yuhki50 | 0:70c8e56bac45 | 401 | #define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1)) |
yuhki50 | 0:70c8e56bac45 | 402 | #define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1))) |
yuhki50 | 0:70c8e56bac45 | 403 | #define TIMER_ENABLE_INTR (TIMSK4 = _BV(OCIE4A)) |
yuhki50 | 0:70c8e56bac45 | 404 | #define TIMER_DISABLE_INTR (TIMSK4 = 0) |
yuhki50 | 0:70c8e56bac45 | 405 | #define TIMER_INTR_NAME TIMER4_COMPA_vect |
yuhki50 | 0:70c8e56bac45 | 406 | |
yuhki50 | 0:70c8e56bac45 | 407 | #define TIMER_CONFIG_KHZ(val) ({ \ |
yuhki50 | 0:70c8e56bac45 | 408 | const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ |
yuhki50 | 0:70c8e56bac45 | 409 | TCCR4A = _BV(WGM41); \ |
yuhki50 | 0:70c8e56bac45 | 410 | TCCR4B = _BV(WGM43) | _BV(CS40); \ |
yuhki50 | 0:70c8e56bac45 | 411 | ICR4 = pwmval; \ |
yuhki50 | 0:70c8e56bac45 | 412 | OCR4A = pwmval / 3; \ |
yuhki50 | 0:70c8e56bac45 | 413 | }) |
yuhki50 | 0:70c8e56bac45 | 414 | |
yuhki50 | 0:70c8e56bac45 | 415 | #define TIMER_CONFIG_NORMAL() ({ \ |
yuhki50 | 0:70c8e56bac45 | 416 | TCCR4A = 0; \ |
yuhki50 | 0:70c8e56bac45 | 417 | TCCR4B = _BV(WGM42) | _BV(CS40); \ |
yuhki50 | 0:70c8e56bac45 | 418 | OCR4A = SYSCLOCK * USECPERTICK / 1000000; \ |
yuhki50 | 0:70c8e56bac45 | 419 | TCNT4 = 0; \ |
yuhki50 | 0:70c8e56bac45 | 420 | }) |
yuhki50 | 0:70c8e56bac45 | 421 | |
yuhki50 | 0:70c8e56bac45 | 422 | //----------------- |
yuhki50 | 0:70c8e56bac45 | 423 | #if defined(CORE_OC4A_PIN) |
yuhki50 | 0:70c8e56bac45 | 424 | # define TIMER_PWM_PIN CORE_OC4A_PIN |
yuhki50 | 0:70c8e56bac45 | 425 | #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) |
yuhki50 | 0:70c8e56bac45 | 426 | # define TIMER_PWM_PIN 6 // Arduino Mega |
yuhki50 | 0:70c8e56bac45 | 427 | #else |
yuhki50 | 0:70c8e56bac45 | 428 | # error "Please add OC4A pin number here\n" |
yuhki50 | 0:70c8e56bac45 | 429 | #endif |
yuhki50 | 0:70c8e56bac45 | 430 | |
yuhki50 | 0:70c8e56bac45 | 431 | //--------------------------------------------------------- |
yuhki50 | 0:70c8e56bac45 | 432 | // Timer5 (16 bits) |
yuhki50 | 0:70c8e56bac45 | 433 | // |
yuhki50 | 0:70c8e56bac45 | 434 | #elif defined(IR_USE_TIMER5) |
yuhki50 | 0:70c8e56bac45 | 435 | |
yuhki50 | 0:70c8e56bac45 | 436 | #define TIMER_RESET |
yuhki50 | 0:70c8e56bac45 | 437 | #define TIMER_ENABLE_PWM (TCCR5A |= _BV(COM5A1)) |
yuhki50 | 0:70c8e56bac45 | 438 | #define TIMER_DISABLE_PWM (TCCR5A &= ~(_BV(COM5A1))) |
yuhki50 | 0:70c8e56bac45 | 439 | #define TIMER_ENABLE_INTR (TIMSK5 = _BV(OCIE5A)) |
yuhki50 | 0:70c8e56bac45 | 440 | #define TIMER_DISABLE_INTR (TIMSK5 = 0) |
yuhki50 | 0:70c8e56bac45 | 441 | #define TIMER_INTR_NAME TIMER5_COMPA_vect |
yuhki50 | 0:70c8e56bac45 | 442 | |
yuhki50 | 0:70c8e56bac45 | 443 | #define TIMER_CONFIG_KHZ(val) ({ \ |
yuhki50 | 0:70c8e56bac45 | 444 | const uint16_t pwmval = SYSCLOCK / 2000 / (val); \ |
yuhki50 | 0:70c8e56bac45 | 445 | TCCR5A = _BV(WGM51); \ |
yuhki50 | 0:70c8e56bac45 | 446 | TCCR5B = _BV(WGM53) | _BV(CS50); \ |
yuhki50 | 0:70c8e56bac45 | 447 | ICR5 = pwmval; \ |
yuhki50 | 0:70c8e56bac45 | 448 | OCR5A = pwmval / 3; \ |
yuhki50 | 0:70c8e56bac45 | 449 | }) |
yuhki50 | 0:70c8e56bac45 | 450 | |
yuhki50 | 0:70c8e56bac45 | 451 | #define TIMER_CONFIG_NORMAL() ({ \ |
yuhki50 | 0:70c8e56bac45 | 452 | TCCR5A = 0; \ |
yuhki50 | 0:70c8e56bac45 | 453 | TCCR5B = _BV(WGM52) | _BV(CS50); \ |
yuhki50 | 0:70c8e56bac45 | 454 | OCR5A = SYSCLOCK * USECPERTICK / 1000000; \ |
yuhki50 | 0:70c8e56bac45 | 455 | TCNT5 = 0; \ |
yuhki50 | 0:70c8e56bac45 | 456 | }) |
yuhki50 | 0:70c8e56bac45 | 457 | |
yuhki50 | 0:70c8e56bac45 | 458 | //----------------- |
yuhki50 | 0:70c8e56bac45 | 459 | #if defined(CORE_OC5A_PIN) |
yuhki50 | 0:70c8e56bac45 | 460 | # define TIMER_PWM_PIN CORE_OC5A_PIN |
yuhki50 | 0:70c8e56bac45 | 461 | #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) |
yuhki50 | 0:70c8e56bac45 | 462 | # define TIMER_PWM_PIN 46 // Arduino Mega |
yuhki50 | 0:70c8e56bac45 | 463 | #else |
yuhki50 | 0:70c8e56bac45 | 464 | # error "Please add OC5A pin number here\n" |
yuhki50 | 0:70c8e56bac45 | 465 | #endif |
yuhki50 | 0:70c8e56bac45 | 466 | |
yuhki50 | 0:70c8e56bac45 | 467 | //--------------------------------------------------------- |
yuhki50 | 0:70c8e56bac45 | 468 | // Special carrier modulator timer |
yuhki50 | 0:70c8e56bac45 | 469 | // |
yuhki50 | 0:70c8e56bac45 | 470 | #elif defined(IR_USE_TIMER_CMT) |
yuhki50 | 0:70c8e56bac45 | 471 | |
yuhki50 | 0:70c8e56bac45 | 472 | #define TIMER_RESET ({ \ |
yuhki50 | 0:70c8e56bac45 | 473 | uint8_t tmp = CMT_MSC; \ |
yuhki50 | 0:70c8e56bac45 | 474 | CMT_CMD2 = 30; \ |
yuhki50 | 0:70c8e56bac45 | 475 | }) |
yuhki50 | 0:70c8e56bac45 | 476 | |
yuhki50 | 0:70c8e56bac45 | 477 | #define TIMER_ENABLE_PWM do { \ |
yuhki50 | 0:70c8e56bac45 | 478 | CORE_PIN5_CONFIG = PORT_PCR_MUX(2) | PORT_PCR_DSE | PORT_PCR_SRE; \ |
yuhki50 | 0:70c8e56bac45 | 479 | } while(0) |
yuhki50 | 0:70c8e56bac45 | 480 | |
yuhki50 | 0:70c8e56bac45 | 481 | #define TIMER_DISABLE_PWM do { \ |
yuhki50 | 0:70c8e56bac45 | 482 | CORE_PIN5_CONFIG = PORT_PCR_MUX(1) | PORT_PCR_DSE | PORT_PCR_SRE; \ |
yuhki50 | 0:70c8e56bac45 | 483 | } while(0) |
yuhki50 | 0:70c8e56bac45 | 484 | |
yuhki50 | 0:70c8e56bac45 | 485 | #define TIMER_ENABLE_INTR NVIC_ENABLE_IRQ(IRQ_CMT) |
yuhki50 | 0:70c8e56bac45 | 486 | #define TIMER_DISABLE_INTR NVIC_DISABLE_IRQ(IRQ_CMT) |
yuhki50 | 0:70c8e56bac45 | 487 | #define TIMER_INTR_NAME cmt_isr |
yuhki50 | 0:70c8e56bac45 | 488 | |
yuhki50 | 0:70c8e56bac45 | 489 | //----------------- |
yuhki50 | 0:70c8e56bac45 | 490 | #ifdef ISR |
yuhki50 | 0:70c8e56bac45 | 491 | # undef ISR |
yuhki50 | 0:70c8e56bac45 | 492 | #endif |
yuhki50 | 0:70c8e56bac45 | 493 | #define ISR(f) void f(void) |
yuhki50 | 0:70c8e56bac45 | 494 | |
yuhki50 | 0:70c8e56bac45 | 495 | //----------------- |
yuhki50 | 0:70c8e56bac45 | 496 | #if (F_BUS == 48000000) |
yuhki50 | 0:70c8e56bac45 | 497 | # define CMT_PPS_VAL 5 |
yuhki50 | 0:70c8e56bac45 | 498 | #else |
yuhki50 | 0:70c8e56bac45 | 499 | # define CMT_PPS_VAL 2 |
yuhki50 | 0:70c8e56bac45 | 500 | #endif |
yuhki50 | 0:70c8e56bac45 | 501 | |
yuhki50 | 0:70c8e56bac45 | 502 | //----------------- |
yuhki50 | 0:70c8e56bac45 | 503 | #define TIMER_CONFIG_KHZ(val) ({ \ |
yuhki50 | 0:70c8e56bac45 | 504 | SIM_SCGC4 |= SIM_SCGC4_CMT; \ |
yuhki50 | 0:70c8e56bac45 | 505 | SIM_SOPT2 |= SIM_SOPT2_PTD7PAD; \ |
yuhki50 | 0:70c8e56bac45 | 506 | CMT_PPS = CMT_PPS_VAL; \ |
yuhki50 | 0:70c8e56bac45 | 507 | CMT_CGH1 = 2667 / val; \ |
yuhki50 | 0:70c8e56bac45 | 508 | CMT_CGL1 = 5333 / val; \ |
yuhki50 | 0:70c8e56bac45 | 509 | CMT_CMD1 = 0; \ |
yuhki50 | 0:70c8e56bac45 | 510 | CMT_CMD2 = 30; \ |
yuhki50 | 0:70c8e56bac45 | 511 | CMT_CMD3 = 0; \ |
yuhki50 | 0:70c8e56bac45 | 512 | CMT_CMD4 = 0; \ |
yuhki50 | 0:70c8e56bac45 | 513 | CMT_OC = 0x60; \ |
yuhki50 | 0:70c8e56bac45 | 514 | CMT_MSC = 0x01; \ |
yuhki50 | 0:70c8e56bac45 | 515 | }) |
yuhki50 | 0:70c8e56bac45 | 516 | |
yuhki50 | 0:70c8e56bac45 | 517 | #define TIMER_CONFIG_NORMAL() ({ \ |
yuhki50 | 0:70c8e56bac45 | 518 | SIM_SCGC4 |= SIM_SCGC4_CMT; \ |
yuhki50 | 0:70c8e56bac45 | 519 | CMT_PPS = CMT_PPS_VAL; \ |
yuhki50 | 0:70c8e56bac45 | 520 | CMT_CGH1 = 1; \ |
yuhki50 | 0:70c8e56bac45 | 521 | CMT_CGL1 = 1; \ |
yuhki50 | 0:70c8e56bac45 | 522 | CMT_CMD1 = 0; \ |
yuhki50 | 0:70c8e56bac45 | 523 | CMT_CMD2 = 30 \ |
yuhki50 | 0:70c8e56bac45 | 524 | CMT_CMD3 = 0; \ |
yuhki50 | 0:70c8e56bac45 | 525 | CMT_CMD4 = 19; \ |
yuhki50 | 0:70c8e56bac45 | 526 | CMT_OC = 0; \ |
yuhki50 | 0:70c8e56bac45 | 527 | CMT_MSC = 0x03; \ |
yuhki50 | 0:70c8e56bac45 | 528 | }) |
yuhki50 | 0:70c8e56bac45 | 529 | |
yuhki50 | 0:70c8e56bac45 | 530 | #define TIMER_PWM_PIN 5 |
yuhki50 | 0:70c8e56bac45 | 531 | |
yuhki50 | 0:70c8e56bac45 | 532 | // defines for TPM1 timer on Teensy-LC |
yuhki50 | 0:70c8e56bac45 | 533 | #elif defined(IR_USE_TIMER_TPM1) |
yuhki50 | 0:70c8e56bac45 | 534 | #define TIMER_RESET FTM1_SC |= FTM_SC_TOF; |
yuhki50 | 0:70c8e56bac45 | 535 | #define TIMER_ENABLE_PWM CORE_PIN16_CONFIG = PORT_PCR_MUX(3)|PORT_PCR_DSE|PORT_PCR_SRE |
yuhki50 | 0:70c8e56bac45 | 536 | #define TIMER_DISABLE_PWM CORE_PIN16_CONFIG = PORT_PCR_MUX(1)|PORT_PCR_SRE |
yuhki50 | 0:70c8e56bac45 | 537 | #define TIMER_ENABLE_INTR NVIC_ENABLE_IRQ(IRQ_FTM1) |
yuhki50 | 0:70c8e56bac45 | 538 | #define TIMER_DISABLE_INTR NVIC_DISABLE_IRQ(IRQ_FTM1) |
yuhki50 | 0:70c8e56bac45 | 539 | #define TIMER_INTR_NAME ftm1_isr |
yuhki50 | 0:70c8e56bac45 | 540 | #ifdef ISR |
yuhki50 | 0:70c8e56bac45 | 541 | #undef ISR |
yuhki50 | 0:70c8e56bac45 | 542 | #endif |
yuhki50 | 0:70c8e56bac45 | 543 | #define ISR(f) void f(void) |
yuhki50 | 0:70c8e56bac45 | 544 | #define TIMER_CONFIG_KHZ(val) ({ \ |
yuhki50 | 0:70c8e56bac45 | 545 | SIM_SCGC6 |= SIM_SCGC6_TPM1; \ |
yuhki50 | 0:70c8e56bac45 | 546 | FTM1_SC = 0; \ |
yuhki50 | 0:70c8e56bac45 | 547 | FTM1_CNT = 0; \ |
yuhki50 | 0:70c8e56bac45 | 548 | FTM1_MOD = (F_PLL/2000) / val - 1; \ |
yuhki50 | 0:70c8e56bac45 | 549 | FTM1_C0V = (F_PLL/6000) / val - 1; \ |
yuhki50 | 0:70c8e56bac45 | 550 | FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0); \ |
yuhki50 | 0:70c8e56bac45 | 551 | }) |
yuhki50 | 0:70c8e56bac45 | 552 | #define TIMER_CONFIG_NORMAL() ({ \ |
yuhki50 | 0:70c8e56bac45 | 553 | SIM_SCGC6 |= SIM_SCGC6_TPM1; \ |
yuhki50 | 0:70c8e56bac45 | 554 | FTM1_SC = 0; \ |
yuhki50 | 0:70c8e56bac45 | 555 | FTM1_CNT = 0; \ |
yuhki50 | 0:70c8e56bac45 | 556 | FTM1_MOD = (F_PLL/40000) - 1; \ |
yuhki50 | 0:70c8e56bac45 | 557 | FTM1_C0V = 0; \ |
yuhki50 | 0:70c8e56bac45 | 558 | FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0) | FTM_SC_TOF | FTM_SC_TOIE; \ |
yuhki50 | 0:70c8e56bac45 | 559 | }) |
yuhki50 | 0:70c8e56bac45 | 560 | #define TIMER_PWM_PIN 16 |
yuhki50 | 0:70c8e56bac45 | 561 | |
yuhki50 | 0:70c8e56bac45 | 562 | // defines for timer_tiny0 (8 bits) |
yuhki50 | 0:70c8e56bac45 | 563 | #elif defined(IR_USE_TIMER_TINY0) |
yuhki50 | 0:70c8e56bac45 | 564 | #define TIMER_RESET |
yuhki50 | 0:70c8e56bac45 | 565 | #define TIMER_ENABLE_PWM (TCCR0A |= _BV(COM0B1)) |
yuhki50 | 0:70c8e56bac45 | 566 | #define TIMER_DISABLE_PWM (TCCR0A &= ~(_BV(COM0B1))) |
yuhki50 | 0:70c8e56bac45 | 567 | #define TIMER_ENABLE_INTR (TIMSK |= _BV(OCIE0A)) |
yuhki50 | 0:70c8e56bac45 | 568 | #define TIMER_DISABLE_INTR (TIMSK &= ~(_BV(OCIE0A))) |
yuhki50 | 0:70c8e56bac45 | 569 | #define TIMER_INTR_NAME TIMER0_COMPA_vect |
yuhki50 | 0:70c8e56bac45 | 570 | #define TIMER_CONFIG_KHZ(val) ({ \ |
yuhki50 | 0:70c8e56bac45 | 571 | const uint8_t pwmval = SYSCLOCK / 2000 / (val); \ |
yuhki50 | 0:70c8e56bac45 | 572 | TCCR0A = _BV(WGM00); \ |
yuhki50 | 0:70c8e56bac45 | 573 | TCCR0B = _BV(WGM02) | _BV(CS00); \ |
yuhki50 | 0:70c8e56bac45 | 574 | OCR0A = pwmval; \ |
yuhki50 | 0:70c8e56bac45 | 575 | OCR0B = pwmval / 3; \ |
yuhki50 | 0:70c8e56bac45 | 576 | }) |
yuhki50 | 0:70c8e56bac45 | 577 | #define TIMER_COUNT_TOP (SYSCLOCK * USECPERTICK / 1000000) |
yuhki50 | 0:70c8e56bac45 | 578 | #if (TIMER_COUNT_TOP < 256) |
yuhki50 | 0:70c8e56bac45 | 579 | #define TIMER_CONFIG_NORMAL() ({ \ |
yuhki50 | 0:70c8e56bac45 | 580 | TCCR0A = _BV(WGM01); \ |
yuhki50 | 0:70c8e56bac45 | 581 | TCCR0B = _BV(CS00); \ |
yuhki50 | 0:70c8e56bac45 | 582 | OCR0A = TIMER_COUNT_TOP; \ |
yuhki50 | 0:70c8e56bac45 | 583 | TCNT0 = 0; \ |
yuhki50 | 0:70c8e56bac45 | 584 | }) |
yuhki50 | 0:70c8e56bac45 | 585 | #else |
yuhki50 | 0:70c8e56bac45 | 586 | #define TIMER_CONFIG_NORMAL() ({ \ |
yuhki50 | 0:70c8e56bac45 | 587 | TCCR0A = _BV(WGM01); \ |
yuhki50 | 0:70c8e56bac45 | 588 | TCCR0B = _BV(CS01); \ |
yuhki50 | 0:70c8e56bac45 | 589 | OCR0A = TIMER_COUNT_TOP / 8; \ |
yuhki50 | 0:70c8e56bac45 | 590 | TCNT0 = 0; \ |
yuhki50 | 0:70c8e56bac45 | 591 | }) |
yuhki50 | 0:70c8e56bac45 | 592 | #endif |
yuhki50 | 0:70c8e56bac45 | 593 | |
yuhki50 | 0:70c8e56bac45 | 594 | #define TIMER_PWM_PIN 1 /* ATtiny85 */ |
yuhki50 | 0:70c8e56bac45 | 595 | |
yuhki50 | 0:70c8e56bac45 | 596 | //--------------------------------------------------------- |
yuhki50 | 0:70c8e56bac45 | 597 | // Unknown Timer |
yuhki50 | 0:70c8e56bac45 | 598 | // |
yuhki50 | 0:70c8e56bac45 | 599 | #else |
yuhki50 | 0:70c8e56bac45 | 600 | # error "Internal code configuration error, no known IR_USE_TIMER# defined\n" |
yuhki50 | 0:70c8e56bac45 | 601 | #endif |
yuhki50 | 0:70c8e56bac45 | 602 | #endif |