Sunday, June 5, 2011

IR Remote Controlled Switchboard (RC5 protocol decoder)

In an earlier article, I explained how a single switch can be controlled by using a simple IR transmitter and a simple receiver circuit. This article explains how to extenmd the same concept to control a number of switches.

Now, as more than one switches are to be controlled, we have to transmit a number of bits through IR transmission to indicate which is key is pressed on the remote and accordingly which switch of the switch-board to control. In this project, this is done by using a ready-made IR remote control which works on RC5 protocol.
RC5 protocol was developed by Phillips for controlling consumer electronics such as TV, VCD-DVD Player,etc. through IR Remote Controller. This type remote controller can be found in Phillips' 90 channel TVs. This is a very popular IR protocol and very easy to understand.
Basically, in a RC5 protocol, when a key on the remote controller is pressed, a total of 14 bits are serially transmitted. This includes 2 Start bits, 1 Toggle bit, 5 Address bit and 6 Data bit. These 14 bits are transmitted on a carrier of 38/36 khz carrier by using Manchester Coding. High and Low time are 889 us. For more on Manchester Coding and RC5 protocol, read this.

At the receiver, TSOP1738 receiver is used. It removes the carrier and provides serial data. This serial data is applied to AT89C51 microcontroller for decoding serially transmitted data and control the output accordingly. Relays are used for switching action and they are controlled by the microcontroller through the ULN2803 IC. Circuit diagram is shown below :

 
Source code of the project is given below. It is in assembly language.

;Program assuming following dataset from receiving side
;
;
; button    signal            hex
; 1        1 0 00000 011110 111    1E
; 2        1 1 00000 011101 111    1D
; 3        1 0 00000 011100 111   1C
; 4        1 1 00000 011011 111    1B
; 5        1 0 00000 011010 111   1A
; 6        1 1 00000 011001 111    19
; 7        1 0 00000 011000 111    18
; 8        1 1 00000 010111 111    17
; 9        1 0 00000 010110 111    16
; STDBY    1 1 00000 010011 111    13

CNT        equ r7              ;Temporary Variable
EXCNT    equ 10H                ;Count

ADDRS    equ 11H                ;Device address
CMD    equ 12H                    ;Command

ATEMP    equ    13H                ;Temperory store
TEMP     equ 14H                ;Temperory store
DLOCK    equ 15H                ;Setting lock permission

OTOG bit 00H                ;Flip bit
TOG    bit 01H                 ;Temp bit for flip

IR    equ P1.1                ;IR Receiver connected to this pin

SWPORT    equ P0                ;Port at which switches are connected
DSPPORT equ p2                ;Display port

CH1 equ p2.0                ;Signal Waiting  bit (determine whether controller is busy or not)
CH2 equ p2.1                ;Successfull command
CH3 equ p2.2                ;Lock status indicator
   
org 0000H                    ;Start of prog

mov SWport,#00H             ;switch all relays off!
mov DSPPORT,#00h            ;Setting display off
mov sp,#50H                    ;Stack pointer initialization
clr TOG                        ;Clear temp bit
mov DLOCK,#02H                ;Setting lock off
setb CH3                    ;Dispalying default status as an unlocked
   
main:

    jb IR,main                ;Wait for first bit   
    clr CH1                    ;Reseting to indicate Busy
   
        mov CNT,#225        ;Reading the start bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#216
        djnz CNT,$
   
    mov c,IR
   
        mov CNT,#225        ;Reading the toggle bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
   
    mov c,IR
    mov TOG,c
   
    mov EXCNT,#04h            ;Reading 4 address bits
    mov a,#00h
    adloop:
   
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
   
    mov c,IR
    rlc a
    djnz EXCNT,adloop
   
        mov CNT,#225        ;Reading 5th address bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#180
        djnz CNT,$
   
    mov c,IR
    rlc a
   
    mov ADDRS,a                ;Storing ADDRESS
   
    mov a,#00h
   
        mov CNT,#225        ;Reading first data bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
       
    clr c                    ; It is always 0 received
    rlc a

        mov CNT,#225        ;Reading second data bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
   
    setb c                    ;It is always 1 received
    rlc a

        mov CNT,#225        ;Reading third data bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
   
    mov c,IR
    rlc a

        mov CNT,#225        ;Reading forth data bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
   
    mov c,IR
    rlc a

        mov CNT,#225        ;Reading fifth data bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
   
    mov c,IR
    rlc a

        mov CNT,#225        ; Reading sixth data bit
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
        mov CNT,#225       
        djnz CNT,$
        mov CNT,#207
        djnz CNT,$
   
    mov c,IR
    rlc a

    mov CMD,a                ;Storing command
   
    mov a,ADDRS                ;Validation 1
    cjne a,#00h,eop1

   
    mov a,#00h                ;Converting both toggles to byte
    mov c,OTOG
    rlc a
    mov TEMP,a
    mov c,TOG
    mov a,#00h
    rlc a
   
    cjne a,TEMP,cont1        ;Validation 2
    jmp eop1
    cont1:

    mov c,TOG
    mov OTOG,c
   
    mov TEMP,#00h
   
    mov a,CMD                ;Converting command to equivalent decimal count
    cjne a,#1Eh,skip1
    mov TEMP,#01h
    skip1:
    cjne a,#1Dh,skip2
    mov TEMP,#02h
    skip2:
    cjne a,#1Ch,skip3
    mov TEMP,#03h
    skip3:
    cjne a,#1Bh,skip4
    mov TEMP,#04h
    skip4:
    cjne a,#1Ah,skip5
    mov TEMP,#05h
    skip5:
    cjne a,#19h,skip6
    mov TEMP,#06h
    skip6:
    cjne a,#18h,skip7
    mov TEMP,#07h
    skip7:
    cjne a,#17h,skip8
    mov TEMP,#08h
    skip8:

    cjne a,#13h,cont2        ;Switching off SWPORT
        jmp offall
    cont2:
   
    cjne a,#1Fh,cont5        ;Checking for lock signal
        jmp checklock
    cont5:
   
    mov a,TEMP                ;Validation 3
    cjne a,#00h,cont3
       
    eop1:
        jmp eop
    cont3:
   
   
    mov a,DLOCK                ;Validation 4
    cjne a,#02h,eop
   
   
    clr c                    ;Inverting SWPORT bit as per count
    mov a,TEMP
    mov CNT,a
    mov a,SWPORT
    again1:
        rrc a
    djnz CNT,again1
    mov ATEMP,a
    mov a,#00h
    rlc a
    cpl a
    rrc a
    mov a,TEMP
    mov CNT,a
    mov a,ATEMP
    again2:
        rlc a
    djnz CNT,again2
    mov SWPORT,a
   
    setb CH2                ;SWPORT written indicator
   
    jmp cont4
    offall:                    ;SWPORT off sub module
   
        mov a,#00h
        mov SWPORT,a
    cont4:
   
    jmp cont6                ;Check lock sub module
    checklock:
       
        mov a,DLOCK
        cjne a,#02h,incmt
            mov DLOCK,#00h
            clr CH3
            jmp cont7
        incmt:
            inc DLOCK
            cjne a,#01,cont8
                setb CH3
            cont8:
        cont7:
    cont6:
   
    eop:
   
    mov EXCNT,#40
    loop1:
        mov CNT,#255
        djnz CNT,$
    djnz EXCNT,loop1

    setb CH1                ;Seting to indicate as Ready
    clr CH2                    ;Clearing as default state
   
   
    ljmp main
end

The code works by sampling data input pin at certain intervals and stores data in registers. After that, according to received data bits, switches are controlled. For example, if '1A' is received, then switch no 5 will be toggled. Similarly, if '17' is received, then switch no 8 will be toggled. Apart form this, if a '0' is pressed once, then it locks the device. If a device is locked, status of switches cannot be changed. For unlocking device, you have to press '0' button twice. After that, it will resume normal operation. Also, when 'STD BY' key of remote is pressed, then all the switches are turned off regardless of their previous state.

This is a very easy to make and fun to use project. You can use it in your house for controlling switches of the switchboard. If you have any problem regarding making this project or want to give Feedback, then post a comment here or mail me at dmehta17@gmail.com.