Theory of operation for the PIC Closed Caption Decoder Eric Smith and Richard Ottosen eric@goonsquad.spies.com http://www.spies.com/~eric/ 9/18/95 Copyright 1995 Eric Smith and Richard Ottosen What is a closed caption decoder? --------------------------------- Closed captioning is done on a number of TV transmissions for the benefit of the hearing impaired. The captions may be prerecorded with a scripted program or added live as with a news broadcast. The captioning is only available with a special decoder. The closed caption decoder takes this hidden information and displays it. The decoding is normally done inside a television receiver and overlayed on the picture being captioned. How to use the PIC closed caption decoder ----------------------------------------- This version a closed caption decoder takes its signal from the video output of a VCR and displays the captions on a personal computer using a serial RS-232 port and running a terminal program. Since the VCR records the entire video signal, the decoder works both with off the air and recorded programs. An LED lights while a program is being closed captioned. Background ---------- [N.B. This section needs a lot of work] The basic operation of closed caption decoding is... ...line 21... ...run-in... ...serial character pair on each line... ...commands... References ---------- "Build the TextGrabber", Electronics Now, November 1994, pg 31. Project circuit takes baseband video in and outputs RS-232 caption. Some information on how caption works, interesting implementation. "Exploring the Vertical Blanking Interval"The Computer Applications Journal: April 1994, pg 24. Discusses several different text and graphic standards. Shows how to decode several of the text formats. Good information to get started understanding closed captioning. "Closed Captioning with the Motorola 68HC05CC1", The Computer Applications Journal, May 1993, pg 12. Uses "custom" microprocessor for placing captions on television screen. Hard to experiment with special processor. Good references in article. "Line 21 Data Services for NTSC", Electronic Industries Association, EIA-608 1992. The standard for closed captioning in the U.S. Power supply ------------ U1, an LM2931-5 low dropout regulator, takes the nominal +9 volts and regulates it down to +5 volts. This regulator is designed to withstand reversed input voltage. C1 must be nonpolarized to also handle a reversed polarity input voltage. If a 78L05 type regulator is substituted, a 1N4001 diode should be added in series with the positive battery terminal (anode to battery) to protect the regulator and circuit from a momentarily reversed battery. If a 9 volt DC wall transformer is used, the above precautions are not needed. The input power is expected to be about +9 volts at 100ma: Ref Number Typical Maximum ----------------------------------------------- U1 LM2931AZ-5 3.7ma 10.9ma U2 PIC16C61 7.2ma 13.2ma U3 Elantec EL4581C 1.7ma 3.0ma U4 LM393 0.4ma 1.0ma Q1 2N5088 0.3ma 0.6ma Q2/Q3 2 @ 2N3906 6.0ma 6.0ma R4/R7 Pullups 5.0ma 10.0ma D5/D6/D7 LED's 6.0ma 9.0ma S1/S2/S3 Switches 0.0ma 0.2ma ------ ------ 30.4ma 54.2ma Sync stripper ------------- The video is filtered to reduce the effects of noise impulses on the off-the-air TV video. The filtered video is applied to the input of the EL4581 (U3) which strips off the composite sync for the micro to use. The CSYNC pin, which is low during the sync period, goes into PA3 (pin 2 of U2). Note that the RSET pin does not have the usual resistor and capacitor pair connected. These parts are not needed if no field or frame signals are needed from the EL4581. Only the composite sync from the sync stripper is used. The other signals are synthesized inside the micro thus freeing up pins on the micro. Since only a sync stripper is needed, a crude and cheap sync stripping circuit using discrete components could be used in situations with a very clean video signal. The EL4581 is an improved version of the LM1881 and observations show that the EL4581 performs much better than the LM1881 when less than ideal off the air signals are being received. The filtered video is also applied to emitter follower Q1. Q1 obtains its bias (1.3 volts to 1.9 volts) from the video clamp at the EL4581 video input pin. Q1 is used to prevent DC restore pulses from feeding glitches back into the sync stripper. Micro output PA2 (U2 pin 1) is pulsed low during the video blanking periods. This charges cap C7 to the difference between the AC coupled video blanking potential and ground. C7 will hold this voltage for several TV lines keeping the blanking level at ground indepenant of the variations in average brightness of the video. R3 sources a small current to ensure that the PA2 clamp always pulls toward ground. Resistor R14 limits the DC restore current to reduce glitches on the video and slow the DC restore response time to more than 1 TV line. When the video has been DC restored, the video is left driving PA2 as an input. Normally you shouldn't put an analog signal into a digital input. In this case since the clamped video is a maximum of about 0.7v, the increase in operating current is acceptable. Data slicer ----------- One half of the voltage comparator (U4A) is used as a peak detector. Micro pin PA1 controls the peak detector to catch peaks when the closed caption data is present in the video. To catch the peaks of the closed caption, the peak detect capacitor is discharged just before the start of the run-in cycles. The output of the comparator is open collector so R4 supplies the charging current for hold capacitor C8. Diode D1 prevents discharge of the peak holding capacitor, by the comparator output, between peaks. Cap C8 is chosen small enough in value to be charged by the current from R4 and large enough in value to hold peaks while discharging through resistors R5 and R6. The negative peaks of the closed caption are transmitted at the same level as video blanking. With DC restore this is 0 volts. Resistors R5 and R6 split the difference between the postive peak held on C8 and ground to set the threshold for the data slicing comparator (U4B) centered between the peaks of the data voltage. The recovered closed caption data is input to the micro on PA0. The data must be on the LSB of the port pin for the software to read it as quickly and easily as possible. The software reads the raw data stream from TV line 21 into an array and then processes this array to find the character codes in the caption. There are 4 samples of raw data for each run-in cycle and also 4 samples per character data bit. Note: A 20 MHz oscillator would allow more precise processing of the video to extract the closed caption sync and data from the video. More memory is needed (more than is availble in some PICs) to save the data sampled at this higher rate. RS-232 driver ------------- The RS-232 driver is a "poor man's totem pole output". The driver is inverting. When port pin PB1 is high, Q2 is held off. With Q2 off, R12 drives Q3's base to -4 volts causing Q3's emitter to follow to -3.3 volts. Diode D4 is reverse biased. When PB1 is low, Q2 saturates at 4.8 volts and forward biases D4 to give 4.1 volts on D4's cathode. R13 gives some protection against reversed transmit/receive RS-232 signals as well as short circuits. The software sets serial communications to 4800 Baud, 8 bits, no parity and 1 stop bit. Optionally, a RS-232 input can be used in place of some of the LED's and switches. The serial input comes in through a series resistor (R?? ) used to protect the input of the micro. A pull down resistor (R?? ) prevents the input from floating when the RS-232 cable is not connected. The input for the serial data uses the micro's interrupt pin (PB0) so that no data from the computer is lost. Since the UART is done in software, however, the closed caption decoding is suspended while commands are being received. Also, the LED that indicates active closed captioning may be replaced by driving the RS-232 carrier detect line. Charge pump ----------- The -4 volts is created by a charge pump driven by output pin PB0. When PB0 is high, capacitor C9 is charged through D3 to +5 volts on the PB0 side and +0.6 volts on the other side. PB0 going low forces the left side of C9 to ground and therefor its right side to -4.4 volts. This forward biases diode D2 and delivers -3.8 volts to charge C10. To maintain a constant DC voltage on C10, PB0 must constantly be switching between the high and low states. The negative 4 volt power supply must supply about 3ma maximum. C10 must be large enough in capacitance to maintain a ripple of .1 volts maximum. This requires PB0 to change at least every 7ms. The program pumps the capacitors whenever it is convenient but never goes more than one TV field with pumping caps C9 and C10. Usually, PB0 is pulsed at a much faster rate. For instance, every time a bit is sent by the UART code PB0 is pulsed. Some notes about the charge pump: Diodes D2 and D3 are specified as 1N4448's to squeeze a few tenths of a volt out of the losses in the charge pump. In most cases, more common 1N4148 or 1N914 type diodes will work fine. If you want even better negative drive voltage you can change D2 and D3 to 1N5817 Schottky rectifier diodes to get about -4.6 volts out of the the charge pump for the RS-232 driver. LED's and switches ------------------ The switches use the internal pullup resistors of the micro. The switch inputs go low when the switch is pressed. The function of the switches is completely under software control. Switches S1, S2 and S3 are not used right now but will probably be used for selecting modes. The LED's are on when the port pin is driven low. The LED current is a maximum of 3ma. This is sufficient for most lighting conditions with a high brightness LED. The function of the LEDs is completely under software control. LED D5 (on PB4) is lit to indicate the presence of closed caption on the video signal. A PZT speaker can also be placed on PB4 to beep at power on time. The other two LEDs (D6 and D7) are used for debugging right now. LEDs D6 and D7 will probably be used as mode indicators. Note: If lines need to be freed up on the micro, the switches and LED's can easily share the same 3 pins. This would be a good idea if a PIC16C622 is used. The micro --------- The micro runs at an oscillator frequency of 16 MHz. This is just a little less than 32 times the 503 KHz run-in frequency of the closed caption signal. The microprocessor is a Microchip PIC16C84 in the prototype. This part is easy to program while testing but is specified to run at 10 Mhz maximum, not 16 MHz. At room temperature, with typical parts, etc, etc, the part works. The part for the final use should be a PIC16C61. The PIC16C622 looks like it might work even better since it has internal voltage comparators. The spec's for the voltage comparators are not real helpful for determining if they will work. Eric intends to get samples of the PIC16C622 for us to try... sometime. Of course the pin assignments have to change a bunch for the '622 to work. The software ------------ The main loop of the program waits for a new video field, counts sync pulses until line 20, does a DC restore during horizontal blanking, clamps the peak detector, waits for the sync pulse that begins line 21, delays until the approximate time of the start of run-in, and releases the peak detector clamping. A 14-byte data buffer called "sample" is used to store up to 112 data samples during line 21. The routine "getsamp" is called to collect these samples. It is written as a series of inlined pairs of instructions like this: rrf porta,w ; get first bit of sample+0 rlf sample+0 The first instruction of the pair is used to copy the LSB of port A (the output of the data slicing comparator) to the carry flag of the status register. The second instruction rotates the carry into the first byte of the sample buffer. This pair of instructions is repeated eight times to acquire the first eight samples. There are 14 consecutive groups of eight pairs, and each group is identical except that the offset ("+0") is incremented for each successive group. An alternative means of sampling could use alternating bit instructions rather than rotates: btfsc porta,data bsf sample+0,7 By virtue of not using a rotate on the input port, this allows the use of an arbitrary bit rather than requiring the LSB, and it will allow other bits of the same port to be used as outputs. The only disadvantage is that it would require the sample buffer to be cleared in advance. The processor clock frequency is approximately 32 times the data rate. The PIC has an internal divide by four, so each data bit is approximately eight CPU cyles wide. Each pair of instructions for the sampling takes two CPU cycles, so there are approximately four samples per data bit. In principle it is only necessary to take one sample per data bit. However, that sample must be taken near the center of the data bit (or at least not near the edge). It is not feasible with the PIC to write code to determine the right time to sample on the fly, so the oversampling by a factor of four allows the correct sample times to be determined after line 21 has been read in its entirety. Note that the run-in frequency is the same as the data rate, so each cycle of run-in consists of approximately two samples high and two samples low. The run-in is intended to make it easy to get a hardware PLL to lock to the data rate and provide accurate sample times in the middle of the data bit times. In this design the leading edge of the start bit is used as the reference time rather than the edges of the lead-in cycles, however since the deoding is performed in software this could be easily changed. After all of the raw samples are captured, the are decoded by a routine called "process". A low level function called "getsbit" is called to retrieve successive samples from the sample buffer; the sample bits are returned in the carry flag and an end-of-buffer indication is returned in the zero flag. First the run-in and the leading edge of the start bit are located. If the number of transitions detected between the beginning of the buffer and the start bit is outside the range tmin..tmax, it is assumed that there is not valid caption data present. Since the sampling occurred at approximately four times the data bit rate, there should typically be four identical samples for each data bit, the code skips two samples to find the middle of the start bit, and then extracts the data by looking at every fourth sample thereafter. This is a rather simplistic decoding method but it works surprisingly well. A better method might involve extracting multiple samples per bit and letting them "vote". This could provide better noise immunity. The routine "parity" is called to check the parity of each decoded data byte. Currently the code doesn't do any sophisticated handling of the control codes which are used for language selection, color, screen positioning, etc. These would be fairly easy to add. The decoded data is emitted to the serial port by calling "xmit", which is a simple software UART. (Actually it is just a UAT since it doesn't receive.) Because this unit is sometimes used with two-line LCD displays, it is desirable to prevent text from scrolling off too quicly. This is done through the use of "lazy carriage returns". This means that when the CR code is detected, it doesn't get immediately transmitted out the serial port. Instead the "lazycr" flag gets set, which will cause a CR to be sent before the next character.