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.



Thursday, April 21, 2011

Interfacing Touch Screen with microcontroller

Please Read : This article was posted by me a long time ago, I am not able to monitor the comments regularly. So, if you need my help regarding any article, please write to me at dmehta17@gmail.com . I will try to answer the queries as quickly as possible.  Also, please don't ask me for the code for any other controller except LPC2148, because I only have code for that controller. For any other controller, you will have to develop the code on your own based on the algorithm explained here. If you've created such code, I request you to share it with me via e-mail. I will publish it here with full credits to its original author.

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

I've always wanted to add something new to my projects. That's why in the 7th semester, when I saw a touch screen just lying in my college, I decided to go for it. Touch screens add an X-factor to otherwise ordinary projects. They are fun to use and more important, their applications are endless. In this article, I will explain how to interface a 4-wire resistive touch screen with microrocntrollers.

Resistive Touch Screens
Of all the kinds of touch screens available, Resistive touch screens are easy to interface, cheap and they have fair sensitivity.Resistive Touch screens are simply transducers. This touch screen has a resistive layers in both X and Y directions. According to the position of the touch, Their X channel and Y channel resistance changes. So, what we have to do is we have to determine the X and Y Channel resistance to get the position of the touch in terms of X and Y co-ordinates.
Here is an Image of simple resistive touch screen. 
 How To Interface Touch Screen with Microcontroller
To Interface Resistive touch screen with a microcontroller, you will need a microcontroller with inbuilt Analog-to-Digital converter having two or more channels. This is needed because, the touch screen will provide data in terms of an analog voltage on two different pins, using which, we have to determine position of the touch. Also, ADC input pins of the microcontroller should be configurable as General Purpose I/O (GPIO). As shown, in the figure, the touch screen has total of 4 wires coming out. Pin configuration is shown in the fig. To read the position of the touch, we have to first read touch position sequentially i.e. first read X position and then read the Y position. To do this, connect X1 and Y2 pins of touch screen to ADC multiplexed GPIO pins of the controller. And connect X2 and Y1 pins of touch screen to simple GPIO pins of the microcontroller.  


 
 
  
Now first, we have to read X position of the touch, for that, u have to program the pin X1 as Logic high (+5v or +3.3v in some cases) and configure pin X2 as logic low (GND). Now, as I told earlier, touch screen contains a resistive layer in both direction. So, when we apply +5v and GND to its pins, it will create a voltage gradient in X direction. Voltage on the X channel will vary according to the X position of the touch. We have to measure this voltage to determine the X position.For that, configure Y2 as ADC input and configure Y1 to high impedance state (in most controller, configuring Y1 as "Input" will work). Then read the value from the ADC. It will give the relative value of the touch. For example, if you have an 8-bit ADC in the controller, when you touch near the right most end, it will give you a value of 255 and when you touch near the left most end, it will give you a value near 0. After reading X position, you will have to read the Y position. For that, configure Y2 as Logic High, Y2 as Logic Low, and X1 as ADC input and X2 as High Impedance state. After that, whole procedure is the same. 


This way, you can interface touch screens with microcontrollers and add something new to your projects. I made a "Touch Screen Calculator" using the LPC2148 microcontroller. Below is the video of the Touch screen Calculator in action. If you need any help regarding this or have any suggestion/feedback, then write a comment here or mail me at dmehta17@gmail.com or you can connect with me on facebook at "http://www.facebook.com/damehta"


 


Here is the 'C' code I've created for reading X and Y positions from the touch screen. The code is for Phillips LPC2148 controller.I've crated two functions : "xcoordinate" and "ycoordinate".When called, functions wait for a touch and when touch screen is touched, returns X and Y co-ordinate respectively.

int xcoordinate(void)
{
 int x;
 PINSEL0 = (PINSEL0 & 0x3FFF3FF0) | 0x00000005; // Configure pins for touch screen
 PINSEL1 = (PINSEL1 & 0xC3FFFFFF) | 0x10000000;
 IO0DIR =  (IO0DIR &  0xDFFF7F7F) | 0x00008080;

 IO0SET = IO0SET | 0x00000080;     // Set pin to '1'
 IO0CLR = IO0CLR | 0x00008000;    // Set pin to '0'

 AD0CR = ( AD0CR & 0xFFD00000 ) | 0x00200208 ;  // Start the ADC

 while(1)
 {
   delayms(10);
   AD0CR = ( AD0CR & 0xF8FFFFFF ) | 0x01000000 ;

 xchkadc:

   x = AD0DR3 & 0x80000000 ;

   if(x == 0x80000000)  // Check if A-D conversion is complete
    {
     
      x = 0x00000000;
      x = AD0DR3 & 0x0000FFC0;
      x = x >> 6;
      x = x & 0x000003FF;
      if(x <= 10)
      {
        goto xexit;
      }
     
      AD0CR = AD0CR & 0xF8FFFFFF;
      return x;     // return the value of touch in x direction
     
    }

   goto xchkadc;

  xexit:  
  AD0CR = AD0CR & 0xF8FFFFFF;  
  }
  return 0;
}

int ycoordinate(void)
{
 int y;

 PINSEL0 = (PINSEL0 & 0x3FFF3FF0) | 0xC0000005;    // Configure pins for touch screen
 PINSEL1 = (PINSEL1 & 0xC3FFFFFF) | 0x00000000;

 IO0DIR = (IO0DIR & 0x9FFFFF7F) | 0x60000000;

 IO0SET = IO0SET | 0x40000000;    //// Set pin to '1'
 IO0CLR = IO0CLR | 0x20000000;    //// Set pin to '0'

 AD1CR = (AD1CR & 0xF0D00000) | 0x00200220 ;    //// Start the ADC

 while(1)
 {

  delayms(10);
  AD1CR = ( AD1CR & 0xF8FFFFFF ) | 0x01000000 ;

   ychkadc:

   y = AD1DR5 & 0x80000000 ;

   if(y == 0x80000000)    // Check if A-D conversion is complete
    {
     
      y = 0x00000000;
      y = AD1DR5 & 0x0000FFC0;
      y = y >> 6;
      y = y & 0x000003FF;
      if(y <= 10)
      {
        goto yexit1;
      }
     
      AD1CR = AD1CR & 0xF8FFFFFF;
      return y;            // return the value of touch in y direction
     
    }

   goto ychkadc;

   yexit1:
  
    AD1CR = AD1CR & 0xF8FFFFFF;
  }

 return 0;
}

Thursday, January 27, 2011

Digital Volume control using CD4066

For any audio amplifier circuit, a volume control is necessary. Basically, volume control circuit is a voltage divider circuit. Output from it is given to the main amplifier or to the mixer circuit. Generally, it is done by using a potentiometer to attenuate the input signal.


In digital systems, the volume control is done digitally, i.e. there is a kind of voltage divider circuit which can be controlled digitally. For sophisticated systems, there are special ICs available, but there is an easy way to do this.

A simple digital volume control can be done using CD4066 IC. CD 4066 is a quad CMOS Bi-lateral switch (For datasheet, click here). It is equivalent to 4 analog switches which can be con
trolled digitally. I used it to vary the attenuation of the signal by using a resistor bank.



As shown in the circuit diagram, the four switches are used to select the total resistance by turning the resistor on or off. As the resistance changes, the attenuation changes accordingly. When all the input bit are high, all resistances are bypassed by the switch and the attenuation is zero. Similarly, when all bits are low, it provides maximum attenuation. Also, this maximum attenuation can be changed by changing the resistor values. As there are four switches, there are a total of 16 (2^4) combinations or levels of attenuation.

This simple circuit is very useful in case when you need a simple yet good digital volume control.