Generating Composite Video

Hello,

I've read the post. You have the same problems :slight_smile: of timing.

Could we try to resolve them together ? My project is working in the loop() function, everything is ok. I try do it with interupt but I don't have an oscilloscope to fine tuning synchro.

Hi everyone,

I just stumbled on this thread while looking into something else entirely (curse you stream of consciousness browsing).

Some of us did this a few years ago with a pair of PIC 18f452s in CSS C at 40MHz. One PIC set up the video environment, and on interrupt we used a second one to read inputs and do our "drawing" routines.

As I recall, it was a pretty crude effort, but it worked well enough for our purposes. You can look at the project notes and code here : http://droolcup.com/pictv

~s

Hello droolcup,

Thank's for your post. I've just seen the video of your project. I have the same result with interrupt, one somme image configuration there some lines are "overshooting", it depends of the white surface on screen...

I think I'll have to write some parts of the code in asm to be sure of timings.

The best way that I've seen to do this is by using 2 micros- one of them is programmed in ASM and reads the pixel data from a ram chip, which the other micro updates. There would be a sync line between the two to make sure one isn't trying to write while the other is reading. This would eliminate timing problems and also allow for more complex programs to be run.

I'm thinking of giving this approach a try soon- what does everyone think?

Maybe this is just crazy talk, but couldn't you try and generate your sync signal with a bi-stable oscillator, you'd want to watch that with the MCU so that you don't keep sending data as your sync goes off, if not using the oscillator directly, you could use it to keep track of were you are in your output (send the sync with the MCU, but use the oscillator to time everything right.

(Please excuse any tyops I've made, it's late, and it's hard for me to keep up with my high standard of typing when I'm tired.)

Hello,

Yes, I'm thinking about soltions with external components. But, at first I want to do it the best without active components. It's a challenge with Arduino Board :). I will try with some asm code include in C code...

Hey Benoît ROUSSEAU,

Just want to say good luck! I hope you'll find a way to get it to work eventually, meanwhile I can dream about arduino pong!

-Z-

I stumbled upon a site where they sell all kinds of electronic video/audio instruments. There's a lot of interesting information about how these guys managed to get the video output running.. They use a ARM7TDMI Microcontroller in conjunction with an AD725 RGB to PAL/NTSC encoder IC from Analog Devices. They run a crystal at 4 times the video burst frequency to get stable output, it still only handles 256 colors at a 160 x 128 resolution. But it looks like lots of fun to play with, it could also be interfaced with an arduino board. Programming the microcontroller itsself requires C skills and the GNU toolchain, a flash utility and an USB board (i bet the arduino can also be used as an Serial/USB interface to program this board).

http://www.critterandguitari.com/catalog/index.php?main_page=product_info&cPath=1&products_id=13

Hi All,

Possibly this should be a new thread, but the "generating video bboard" is pretty close to my question:

Has anyone written code to detect the vertical blanking interval in an incoming NTSC composite video signal (RS-170A)?

I'm creating a project in which I need to switch a circuit on and off, precisely in sync with the video fields from an NTSC video source. In order to do this, I would need to sample the video signal fairly frequently and write an analyzer/detector for the vertical blanking interval, which is used to synchronise the video frames.

Any advice would be greatly appreciated before I bore headlong into the oscilloscope.
Thanks,
Golan

hi

you can get a single-chip solution for that: they're called sync separators. The LM1881 comes to mind. It only needs +5V, 3 capacitors and two resistors. In return, it provides the vertical and horizontal sync, as well as the field (odd/even) and the back porch signal form a composite video signal. It's about $3 US.

D

PS: there is an interesting article at this link, with code and schematics, on using the above chip with an AVR to create a motion detection system.

this was just posted via Make Magazine... an AVR based video overlay. Interestingly, it uses the LM1881 to separate sync, and then one pin pulls the video level to generate the overlay.

D

I managed to get the Arduino generating a PAL signal. My TV syncs to this signal without any problems. I followed the specs for a PAL signal as close as possible. It uses asm nops to try and get more precise delay times. Each nop is a 65ns delay (1 instruction at 16 Mhz...it should be 65ns but i don't have a good enough oscilloscope to check). The PAL signal has +- 0.3 microseconds leeway on the sync signals anyway so it works ok.

The code generates a white bar in the middle of the TV. Here is the first half.

int pixel = 6;  // Connected to 550ohm resistor (Should be 470ohm)
int sync = 7;   // Connected to 1k ohm resistor
int i;

void setup()
{
  pinMode(pixel,OUTPUT); // Switch to output pin
  pinMode(sync,OUTPUT);  // Switch to output pin
  cli();                // Turn off interupts
}


void loop()
{
  //
  // Generate 5 Narrow Equalization pulses
  //
  PORTD = B10000000; // Make the sync high to start with
  for(i=0;i<5;i++)
  {
     PORTD = B00000000; // Sync pulse goes low and delay 2.3 microseconds
     delayMicroseconds(1);
     delayMicroseconds(1);
     __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
     PORTD = B10000000; // Sync pulse goes high and delay 29.7 microseconds
     delayMicroseconds(29);
     __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
  }
  
  //
  // Generate the 5 Field Sync Pulses
  //
  for(i=0;i<5;i++)
  {
    PORTD = B00000000; // Sync goes low and delay 27.3 microseconds
    delayMicroseconds(27);
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
    PORTD = B10000000; // Sync goes high and delay 4.7 microseconds
    delayMicroseconds(1);
    delayMicroseconds(1);
    delayMicroseconds(1);
    delayMicroseconds(1);
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
  }
  //
  // Generate 5 Narrow Equalization pulses
  //
  for(i=0;i<5;i++)
  {
     PORTD = B00000000; // Sync pulse goes low and delay 2.3 microseconds
     delayMicroseconds(1);
     delayMicroseconds(1);
     __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
     PORTD = B10000000;
     delayMicroseconds(29); // Sync pulse goes high and delay 29.7 microseconds
     __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
     
  }
  // Generate 18 Blank Frames
  for(i=0;i<18;i++)
  {
    PORTD = B00000000;    // Pull sync pin low -> 0 volts
     delayMicroseconds(4);
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
    PORTD = B10000000; // Pull sync pin high
    delayMicroseconds(59);
  }
  //   
  // Generate half the Image 
  //
  for(i=0;i<285;i++)
  {
    //
    // Front Porch
    // 
    PORTD = B10000000;
    delayMicroseconds(1); // Front Porch: 1.65 microseconds
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
    
    //
    // Generate sync pulse
    //
    PORTD = B00000000;    // Sync pulse pulled low: 4.7 microseconds
     delayMicroseconds(4);
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
    
    //
    // Back Porch
    //
    PORTD = B10000000;    // This is the back porch: 5.6 microseconds.
    delayMicroseconds(5);
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
    
    //
    // Drawing starts here: Total time available is 53 microseconds
    //
    PORTD = B10000000;    // Draw black
    delayMicroseconds(20);
    PORTD = B11000000;    // Draw white
    delayMicroseconds(10);
    PORTD = B10000000;    // Draw black
    delayMicroseconds(22);
    

  }

And the other half....just a copy of the first half :slight_smile: Cut and paste both of these into one file and it should be good to go.

 //
  // Generate 5 Narrow Equalization pulses
  //
  PORTD = B10000000; // Make the sync high to start with
  for(i=0;i<5;i++)
  {
     PORTD = B00000000; // Sync pulse goes low and delay 2.3 microseconds
     delayMicroseconds(1);
     delayMicroseconds(1);
     __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
     PORTD = B10000000; // Sync pulse goes high and delay 29.7 microseconds
     delayMicroseconds(29);
     __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
  }
  
  //
  // Generate the 5 Field Sync Pulses
  //
  for(i=0;i<5;i++)
  {
    PORTD = B00000000; // Sync goes low and delay 27.3 microseconds
    delayMicroseconds(27);
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t"); // 0.3 microseconds
    PORTD = B10000000; // Sync goes high and delay 4.7 microseconds
    delayMicroseconds(1);
    delayMicroseconds(1);
    delayMicroseconds(1);
    delayMicroseconds(1);
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t"); // 0.7 microseconds
  }
  //
  // Generate 5 Narrow Equalization pulses
  //
  for(i=0;i<5;i++)
  {
     PORTD = B00000000; // Sync pulse goes low and delay 2.3 microseconds
     delayMicroseconds(1);
     delayMicroseconds(1);
     __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t"); // 0.3 microseconds
     PORTD = B10000000;
     delayMicroseconds(29); // Sync pulse goes high and delay 29.7 microseconds
     __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t"); // 0.7 microseconds
     
  }
  
  // Generate 18 Blank Frames
  for(i=0;i<18;i++)
  {
    PORTD = B00000000;    //0 volts
     delayMicroseconds(4);
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t"); //0.7 microseconds
    PORTD = B10000000;
    delayMicroseconds(59);
  }
  
     
 //   
  // Generate the next half of the Image 
  //
  for(i=0;i<285;i++)
  {
    //
    // Front Porch
    // 
    PORTD = B10000000;
    delayMicroseconds(1); // Front Porch: 1.65 microseconds
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
    
    //
    // Generate sync pulse
    //
    PORTD = B00000000;    // Sync pulse pulled low: 4.7 microseconds
     delayMicroseconds(4);
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
    
    //
    // Back Porch
    //
    PORTD = B10000000;    // This is the back porch: 5.6 microseconds.
    delayMicroseconds(5);
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
    
    //
    // Drawing starts here: Total time available is 53 microseconds
    //
    PORTD = B10000000;    // Draw black
    delayMicroseconds(20);
    PORTD = B11000000;    // Draw white
    delayMicroseconds(10);
    PORTD = B10000000;    // Draw black
    delayMicroseconds(22);
    
    
  }
}

Nice, I'll have to give this a go in a bit :slight_smile:

Yet another link, this one is written for Arduino in pure C (not Processing nor assembly)

http://hklab.net/wiki/TV_Video_Signal_Generator_with_Arduino

I'm a fortunate man because I have an oscilloscope and I have seen that

  • delayMicroseconds doesn't work for delays lower than ~5us
  • digitalWrite() is terribly sloooooow so don't use it
  • When the loop() function finishes, there is a 10us extra delay (?)

Conclusion: don't use Processing to accomplish a hard real-time task like this. Use C or assembly.

Sorry for my poor english.
Regards,
Javi

Its much more precise using ASM or C but you can generate a valid video signal using arduino. The PAL standard for example allows +-0.3 microseconds for its timing pulses. By using some asm nops you can tune your code to work ok. The main problem people seem to be having is they aren't generating proper signals....not using eq pulses and field sync pulses for example in their code. Also it is important to disable the interrupts. If you get hold of the specifications for TV signals, and follow them, you can generate something that will work ok on most modern tv's.

If you don't have an oscilloscope then you can use a timing trick mentioned in this thread http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1174587934/10#10

to measure timing precisely. I'm gonna get hold of a DSO in a few weeks and I'll write a better video gen example then.

Its much more precise using ASM or C but you can generate a valid video signal using arduino.

Yes. I agree with you but when I tried Processing transient pulses appeared and they corrupted my tv signal. See this picture in which I show you the transient pulses (in a red circle) due to slowly digitalWrite() calls, the signal timing using delayMicroseconds() is very bad also.

I don't know yet AVR assembly so I decided to use C

Regads,
Javi

I got the same signal when I first tried it. The solution is don't use digitalwrite, you can only change one pin at a time using that function(so it takes some time to change once and then again so you get step). Just output directly to the port

e.g portb=00000011;

instead of calling digitalwrite() twice. Once I did that I got a clean signal without the steps as shown in your oscilloscope. Also make sure interrupts are disabled. Cheers. Maybe that will help.

Just output directly to the port
e.g portb=00000011;
instead of calling digitalwrite() twice.

Good idea!
With this trick, now, there is not a thin white border in the gray bars caused for micro-transient pulses.
Thanks.