PIC Beginner - Infrared Receiver and IR Protocol


Closed Thread
Results 1 to 18 of 18
  1. #1
    Join Date
    Aug 2009
    Posts
    9

    Default PIC Beginner - Infrared Receiver and IR Protocol

    I'm a beginner for PIC Basic Program. I want to control my PC Program with IR Remote Control. In my place, we can get various IR remote control but a few type of PIC : 16F84A, 16F628A, 16F648A, 16F877A and 18F4550 can available.

    I use 16F84A and 4MHz Crystal, RS232 for IR Remote Control.

    My first program is IR Remote Receiver with Sony IR Protocol. I choose a DVD Remote Control (Made in China). I tested my receiver to my PC via COM port. It did not work. So I choose universal remote control and assign SONY. Now It's ok.

    Secondly, I write my program with RC-5 Protocol. This code is posted by Bruce (http://www.picbasic.co.uk/forum/arch...php/t-308.html) It's also ok for universal remote that assigned for Philips. But it don't work for that DVD Remote Control.

    Then I tested simply that DVD Remote Control's IR signal with LED. LED is worked between 900ms and 920 ms. It may be Header of IR train. I searched data of all protocols (http://www.sbprojects.com/knowledge/ir/ir.htm). I thought that DVD remote use NEC protocol. I also found that most of China DVD player's remote control use NEC IR protocol in a manual of DVD Player.

    I learn NEC IR Protocol. It's type is different for Sony Protocol. Sony use Pulse Width Modulation [PWM]. But NEC use Pulse Distance Modulation [PDM]. I think that PulsIn is use to measure the pulse width. Is it right? So what is use to measure the distance between pulse. Is it [PulsIn + PAUSEUS]?

    For NEC Protocol, I refer dhouston's post (http://www.picbasic.co.uk/forum/show...7859#post77859). My receiver's LED is ok for that DVD remote any key press but in my PC program all signal is Zero.

    Any help would be greatly appreciated.

    If this receiver is ok, I want to do Universal Infrared Receiver (UIR) with USB (with ref: eg. A universal algorithm for implementing an infrared decoder: www.edn.com/article/CA6653644.html.

    Regard

    Moe
    Last edited by komoe01; - 29th August 2009 at 22:04.

  2. #2
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    A schematic of your IR LED circuitry is necessary so we can see whether you need to measure pulses or spaces. PulsIn measures spaces as well as pulses - you merely need to indicate the polarirty. There are some example circuits here...

  3. #3
    Join Date
    Aug 2009
    Posts
    9


    Did you find this post helpful? Yes | No

    Default PIC Beginner - Infrared Receiver and IR Protocol SONY AND NEC

    Thanks for reply

    IR LED circuitry is here.( please see attachment ).

    I use this circuit and test Sony Protocol. LED is blink for any key and correct key code is accept in PC via RS232.

    Here is code for Sony.

    'start code for Sony Protocol --------------------------------------------------------------

    'PIC 16F84A Sony SIRC Protocol

    Include "Modedefs.Bas"
    Define OSC 4 ' Set Xtal Frequency

    Header var word
    Body var word
    i var byte
    tmp var Byte
    irButton var byte
    irDevice var byte

    irIN var PortA.2 'IR Input
    ToCOM var PortB.4 'Serial Data to compute COM port [RS232]

    'next time use both Sony and NEC Protocol in a PIC
    'Same Key from different remote do different tasks
    'eg Key 1 from Sony Remote ----> task1 in PC
    ' Key 1 from NEC Remote -----> task2 in PC

    'SonyLED is blink for Sony and nec LED is blink for NEC Remote
    SonyLED var PortB.2 'Green LED for Sony Remote keypress
    necLED var PortB.3 'Red LED for NEC Remote keypress (Reserved)

    TrisA = %00000100 ' Input RA2 only
    TrisB = 0 ' Output All Port B

    Start:
    LOW SONYLED : LOW NECLED
    IRBUTTON=255: IRDEVICE=255

    'in Header is Sony 240
    Pulsin irin,0,header ' Measure the header length.
    if header > 270 or header < 200 then goto Start

    'if correct header read body 12 bit
    for i =0 to 11
    pulsin irin,0,tmp ' Measure the body length.
    if tmp >= 90 then
    Body.0[i]=1 'in Sony Logic 1 is 120
    else
    Body.0[i]=0 'in Sony Logic 0 is 60
    endif
    next

    IRBUTTON = Body & %01111111 'Mask 7 bit
    IRDEVICE = (Body >>7) & %00011111 'Mask 5 bit

    If IRDEVICE=255 then goto start

    Serout ToCom ,N9600,[IRBUTTON] 'to PC COM - Port

    if IRBUTTON >=0 and IRBUTTON < 255 then
    high Sonyled
    pause 300
    low Sonyled
    endif

    pause 100
    goto start


    'end of code for Sony Protocol ------------------------------------------------------

    I use this circuit again for NEC or CHINA DVD Remote Control.

    ' start code for NEC OR that DVD REMOTE -----------------------------
    'PIC 16F84A NEC Protocol Protocol

    Include "Modedefs.Bas"
    Define OSC 4 ' Set Xtal Frequency

    DEFINE PULSIN_MAX 968 '>968 RETURNS 0

    Header var word
    Body var word
    i var byte
    tmp var Byte
    irButton var byte
    irDevice var byte

    irIN var PortA.2 'IR Input
    ToCOM var PortB.4 'Serial Data to compute COM port [RS232]

    'nec LED is blink for NEC Remote
    necLED var PortB.3 'Red LED for NEC Remote keypress

    TrisA = %00000100 ' Input RA2 only
    TrisB = 0 ' Output All Port B

    Start:
    LOW NECLED
    IRBUTTON=255: IRDEVICE=255

    'in Header is NEC 900
    Pulsin irin,0,header ' Measure the header length.
    if header < 792 then goto Start

    While irIN=0:Wend 'wait pulse

    'if correct header read body 32 bit
    for i =0 to 31
    pulsin irin,0,tmp ' Measure the body length.

    If ((tmp<40) Or (tmp>180)) Then start

    if tmp >= 75 then
    Body.0[i]=1
    endif
    next

    IRBUTTON = Body & %11111111 'Mask 8 bit
    IRDEVICE = (Body >>16) & %11111111 'Mask 8 bit

    If IRDEVICE=255 then goto start

    Serout ToCom ,N9600,[IRBUTTON] 'to PC COM - Port

    if IRBUTTON >=0 and IRBUTTON < 255 then
    high necLED
    pause 300
    low necLED
    endif

    pause 100
    goto start

    ' end of code for NEC OR that DVD REMOTE ----------------------------

    LED is blink for Both remote control. But Data in my PC software All keys are same number.

    How can I fix it.

    Thanks.

    Moe
    Attached Images Attached Images  
    Last edited by komoe01; - 30th August 2009 at 12:02.

  4. #4
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    From your schematic it appears you are using an IR receiver. Which one?

    IR receivers usually are active low while the code I supplied for the NEC protocol was for RF where the receiver is active high. Also, my code was meant to work with the RF transmitter code that accompanied it which used slightly different timing than the NEC IR protocol.

    The EDN article is wrong about details of the NEC IR protocol. It is explained in this NEC datasheet.You will need to modify my RF code in order to receive/decode IR codes.
    Code:
    '12F629
    
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
    
    DEFINE PULSIN_MAX 1140				'>1140 RETURNS 0
    DEFINE DEBUG_REG GPIO
    DEFINE DEBUG_BIT 2				'GPIO.2
    DEFINE DEBUG_MODE 1 				'Inverted
    DEFINE DEBUG_BAUD 9600
    DEFINE OSCCAL_1K 1
    
    IR      VAR     byte[4]
    pulse   VAR     byte
    i       VAR     byte	         	
    stx     VAR     word            	'start of transmission
    
            CMCON = 7                   'comparators off
            Debug "IR NEC PROTOCOL"
    init:   IR[0]=0:IR[1]=0:IR[2]=0:IR[3]=0:i=0
            PulsIn GPIO.1, 0, stx	
            If (stx<760) Then init  		
            'debug #stx                 
            While GPIO.1=1:Wend			'wait space
            Repeat
              PulsIn GPIO.1, 1, pulse
              If (pulse>100) Then
                IR.0(i)=1				'set bit
              EndIf
              i=i+1 
            Until (i>31)
            For i = 0 To 3
              Debug IHEX2(IR[i] REV 8)
            Next
            GoTo init
    
            End
    I have not tested the above code (I don't have time to build a circuit with an IR receiver.) and you will need to convert it for your PIC and the pins you are using.

    Note also that my code does not deal with the NEC shortcut repeat code.
    Last edited by dhouston; - 30th August 2009 at 12:55.

  5. #5
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Change...
    Code:
            For i = 0 To 3
              Debug IHEX2(IR[i] REV 8)
            Next
    to...
    Code:
            For i = 0 To 3
              Debug IHEX2 IR[i]
            Next
    The bit order is already reversed as the bits are set by...
    Code:
    RF.0(i)=1

  6. #6
    Join Date
    Apr 2003
    Location
    Cambridge - UK
    Posts
    1,030


    Did you find this post helpful? Yes | No

    Default

    Moe, you might want to read this article: Controlling the world from your armchair

  7. #7
    Join Date
    Apr 2003
    Location
    Cambridge - UK
    Posts
    1,030


    Did you find this post helpful? Yes | No

    Default

    Moe, Infrared remote control decoding maybe considered something of a black art, however, this tutorial will show you that its principals are quite straightforward, and easy to implement on a PIC microcontroler.

    Thanks to BASIC’s shallow learning curve, software designs that used to take weeks can now be realised in a just few hours. This article presents software solutions in the PicBASIC Pro language.

    You might want to read the article: Controlling the world from your armchair

  8. #8
    Join Date
    Apr 2003
    Location
    Cambridge - UK
    Posts
    1,030


    Did you find this post helpful? Yes | No

    Default

    The article is an extract from the book, EXPERIMENTING with the PICBASIC PRO COMPILER, written by Les Johnson.

    The book is cuerrently out of publication, however we are considering publishing an online version as part of the new PIC BASIC portal that we are developing to support PIC BASIC Users.

  9. #9
    Join Date
    Aug 2009
    Posts
    9


    Did you find this post helpful? Yes | No

    Default Pulsin and IR

    dhouston,
    When I change code, old result 0 turn to 255. All the keys press are same.




    Header Logic 0 Logic 1 Logic 1 logic 0

    2400 600 600 600 1200 600 1200 600 600
    x a b c d e f g h i j
    +------------------------+ +------+ +--------------+ +--------------+ +------+
    | | | | | | | | | |
    | | | | | | | | | |
    | | | | | | | | | |
    ---+ +------+ +------+ +------+ +------+ +------
    x____________________________|_____________|______ _____________|___________________|_____________|
    pulsin header 1st loop 2nd loop 3rd loop 4th loop

    return width of length





    -- code start -------------------------------
    Code:
              
             pulsin irIn , 0 , Header       'Line 1
        
                for i =0 to 3
                   pulsin irin, 0 , tmp       'line 3
    
                   if tmp > 90 then                
                        body.0[i] = 1         'line 5 
                   else
                        body.0[i] = 0         'line 7  
                   endif
                next
    -- end of code -------------------------------

    I think pulsin work as follow:

    1. pulsin is timeout when no rising edge was found. return width is zero.
    2. pulsin is time out when rising edge was found and then until falling edge. return the width length.


    so pulsin in code run like that:


    Line 1 : pulsin start measure (x) to end edge (b). Width is (a) to (b) so Header = 2400 (space (x) to (a) is not count.

    Loop 0 (1st loop)
    Line 3 : pulsin start measure (b) to end edge (d). Width is (c) to (d) so tmp = 600 (space (b) to (c) is not count.
    Line 5 and 7: see the width lenght (not space) and decided logic 0
    Loop 1 (2nd loop)
    Line 3 : pulsin start measure (d) to end edge (f). Width is (e) to (f) so tmp = 1200 (space (d) to (e) is not count.
    Line 5 and 7: see the width lenght (not space) and decide logic 1
    Loop 2 (3rd loop)
    Line 3 : pulsin start measure (f) to end edge (h). Width is (g) to (h) so tmp = 1200 (space (f) to (g) is not count.
    Line 5 and 7: see the width lenght (not space) and decide logic 1
    Loop 3 (4th loop)
    Line 3 : pulsin start measure (h) to end edge (j). Width is (i) to (j) so tmp = 600 (space (h) to (i) is not count.
    Line 5 and 7: see the width lenght (not space) and decide logic 0

    PULSIN can only measure the width length.

    When this code is used for NEC Protocol, all pulse are equal depending upon if statement (Logic 0 or 1)
    Because In NEC, logic 1 or 0 is determined by space length. How can I measure space with pulsin.

    lester
    Thanks.

    I already read Les Johnson's article and "Section 6: Experimenting with Remote Control" of that book. I already tested it. All are ok. But it's Sony IR Protocol (Pulse Width Modulation) that quite different from NEC IR Protocol (Pulse Distance Modulation).
    I admire you and dhouston when I found your join date.
    I start and learn picbasic at last week of July of this year. I search and collect PIC books (picbasic, C and Assembly) and article. I found about 60 books and articles. But I cannot find enough explanation for a command. eg. I want to know pulsin in detail. How pulsin work? examples relating pulsin. Pulsin in "PicBasic Pro Manual" is general. Picbasic is best language for me although I've little experience with C Language.


    Moe
    Last edited by komoe01; - 31st August 2009 at 13:27.

  10. #10
    Join Date
    Aug 2009
    Posts
    9


    Did you find this post helpful? Yes | No

    Default please see ir wave

    Sorry sample wave is broken.



    If wave picture is not found, Please see it in attachment picture

    Moe
    Attached Images Attached Images  
    Last edited by komoe01; - 31st August 2009 at 13:35.

  11. #11
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by komoe01 View Post
    When I change code, old result 0 turn to 255. All the keys press are same.
    It works fine for me. It's impossible for me to guess at what changes you have made to it. Try to test it using my code exactly as written. Once that works, try modifying it for your application, going step by step to isolate where the problem is being introduced.

    The waves you've attached (including the second one) are not NEC IR protocol.

    How are you sending the IR codes?

  12. #12
    Join Date
    Aug 2009
    Posts
    9


    Did you find this post helpful? Yes | No

    Default IR and PIC16FXXX

    dhouston

    The wave image is a sample (part of the sony protocol).


    Try to test it using my code exactly as written.
    Ok. dhouston

    But 12F629 pic is not available for me. Just a few PIC model can buy in my country. I have to use 16F84A or 16F628A. Beside I use IR, not RF.
    I'm a beginner for pic so I've not familiar with DEBUG that used in your code.

    So I write code to PIC than I see the result of LED signal and Computer program.

    When I test it using your code exactly, what can I see the result from where? Can I use Infrared remote control? or I must built your transmitter to test receiver.

    I think I must to change some code.

    I do my best as you say!

    Thanks.

    Moe

  13. #13
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    The code I posted above in this thread is for IR. If you study it you will see that the method is identical to Lester's.

    I have one 12F629 sending the 32 bit codes and another receiving the codes. Instead of IR transmitter and receiver I have the transmitter PIC connected directly to the receiver PIC. I do not have an IR receiver handy. However, I've used the same basic code in devices which did have an IR receiver.

    I quickly modified a simple test program written for other purposes to display the DEBUG output. See the attachment. You should see the same output ($50$AF$42$BD) every 15 secs) in whatever terminal program you are using.

    As for DEBUG, read the manual - it's similar to SerOut and SerOut2.

    You should be able to use my code merely by changing the GPIO.x pins to whatever RA.x pins you are using. I haven't used a 16F84A in ages so I'm not going to write the conversion for you.
    Attached Images Attached Images  
    Last edited by dhouston; - 31st August 2009 at 17:56.

  14. #14
    Join Date
    Aug 2009
    Posts
    9


    Did you find this post helpful? Yes | No

    Thumbs up Two IR Protocol in one PIC

    I changed pin assign.

    The result is successfully done.

    I test ten different kind of DVD Remote. Key positions are changed but all are work well.

    Now I run my program for two different tasks using same key of two different remote (Sony and NEC Protocol).

    ** two ir protocol can use in same pic **

    Adouston,
    THANK YOU VERY MUCH for your kindly help.

    I read NEC protocol and compare code.

    Header is 9ms
    - no header found, restart
    If (stx<760) Then init
    Space between header and body is 4.5ms
    -wait space
    While PortA.2=1:Wend
    Each pulse is a 560µs long
    A logical "1" takes 2.25ms to transmit (pulse 560µs + space )
    a logical "0" is only half of that, being 1.125ms (pulse 560µs + space )

    Please let me know:
    1. How to calculate (pulse>100) is logic 1.
    2. and PULSIN_MAX 1140 . why do you use 1140

    I say again. Thanks.

    Moe.
    Last edited by komoe01; - 1st September 2009 at 09:55.

  15. #15
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by komoe01 View Post
    Please let me know:
    1. How to calculate (pulse>100) is logic 1.
    2. and PULSIN_MAX 1140 . why do you use 1140
    I normally use RF where pulses & spaces are slightly different. I did the conversions to IR quickly, from memory and doing the math in my head so both figures are a bit on the high side.

    IR transmitters (i.e. remotes) that used the NEC chip actually used numbers of carrier cycles as the unit of time rather than absolute number of µS. They were also battery operated so there could be some variation due to voltage variations. It is recommended to allow ±20% although that's probably generous with more modern remotes (the protocol has been around for about 40 years).

    120% of 900 = 1080 instead of 1140.

    With this method we are only able to measure the spaces which can be 0.56mS or 1.125mS. We just want to determine whether it is one or the other. A figure about halfway between the two values should work so 85 might be better than the 100 I used.

    The NEC protocol is used by many manufacturers and you will encounter some that do not adhere exactly to the values given in the NEC datasheet so you need to build in a little flexibility.
    Last edited by dhouston; - 1st September 2009 at 12:56.

  16. #16
    Join Date
    Aug 2009
    Posts
    9


    Did you find this post helpful? Yes | No

    Default End of Thread

    Thanks
    dhouston
    for your explanation.

    Thanks
    lester
    for your suggestion.

    Now I should stop this thread.


    Moe

  17. #17
    Join Date
    Nov 2009
    Posts
    3


    Did you find this post helpful? Yes | No

    Default

    Lester, is there somewhere we can download the include files from the book? I'd like to use the Sony IR files in a project. Thanks!

    Quote Originally Posted by lester View Post
    The article is an extract from the book, EXPERIMENTING with the PICBASIC PRO COMPILER, written by Les Johnson.

    The book is cuerrently out of publication, however we are considering publishing an online version as part of the new PIC BASIC portal that we are developing to support PIC BASIC Users.

  18. #18
    Join Date
    Jun 2007
    Posts
    25


    Did you find this post helpful? Yes | No

    Default Re: PIC Beginner - Infrared Receiver and IR Protocol

    dears ,

    i know its a very long time , but please am stick on this , can you post a full working code for pic16f84a to decode nec protocol .
    many many thanks .

Similar Threads

  1. pic to pic ir link versus wired link : help please anyone
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 30th May 2008, 22:01
  2. PIC to PIC "wired" serial one-way communication - SERIN2/SEROUT2
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 14th April 2008, 21:02
  3. Infrared Receiver
    By emavil in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 8th December 2006, 02:03
  4. how to get pic to pic communication
    By kinsiro in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 30th August 2005, 18:12
  5. Can this IR Xmitter/Rcvr be improved?
    By peterdeco1 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 24th August 2005, 12:27

Members who have read this thread : 4

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts