Software
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.

Construction Tips
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.

Operation
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.

Paging Previous 1 2



Back to Top