There are two main portions of code that you'll see explained and in detail below:
-Color For Loops + Hsync
-Vertical Synchronization
3 while loops are used to generate the three different colors that display on the screen. Here is an excerpt of the code for generating the 200 lines of red. The timing is seen in the comments so you can easily see how it matches what we need.
200 Lines of Red
------------« Begin Code »------------
.. ... while(i < 200){ //Green Color High GREEN_ON; //2.2uS Back Porch delayMicroseconds(2); __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t"); //20uS Color Data delayMicroseconds(20); // 1uS //Green Color Low GREEN_OFF; //Low //1uS Front Porch delayMicroseconds(1); // 1uS i++; //3.2uS Horizontal Sync HSYNC_HIGH; //HSYNC High delayMicroseconds(3); __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"); HSYNC_LOW; //HSYNC Low //26.4uS Total } ... ..
------------« End Code »------------
In this next excerpt of code, you can see how the Vertical synchronization looks almost the same as the normal horizontal line output. The difference is that instead of being counted in pixels like the Hsync (128 pixels), the vertical sync is counted in lines (4 lines).
Vertical Synchronization
------------« Begin Code »------------
.. ... //VSYNC High VSYNC_HIGH; //4 Lines Of VSYNC while(i<4){ //2.2uS Back Porch delayMicroseconds(2); __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t"); //20 uS Of Color Data delayMicroseconds(20);// 20uS //1uS Front Porch delayMicroseconds(1); // 1uS i++; //HSYNC for 3.2uS HSYNC_HIGH; //High delayMicroseconds(3); __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"); HSYNC_LOW; //Low //26.4uS Total } //Clear The i counter i=0; //VSYNC Low VSYNC_LOW; ... ..
------------« End Code »------------
That is most of the code, but there is more than you can read through by downloading the sketch above. Either way, we've built the program, connected the hardware...how about we give the system a test and see how it works!