IR Receiver Code

Anonymous
2014-02-14
2014-03-05
  • Anonymous - 2014-02-14

    Hello all,

    Here's some code that demonstrates how to receive codes from a handheld remote control unit. I've used both a Sony TV remote which I got for $2.00 from All Electronics, and also a universal remote set to Sony TV mode. The program gets both the device number and the key number, and also translates the key number to English. The received results are displayed on an LCD.

    ;A program to receive IR codes sent by a Sony
    ;compatible handheld remote control.
    
    #chip 16F88, 8                    ;PIC16F88 running at 8 MHz
    #config mclr=off                  ;reset handled internally
    #config osc=int                   ;use internal clock
    
    ;----- Constants
    
    #define LCD_IO      4             ;4-bit mode
    #define LCD_RS      PortB.2       ;pin 8 is Register Select
    #define LCD_Enable  PortB.3       ;pin 9 is Enable
    #define LCD_DB4     PortB.4       ;DB4 on pin 10
    #define LCD_DB5     PortB.5       ;DB5 on pin 11
    #define LCD_DB6     PortB.6       ;DB6 on pin 12
    #define LCD_DB7     PortB.7       ;DB7 on pin 13
    #define LCD_NO_RW   1             ;ground RW line on LCD
    #define IR          PortA.0       ;sensor on pin 17
    
    ;----- Variables
    
    dim device, cmd, count, i as byte
    dim pulse(12)                     ;pulse count array
    dim button as string              ;ASCII for button label
    
    ;----- Program
    
    dir PortA in                      ;A.0 is IR input
    dir PortB out                     ;B.2 - B.6 for LCD
    
    cls                               ;clear the LCD
    print "Dev:    Cmd:"              ;logo for top line
    locate 1,0
    print "Button:"                   ;logo for second line
    
    do
      getIR, cmd                      ;wait for IR signal
      printCmd                        ;show device and command
      printKey                        ;show key label
      wait 10 mS                      ;ignore any repeats
    loop                              ;repeat forever
    
    ;----- Subroutines
    
    sub getIR
      tarry1:
        count = 0                     ;wait for start bit
        do while IR = 0               ;measure width (active low)
          wait 100 uS                 ;24 X 100 uS = 2.4 mS
          count += 1      
        loop
      if count < 20 then goto tarry1  ;less than this so wait
    
      for i=1 to 12                   ;read/store the 12 pulses
        tarry2:
          count = 0
          do while IR = 0             ;zero = 6 units = 0.6 mS
            wait 100 uS               ;one = 12 units = 1.2 mS
            count += 1
          loop
        if count < 4 then goto tarry2 ;too small to be legit
        pulse(i) = count              ;else store pulse width
      next 
    
      cmd = 0                         ;command built up here
      for i = 1 to 7                  ;1st seven bits are the cmd
        cmd = cmd / 2                 ;shift into place
        if pulse(i) > 10 then         ;longer than 10 mS
           cmd = cmd + 64             ;so call it a one    
        end if
      next
    
      device = 0                      ;device number built up here
      for i=8 to 12                   ;next 5 bits are device number
        device = device / 2           
        if pulse(i) > 10 then
           device = device + 16
        end if
      next  
    end sub 
    
    sub printCmd            ;print device number
      locate 0,5
      print "   "
      locate 0,5
      print device
    
      locate 0,13           ;print raw command number
      print "   "
      locate 0,13
      print cmd
    end sub
    
    sub PrintKey            ;print translated button
      locate 1,9
      print "       "
      locate 1,9
    
      select case cmd       ;translate command code
        case 0
          button = "One"
        case 1
          button = "Two"
        case 2
          button = "Three"      
        case 3
          button = "Four"
        case 4
          button = "Five"
        case 5
          button = "Six"      
        case 6
          button = "Seven"
        case 7
          button = "Eight"
        case 8
          button = "Nine"     
        case 9
          button = "Zero"
        case 10
          button = "#####"
        case 11
          button = "Enter"      
        case 12
          button = "#####"
        case 13
          button = "#####"
        case 14
          button = "#####"      
        case 15
          button = "#####"
        case 16
          button = "Chan+"
        case 17
          button = "Chan-"      
        case 18
          button = "Vol+"
        case 19
          button = "Vol-"
        case 20
          button = "Mute"     
        case 21
          button = "Power"      
        case else
          button = "     "
      end select  
      print button
    end sub
    

    Thomas Henry

     
  • Anobium

    Anobium - 2014-02-14

    Very nice.

    What device are you using as your IR input? Connection and pull ups?

    May I use the code in the new help file as an example of SELECT CASE?

     
  • Anonymous - 2014-02-14

    Yes, feel free to use this any way you want. Attached is the schematic with the connection details. I got the IR receiver for fifty cents several years ago from All Electronics.

     
  • JANIS

    JANIS - 2014-02-27

    Hello Thomas Henry!
    This is a super project. How long are creating a program code?
    Why did you choose sony remote control?

     
  • JANIS

    JANIS - 2014-02-27

    Hello Thomas Henry!
    This is a super project. How long are creating a program code?
    Why did you choose sony remote control?

     
  • Anonymous - 2014-02-28

    Hi Janis,

    It didn't take very long to whip up the code at all. That's how easy Great Cow Basic is to use. I don't think it took more than hour or two to get it working.

    The only reason I picked the Sony controller was because I found it surplus for $2.00 from All Electronics! I feel certain you could modify the coding to work with most any controller.

    Thanks for your comments,

    Thomas Henry

     
  • JANIS

    JANIS - 2014-03-03

    New help files I can not open content! Content index on left is visible, but when you open it, help description is not visible!

     
  • Anobium

    Anobium - 2014-03-04

    You need to move the file to a Windows trusted location or change the security of the file to be a trusted file.

    Select the downloaded file, select Properties/General Tab/Select Unblock.

     
    Last edit: Anobium 2014-03-04
  • JANIS

    JANIS - 2014-03-05

    Thank you!
    I would like to create a program code for any IR remote. Read any IR remote control code and saved in EEPROM, then the pre-programmed with the remote control keys could change the value of the necessary bits.

     
  • Anobium

    Anobium - 2014-03-05

    Get busy!! This is a great idea.

     

Log in to post a comment.