Skip to content

Instantly share code, notes, and snippets.

@SeeJayDee
Last active June 24, 2024 03:36
Show Gist options
  • Save SeeJayDee/caa9b5cc29246df44e45b8e7d1b1cdc5 to your computer and use it in GitHub Desktop.
Save SeeJayDee/caa9b5cc29246df44e45b8e7d1b1cdc5 to your computer and use it in GitHub Desktop.
tiny_IRremote - Arduino IRremote ported to the ATtiny
/*
* tiny_IRremote
* Version 0.2 July, 2016
* Christian D'Abrera
* Fixed what was originally rather broken code from http://www.gammon.com.au/Arduino/
* ...itself based on work by Ken Shirriff.
*
* This code was tested for both sending and receiving IR on an ATtiny85 DIP-8 chip.
* IMPORTANT: IRsend only works from PB4 ("pin 4" according to Arduino). You will need to
* determine which physical pin this corresponds to for your chip, and connect your transmitter
* LED there.
*
* Copyright 2009 Ken Shirriff
* For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
*
* Interrupt code based on NECIRrcv by Joe Knapp
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
* Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
*/
#include "tiny_IRremote.h"
#include "tiny_IRremoteInt.h"
// Provides ISR
#include <avr/interrupt.h>
volatile irparams_t irparams;
// These versions of MATCH, MATCH_MARK, and MATCH_SPACE are only for debugging.
// To use them, set DEBUG in tiny_IRremoteInt.h
// Normally macros are used for efficiency
#ifdef DEBUG
#error debug enabled
int MATCH(int measured, int desired) {
Serial.print("Testing: ");
Serial.print(TICKS_LOW(desired), DEC);
Serial.print(" <= ");
Serial.print(measured, DEC);
Serial.print(" <= ");
Serial.println(TICKS_HIGH(desired), DEC);
return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired);
}
int MATCH_MARK(int measured_ticks, int desired_us) {
Serial.print("Testing mark ");
Serial.print(measured_ticks * USECPERTICK, DEC);
Serial.print(" vs ");
Serial.print(desired_us, DEC);
Serial.print(": ");
Serial.print(TICKS_LOW(desired_us + MARK_EXCESS), DEC);
Serial.print(" <= ");
Serial.print(measured_ticks, DEC);
Serial.print(" <= ");
Serial.println(TICKS_HIGH(desired_us + MARK_EXCESS), DEC);
return measured_ticks >= TICKS_LOW(desired_us + MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS);
}
int MATCH_SPACE(int measured_ticks, int desired_us) {
Serial.print("Testing space ");
Serial.print(measured_ticks * USECPERTICK, DEC);
Serial.print(" vs ");
Serial.print(desired_us, DEC);
Serial.print(": ");
Serial.print(TICKS_LOW(desired_us - MARK_EXCESS), DEC);
Serial.print(" <= ");
Serial.print(measured_ticks, DEC);
Serial.print(" <= ");
Serial.println(TICKS_HIGH(desired_us - MARK_EXCESS), DEC);
return measured_ticks >= TICKS_LOW(desired_us - MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS);
}
#endif
void IRsend::sendNEC(unsigned long data, int nbits)
{
enableIROut(38);
mark(NEC_HDR_MARK);
space(NEC_HDR_SPACE);
for (int i = 0; i < nbits; i++) {
if (data & TOPBIT) {
mark(NEC_BIT_MARK);
space(NEC_ONE_SPACE);
}
else {
mark(NEC_BIT_MARK);
space(NEC_ZERO_SPACE);
}
data <<= 1;
}
mark(NEC_BIT_MARK);
space(0);
}
void IRsend::sendSony(unsigned long data, int nbits) {
enableIROut(40);
mark(SONY_HDR_MARK);
space(SONY_HDR_SPACE);
data = data << (32 - nbits);
for (int i = 0; i < nbits; i++) {
if (data & TOPBIT) {
mark(SONY_ONE_MARK);
space(SONY_HDR_SPACE);
}
else {
mark(SONY_ZERO_MARK);
space(SONY_HDR_SPACE);
}
data <<= 1;
}
}
void IRsend::sendRaw(unsigned int buf[], int len, int hz)
{
enableIROut(hz);
for (int i = 0; i < len; i++) {
if (i & 1) {
space(buf[i]);
}
else {
mark(buf[i]);
}
}
space(0); // Just to be sure
}
// Note: first bit must be a one (start bit)
void IRsend::sendRC5(unsigned long data, int nbits)
{
enableIROut(36);
data = data << (32 - nbits);
mark(RC5_T1); // First start bit
space(RC5_T1); // Second start bit
mark(RC5_T1); // Second start bit
for (int i = 0; i < nbits; i++) {
if (data & TOPBIT) {
space(RC5_T1); // 1 is space, then mark
mark(RC5_T1);
}
else {
mark(RC5_T1);
space(RC5_T1);
}
data <<= 1;
}
space(0); // Turn off at end
}
// Caller needs to take care of flipping the toggle bit
void IRsend::sendRC6(unsigned long data, int nbits)
{
enableIROut(36);
data = data << (32 - nbits);
mark(RC6_HDR_MARK);
space(RC6_HDR_SPACE);
mark(RC6_T1); // start bit
space(RC6_T1);
int t;
for (int i = 0; i < nbits; i++) {
if (i == 3) {
// double-wide trailer bit
t = 2 * RC6_T1;
}
else {
t = RC6_T1;
}
if (data & TOPBIT) {
mark(t);
space(t);
}
else {
space(t);
mark(t);
}
data <<= 1;
}
space(0); // Turn off at end
}
void IRsend::mark(int time) {
// Sends an IR mark for the specified number of microseconds.
// The mark output is modulated at the PWM frequency.
GTCCR |= _BV(COM1B1); // Enable pin 3 PWM output (PB4 - Arduino D4)
delayMicroseconds(time);
}
/* Leave pin off for time (given in microseconds) */
void IRsend::space(int time) {
// Sends an IR space for the specified number of microseconds.
// A space is no output, so the PWM output is disabled.
GTCCR &= ~(_BV(COM1B1)); // Disable pin 3 PWM output (PB4 - Arduino D4)
delayMicroseconds(time);
}
void IRsend::enableIROut(int khz) {
// Enables IR output. The khz value controls the modulation frequency in kilohertz.
// The IR output will be on pin 3 (PB4 - Arduino D4) (OC1B).
// This routine is designed for 36-40KHz; if you use it for other values, it's up to you
// to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.)
// TIMER1 is used in fast PWM mode, with OCR1Ccontrolling the frequency and OCR1B
// controlling the duty cycle.
// There is no prescaling, so the output frequency is 8MHz / (2 * OCR1C)
// To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin.
// A few hours staring at the ATmega documentation and this will all make sense.
// See my Secrets of Arduino PWM at http://arcfn.com/2009/07/secrets-of-arduino-pwm.html for details.
// Disable the Timer1 Interrupt (which is used for receiving IR)
TIMSK &= ~_BV(TOIE1); //Timer1 Overflow Interrupt
pinMode(4, OUTPUT); // (PB4 - Arduino D4 - physical pin 3)
digitalWrite(4, LOW); // When not sending PWM, we want it low
// CTC1 = 1: TOP value set to OCR1C
// CS = 0001: No Prescaling
TCCR1 = _BV(CTC1) | _BV(CS10);
// PWM1B = 1: Enable PWM for OCR1B
GTCCR = _BV(PWM1B);
// The top value for the timer. The modulation frequency will be SYSCLOCK / OCR1C.
OCR1C = SYSCLOCK / khz / 1000;
OCR1B = OCR1C / 3; // 33% duty cycle
}
IRrecv::IRrecv(int recvpin)
{
irparams.recvpin = recvpin;
}
// initialization
void IRrecv::enableIRIn() {
// setup pulse clock timer interrupt
GTCCR = 0; // normal, non-PWM mode
//Prescale /4 (8M/4 = 0.5 microseconds per tick)
// Therefore, the timer interval can range from 0.5 to 128 microseconds
// depending on the reset value (255 to 0)
TCCR1 = _BV(CS11) | _BV(CS10);
//TIMER1 Overflow Interrupt Enable
TIMSK |= _BV(TOIE1);
RESET_TIMER1;
sei(); // enable interrupts
// initialize state machine variables
irparams.rcvstate = STATE_IDLE;
irparams.rawlen = 0;
// set pin modes
pinMode(irparams.recvpin, INPUT);
}
// TIMER1 interrupt code to collect raw data.
// Widths of alternating SPACE, MARK are recorded in rawbuf.
// Recorded in ticks of 50 microseconds.
// rawlen counts the number of entries recorded so far.
// First entry is the SPACE between transmissions.
// As soon as a SPACE gets long, ready is set, state switches to IDLE, timing of SPACE continues.
// As soon as first MARK arrives, gap width is recorded, ready is cleared, and new logging starts
ISR(TIM1_OVF_vect)
{
RESET_TIMER1;
uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin);
irparams.timer++; // One more 50us tick
if (irparams.rawlen >= RAWBUF) {
// Buffer overflow
irparams.rcvstate = STATE_STOP;
}
switch(irparams.rcvstate) {
case STATE_IDLE: // In the middle of a gap
if (irdata == MARK) {
if (irparams.timer < GAP_TICKS) {
// Not big enough to be a gap.
irparams.timer = 0;
}
else {
// gap just ended, record duration and start recording transmission
irparams.rawlen = 0;
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
irparams.timer = 0;
irparams.rcvstate = STATE_MARK;
}
}
break;
case STATE_MARK: // timing MARK
if (irdata == SPACE) { // MARK ended, record time
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
irparams.timer = 0;
irparams.rcvstate = STATE_SPACE;
}
break;
case STATE_SPACE: // timing SPACE
if (irdata == MARK) { // SPACE just ended, record it
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
irparams.timer = 0;
irparams.rcvstate = STATE_MARK;
}
else { // SPACE
if (irparams.timer > GAP_TICKS) {
// big SPACE, indicates gap between codes
// Mark current code as ready for processing
// Switch to STOP
// Don't reset timer; keep counting space width
irparams.rcvstate = STATE_STOP;
}
}
break;
case STATE_STOP: // waiting, measuring gap
if (irdata == MARK) { // reset gap timer
irparams.timer = 0;
}
break;
}
}
void IRrecv::resume() {
irparams.rcvstate = STATE_IDLE;
irparams.rawlen = 0;
}
// Decodes the received IR message
// Returns 0 if no data ready, 1 if data ready.
// Results of decoding are stored in results
int IRrecv::decode(decode_results *results) {
results->rawbuf = irparams.rawbuf;
results->rawlen = irparams.rawlen;
if (irparams.rcvstate != STATE_STOP) {
return ERR;
}
#ifdef DEBUG
Serial.println("Attempting NEC decode");
#endif
if (decodeNEC(results)) {
return DECODED;
}
#ifdef DEBUG
Serial.println("Attempting Sony decode");
#endif
if (decodeSony(results)) {
return DECODED;
}
#ifdef DEBUG
Serial.println("Attempting RC5 decode");
#endif
if (decodeRC5(results)) {
return DECODED;
}
#ifdef DEBUG
Serial.println("Attempting RC6 decode");
#endif
if (decodeRC6(results)) {
return DECODED;
}
if (results->rawlen >= 6) {
// Only return raw buffer if at least 6 bits
results->decode_type = UNKNOWN;
results->bits = 0;
results->value = 0;
return DECODED;
}
// Throw away and start over
resume();
return ERR;
}
long IRrecv::decodeNEC(decode_results *results) {
long data = 0;
int offset = 1; // Skip first space
// Initial mark
if (!MATCH_MARK(results->rawbuf[offset], NEC_HDR_MARK)) {
return ERR;
}
offset++;
// Check for repeat
if (irparams.rawlen == 4 &&
MATCH_SPACE(results->rawbuf[offset], NEC_RPT_SPACE) &&
MATCH_MARK(results->rawbuf[offset+1], NEC_BIT_MARK)) {
results->bits = 0;
results->value = REPEAT;
results->decode_type = NEC;
return DECODED;
}
if (irparams.rawlen < 2 * NEC_BITS + 4) {
return ERR;
}
// Initial space
if (!MATCH_SPACE(results->rawbuf[offset], NEC_HDR_SPACE)) {
return ERR;
}
offset++;
for (int i = 0; i < NEC_BITS; i++) {
if (!MATCH_MARK(results->rawbuf[offset], NEC_BIT_MARK)) {
return ERR;
}
offset++;
if (MATCH_SPACE(results->rawbuf[offset], NEC_ONE_SPACE)) {
data = (data << 1) | 1;
}
else if (MATCH_SPACE(results->rawbuf[offset], NEC_ZERO_SPACE)) {
data <<= 1;
}
else {
return ERR;
}
offset++;
}
// Success
results->bits = NEC_BITS;
results->value = data;
results->decode_type = NEC;
return DECODED;
}
long IRrecv::decodeSony(decode_results *results) {
long data = 0;
if (irparams.rawlen < 2 * SONY_BITS + 2) {
return ERR;
}
int offset = 1; // Skip first space
// Initial mark
if (!MATCH_MARK(results->rawbuf[offset], SONY_HDR_MARK)) {
return ERR;
}
offset++;
while (offset + 1 < irparams.rawlen) {
if (!MATCH_SPACE(results->rawbuf[offset], SONY_HDR_SPACE)) {
break;
}
offset++;
if (MATCH_MARK(results->rawbuf[offset], SONY_ONE_MARK)) {
data = (data << 1) | 1;
}
else if (MATCH_MARK(results->rawbuf[offset], SONY_ZERO_MARK)) {
data <<= 1;
}
else {
return ERR;
}
offset++;
}
// Success
results->bits = (offset - 1) / 2;
if (results->bits < 12) {
results->bits = 0;
return ERR;
}
results->value = data;
results->decode_type = SONY;
return DECODED;
}
// Gets one undecoded level at a time from the raw buffer.
// The RC5/6 decoding is easier if the data is broken into time intervals.
// E.g. if the buffer has MARK for 2 time intervals and SPACE for 1,
// successive calls to getRClevel will return MARK, MARK, SPACE.
// offset and used are updated to keep track of the current position.
// t1 is the time interval for a single bit in microseconds.
// Returns -1 for error (measured time interval is not a multiple of t1).
int IRrecv::getRClevel(decode_results *results, int *offset, int *used, int t1) {
if (*offset >= results->rawlen) {
// After end of recorded buffer, assume SPACE.
return SPACE;
}
int width = results->rawbuf[*offset];
int val = ((*offset) % 2) ? MARK : SPACE;
int correction = (val == MARK) ? MARK_EXCESS : - MARK_EXCESS;
int avail;
if (MATCH(width, t1 + correction)) {
avail = 1;
}
else if (MATCH(width, 2*t1 + correction)) {
avail = 2;
}
else if (MATCH(width, 3*t1 + correction)) {
avail = 3;
}
else {
return -1;
}
(*used)++;
if (*used >= avail) {
*used = 0;
(*offset)++;
}
#ifdef DEBUG
if (val == MARK) {
Serial.println("MARK");
}
else {
Serial.println("SPACE");
}
#endif
return val;
}
long IRrecv::decodeRC5(decode_results *results) {
if (irparams.rawlen < MIN_RC5_SAMPLES + 2) {
return ERR;
}
int offset = 1; // Skip gap space
long data = 0;
int used = 0;
// Get start bits
if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR;
if (getRClevel(results, &offset, &used, RC5_T1) != SPACE) return ERR;
if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR;
int nbits;
for (nbits = 0; offset < irparams.rawlen; nbits++) {
int levelA = getRClevel(results, &offset, &used, RC5_T1);
int levelB = getRClevel(results, &offset, &used, RC5_T1);
if (levelA == SPACE && levelB == MARK) {
// 1 bit
data = (data << 1) | 1;
}
else if (levelA == MARK && levelB == SPACE) {
// zero bit
data <<= 1;
}
else {
return ERR;
}
}
// Success
results->bits = nbits;
results->value = data;
results->decode_type = RC5;
return DECODED;
}
long IRrecv::decodeRC6(decode_results *results) {
if (results->rawlen < MIN_RC6_SAMPLES) {
return ERR;
}
int offset = 1; // Skip first space
// Initial mark
if (!MATCH_MARK(results->rawbuf[offset], RC6_HDR_MARK)) {
return ERR;
}
offset++;
if (!MATCH_SPACE(results->rawbuf[offset], RC6_HDR_SPACE)) {
return ERR;
}
offset++;
long data = 0;
int used = 0;
// Get start bit (1)
if (getRClevel(results, &offset, &used, RC6_T1) != MARK) return ERR;
if (getRClevel(results, &offset, &used, RC6_T1) != SPACE) return ERR;
int nbits;
for (nbits = 0; offset < results->rawlen; nbits++) {
int levelA, levelB; // Next two levels
levelA = getRClevel(results, &offset, &used, RC6_T1);
if (nbits == 3) {
// T bit is double wide; make sure second half matches
if (levelA != getRClevel(results, &offset, &used, RC6_T1)) return ERR;
}
levelB = getRClevel(results, &offset, &used, RC6_T1);
if (nbits == 3) {
// T bit is double wide; make sure second half matches
if (levelB != getRClevel(results, &offset, &used, RC6_T1)) return ERR;
}
if (levelA == MARK && levelB == SPACE) { // reversed compared to RC5
// 1 bit
data = (data << 1) | 1;
}
else if (levelA == SPACE && levelB == MARK) {
// zero bit
data <<= 1;
}
else {
return ERR; // Error
}
}
// Success
results->bits = nbits;
results->value = data;
results->decode_type = RC6;
return DECODED;
}
/*
* tiny_IRremote
* Version 0.2 July, 2016
* Christian D'Abrera
* Fixed what was originally rather broken code from http://www.gammon.com.au/Arduino/
* ...itself based on work by Ken Shirriff.
*
* This code was tested for both sending and receiving IR on an ATtiny85 DIP-8 chip.
* IMPORTANT: IRsend only works from PB4 ("pin 4" according to Arduino). You will need to
* determine which physical pin this corresponds to for your chip, and connect your transmitter
* LED there.
*
* Copyright 2009 Ken Shirriff
* For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.htm http://arcfn.com
*
* Interrupt code based on NECIRrcv by Joe Knapp
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
* Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
*/
#ifndef tiny_IRremote_h
#define tiny_IRremote_h
// The following are compile-time library options.
// If you change them, recompile the library.
// If DEBUG is defined, a lot of debugging output will be printed during decoding.
// TEST must be defined for the IRtest unittests to work. It will make some
// methods virtual, which will be slightly slower, which is why it is optional.
// #define DEBUG
// #define TEST
// Results returned from the decoder
class decode_results {
public:
int decode_type; // NEC, SONY, RC5, UNKNOWN
unsigned long value; // Decoded value
int bits; // Number of bits in decoded value
volatile unsigned int *rawbuf; // Raw intervals in .5 us ticks
int rawlen; // Number of records in rawbuf.
};
// Values for decode_type
#define NEC 1
#define SONY 2
#define RC5 3
#define RC6 4
#define UNKNOWN -1
// Decoded value for NEC when a repeat code is received
#define REPEAT 0xffffffff
// main class for receiving IR
class IRrecv
{
public:
IRrecv(int recvpin);
int decode(decode_results *results);
void enableIRIn();
void resume();
private:
// These are called by decode
int getRClevel(decode_results *results, int *offset, int *used, int t1);
long decodeNEC(decode_results *results);
long decodeSony(decode_results *results);
long decodeRC5(decode_results *results);
long decodeRC6(decode_results *results);
}
;
// Only used for testing; can remove virtual for shorter code
#ifdef TEST
#define VIRTUAL virtual
#else
#define VIRTUAL
#endif
class IRsend
{
public:
IRsend() {}
void sendNEC(unsigned long data, int nbits);
void sendSony(unsigned long data, int nbits);
void sendRaw(unsigned int buf[], int len, int hz);
void sendRC5(unsigned long data, int nbits);
void sendRC6(unsigned long data, int nbits);
// private:
void enableIROut(int khz);
VIRTUAL void mark(int usec);
VIRTUAL void space(int usec);
}
;
// Some useful constants
#define USECPERTICK 50 // microseconds per clock interrupt tick
#define RAWBUF 76 // Length of raw duration buffer
// Marks tend to be 100us too long, and spaces 100us too short
// when received due to sensor lag.
#define MARK_EXCESS 100
#endif
/*
* tiny_IRremote
* Version 0.2 July, 2016
* Christian D'Abrera
* Fixed what was originally rather broken code from http://www.gammon.com.au/Arduino/
* ...itself based on work by Ken Shirriff.
*
* This code was tested for both sending and receiving IR on an ATtiny85 DIP-8 chip.
* IMPORTANT: IRsend only works from PB4 ("pin 4" according to Arduino). You will need to
* determine which physical pin this corresponds to for your chip, and connect your transmitter
* LED there.
*
* Copyright 2009 Ken Shirriff
* For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
*
* Interrupt code based on NECIRrcv by Joe Knapp
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
* Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
*/
#ifndef tiny_IRremoteint_h
#define tiny_IRremoteint_h
#include <Arduino.h>
#define CLKFUDGE 5 // fudge factor for clock interrupt overhead
#define CLK 256 // max value for clock (timer 2)
#define PRESCALE 4 // TIMER1 clock prescale
#if defined (F_CPU)
#define SYSCLOCK F_CPU // main Arduino clock
#else
#define SYSCLOCK 8000000 // default ATtiny clock
#endif
#define CLKSPERUSEC (SYSCLOCK/PRESCALE/1000000) // timer clocks per microsecond
#define ERR 0
#define DECODED 1
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
// clock timer reset value
#define INIT_TIMER_COUNT1 (CLK - USECPERTICK*CLKSPERUSEC + CLKFUDGE)
#define RESET_TIMER1 TCNT1 = INIT_TIMER_COUNT1
// pulse parameters in usec
#define NEC_HDR_MARK 9000
#define NEC_HDR_SPACE 4500
#define NEC_BIT_MARK 560
#define NEC_ONE_SPACE 1600
#define NEC_ZERO_SPACE 560
#define NEC_RPT_SPACE 2250
#define SONY_HDR_MARK 2400
#define SONY_HDR_SPACE 600
#define SONY_ONE_MARK 1200
#define SONY_ZERO_MARK 600
#define SONY_RPT_LENGTH 45000
#define RC5_T1 889
#define RC5_RPT_LENGTH 46000
#define RC6_HDR_MARK 2666
#define RC6_HDR_SPACE 889
#define RC6_T1 444
#define RC6_RPT_LENGTH 46000
#define TOLERANCE 25 // percent tolerance in measurements
#define LTOL (1.0 - TOLERANCE/100.)
#define UTOL (1.0 + TOLERANCE/100.)
#define _GAP 5000 // Minimum map between transmissions
#define GAP_TICKS (_GAP/USECPERTICK)
#define TICKS_LOW(us) (int) (((us)*LTOL/USECPERTICK))
#define TICKS_HIGH(us) (int) (((us)*UTOL/USECPERTICK + 1))
#ifndef DEBUG
#define MATCH(measured_ticks, desired_us) ((measured_ticks) >= TICKS_LOW(desired_us) && (measured_ticks) <= TICKS_HIGH(desired_us))
#define MATCH_MARK(measured_ticks, desired_us) MATCH(measured_ticks, (desired_us) + MARK_EXCESS)
#define MATCH_SPACE(measured_ticks, desired_us) MATCH((measured_ticks), (desired_us) - MARK_EXCESS)
// Debugging versions are in tiny_IRremote.cpp
#endif
// receiver states
#define STATE_IDLE 2
#define STATE_MARK 3
#define STATE_SPACE 4
#define STATE_STOP 5
// information for the interrupt handler
typedef struct {
uint8_t recvpin; // pin for IR data from detector
uint8_t rcvstate; // state machine
unsigned int timer; // state timer, counts 50uS ticks.
unsigned int rawbuf[RAWBUF]; // raw data
uint8_t rawlen; // counter of entries in rawbuf
}
irparams_t;
// Defined in tiny_IRremote.cpp
extern volatile irparams_t irparams;
// IR detector output is active low
#define MARK 0
#define SPACE 1
#define TOPBIT 0x80000000
#define NEC_BITS 32
#define SONY_BITS 12
#define MIN_RC5_SAMPLES 11
#define MIN_RC6_SAMPLES 1
#endif
@zamfi
Copy link

zamfi commented Oct 1, 2017

z3t0's library does indeed use TIMER0, and breaks delay and millis on the David Mellis ATtiny core that shows up in the Arduino "Boards Manager".

This gist worked perfectly -- thanks a bunch!

@dsculptor
Copy link

Thank you my saviour!

@5shekel
Copy link

5shekel commented Feb 28, 2018

its a great alternative, I had to use it for the other timer was busy doing tinyTouch.
but its now my goto ir library for attiny85

http://idiot.io/2017/10/05/irTran.html

@DaveCalaway
Copy link

Great code! Thx you so much!

@vikasdew
Copy link

great work!!!
have you any library for atmeag8??

@hdelei
Copy link

hdelei commented Jul 28, 2018

Finally I got my Attiny85 sending readable IR signal. Thank you!!!

@Pokornz
Copy link

Pokornz commented Jul 29, 2018

Can't get it to work. I did only a simple example where the LED lights up when it gets NEC command and shuts off when REPEAT. Anyone knows how it could be fixed?

C:\Users\ondre\AppData\Local\Temp\arduino_cache_553552\core\core_digistump_avr_digispark-tiny_f0eec75f47031ccf3aac271df735eaea.a(wiring.c.o): In function `__vector_4':

C:\Users\ondre\AppData\Local\Arduino15\packages\digistump\hardware\avr\1.6.7\cores\tiny/wiring.c:93: multiple definition of `__vector_4'

libraries\tiny_IRremote\tiny_IRremote.cpp.o:C:\Users\ondre\Documents\Arduino\libraries\tiny_IRremote/tiny_IRremote.cpp:272: first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Digispark (Default - 16.5mhz).

@blamh
Copy link

blamh commented Aug 22, 2018

Thank you for this. It works perfectly. Just what I needed.

@aghyool
Copy link

aghyool commented Mar 20, 2020

Can't get it to work. I did only a simple example where the LED lights up when it gets NEC command and shuts off when REPEAT. Anyone knows how it could be fixed?

C:\Users\ondre\AppData\Local\Temp\arduino_cache_553552\core\core_digistump_avr_digispark-tiny_f0eec75f47031ccf3aac271df735eaea.a(wiring.c.o): In function `__vector_4':

C:\Users\ondre\AppData\Local\Arduino15\packages\digistump\hardware\avr\1.6.7\cores\tiny/wiring.c:93: multiple definition of `__vector_4'

libraries\tiny_IRremote\tiny_IRremote.cpp.o:C:\Users\ondre\Documents\Arduino\libraries\tiny_IRremote/tiny_IRremote.cpp:272: first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Digispark (Default - 16.5mhz).

Also I Have This Problem. Would Any Body Help Me?

@christer-watson
Copy link

Thanks for this project. I am trying to modify your code to output on PB1 instead of PB4. I have never programming an tiny85 before. If this is not possible, please let me know. I tried the following changes, all in tiny_IRremote.cpp:

pinMode(1, OUTPUT); // (PB1)
digitalWrite(1, LOW); // When not sending PWM, we want it low

and a little higher in the code:
// GTCCR |= _BV(COM1B1); // Enable pin 3 PWM output (PB4 - Arduino D4)
PORTB |= (1 << PB1); //replaces digitalWrite(PB3, HIGH);
and
// GTCCR &= ~(_BV(COM1B1)); // Disable pin 3 PWM output (PB4 - Arduino D4)
PORTB &= ~(1 << PB1); //replaces digitalWrite(PB3, LOW);

It doesn't work. I am trying to build an IR remote. The output is connected to a green LED and IR LED. I set it to send a volume down command once every 5 seconds. The green LED successfully turns on once every 5 seconds, but the TV doesn't respond. Any suggestions or hints would be appreciated!

@SeeJayDee
Copy link
Author

@Pokornz @aghyool

It looks like the Digispark core for ATTiny85 uses TIMER1 for millis(), which conflicts with the ISR used here for receiving IR data. You could potentially try z3t0's updated library, or use a different tiny85 core for your Digispark. I don't know how this would affect uploading over USB though. Adafruit's Trinket/Gemma boards also support USB upload so you could try those cores as well.

@christer-watson
Copy link

Thanks for the response. I'll take a closer look at z3t0's updated library. However, I am only using the sending option. I don't need to receive any IR data. If I remove that code (e.g. ISR), can you imagine if it is possible to change the output to PB1?

@SeeJayDee
Copy link
Author

It doesn't work. I am trying to build an IR remote. The output is connected to a green LED and IR LED. I set it to send a volume down command once every 5 seconds. The green LED successfully turns on once every 5 seconds, but the TV doesn't respond. Any suggestions or hints would be appreciated!

@christer-watson
IR signals are encoded with a 36 to 57 kHz (depending on protocol) modulation signal so that the receiver can distinguish them from ambient infrared noise. Hence the transmitter LED must receive a pulsed PWM input at the correct frequency, with 33% duty cycle.
This post from Adafruit summarizes it nicely.

and a little higher in the code:
// GTCCR |= _BV(COM1B1); // Enable pin 3 PWM output (PB4 - Arduino D4)
PORTB |= (1 << PB1); //replaces digitalWrite(PB3, HIGH);
and
// GTCCR &= ~(_BV(COM1B1)); // Disable pin 3 PWM output (PB4 - Arduino D4)
PORTB &= ~(1 << PB1); //replaces digitalWrite(PB3, LOW);

From your code, it looks like you have disabled PWM output on Dig-Pin-4 but haven't also enabled it on Dig-Pin-1.
We need to switch the carrier signal (PWM) on and off for each '1' or '0', not simply flash the LED.
Hence a simple write to PORTB or a call to digitalWrite() for pin 1 will not work.

Fortunately pin 1 is PWM-capable, but you will need to change some other things.
The PWM output on pin 4 is "PWM1B", and its duty cycle is driven by output compare register OCR1B.
The PWM output on pin 1 is "PWM1A", and its duty cycle is driven by output compare register OCR1A.

You could probably swap them over with a simple find-and-replace:-

  • replace 4 with 1 in all calls to pinMode(), digitalWrite(), and digitalRead() (if any)
  • replace all instances of PB3 with PB1
  • replace all instances of COM1B0 and COM1B1 with COM1A0 and COM1A1 respectively
  • replace all instances of OCR1B with OCR1A
  • replace all instances of PWM1B with PWM1A

... and so on.

That's all I can think of, off the top of my head.
You can find more info on the functionality of TIMER1 in the ATTiny85 datasheet, page 83 (section 12).

Hope this helps!

@christer-watson
Copy link

It helps tremendously. I will work on these changes tomorrow and let you know how they work out. Thanks again!

@christer-watson
Copy link

christer-watson commented Jul 11, 2020

I have made those changes, but no luck. I have also read through a lot of the supporting documentation about NEC codes and IR remote libraries, but haven't been able to figure out the problem. I have forked your code and deleted everything except the material related to NEC IR emitting. If you could spend a little time reviewing it, I would appreciate it a lot. I am specifically worried that the bits are not correctly set for timer1 to be linked to PB1. The github code link is:
https://gist.github.com/christer-watson/5efc1ea7bd8d7c4f74cd8a00bfdc69b6

@christer-watson
Copy link

Just another update. I got it working. Your description was really helpful. There was one item not on the list, however. The references to GTCCR had to switch to TCCR1. I have a copy of the code that works for me at the github link above.

.... now to the soldering part of my project...

@SeeJayDee
Copy link
Author

@christer-watson
Ah! Yes, sorry about that omission lol. I forgot about TCCR1/GTCCR because regular atmegas I've been working on keep all their COMnXn bits in one register. The Tiny85 is weird how it splits those bits across different registers.

Anyway I'm glad that you got it working. Good luck with the soldering! :)

@Kaseftamjid
Copy link

Hi,
I am working on an attiny45 project that requires both NEC receieve and millis to be used. So no option but to use your lib that runs on timer1.
But the code is too large to fit. I only need to decode NEC, so trimming other parts of this should be more than sufficient to fit inside in my case. Could anyone please help me with this?

@christer-watson
Copy link

Kaseftamjid,
I had to something similar. I was making a TV remote using an attiny85 and, because of hardware stuff, had to switch the port used for IR output. You can see some of the changes necessary from the above comments. I forked the project and, I think, all the code is available at the following link:

https://gist.github.com/christer-watson/5efc1ea7bd8d7c4f74cd8a00bfdc69b6

Just to be clear, I removed almost everything from this code except the NEC emitting material and I switched the output port (i.e., switched from the general timer to timer 1). If you run into problems, feel free to post again (although perhaps on the forked project??)

@Kaseftamjid
Copy link

christer-watson,
Thank you very much for this, But it seems this only sends NEC, while i only need to decode NEC. Nonetheless, I wasnt sure before that libraries could be modified to do fraction of the work, But now im confident it can be done, even though im not sure how to do it.

@christer-watson
Copy link

Whoops. I read your comment too quickly. Yes, I was sending NEC signals, not receiving NEC signals. I would encourage reading lots and lots of webpages on Attiny emitting and receiving. For me, the approach that worked best was:

  1. it tooks a week or two of reading lots of examples before it started to make sense, so be patient with yourself
  2. try to figure out what each function does in the code above. Not how it works, but what it does. For example,
    void IRsend::sendNEC(unsigned long data, int nbits)
    does the main work for sending the NEC signal. Most of the names are closely related to what it does.
    Good luck!

@NathanBxer
Copy link

NathanBxer commented Jun 7, 2021

Any chance of some install instructions and some example send and receive?

@VikasKumar08
Copy link

Can anyone have Attiny412 IR remote library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment