On Thu, 11 Sep 1997 13:03:58 -0500 John Payson writes: >> How about a PIC that moniters the horiz and vert sync, and >simply >> inserts its own signal into the video? Basically the psuedo >code might >> be the following: >> >> 7. wait for horiz pulse >> >> The PIC clock needs to synchronize to the incoming video, I was >planning >> to do that by using software to compare the sync pulses with the >state of >> the program, and output pulses to a varicap diode that adjusts the >> frequency of the PIC oscillator. Anyone tried this before? Just >> synchronizing the software will let the text jitter (maybe slowly, >maybe >> quickly) one pixel or so back and forth. Also a custom crystal >would be >> required. So I'd really like to synchronize the oscillator itself. Having now implemented this method I'd like to report some useful results to the list. There aren't any readily-available crystals that work out exactly to video frequnecies. (One would think that 14.3818 would do, but since the PIC divides by 4 there are 221.5 PIC instructions per NTSC line) Even if there were one would have to account for differences in the camera clock or differences in the standard (black and white video is 15.750 KHz, color 15.734265) Using any old clock rate and a conventional 3-cycle "wait for sync" gives the overlaid video a jagged appearance, it is readable though. I didn't try John's suggestion to use an interrupt to wait for sync with 1-cycle resolution, which should look better but still not quite right. So I went to making the clock frequency variable. I didn't try the RC oscillator; it may work at 12 MHz but I doubt it. The PIC16F84 is of course only rated to 10 so using it at 12 is a stretch. At higher frequencies a popular form of variable oscillator has a resonant inductor and capacitor, and a voltage-variable capacitor to change the frequency. The internal PIC oscillator doesn't like to run with a LC circuit instead of a crystal. I finally found that the key is to add a small capacitor of about 40 pf in series with the LC tank. If the capacitor is too large the oscillator won't always start and if it is too small it won't stay running at low voltage. The oscillator circuit: ------- 40pf X1 ------| LC |-----||----------- X2 | ------- | | === 30pf === 68pf === 30pf | | | GND === VC GND ^ | GND Not shown is a 100K resistor bringing the control voltage into the junction of the 68 pf capacitor and the varicap. LC is a small 10.7 MHz IF transformer, using the whole high-impedance winding leaving the low-impedance winding open. That's about all I can say about it since it was pulled from junk and doesn't have any recognizable numbers on it. Similarly I don't know anything about the varicap, though if selecting a new one I'd pick one with about 40 pf maximum capacitance. I haven't tried it, but the LC having much less Q than a crystal probably starts almost instantly, so approaches that start and stop the oscillator may be feasible. The performance of this oscillator is that the frequency after being divided down by software varies from about 15.55 to 15.95 Khz as the control voltage increases from 0 to 5V. The actual frequency at the PIC X pins is about 12.09 MHz. Though designed for 10.7 MHz, turning the core of the transformer most of the way out will get it to resonate at 12. I wanted to do the phase comparator in software, which is somewhat complicated by the fact that it won't act in continuous time. I take 2 samples of the sync input one instruction apart: rrf PORTB,w ;Sync connected to B0 - into C bit movfw PORTB ;Get second sample in W.0 When the PIC is in sync the first sample will read 1, the second 0. If this is the case then the control voltage is left alone. Otherwise it is adjusted by outputting a short (5 cycles) pulse on another port pin. If both samples are 1, the PIC has sampled before the sync edge, so it is running too fast. The pin should output a low pulse. If both are 0, the PIC has sampled after the start of the sync pulse; it is too slow. So output a high pulse. If 01 is sampled (this should almost never happen), any response is OK. If the proper result of 10 is sampled, leave the pin in input mode. The analog loop filter between the adjustment pulse output pin and the oscillator adjustment is critical. I'm still working on it. Here's something to get started with: ADJ--- 10K ---------------- 100K ------ VC | | 30K | | | 0.01uF === === .0015uF | | GND GND Putting a large (2M) resistor to leak the control voltage up towards 5V, so the PIC is generally always working to pull it back down, seems to make the overlaid video more stable. Results have been good. Adding a conventional "if sync not low, loop and wait for it" after the two samples in the horizontal routine stabilizes things a lot. In addition to helping acquire lock in the first place, this feature ensures that if the oscillator is misadjusted, jagged characters will result but they will not run all over the screen. My one-transistor sync seperator doesn't give the cleanest possible sync. Garbage in, garbage out as they say. This circuit is only intended to work with video direct from a camera. It probably won't work well if at all with over-the-air video (For that, use an external phase comparator likely of the xor type)