After plugging the new DVD player into the s-video connector of the big screen TV and seeing the image, I knew there was no going back to composite video. A few weeks later I purchased a programmable remote to control my home theatre system and I became a highly-optimized couch potato. But my happiness would not last.
A few months ago we added a new digital cable service. After installation, I realized that the decoder box had an s-video output. The problem was that my TV had only one s-video input and the DVD player was connected to it. I looked around and found a manual s-video switch box at Radio Shack. It saved having to switch out the cable behind the TV but I was now a sub-optimal couch potato. Happiness (and optimization) could only be found with a remote controlled s-video switch. It was time to heat up the soldering iron and write some PIC code.
The requirements for this project consisted of:
|1.||Use a common IR format and detector so that a standard, off the shelf remote could act as the controller.|
|2.||Support 4 input channels of s-video, composite video, stereo audio.|
|3.||Have a "learn" mode so that codes other than the programmed default can be used to switch channels.|
|4.||Mechanical form factor shall be no larger than Radio Shack mechanical switch.|
|5.||Channels can be selected with front panel switches as well as with a remote.|
Before starting this project, I researched the format of IR remote controls. There are a few major standards for IR protocols including the ubiquitous RC5 codes. This format consists of 14 bits that are transmitted using Manchester codes. I eventually settled on the one used by Sony which is also known as the RECS 80 standard. The reasons for this were:
|1.||It's a simple protocol and easy to decode.|
|2.||Sony has a number of excellent, after-market "universal" remotes controls.|
|3.||I own a "universal" Sony remote control.|
|4.||It's logical to map the codes for the different channels to the audio amplifier. I own a Sony amplifier.|
Sony uses a 12-bit protocol that consists of a 5-bit ID code followed by a 7-bit command code. The data is transmitted least significant bit first after a Start or AGC bit. On my oscilloscope, I observed the Start bit to have a duration of 2.6msec. A "1" was low for 1.44msec and followed by a 440usec terminator. A zero was low for 760usec and followed by a 440usec terminator.
The code to set a Sony amplifier to the Video1 input is 822 Hex and can be seen, LSB first, in the observed data stream shown below.
Other observed codes for input selection for a Sony amplifier were:
|1.||822H - Video1|
|2.||81EH - Video2|
|3.||81DH - AUX|
|4.||821H - Tuner|
|5.||825H - CD|
|6.||823H - Tape1|
|7.||869H - Tape2|
|8.||86AH - TV|
|9.||820H - Phono|
I chose, as default codes, Video1, Video2, CD, and TV. However, you can teach the switch to remember any combination of codes as long as it adheres to the Sony format.
The three main devices which make up this design are the Panasonic PNA4613MOOYB IR sensor (available from DigiKey), the PIC16F84 microprocessor, and the Linear Technology LT1204 4-input Video Mux. Just for fun, I used bi-color LED's to provide a visual indication of the selected channel. Note that you can easily modify this circuit to perform tasks other than switching a video mux. Just tap off ADDR0 and ADDR1 with your own circuit. Or you can use the LED drivers as a set of 1 of 4 outputs.
The Panasonic IR sensor provides the optical sensing, 40Khz filtering, amplification, and detection circuits necessary to extract the signals from the remote.
The PIC chip has five pushbutton-inputs, one IR Sensor input, four LED indicator outputs, and two coded address selection outputs. An 8Mhz crystal results in an instruction cycle of 500nsec or roughly two million instructions per second - more than adequate bandwidth for this application. The bi-color LED's have a simple circuit which allow them to be controlled by a single line. When the input is high, the green LED is on and the transistor is off. When the input is low, the green LED is off and the transistor turns on to provide current for the red LED. The PIC chip outputs can source up to 25mA which is more than enough for an LED.
The LT1204 4-input Video Mux has a 30Mhz bandwidth which is adequate for these signals. In our application we are terminating the inputs with 75ohms. Because of this, we have to boost the gain by two on the output. Other devices can be substituted for this multiplexer including the Analog Devices AD8184 or the MAX454. Note that if the Analog Devices part is used, a video amplifier with a gain of two will have to follow the outputs. For the audio switching, a CD4052 dual 4:1 mux is used.
Power for the circuit is provided with a 5 volt linear regulator driven by a common 9VAC wall transformer. To get the -5VDC required by the muxes, a Linear Technology LTC660 inverter is used. An LC circuit is used to squelch any residual switching noise from the inverter before going to the muxes.
The schematic has been partitioned into three sections: 1) the front end which includes the PIC chip, the IR sensor and the LED's, 2) the back end which consists of the muxes and connectors, and 3) the power supply.
The PIC program is straightforward. After turn-on, channel 1 is selected by default. The code then goes into loop where it scans the buttons, maintains a timer and waits for an interrupt from the IR sensor. If a button is pressed, it undergoes a debounce algorithm before being processed. If an IR signal comes in, each of the bits undergo a timing analysis. If a bit qualifies as being valid, it is shifted into a buffer. If a bit is invalid, the process is aborted. After 12 bits have been shifted in, the code is compared to the four values stored in EEPROM. If a match is found, the PIC switches to the appropriate channel. The program button allows the user to program any of the four channels with code different from the default. The new code is then stored in EEPROM and is retained even if power is removed from the circuit.
The code was written as a basic multitasking system with interrupt driven events generating flags that are detected and acted upon by an executive loop. The code in the interrupt service routine (ISR) was kept deliberately short to prevent the possibility of blocking a concurrent event.
The core routines in the code are the main loop, the interrupt service routine, button decoding, and IR data decoding. At power up, the PIC hardware is setup to generate an interrupt on the falling edge of the IR detector output. In addition, the timer is set up to generate an interrupt every 128 microseconds. This period is the basic "tic" used for debouncing the switches, measuring the width of pulses, and for generating delays.
The main loop called "exec_loop" monitors five of the bits in PORTB to detect if a button has been depressed. If any of the bits are low, the "button_scan" routine is called where the depressed button is isolated and debounced. A code is returned from this routine which is then passed to the "parse_buttons" routine. The physical switching of the signals going to the LED's and the MUX's is nested in "parse_buttons". The loop also monitors a flag called "INCOMING_IR". This flag is set by the interrupt service routine when a falling edge is detected from the IR decoder. When "INCOMING_IR" is true, the "ir_decode" subroutine is called. If a valid IR code is received, it is encoded as a button and the "parse_buttons" routine is called again.
A special case in the "parse_buttons" routine is when the program button is pushed. When this occurs, the "prog_butt" routine is called which causes the LED's to blink green and red while waiting for the user to push one of the four channel select buttons. Once that occurs, the "wait_4_ir" routine is called. Here the LED associated with the pushed button is set to the color yellow by modulating it with a 50% duty cycle while the processor waits for an IR sequence to arrive from the decoder. If a valid code is received, the "parse_buttons" routine then calls the "save_code" routine where the code is stored in EEPROM.
The data in the EEPROM consists of ten bytes: a high and a low byte for each of the IR codes and two bytes of checksum data. If at power-up, the stored checksum does not equal the value calculated from the four codes, the software writes the default codes into the EEPROM.
One last section of the code that's worth mentioning is the "ir_decode" subroutine. This is where the 12-bit code from the remote is shifted into the "IR_H" and "IR_L" registers. In the routine, we montitor the "INCOMING_IR" flag. When this is set by the ISR, a register called "UP_COUNTER" is cleared. This register is incremented every 128usec by the ISR. The code then waits for the rising edge of the IR signal. When this occurs, the value in "UP_COUNTER" is compared to the width of a zero and the width of a one. A tolerance of two tics is allowed for this measurement to compensate for variations in remotes as well a variations in the timing loop. If the bit with is valid, its value is shifted from the left into the "IR_H" and "IR_L" registers. After twelve bits have been shifted in, a 100msec delay occurs to allow any repeated or erroneous codes to complete.
Twist the signal and matching ground leads from the S-Video connectors. This will decrease any possibility of radiation and at the same time make them less susceptible to interference.
For packaging, I took my Radio Shack switch, pulled out the mechanical switches and placed my electronics on the inside. This gave me a more aesthetic looking assembly with the benefit of being able to use all the s-video and RCA connectors on the back.
To program the PIC chip, you will need a PIC Assembler and programmer. I used the PICSTART PLUS development system from Microchip Technology.
At power up, channel 1 is selected as a default. Pressing the four buttons will select channels 1-4. The code in the PIC chip selects, as default codes, Sony Amplifier inputs for Video1, Video2, AUX, and CD. Transmitting these codes from a Sony compatible IR-remote will cause the unit to select each of the above channels.
Pressing the program button will cause the LEDs to flash until until one of the four select buttons is pressed. Once this is done, the LED associated with the pressed button will start to flash indicating that the software is waiting for a new IR code assignment for the selected channel. Hitting any button at this point will abort this operation. Point the remote at the IR sensor and select the code that you want the AV switch to "learn". A six second light show will indicate reception, acceptance, and storage of the new code.
Now grab some chips, rent a DVD, settle into your easy chair, and resume your role as a highly optimized couch potato.