Nov-3rd-2008

Using IR remote with AVR MCUs – Part II

Hello Friends,

Welcome back. In previous tutorial I introduced my IR remote decoding library. In this tutorial I will continue our discussion and show you how to add IR remote controls support to your AVR projects.

Step I

Download the library files and unzip them in a folder.

Step II

In AVR studio create a new AVR-GCC project. Then copy the following files to the project folder.

IR remote related.

Source Files

remote.c

Header Files

remote.h

rckeys.h

LCD Related

Source Files

lcd.c

Header Files

lcd.h

myutils.h

Note: Include file from the

\lib\ATmega8 if you are using ATmega8

\lib\ATmega16 if you are using ATmega16 or ATmega32

After The files have been copied add them to your project by right clicking project view and selecting “Add Existing Source File(s)…” and then select the “lcd.c”. Similarly add other source files. In the same way add the header files by selecting "Add Existing Header File(s)..."

avr studio

Adding files to projects.

 

If instead of AVR Studio you use Makefiles then add all the source files names in the source section of the makefile along with your main c file (which has same name as your project and has the main function).

Step III.

In your main C source file include the following files

#include "remote.h"

in addition to standard files

in your main() function initialize Remote Subsystem by calling

RemoteInit();

And your done with initialization.

Step IV

Once initialized the remote subsystem automatically receives command sent by remote and store them in a circular FIFO(first in first out queue). You can call the function GetRemoteCmd() to read from the buffer. The return type is a unsigned char (8bit) so the value can be from 0-255. This is the keycode of the key pressed on the remote . To know the key code of various keys simply run this example on MCU and press keys the key code will be displayed on the LCD. The GetRemoteCmd takes one argument which can be either 0 or 1. When you pass 1 the function checks the queue for command and if there none then it wait till a key is pressed on the remote control. It returns only if a key is pressed and return its keycode. In contrast to this if you pass 0 the function returns immediately if no key is pressed it returns a special constant RC_NONE which is defined as 255 in the header file. Otherwise it returns the key code of pressed key.

To make every thing more clear I am giving the complete sample program which will just wait for keypress and then display its code.

Sample Code


/*#########################################################

Demo program for testing the IR Remote control libraries.

-----------------------------------------------------------

Copyright Avinash Gupta 2008 avinash@eXtremeElectronics.co.in

Web: www.eXtremeElectronics.co.in

Please give credit or reference to www.eXtremeElectronics.co.in
if you use this in your projects and find it useful.


##########################################################*/

#include <avr/io.h>

#include "remote.h"
#include "lcd.h"

void main()
{
   uint8_t cmd=0;

   RemoteInit();

   LCDInit(LS_BLINK);

   LCDClear();

   LCDWriteString("IR RemoteDecoder");
   LCDWriteStringXY(0,1,"Key Code:");

   while(1)
   {
      cmd=GetRemoteCmd(1);
      LCDWriteIntXY(10,1,cmd,3);
   }
}


See also

Subscribe

If you are an AVR MCU fan, subscribe to my feed powered by Feedburner® and get all tutorials, codes and more delivered to you Inbox !!!

Comment

Don't forget to post a comment about how you liked this article or if you have any doubt and need help.


29 Responses to “Using IR remote with AVR MCUs – Part II”

  1. 1
    hamid Says:

    hi/please guide me that can i program a atmega32 with tow or more channel of adc in bascom compiler.thanks alot/best regard

  2. 2
    Avinash Says:

    Hi Hamid,
    I am not a BASCOM user but I prefer C for programming. But I will help you. I will contact you withing few days.

  3. 3
    rhys Says:

    Hi There,

    Thankyou for the library! It has been very helpfull in my learning curve! Could you maybe do tutorial for SPI Interface?

  4. 4
    Avinash Says:

    Hi Rhys,
    Thanks. Nice to know you liked my tutorials. I will surly write a tutorial on SPI.

  5. 5
    Rhys Says:

    Hi avanish,

    Thankyou for offering to do a SPI tutorial. First, can i ask, will this IR code run on the ATTINY44?

    Thankyou

  6. 6
    Avinash Says:

    Hello Rhys,

    First test it on ATmega8/16/32 because I am not sute what remote control protocol are more popular in your country. As I said there are two common format, Sony RC5 and NEC format. My Lib works for NEC as these are popular in India.

    If you get it working then you can port it to ATTINY44 but check
    1) Does the TIMER0 has compare unit ?

    May be you have to change the NAME of some registers and ISRs to get it running.

    I have not worked it ATTINY44 so pls Consult its database.

    When you are about to port it do contact me and I will help.

  7. 7
    Mike Says:

    Do you know of any problems if i used a RPM7138 IR sensor instead of a TSOP1738 ? I read that they are the same but it doesn’t seem to work for me.

  8. 8
    Akshay Says:

    Great Job!
    Thanx for the AVR Tutorials.Your tutorials are helping me a lot.Keep writing the Tutorials in future.

  9. 9
    Anshul Says:

    Hi Avinash
    Thank you very much for this great tutorial.
    But i have a problem .. which TV ir remote(sony,samsung etc) should i use for this decoder…?

  10. 10
    Avinash Says:

    @Anshul
    Use a DVD Played remote. They are easily available and cheap (Rs 40) and have maximum number of buttons. With nice navigation keys!

  11. 11
    Anshul Says:

    Hi Avinash ..Thank u for reply
    i hav Philips DVD player i tried this decoder on its remote. but m not getting the output…

  12. 12
    raghu Says:

    thnks for da lib but whn i use dem in my project i am geting an some errors could u please check dem out once more…..da errors ar occurring only whn i change da project setings from atmega32 to atmega16

  13. 13
    raghu Says:

    i have use bpl remote n lg remote also but thr is no decoded info on da lcd ……help pls

  14. 14
    Zagg Says:

    Hi! Many thanks for your great website !
    Its possible to use a TSOP1136 (36KHz) in replacement of the TSOP1738 (38KHz).
    I think the only diff is 36Khz/38KHz carrier.
    What I need to modify in the library for that works please ?
    Thanks in advance !

    Regards,

  15. 15
    sagar Says:

    Hi Avinash
    Thank you very much for this great tutorial.but i have tested ur libarary on Atmega8 with NEC format remote,it doesent work plese plese check it again bz i realey need it
    thank you.

  16. 16
    sagar Says:

    hi Avinash.
    in your responce no 48 ur say Crystal used is 12Mhz or 16Mhz Only but in your tutorial on IR remote interfacing with atmega8 lib it is compatable with 8/12/16Mhz,plez guide me which crystal frq.did i prefer

  17. 17
    Avinash Says:

    @Sagar use any crystal (12MhZ OR 16 mhZ)

    V E R Y I M P O RT A N T
    The chip must be configured to use the external crystal, other wise it is useless to attach external crystal.
    Fuse must be set as follows to enable that
    LOW FUSE = FF (Hex) HIGH FUSE =C9 (hex)

    Also in AVR Studio or Makefile correct frequency of crystal used must be set.

  18. 18
    sagar Says:

    hi Avinash
    thank you for very much for ur replay; But in ur “IR remote decoding library”the Atmega8 is compatable with 8Mhz and it uses 16-Bit Timer Timer1 only(if im correct).
    but in
    ISR(INT1_vect)
    {
    GICR&=(~(1<<INT1)); //Disable INT1
    sei();
    if(stop) return;
    unsigned int TempTime=Time;
    Time=0;
    TCNT0=0;???}
    I used internal 8Mhz & in main function i #define F_CPU 8000000UL LCD working very nicely,plese guide me bcz i realey need it

  19. 19
    S.M.Abdullah Says:

    Hi,

    I need to know which remote control should I used with NEC format protocol. If Philips or LG remote control works with this library.

    Thnaks

  20. 20
    prasanta Says:

    can i have the header file of remote.h

  21. 21
    Avinash Says:

    @Prasanta

    The link is in the above article!

  22. 22
    kunal Says:

    Dear avinash,
    I’ve made hardware as per ckt dia. but its notworking.
    I’ve one doubt, carrier for Philips RC5 is 36Khz.
    can above ckt will work on TOSP 1738 or it will work on TOSP 1736. Pls guide.

    Thanks in advance.

  23. 23
    murx Says:

    Hi Avinash

    Thanks for your effort on this stuff!

    I tried to use your lib on a ATmega16 with 16MHz XTal, but the content of GetRemoteCmd(0) is always “06″ no matter if i press any button or not.

    -The “hardware” works fine (i checked it with a simple code)
    -The Fusebits are set how you recommend it (FF/C9)

    Do you have some hint what could be wrong?
    I use WinAVR GCC

    Thanks in advance and Greets from Switzerland
    murx

  24. 24
    midOSmid Says:

    Great tutorials. Extremely useful.
    It’s amazing that you share your knowledge and experience with others.
    Good Luck

  25. 25
    kunal Says:

    Thanks Avinash,
    it works.
    i used TOSP 1730 and TOSP 1738 it works on both.
    i used remote control BPL RC604 (it is available in any remote shop for Rs. 50).
    Thanks again.

  26. 26
    KK Says:

    Hi, Avinash,

    I am using ATMEGA16 as my 1st atmega MCU.
    I am trying to write prg to control DVD, TV, AVPC all together in a single remote unit. Before that I should decode the IR first, but I fail as I searched the web which 2 methods are good for that. One is ICP and the other is just a timer.

    My fosc is 11.0592MHz without clock divde and fclk for ICP is 11.0592M/256=43.2KHz(23us). The capture mode is falling edge in ICP1 of timer1. I worked on CodevisionAVR, I think it is not related to platform. My timer0 is worked in 11.0592MHz/64=172.8KHz. Will interrupt priority caused problem????

    I just think the time is fast enough to dectect :

    NEC format
    Most remotes here are NEC format. I tried two days. The result is disappointed. Therefore, can u help me a bit with sending me your successful code for further evaluation. Pls help…..Thank you!

  27. 27
    rajkumar Says:

    Thanks a lot avinash. Your website is just too good for freshers like me. I am getting a lot of help from ur tutorials.

  28. 28
    AVR Project - Remote Controlled Fan Regulator. | eXtreme Electronics Says:

    [...] remotely control the speed of an AC fan and to switch it on or off. The remote control is a cheap NEC Format remote, usually supplied with small DVD players. Three buttons are used to command the circuit. The UP key [...]

  29. 29
    Michelle Says:

    hi Avinash,

    I am using a c program in the microcontroller, but i have a problem regarding about how to add an interrupts to my program. It is about this that when i will send a signal from a remote contol it will bypass all the running programs and make priority to the signal fom the remote control.. Please help me with this one… thanks!
    I can send you all the necessary info regarding with this. Again Thanks!

Leave a Reply

Comments

    • wlewis: @Av.. To my knowledge, practically all the non-ic humidity sensors are frequency dependant...
    • wlewis: @Av.. about ADC.. how about a tutorial that does a 10bit conversion?
    • wlewis: @Av I have a great idea for a tutorial… sensors which output are measured by frequency....
    • BoB: Hello… I’m still a newbie in this field..so, may I ask…this application used...
    • Hill: Why dont you mention in your URL which PICs these burner supports? It will help avoid guesswork...
    • victor: I have to rectify, that is not an issue, instead is a LCD’s McU limitation in locations...
    • wlewis: Avrdude code for atmega32 // 16mhz crystal // Jtag disabled. avrdude -p m32 -b 19200 -P COM3...

Video

  • Comments

    • wlewis: @Av.. To my knowledge, practically all the non-ic humidity sensors are frequency dependant...
    • wlewis: @Av.. about ADC.. how about a tutorial that does a 10bit conversion?
    • wlewis: @Av I have a great idea for a tutorial… sensors which output are measured by frequency....
    • BoB: Hello… I’m still a newbie in this field..so, may I ask…this application used...
    • Hill: Why dont you mention in your URL which PICs these burner supports? It will help avoid guesswork...
    • victor: I have to rectify, that is not an issue, instead is a LCD’s McU limitation in locations...
    • wlewis: Avrdude code for atmega32 // 16mhz crystal // Jtag disabled. avrdude -p m32 -b 19200 -P COM3...