SHOWALL FUN PRANKS HIGH VOLTAGE LAZARUS-64 PHOTOGRAPHY SPY GADGETS VIDEO GAME
Figure 8 - A VGA signal is based on horizontal and vertical timing
Figure 8 - A VGA signal is based on horizontal and vertical timing

Generating a clean and stable VGA signal is not actually much of a task for a microcontroller, as long as you write code that accounts for every single clock cycle all of the time. Now this may sound like a monumental task when you consider a single frame of 640 x 480 video contains 419,200 cycles, and these 419,200 cycles have to happen 60 times per second with not a single cycle lost at any time! The good news is that the video rendering code can be placed in an interrupt service routine so that you are free to use the blanking periods to draw your graphics. In the crude hand drawing of a video frame, you can see that a video frame is made of an active area and a blanking area. The active area is the portion of the frame that is sent to your monitor and the blanking area is just wasted time that was needed in the days when a display used a cathode ray tube (CRT). CRT based video systems needed some time to actually bring the beam back from the side and bottom of the frame during a synchronization pulse, so this time was built in to compensate. Modern displays don't need this free time, but standards have been preserved to allow older monitors to function.

We will take advantage of this dead time to actually plot some graphics in memory so they are displayed during the next active period. There is not a huge amount of free time in a frame, only about 20% of the frame, but that is still almost 100,000 cycles that can be used to generate some video in the background. Later, when the second buffer is added, the flicker caused by seeing the graphics being drawn as the beam catches up to the buffer will be eliminated, allowing all drawing to happen in the background. The numbers and pulses shown along with the video frame in the drawing represent all that is needed in order to compute the timing necessary to create a VGA video signal. Of course, there is a lot more to the VGA standard, and many good documents are available on the web if you want to dig into the details. The timing is shown for the standard 640 x 480 VGA screen running on a 25.175MHz pixel clock with a refresh rate of 60Hz. Although I am going to "bodge" these numbers to force the clock down to 10MHz later, the concepts are all the same.

The green shaded area represents what happens on a horizontal line as the beam passes from left to right along the video screen to build an image. Horizontal time is measured in clocks or cycles, and it is the limiting factor as to how many pixels can be drawn in the short time it takes the beam to travel across the screen. Although it is up to the host to tell the monitor what "mode" is being used, the monitor will expect that you conform to the timing standards, sending sync pulses and pixels in the expected time frame at the correct times. For instance, the mode shown in the drawing of for the 640 x 480 VGA standard, so there are a total of 800 clocks across one horizontal line as represented by the green shaded area of the drawing. Out of those 800 clocks, 640 of them are used to send horizontal pixels during the active state (HPX). This is then followed by the front porch (HFP) of 16 clocks, then the horizontal sync pulse (HSP) is set low for a duration of 96 clocks. The line is then completed by the horizontal back porch (HBP) of 48 clocks.

The vertical definition is very similar to the horizontal definition, except that it is counted in lines instead of clocks. So one single line of vertical data is equal to one full line of 800 horizontal clocks. This is shown as the blue shaded area in the hand drawing. There are a total of 524 vertical lines in the 640 x 480 VGA standard, and this number cannot be changed due to the fact that a line is made up of one full horizontal clock period. The vertical active lines (VLN) are the lines that contain visible pixels, so there are 480 of these. After the active pixels are all drawn (640 x 480), the vertical front porch (VFP) happens for 11 lines, followed by a negative vertical sync pulse (VSP) for two more lines. The vertical frame ends with the vertical back porch of 31 lines and then the next frame is drawn. Each full frame happens at a rate of 60Hertz, so 60 frames per second are draw - a lot of work and careful timing for a microcontroller!



Figure 9 - Running the basic sync and palette generator on a monitor
Figure 9 - Running the basic sync and palette generator on a monitor

Once you have your AVR connected to the DAC and sync pins, upload the basic sync and palette generator code presented below. The monitor should instantly lock onto the signal and display 256 large boxes, each with a different color. If your monitor fails to lock, or flashes an error, you may have the horizontal and vertical sync pins reversed. Don't worry, you cannot damage your monitor by hooking up the pins wrong, it just won't display anything. Once you have the video display working, navigate your monitor's menus (if there are any) and select the function to display the video source properties. Your monitor should be perfectly locked on and showing 640 x 480 @ 60Hz, as this is the format I am using in the source code.

Back Home Last Next
You are Viewing... Page 5 of 16
Lucid Science Electronics from the Fringe AtomicZombie Hack-a-day SparkFun