Searching \ for 'Sharp IR source' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page:
Search entire site for: 'Sharp IR source'.

Truncated match.
PICList Thread
'Sharp IR source'
1995\11\20@023956 by nigelg

picon face
I've had quite a few requests for the code, so I'm posting it to the list.


The remote control format currently used in Sharp products consists of 250uS
pulses of 40KHz (10 cycles), the actual signal is carried in the gaps between
these pulses, where a 500uS gap is a '0', and a 1500uS gap a '1'. The format
uses 15 bits, which is then repeated with 10 of the bits inverted to enable
validation of a correctly received code.

As is obvious from the above discription, the actual length of each word is
dependent on the data it contains - the second word always appears 62ms after
the start pulse from the first word, this means the time from the end of the
first word to the beginning of the second varies with the data.

The codes consist of 4 parts.

The first 5 bits are the system code, this distinguishes between TV, VCR, etc.

The next 6 bits are the actual data code, eg '1', '2', 'Vol Up' etc.

The next 3 bits are extension bits, these are used if more than 63 commands are

The last bit signifys wether the current word is inverted or not, '0' for a
normal word, '1' for the inverted version.

During the inverted word, only the last 10 bits are inverted, the system code
remains identical.


The hardware is very simple, it uses an IR preamp IC from a TV. This is a
three pin device which has connections for Earth, Power, and IR out (requires
a pull-up resistor). The version I'm using is a replacement for a Tatung 190
series, although any should be suitable - the IR out connection is normally
high, and goes low when 40KHz modulated Infrared is detected.

Power to the IC should be fed through a small resistor (220 ohms or so), and
decoupled with a 10uF capacitor. The IR out pin is connected to the 5 volt
supply via a 4.7kohm pull-up resistor.

I've got this built on a piece of vero-board along with 4 switches connected
to PortA and 8 LED's connected to PortB. This then connects via ribbon cable
to Don McKenzie's PICEX board, and from there to Don's programmer board, this
allows fairly rapid in-circuit programming and testing.

The IR input is connected to the RTCC input, but it can be used on any of
the input ports - just alter the constant IR_In at the beginning.


This is the first PIC project I've tried to write, the initial idea was to
be able to use an RC to turn on or off 8 'devices', for testing purposes these
are LED's connected to PortB via 330 ohm resistors. At the present the
software also checks the keys on PortA, it then toggles the relevent LED, I
started adding this on the assumption that local keys would be useful.

As it stands, it uses codes from a Sharp Audio RC, I have also tried it using
remotes for Sharp TV's, VCR's, and CD players - in these cases just alter the
System code, examples are provided commented out.

I don't claim that the design is finished yet, but it does work, and should
make a useful starting point for anyone wishing to receive IR remote controls.
If anyone has any helpful comments, I'd be more than pleased to receive them.

;Read Sharp IR remote & turn LED's on or off.

               LIST      P=16C84, F=INHX8M
               include ""

Data_Hi         Equ     0x10  ;buffers for first IR data
Data_Lo         Equ     0x11
Data1_Hi        Equ     0x12  ;buffers for second (inverted) IR data
Data1_Lo        Equ     0x13
Num_Bits        Equ     0x14  ;register for number of bits to read
Delay_Count     EQU     0x15  ;used in key de-bounce routine
Delay_2         EQU     0x16  ;ditto
Count           EQU     0x17  ;register for count of pulse width
Check           Equ     0x18  ;Error Flags register
Key_Code        Equ     0x19
IR_In           Equ     4     ;bit position of IR sensor in PORTA
IR_Error        Equ     1     ;Incorrect system flag
IR_Timeout      Equ     2     ;Timed out flag
IR_System       EQU     0x24  ;Sharp audio RC
;IR_System       Equ     0x14  ;Sharp CD player RC
;IR_System       Equ     0x40  ;Sharp TV RC
;IR_System       Equ     0x60  ;Sharp VCR RC
Power           Equ     0x68  ;individual key values for Audio RC.
XBass           Equ     0x74
Ch_Up           Equ     0x88
Ch_Down         Equ     0x48
Vol_Up          Equ     0x28
Vol_Down        Equ     0xa8
CD              Equ     0xd8
Tuner           Equ     0x98
Tape            Equ     0x58
Band            Equ     0x6c
Vid_Aux         Equ     0xf9

               ORG 0000

Start           CALL    Init_Ports

Main_Loop       CLRF    Key_Code           ;clear returned key value.
               CALL    Check_ForIR        ;see if IR key pressed.
               CALL    Check_ForKey       ;see if key pressed
               CALL    Display            ;update display
               GOTO    Main_Loop


Init_Ports      BSF     STATUS    , RP0
               CLRF    TRISB
               MOVLW   0xFF
               MOVWF   TRISA
               BCF     STATUS    , RP0
               CLRF    PORTA
               CLRF    PORTB

;Display subroutine, actions value in Key_Code

Display         MOVLW    0x00          ;0, no key match found.
               XORWF    Key_Code, w
               BTFSC   STATUS, Z
               Return                 ;so return, no action
               MOVLW    0x01          ;Power - turns LED 0 on
               XORWF    Key_Code, w
               BTFSC   STATUS, Z
               BSF     PORTB, 0
               MOVLW    0x02          ;XBass - turns LED 0 off
               XORWF    Key_Code, w
               BTFSC   STATUS, Z
               BCF     PORTB, 0
               MOVLW    0x03          ;Ch_Up - turns LED 1 on
               XORWF    Key_Code, w
               BTFSC   STATUS, Z
               BSF     PORTB, 1
               MOVLW    0x04          ;Ch_Down - turns LED 1 off
               XORWF    Key_Code, w
               BTFSC   STATUS, Z
               BCF     PORTB, 1
               MOVLW    0x05          ;Vol_Up - turns LED 2 on
               XORWF    Key_Code, w
               BTFSC   STATUS, Z
               BSF     PORTB, 2
               MOVLW    0x06          ;Vol_Down - turns LED 2 off
               XORWF    Key_Code, w
               BTFSC   STATUS, Z
               BCF     PORTB, 2
               MOVLW    0x07          ;CD - turns LED 3 on
               XORWF    Key_Code, w
               BTFSC   STATUS, Z
               BSF     PORTB, 3
               MOVLW    0x08          ;Tuner - turns LED 3 off
               XORWF    Key_Code, w
               BTFSC   STATUS, Z
               BCF     PORTB, 3
               MOVLW    0x09          ;Band - turns LED 4 on
               XORWF    Key_Code, w
               BTFSC   STATUS, Z
               BSF     PORTB, 4
               MOVLW    0x0a          ;Vid_Aux - turns LED 4 off
               XORWF    Key_Code, w
               BTFSC   STATUS, Z
               BCF     PORTB, 4
               MOVLW    0x11          ;First Key pressed, toggle LED 0
               XORWF    Key_Code, w
               BTFSS   STATUS, Z
               Goto    Try_Second
               MOVLW   0x01
               XORWF   PORTB, f
Try_Second      MOVLW    0x12          ;Second Key pressed, toggle LED 1
               XORWF    Key_Code, w
               BTFSS   STATUS, Z
               Goto    Try_Third
               MOVLW   0x02
               XORWF   PORTB, f
Try_Third       MOVLW    0x13          ;Third Key pressed, toggle LED 2
               XORWF    Key_Code, w
               BTFSS   STATUS, Z
               Goto    Try_Fourth
               MOVLW   0x04
               XORWF   PORTB, f
Try_Fourth      MOVLW    0x14          ;Fourth Key pressed, toggle LED 3
               XORWF    Key_Code, w
               BTFSS   STATUS, Z
               MOVLW   0x08
               XORWF   PORTB, f

;IR subroutines

Check_ForIR     BTFSC   PORTA, IR_In       ;look for IR start.
               Return                     ;return if no signal.
               CALL    Read_IR            ;Get first IR signal
               BTFSC   Check, IR_Timeout  ;check if timed out.
               MOVFW   Data_Hi
               MOVWF   Data1_Hi           ;move first readings to buffer
               MOVFW   Data_Lo
               MOVWF   Data1_Lo
Wait_Negative   BTFSC   PORTA, IR_In       ;look for second IR start.
               Goto    Wait_Negative
               CALL    Read_IR            ;get second IR signal
               BTFSC   Check, IR_Timeout  ;check if timed out

;validate the IR data

Check_IR        BTFSS   Data_Lo, 0
               Goto    Flip_First       ;invert the reversed data
               MOVLW   0x03
               XORWF   Data_Hi, f
               MOVLW   0xff
               XORWF   Data_Lo, f
               GOTO    Chk_Valid
Flip_First      MOVLW   0x03
               XORWF   Data1_Hi, f
               MOVLW   0xff
               XORWF   Data1_Lo, f
Chk_Valid       BCF     Check, IR_Error  ;clear the error flag
               MOVFW   Data1_Hi
               XORWF   Data_Hi, w       ;make sure hi bytes match
               BTFSS   STATUS, Z
               BSF     Check, IR_Error  ;else set error flag
               MOVFW   Data1_Lo
               XORWF   Data_Lo, w       ;same for lo byte
               BTFSS   STATUS, Z
               BSF     Check, IR_Error
               MOVFW   Data_Hi
               ANDLW   0xFC             ;mask off lower 2 bits
               XORLW   IR_System        ;check if correct system
               BTFSS   STATUS, Z
               BSF     Check, IR_Error  ;set error flag if wrong system code
               RRF     Data_Hi, f       ;shift data word to correct place
               RRF     Data_Lo, f
               RRF     Data_Hi, f       ;Move all data to Data_Lo
               RRF     Data_Lo, f       ;ignore the ninth bit

;Search index for key match.

               MOVLW    0x0b           ;number of valid commands in table
               MOVWF    Key_Code
Try_Next        MOVF     Key_Code, W    ;Move index value to W
               CALL     Table          ;Get Key value in W
               XORWF    Data_Lo, W
               BTFSC   STATUS, Z
               GOTO    Key_Matched     ;match found, exit routine
               DECFSZ  Key_Code, f     ;try next code
               GOTO    Try_Next
Key_Matched     RETURN                  ;return key in Key_Code, 0 for no-match

Read_IR         Clrf    Data_Hi            ;Clear data buffers
               Clrf    Data_Lo
               BCF     Check, IR_Timeout  ;clear timeout flag
               MOVLW   0x0f               ;set number of bits to read.
               MOVWF   Num_Bits
Bit_Loop        Call    Count_Pulse        ;returns value in Count
               BTFSC   Check, IR_Timeout  ;Quit if timed out
               MOVLW   0x54               ;load W with mid Count value
               SUBWF   Count, W           ;subtract from Count value
               BTFSC   STATUS, C          ;was returned Count equal to
               Goto    One_Pulse          ; a '1'
               BCF     STATUS, C
               Goto    Zero_Pulse         ;or a '0'
One_Pulse       BSF     STATUS, C
Zero_Pulse      RLF     Data_Lo, f         ;rotate bit into buffers
               RLF     Data_Hi, f
               DECFSZ  Num_Bits, f        ;done all bits?.
               Goto    Bit_Loop           ;else go fetch next bit
               CALL    Wait_Positive      ;wait for end of last IR pulse

Count_Pulse     Clrf    Count
               CALL    Wait_Positive      ;wait till end of pulse.
Pulse_Loop      BTFSS   PORTA, IR_In       ;count space until next negative
               Goto    Pulse_End
               MOVLW   0x03               ;use a loop for delay
               MOVWF   Delay_Count        ;to keep Count in one register
Count_Loop      DECFSZ  Delay_Count , f
               GOTO Count_Loop
               INCFSZ  Count, f           ;check if timeout!
               Goto    Pulse_Loop
               BSF     Check, IR_Timeout
Pulse_End       RETURN

Wait_Positive   BTFSS   PORTA, IR_In       ;look for end of IR pulse.
               Goto    Wait_Positive
               Return                     ;return after positive going.

;key reading routines.

Check_ForKey    MOVLW    0x00              ;not 0, IR key match already found.
               XORWF    Key_Code, w
               BTFSS   STATUS, Z
               Return                     ;so return, don't check keys
Key_Zero        BTFSC   PORTA, 0           ;check for key press
               GOTO    Key_One            ;Try next key
               Call    Bounce_Delay       ;key pressed, de-bounce.
Key0_Pressed    BTFSS   PORTA, 0           ;wait for key release
               Goto    Key0_Pressed
               MOVLW   0x11               ;set key_code value for first key
               MOVWF   Key_Code
Key_One         BTFSC   PORTA, 1           ;check for key press
               Goto    Key_Two            ;Try next key
               Call    Bounce_Delay       ;key pressed, de-bounce.
Key1_Pressed    BTFSS   PORTA, 1           ;wait for key release
               Goto    Key1_Pressed
               MOVLW   0x12               ;set key_code value for 2nd key
               MOVWF   Key_Code
Key_Two         BTFSC   PORTA, 2           ;check for key press
               Goto    Key_Three          ;return if not pressed
               Call    Bounce_Delay       ;key pressed, de-bounce.
Key2_Pressed    BTFSS   PORTA, 2           ;wait for key release
               Goto    Key2_Pressed
               MOVLW   0x13               ;set key_code value for 3rd key
               MOVWF   Key_Code
Key_Three       BTFSC   PORTA, 3           ;check for key press
               Return                     ;return if not pressed
               Call    Bounce_Delay       ;key pressed, de-bounce.
Key3_Pressed    BTFSS   PORTA, 3           ;wait for key release
               Goto    Key3_Pressed
               MOVLW   0x14               ;set key_code value for 4th key
               MOVWF   Key_Code

Bounce_Delay    MOVLW   01h
               MOVWF   Delay_Count
Loop1           MOVWF   Delay_2
Loop2           DECFSZ  Delay_2     , f
               GOTO    Loop2
               DECFSZ  Delay_Count , f
               GOTO Loop1

;index table for IR keys

Table           ADDWF   PCL
               RETLW   0x00
               RETLW   Power
               RETLW   XBass
               RETLW   Ch_Up
               RETLW   Ch_Down
               RETLW   Vol_Up
               RETLW   Vol_Down
               RETLW   CD
               RETLW   Tuner
               RETLW   Band
               RETLW   Vid_Aux
               RETLW   Tape


         | Nigel Goodwin   | Internet : |
         | Lower Pilsley   |                                        |
         | Chesterfield    |                                        |
         | England         |                                        |

More... (looser matching)
- Last day of these posts
- In 1995 , 1996 only
- Today
- New search...