Skip to content

Commit 1953f28

Browse files
committed
Add Timer0 code for ATtiny
1 parent 874e275 commit 1953f28

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

IRremoteInt.h

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,15 @@
6060
#define IR_USE_TIMER2 // tx = pin 14
6161

6262
// Tested with ATtiny85, presumably works with ATtiny45, possibly with ATtiny25
63+
// The attiny core uses Timer 0 for millis() etc., so using timer 1 is advisable
64+
// for IR out. Pin 4 also conveniently is not used in any role for ISP.
65+
// The Arduino-tiny core uses Timer 1 for millis(), so using timer 0 is advisable
66+
// for IR out. Pin 0 is used by default for IR out in the Tinyspark IR shield, but
67+
// is not usable for PWM waveforms where the frequency, not just the duty cycle,
68+
// needs to be controlled.
6369
#elif defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
64-
#define IR_USE_TIMER1_TINY // tx = pin 4 (OC1B)
65-
70+
// #define IR_USE_TIMER1_TINY // tx = pin 4 (OC1B)
71+
#define IR_USE_TIMER0 // tx = pin 1 (OC0B)
6672
// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc
6773
#else
6874
//#define IR_USE_TIMER1 // tx = pin 9
@@ -319,6 +325,45 @@ extern volatile irparams_t irparams;
319325
#define TIMER_PWM_PIN 4
320326

321327

328+
// defines for timer0 (8 bits). Tested on ATtiny85, may also work on other
329+
// processors, but most of them use Timer 0 to keep system time
330+
#elif defined(IR_USE_TIMER0)
331+
#define TIMER_RESET
332+
#define TIMER_ENABLE_PWM (TCCR0A |= _BV(COM0B1))
333+
#define TIMER_DISABLE_PWM (TCCR0A &= ~(_BV(COM0B1)))
334+
#define TIMER_ENABLE_INTR (TIMSK = _BV(OCIE0A))
335+
#define TIMER_DISABLE_INTR (TIMSK = 0)
336+
#define TIMER_INTR_NAME TIMER0_COMPA_vect
337+
#define TIMER_CONFIG_KHZ(val) ({ \
338+
const uint8_t pwmval = SYSCLOCK / 2000 / (val); \
339+
TCCR0A = _BV(WGM00); \
340+
TCCR0B = _BV(WGM02) | _BV(CS00); \
341+
OCR0A = pwmval; \
342+
OCR0B = pwmval / 3; \
343+
})
344+
#define TIMER_COUNT_TOP (SYSCLOCK * USECPERTICK / 1000000)
345+
#if (TIMER_COUNT_TOP < 256)
346+
#define TIMER_CONFIG_NORMAL() ({ \
347+
TCCR2A = _BV(WGM01); \
348+
TCCR2B = _BV(CS00); \
349+
OCR2A = TIMER_COUNT_TOP; \
350+
TCNT2 = 0; \
351+
})
352+
#else
353+
#define TIMER_CONFIG_NORMAL() ({ \
354+
TCCR2A = _BV(WGM01); \
355+
TCCR2B = _BV(CS01); \
356+
OCR2A = TIMER_COUNT_TOP / 8; \
357+
TCNT2 = 0; \
358+
})
359+
#endif
360+
#if defined(CORE_OC0A_PIN)
361+
#define TIMER_PWM_PIN CORE_OC0B_PIN
362+
#else
363+
#define TIMER_PWM_PIN 1 /* Attiny core */
364+
#endif
365+
366+
322367
// defines for timer3 (16 bits)
323368
#elif defined(IR_USE_TIMER3)
324369
#define TIMER_RESET
@@ -465,6 +510,10 @@ extern volatile irparams_t irparams;
465510
#define BLINKLED 0
466511
#define BLINKLED_ON() (PORTD |= B00000001)
467512
#define BLINKLED_OFF() (PORTD &= B11111110)
513+
#elif defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
514+
#define BLINKLED 4
515+
#define BLINKLED_ON() (PORTB |= B00001000)
516+
#define BLINKLED_OFF() (PORTB &= B11110110)
468517
#else
469518
#define BLINKLED 13
470519
#define BLINKLED_ON() (PORTB |= B00100000)

0 commit comments

Comments
 (0)