Difference between revisions of "Avr rc5 remote control"

From Electriki
Jump to navigationJump to search
(AVR (ATMega 16), Philips RC5 Remote Control, Example code in C)
 
Line 74: Line 74:
 
}
 
}
 
</pre>
 
</pre>
 +
 +
== External Links ==
 +
*[http://www.atmel.com/dyn/resources/prod_documents/doc1473.pdf Application Note AVR410: RC5 IR Remote Control Reciever]

Revision as of 17:07, 19 July 2009


Introduction

Interfacing a standard Philips RC5 protocol remote control to an AVR is easy. It doesn't take neither much hardware nor code to do so. I've found that there is an official application note from Atmel describing how to do this. Atmels example is done in assembler and is fault tolerant, but it's difficult to understand. By doing it in C it becomes very simple and understandable.


The Code

#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>

void init_int0()
{
	// Interrupt on INT0 pin going low
	MCUCR = (1 << ISC01);

	// Turn on INT0!
	GICR |= (1 << INT0);
}

typedef union { 
	uint16_t raw;
	struct {
		unsigned cmd    : 6;	// LSB
		unsigned addr   : 5;
		unsigned toggle : 1;
		unsigned start  : 2;
		unsigned        : 2;	// MSB
	};
} rc5data;


static volatile int flag = 0;
static volatile rc5data data;

SIGNAL (INT0_vect)
{	
	int pos = 0;

	if (!flag) {
		data.raw = 0;

		// triggers on faling edge of start bit 1.
		_delay_ms(0.2);

		while (pos < 14) {
			data.raw = (data.raw << 1) | ((~PIND & 0x04) >> 2);
			_delay_ms(1.8);
			pos++;
		}

		flag = 1;
	}
}

int main()
{
	init_int0();
	sei(); 						// Enable interrupts

	while(1) {	

		while(!flag);

		// Do with data from the remote control here what you like.
		flag = 0;
	}

	return 0;
}

External Links