Перейти к содержанию

Arduino С Использованием Irremoute


Рекомендуемые сообщения

код библиотеки?

/*
* IRremote
* Version 0.11 August, 2009
* Copyright 2009 Ken Shirriff
* For details, see [url="http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html"]http://arcfn.com/200...te-library.html[/url]
*
* Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers
* Modified  by Mitra Ardron <mitra@mitra.biz>
* Added Sanyo and Mitsubishi controllers
* Modified Sony to spot the repeat codes that some Sony's send
*
* Interrupt code based on NECIRrcv by Joe Knapp
* [url="http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556"]http://www.arduino.c...?num=1210243556[/url]
* Also influenced by [url="http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/"]http://zovirl.com/20...ith-an-arduino/[/url]
*
* JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
*/

#include "IRremote.h"
#include "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 IRremoteInt.h
// Normally macros are used for efficiency
#ifdef DEBUG
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::sendPanasonic(unsigned int address, unsigned long data) {
enableIROut(35);
mark(PANASONIC_HDR_MARK);
space(PANASONIC_HDR_SPACE);

for(int i=0;i<16;i++)
{
	mark(PANASONIC_BIT_MARK);
	if (address & 0x8000) {
		space(PANASONIC_ONE_SPACE);
	} else {
		space(PANASONIC_ZERO_SPACE);
	}
	address <<= 1;		
}	
for (int i=0; i < 32; i++) {
	mark(PANASONIC_BIT_MARK);
	if (data & TOPBIT) {
		space(PANASONIC_ONE_SPACE);
	} else {
		space(PANASONIC_ZERO_SPACE);
	}
	data <<= 1;
}
mark(PANASONIC_BIT_MARK);
space(0);
}
void IRsend::sendJVC(unsigned long data, int nbits, int repeat)
{
enableIROut(38);
data = data << (32 - nbits);
if (!repeat){
	mark(JVC_HDR_MARK);
	space(JVC_HDR_SPACE);
}
for (int i = 0; i < nbits; i++) {
	if (data & TOPBIT) {
		mark(JVC_BIT_MARK);
		space(JVC_ONE_SPACE);
	}
	else {
		mark(JVC_BIT_MARK);
		space(JVC_ZERO_SPACE);
	}
	data <<= 1;
}
mark(JVC_BIT_MARK);
space(0);
}
void IRsend::mark(int time) {
 // Sends an IR mark for the specified number of microseconds.
 // The mark output is modulated at the PWM frequency.
 TIMER_ENABLE_PWM; // Enable pin 3 PWM output
 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.
 TIMER_DISABLE_PWM; // Disable pin 3 PWM output
 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 (OC2B).
 // 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.)
 // TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B
 // controlling the duty cycle.
 // There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A)
 // 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 [url="http://arcfn.com/2009/07/secrets-of-arduino-pwm.html"]http://arcfn.com/200...rduino-pwm.html[/url] for details.


 // Disable the Timer2 Interrupt (which is used for receiving IR)
 TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt

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

 // COM2A = 00: disconnect OC2A
 // COM2B = 00: disconnect OC2B; to send signal set to 10: OC2B non-inverted
 // WGM2 = 101: phase-correct PWM with OCRA as top
 // CS2 = 000: no prescaling
 // The top value for the timer.  The modulation frequency will be SYSCLOCK / 2 / OCR2A.
 TIMER_CONFIG_KHZ(khz);
}

IRrecv::IRrecv(int recvpin)
{
 irparams.recvpin = recvpin;
 irparams.blinkflag = 0;
}

// initialization
void IRrecv::enableIRIn() {
 cli();
 // setup pulse clock timer interrupt
 //Prescale /8 (16M/8 = 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)
 TIMER_CONFIG_NORMAL();

 //Timer2 Overflow Interrupt Enable
 TIMER_ENABLE_INTR;

 TIMER_RESET;

 sei();  // enable interrupts

 // initialize state machine variables
 irparams.rcvstate = STATE_IDLE;
 irparams.rawlen = 0;

 // set pin modes
 pinMode(irparams.recvpin, INPUT);
}

// enable/disable blinking of pin 13 on IR processing
void IRrecv::blink13(int blinkflag)
{
 irparams.blinkflag = blinkflag;
 if (blinkflag)
pinMode(BLINKLED, OUTPUT);
}

// TIMER2 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(TIMER_INTR_NAME)
{
 TIMER_RESET;

 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;
 }

 if (irparams.blinkflag) {
if (irdata == MARK) {
  BLINKLED_ON();  // turn pin 13 LED on
}
else {
  BLINKLED_OFF();  // turn pin 13 LED off
}
 }
}

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 Sanyo decode");
#endif
 if (decodeSanyo(results)) {
return DECODED;
 }
#ifdef DEBUG
 Serial.println("Attempting Mitsubishi decode");
#endif
 if (decodeMitsubishi(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;
 }
#ifdef DEBUG
Serial.println("Attempting Panasonic decode");
#endif
if (decodePanasonic(results)) {
	return DECODED;
}
#ifdef DEBUG
Serial.println("Attempting JVC decode");
#endif
if (decodeJVC(results)) {
	return DECODED;
}
 // decodeHash returns a hash on any input.
 // Thus, it needs to be last in the list.
 // If you add any decodes, add them before this.
 if (decodeHash(results)) {
return DECODED;
 }
 // Throw away and start over
 resume();
 return ERR;
}

// NECs have a repeat only 4 items long
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 = 0; // Dont skip first space, check its size

 // Some Sony's deliver repeats fast after first
 // unfortunately can't spot difference from of repeat from two fast clicks
 if (results->rawbuf[offset] < SONY_DOUBLE_SPACE_USECS) {
// Serial.print("IR Gap found: ");
results->bits = 0;
results->value = REPEAT;
results->decode_type = SANYO;
return DECODED;
 }
 offset++;

 // 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;
}

// I think this is a Sanyo decoder - serial = SA 8650B
// Looks like Sony except for timings, 48 chars of data and time/space different
long IRrecv::decodeSanyo(decode_results *results) {
 long data = 0;
 if (irparams.rawlen < 2 * SANYO_BITS + 2) {
return ERR;
 }
 int offset = 0; // Skip first space
 // Initial space  
 /* Put this back in for debugging - note can't use #DEBUG as if Debug on we don't see the repeat cos of the delay
 Serial.print("IR Gap: ");
 Serial.println( results->rawbuf[offset]);
 Serial.println( "test against:");
 Serial.println(results->rawbuf[offset]);
 */
 if (results->rawbuf[offset] < SANYO_DOUBLE_SPACE_USECS) {
// Serial.print("IR Gap found: ");
results->bits = 0;
results->value = REPEAT;
results->decode_type = SANYO;
return DECODED;
 }
 offset++;

 // Initial mark
 if (!MATCH_MARK(results->rawbuf[offset], SANYO_HDR_MARK)) {
return ERR;
 }
 offset++;

 // Skip Second Mark
 if (!MATCH_MARK(results->rawbuf[offset], SANYO_HDR_MARK)) {
return ERR;
 }
 offset++;

 while (offset + 1 < irparams.rawlen) {
if (!MATCH_SPACE(results->rawbuf[offset], SANYO_HDR_SPACE)) {
  break;
}
offset++;
if (MATCH_MARK(results->rawbuf[offset], SANYO_ONE_MARK)) {
  data = (data << 1) | 1;
}
else if (MATCH_MARK(results->rawbuf[offset], SANYO_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 = SANYO;
 return DECODED;
}

// Looks like Sony except for timings, 48 chars of data and time/space different
long IRrecv::decodeMitsubishi(decode_results *results) {
 // Serial.print("?!? decoding Mitsubishi:");Serial.print(irparams.rawlen); Serial.print(" want "); Serial.println( 2 * MITSUBISHI_BITS + 2);
 long data = 0;
 if (irparams.rawlen < 2 * MITSUBISHI_BITS + 2) {
return ERR;
 }
 int offset = 0; // Skip first space
 // Initial space  
 /* Put this back in for debugging - note can't use #DEBUG as if Debug on we don't see the repeat cos of the delay
 Serial.print("IR Gap: ");
 Serial.println( results->rawbuf[offset]);
 Serial.println( "test against:");
 Serial.println(results->rawbuf[offset]);
 */
 /* Not seeing double keys from Mitsubishi
 if (results->rawbuf[offset] < MITSUBISHI_DOUBLE_SPACE_USECS) {
// Serial.print("IR Gap found: ");
results->bits = 0;
results->value = REPEAT;
results->decode_type = MITSUBISHI;
return DECODED;
 }
 */
 offset++;

 // Typical
 // 14200 7 41 7 42 7 42 7 17 7 17 7 18 7 41 7 18 7 17 7 17 7 18 7 41 8 17 7 17 7 18 7 17 7

 // Initial Space
 if (!MATCH_MARK(results->rawbuf[offset], MITSUBISHI_HDR_SPACE)) {
return ERR;
 }
 offset++;
 while (offset + 1 < irparams.rawlen) {
if (MATCH_MARK(results->rawbuf[offset], MITSUBISHI_ONE_MARK)) {
  data = (data << 1) | 1;
}
else if (MATCH_MARK(results->rawbuf[offset], MITSUBISHI_ZERO_MARK)) {
  data <<= 1;
}
else {
  // Serial.println("A"); Serial.println(offset); Serial.println(results->rawbuf[offset]);
  return ERR;
}
offset++;
if (!MATCH_SPACE(results->rawbuf[offset], MITSUBISHI_HDR_SPACE)) {
  // Serial.println("B"); Serial.println(offset); Serial.println(results->rawbuf[offset]);
  break;
}
offset++;
 }

 // Success
 results->bits = (offset - 1) / 2;
 if (results->bits < MITSUBISHI_BITS) {
results->bits = 0;
return ERR;
 }
 results->value = data;
 results->decode_type = MITSUBISHI;
 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;
}
long IRrecv::decodePanasonic(decode_results *results) {
unsigned long long data = 0;
int offset = 1;

if (!MATCH_MARK(results->rawbuf[offset], PANASONIC_HDR_MARK)) {
	return ERR;
}
offset++;
if (!MATCH_MARK(results->rawbuf[offset], PANASONIC_HDR_SPACE)) {
	return ERR;
}
offset++;

// decode address
for (int i = 0; i < PANASONIC_BITS; i++) {
	if (!MATCH_MARK(results->rawbuf[offset++], PANASONIC_BIT_MARK)) {
		return ERR;
	}
	if (MATCH_SPACE(results->rawbuf[offset],PANASONIC_ONE_SPACE)) {
		data = (data << 1) | 1;
	} else if (MATCH_SPACE(results->rawbuf[offset],PANASONIC_ZERO_SPACE)) {
		data <<= 1;
	} else {
		return ERR;
	}
	offset++;
}
results->value = (unsigned long)data;
results->panasonicAddress = (unsigned int)(data >> 32);
results->decode_type = PANASONIC;
results->bits = PANASONIC_BITS;
return DECODED;
}
long IRrecv::decodeJVC(decode_results *results) {
long data = 0;
int offset = 1; // Skip first space
// Check for repeat
if (irparams.rawlen - 1 == 33 &&
	MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK) &&
	MATCH_MARK(results->rawbuf[irparams.rawlen-1], JVC_BIT_MARK)) {
	results->bits = 0;
	results->value = REPEAT;
	results->decode_type = JVC;
	return DECODED;
}
// Initial mark
if (!MATCH_MARK(results->rawbuf[offset], JVC_HDR_MARK)) {
	return ERR;
}
offset++;
if (irparams.rawlen < 2 * JVC_BITS + 1 ) {
	return ERR;
}
// Initial space
if (!MATCH_SPACE(results->rawbuf[offset], JVC_HDR_SPACE)) {
	return ERR;
}
offset++;
for (int i = 0; i < JVC_BITS; i++) {
	if (!MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK)) {
		return ERR;
	}
	offset++;
	if (MATCH_SPACE(results->rawbuf[offset], JVC_ONE_SPACE)) {
		data = (data << 1) | 1;
	}
	else if (MATCH_SPACE(results->rawbuf[offset], JVC_ZERO_SPACE)) {
		data <<= 1;
	}
	else {
		return ERR;
	}
	offset++;
}
//Stop bit
if (!MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK)){
	return ERR;
}
// Success
results->bits = JVC_BITS;
results->value = data;
results->decode_type = JVC;
return DECODED;
}

/* -----------------------------------------------------------------------
* hashdecode - decode an arbitrary IR code.
* Instead of decoding using a standard encoding scheme
* (e.g. Sony, NEC, RC5), the code is hashed to a 32-bit value.
*
* The algorithm: look at the sequence of MARK signals, and see if each one
* is shorter (0), the same length (1), or longer (2) than the previous.
* Do the same with the SPACE signals.  Hszh the resulting sequence of 0's,
* 1's, and 2's to a 32-bit value.  This will give a unique value for each
* different code (probably), for most code systems.
*
* [url="http://arcfn.com/2010/01/using-arbitrary-remotes-with-arduino.html"]http://arcfn.com/201...th-arduino.html[/url]
*/

// Compare two tick values, returning 0 if newval is shorter,
// 1 if newval is equal, and 2 if newval is longer
// Use a tolerance of 20%
int IRrecv::compare(unsigned int oldval, unsigned int newval) {
 if (newval < oldval * .8) {
return 0;
 }
 else if (oldval < newval * .8) {
return 2;
 }
 else {
return 1;
 }
}

// Use FNV hash algorithm: [url="http://isthe.com/chongo/tech/comp/fnv/#FNV-param"]http://isthe.com/cho.../fnv/#FNV-param[/url]
#define FNV_PRIME_32 16777619
#define FNV_BASIS_32 2166136261

/* Converts the raw code values into a 32-bit hash code.
* Hopefully this code is unique for each button.
* This isn't a "real" decoding, just an arbitrary value.
*/
long IRrecv::decodeHash(decode_results *results) {
 // Require at least 6 samples to prevent triggering on noise
 if (results->rawlen < 6) {
return ERR;
 }
 long hash = FNV_BASIS_32;
 for (int i = 1; i+2 < results->rawlen; i++) {
int value =  compare(results->rawbuf[i], results->rawbuf[i+2]);
// Add value into the hash
hash = (hash * FNV_PRIME_32) ^ value;
 }
 results->value = hash;
 results->bits = 32;
 results->decode_type = UNKNOWN;
 return DECODED;
}

/* Sharp and DISH support by Todd Treece ( [url="http://unionbridge.org/design/ircommand"]http://unionbridge.o...esign/ircommand[/url] )

The Dish send function needs to be repeated 4 times, and the Sharp function
has the necessary repeat built in because of the need to invert the signal.

Sharp protocol documentation:
[url="http://www.sbprojects.com/knowledge/ir/sharp.htm"]http://www.sbproject...ge/ir/sharp.htm[/url]

Here are the LIRC files that I found that seem to match the remote codes
from the oscilloscope:

Sharp LCD TV:
[url="http://lirc.sourceforge.net/remotes/sharp/GA538WJSA"]http://lirc.sourcefo...sharp/GA538WJSA[/url]

DISH NETWORK (echostar 301):
[url="http://lirc.sourceforge.net/remotes/echostar/301_501_3100_5100_58xx_59xx"]http://lirc.sourcefo..._5100_58xx_59xx[/url]

For the DISH codes, only send the last for characters of the hex.
i.e. use 0x1C10 instead of 0x0000000000001C10 which is listed in the
linked LIRC file.
*/

void IRsend::sendSharp(unsigned long data, int nbits) {
 unsigned long invertdata = data ^ SHARP_TOGGLE_MASK;
 enableIROut(38);
 for (int i = 0; i < nbits; i++) {
if (data & 0x4000) {
  mark(SHARP_BIT_MARK);
  space(SHARP_ONE_SPACE);
}
else {
  mark(SHARP_BIT_MARK);
  space(SHARP_ZERO_SPACE);
}
data <<= 1;
 }

 mark(SHARP_BIT_MARK);
 space(SHARP_ZERO_SPACE);
 delay(46);
 for (int i = 0; i < nbits; i++) {
if (invertdata & 0x4000) {
  mark(SHARP_BIT_MARK);
  space(SHARP_ONE_SPACE);
}
else {
  mark(SHARP_BIT_MARK);
  space(SHARP_ZERO_SPACE);
}
invertdata <<= 1;
 }
 mark(SHARP_BIT_MARK);
 space(SHARP_ZERO_SPACE);
 delay(46);
}

void IRsend::sendDISH(unsigned long data, int nbits)
{
 enableIROut(56);
 mark(DISH_HDR_MARK);
 space(DISH_HDR_SPACE);
 for (int i = 0; i < nbits; i++) {
if (data & DISH_TOP_BIT) {
  mark(DISH_BIT_MARK);
  space(DISH_ONE_SPACE);
}
else {
  mark(DISH_BIT_MARK);
  space(DISH_ZERO_SPACE);
}
data <<= 1;
 }
}

Изменено пользователем Alex
Ссылка на комментарий
Поделиться на другие сайты

Реклама: ООО ТД Промэлектроника, ИНН: 6659197470, Тел: 8 (800) 1000-321

Вывод частоты реализует макрос TIMER_CONFIG_KHZ, а его реализации нет.

В общем, сделайте просто свою функцию, по типу sendRaw, а space и mark замените на вывод нуля/единицы на PIN, с задержкой в buf микросекунд.

Вывод частоты реализует макрос TIMER_CONFIG_KHZ, а его реализации нет.
Не, вру, нашёл :

#define TIMER_CONFIG_KHZ(val) ({ \
const uint8_t pwmval = SYSCLOCK / 2000 / (val); \
TCCR2A			 = _BV(WGM20); \
TCCR2B			 = _BV(WGM22) | _BV(CS20); \
OCR2A			 = pwmval; \
OCR2B			 = pwmval / 3; \
})

Никаких ограничений. Так что, как поведёт себя библиотека при нулевом значении - хз...

А вот реализация функций mark и space :

void IRsend::mark(int time) {
 // Sends an IR mark for the specified number of microseconds.
 // The mark output is modulated at the PWM frequency.
 TIMER_ENABLE_PWM; // Enable pin 3 PWM output
 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.
 TIMER_DISABLE_PWM; // Disable pin 3 PWM output
 delayMicroseconds(time);
}

Собственно, как и предполагалось - просто вкл/выкл ШИМа и задержка на time микросекунд.

Изменено пользователем Alex
Ссылка на комментарий
Поделиться на другие сайты

20% скидка на весь каталог электронных компонентов в ТМ Электроникс!

Акция "Лето ближе - цены ниже", успей сделать выгодные покупки!

Плюс весь апрель действует скидка 10% по промокоду APREL24 + 15% кэшбэк и бесплатная доставка!

Перейти на страницу акции

Реклама: ООО ТМ ЭЛЕКТРОНИКС, ИНН: 7806548420, info@tmelectronics.ru, +7(812)4094849

void IRsend::mark(int time) {

// Sends an IR mark for the specified number of microseconds.

// The mark output is modulated at the PWM frequency.

//TIMER_ENABLE_PWM; // Enable pin 3 PWM output

PORTD &= ~(1<<3);

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.

//TIMER_DISABLE_PWM; // Disable pin 3 PWM output

PORTD |= 1<<3;

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 (OC2B).

// 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.)

// TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B

// controlling the duty cycle.

// There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A)

// 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 <a data-cke-saved-href="<a href="http://arcfn.com/2009/07/secrets-of-arduino-pwm.html" rel="nofollow">http://arcfn.com/2009/07/secrets-of-arduino-pwm.html</a>" href="<a href="http://arcfn.com/2009/07/secrets-of-arduino-pwm.html" rel="nofollow">http://arcfn.com/2009/07/secrets-of-arduino-pwm.html</a>" rel="nofollow"><a href="http://arcfn.com/2009/07/secrets-of-arduino-pwm.html" rel="nofollow">http://arcfn.com/2009/07/secrets-of-arduino-pwm.html</a></a> for details.

// Disable the Timer2 Interrupt (which is used for receiving IR)

//TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt

pinMode(3, OUTPUT);

digitalWrite(3, HIGH); // When not sending PWM, we want it low

// COM2A = 00: disconnect OC2A

// COM2B = 00: disconnect OC2B; to send signal set to 10: OC2B non-inverted

// WGM2 = 101: phase-correct PWM with OCRA as top

// CS2 = 000: no prescaling

// The top value for the timer. The modulation frequency will be SYSCLOCK / 2 / OCR2A.

//TIMER_CONFIG_KHZ(khz);

}

Вот похоже что-то на решение проблемы, но я когда пробовал не получилось

Ссылка на комментарий
Поделиться на другие сайты

Организация питания на основе надежных литиевых аккумуляторов EVE и микросхем азиатского производства

Качественное и безопасное устройство, работающее от аккумулятора, должно учитывать его физические и химические свойства, профили заряда и разряда, их изменение во времени и под влиянием различных условий, таких как температура и ток нагрузки. Мы расскажем о литий-ионных аккумуляторных батареях EVE и нескольких решениях от различных китайских компаний, рекомендуемых для разработок приложений с использованием этих АКБ. Представленные в статье китайские аналоги помогут заменить продукцию западных брендов с оптимизацией цены без потери качества. Подробнее>>

Реклама: АО КОМПЭЛ, ИНН: 7713005406, ОГРН: 1027700032161

Сравнительное тестирование аккумуляторов EVE Energy и Samsung типоразмера 18650

Инженеры КОМПЭЛ провели сравнительное тестирование аккумуляторов EVE и Samsung популярного для бытовых и индустриальных применений типоразмера 18650. 

Для теста были выбраны аккумуляторы литий-никельмарганцевой системы: по два образца одного наименования каждого производителя – и протестированы на двух значениях тока разряда: 0,5 А и 2,5 А. Испытания проводились в нормальных условиях на электронной нагрузке EBD-USB от ZKEtech, а зарядка осуществлялась от лабораторного источника питания в режиме CC+CV в соответствии с рекомендациями в даташите на определенную модель. Подробнее>>

Реклама: АО КОМПЭЛ, ИНН: 7713005406, ОГРН: 1027700032161

void setup() {

pinMode(113, OUTPUT);

}

void loop()

{

digitalWrite(13, HIGH);

delayMicroseconds(9000);

digitalWrite(13, LOW);

delayMicroseconds(4500);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

// zdes

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(1600);

digitalWrite(13, HIGH);

delayMicroseconds(560);

digitalWrite(13, LOW);

delayMicroseconds(560);

digitalWrite(13, HIGH);

delayMicroseconds(600);

digitalWrite(13, LOW);

delayMicroseconds(560);

delay(5000);

}

вот так написал, через светодиод теперь не работает, и не включает кондиционер, хотя все сделано по протоколу, скажите, а светодиоду не пофигу шим или просто вкл/выкл, что бы такую команду послать? Щас уже не получится, но завтра сниму плату и попробую воткнуть этот сигнал прямо на вход, какой все таки способ лучше использовать для развязки их по току, или лучше вообще снять напряжение для питания друины прямо с платы кондиционера?

Ссылка на комментарий
Поделиться на другие сайты

Литиевые аккумуляторы EVE Energy и решения для управления перезаряжаемыми источниками тока (материалы вебинара)

Опубликованы материалы вебинара Компэл, посвященного литиевым аккумуляторам EVE Energy и решениям для управления перезаряжаемыми источниками тока.

На вебинаре мы представили информацию не только по линейкам аккумуляторной продукции EVE, но и по решениям для управления ею, что поможет рассмотреть эти ХИТ в качестве дополнительной альтернативы для уже выпускающихся изделий. Также рассмотрели нюансы работы с производителем и сервисы, предоставляемые Компэл по данной продукции. Подробнее>>

Реклама: АО КОМПЭЛ, ИНН: 7713005406, ОГРН: 1027700032161

через светодиод теперь не работает, .....

а светодиоду не пофигу шим или просто вкл/выкл...?

:wall: и не должно - несущей ведь нет!

Светодиоду пофигу, а ИК-приемнику - нет. Вы опять ничего не поняли про несущую и ее роль?

Питание можете брать откуда угодно, если выход ардуины будете напрямую соединять (без опторазвязки), то "массы" устройств должны быть соединены.

Ссылка на комментарий
Поделиться на другие сайты

Литиевые батарейки и аккумуляторы от мирового лидера  EVE в Компэл

Компания Компэл, официальный дистрибьютор EVE Energy, бренда №1 по производству химических источников тока (ХИТ) в мире, предлагает продукцию EVE как со склада, так и под заказ. Компания EVE широко известна в странах Европы, Америки и Юго-Восточной Азии уже более 20 лет. Недавно EVE была объявлена поставщиком новых аккумуляторных элементов круглого формата для электрических моделей «нового класса» компании BMW.

Продукция EVE предназначена для самого широкого спектра применений – от бытового до промышленного. Подробнее>>

Реклама: АО КОМПЭЛ, ИНН: 7713005406, ОГРН: 1027700032161

Все, попробовал, ничего не получается, ставил третье число в irremoute библиотеке sendRow равное ноль, ничего не происходит, тот скетч, простейший, что вверху написан тоже не запускает, пробовал и напрямую соединять нули и сигнал подавать на выход фотоприемника, и через маленький керамический конденсатор, ноль эмоций((((Может есть еще какие-то мысли?

Ссылка на комментарий
Поделиться на другие сайты

Может есть еще какие-то мысли?
Меня вот какие мысли посещают. У Вас, после всех этих эксперементальных тыков, вообще заработает ли хоть что-то ? Хотя бы, то, что уже работало до этого :)
Ссылка на комментарий
Поделиться на другие сайты

Прошло еще немного времени, с тех пор не отходил от нее, получилось, все работает, через пень-колоду правда но работает... Вместо фотика стоит транзистор 2n4401, на базу генерится код с друины а сам он пропускает все необходимое дальше. Если код повторяется три-четыре раза, то она срабатыает, если код нагрудается еще чем-то, например пишу что через serial придет единица и тогда включи кондиционер, уже начинаются проблемы... Очень интересно. Сделал отдельную функцию, которая включает, вызываю ее - включается норм, пижу если в serial пришла "1" то serialprint ("пришла единица") и вызвать функцию включение, в сериал пишет что единица пришла кондей не включает. весь вечер возился, добавлял запрет на прерывания, ничего не помогает. Получилось сделать с микрушкой на другой порт, если нажал кнопку, то на кондиционере вкл. А с com портом так и не связал((( Спасибо огромное всем, было очень интересно... Буду дальше разбираться, развиваться, надеюсь тоже когда - нибудь буду полезен ;)

Ссылка на комментарий
Поделиться на другие сайты

. Вместо фотика стоит транзистор 2n4401, на базу генерится код с друины а сам он пропускает все необходимое дальше.

Значит, нужно инвертировать посылки (нули с единицами поменять местами) и включить без транзистора.

Ссылка на комментарий
Поделиться на другие сайты

Присоединяйтесь к обсуждению

Вы публикуете как гость. Если у вас есть аккаунт, авторизуйтесь, чтобы опубликовать от имени своего аккаунта.
Примечание: Ваш пост будет проверен модератором, прежде чем станет видимым.

Гость
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Ответить в этой теме...

×   Вставлено с форматированием.   Восстановить форматирование

  Разрешено использовать не более 75 эмодзи.

×   Ваша ссылка была автоматически встроена.   Отображать как обычную ссылку

×   Ваш предыдущий контент был восстановлен.   Очистить редактор

×   Вы не можете вставлять изображения напрямую. Загружайте или вставляйте изображения по ссылке.

Загрузка...
  • Последние посетители   0 пользователей онлайн

    • Ни одного зарегистрированного пользователя не просматривает данную страницу
×
×
  • Создать...