Sunday, 17 June 2012

PAL TV Signals using a PIC

Whist I was leaning about PIC microcontrollers the thought had crossed my mind about the possibility of simple computer system, in such a system it would certainly be nice to use a real TV as the output display rather than a large expensive LCD module.






After doing some Google'ing I stumbled upon these superb explanations of how PAL TV signals work.

http://martin.hinner.info/vga/pal.html

http://www.rickard.gunee.com/projects/video/pic/gamesys.php


My first attempt used only Horizontal Sync signals followed by a striped pattern, the result is the image you can see above. Because there is no Vertical Sync the image is actually shifting vertically, but because of the nature of my pattern, it gives the appearance of a fixed stable picture.

Of course this is pretty useless for displaying anything useful as the picture needs to be static. So the next test was add Vertical Sync; In order to test the picture I'd need to alter the format of my test pattern to something that would show up if the VSync didn't work properly, ideally I should have used a checkerboard pattern but I opted for a simple code change. For this I simply added a black border of 50 scanlines, top and bottom of my striped pattern; and the result was the image below.




















In this picture you can see the TV's text "AV"; this is not part of the signal generated, only the striped pattern is generated.



This signal produced is PAL non-interlaced, which means I don't generate field1 then field2 scanlines, I only generate a continuous set of scanlines.


The basic C code looks like..


        doVSyncNonInterlacePre();

        for(ucRasterLine=50; ucRasterLine!=0; ucRasterLine--) {
            doHSync();
            doBlackScanLine();
        }

        for(ucRasterLine=204; ucRasterLine!=0; ucRasterLine--) {
            doHSync();
            doScanLine();
        }

        for(ucRasterLine=50; ucRasterLine!=0; ucRasterLine--) {
            doHSync();
            doBlackScanLine();
        }

        doVSyncNonInterlacePost();



A simple explanation of the code is that my picture starts with a VSync non-interlace pre sequence, then for 50 scanlines it generates the HSync signal followed by a black scanline; Then the visible pattern is the HSync and stripes for 204 scanlines; and then the bottom black border, which is actually the same as the first top black border; And it finishes off with a VSync non-interlace post sequence; after which it repeats indefinitely.





In this example I decided to re-write the code to make it display a pattern dynamically across the horizontal. The pattern was white, dark grey and black bars. the gray is very dark! and doesn't show up much. The code required to make it dynamic requires more cpu cycles, this means the more time calculating what to display, the bigger your pixels will be! 


















I'd like to play around with pal signals a little more in the future, but I think I will need to get a much faster PIC, my poor 16f877A running at 5Mhz ( 20Mhz / 4 ) is just far too slow.



.. to be continued!!! ;-)