By the way, the big news this weekend was that HSC electronics is NOT moving as was rumored earlier in the year. Speaking of HSC, this coming weekend is the annual HSC sidewalk sale. (This year they’re calling it a warehouse clearance sale due to downsizing at their present location.)
The LCD I chose for this project needs four main electrical signals to display video. Three of them carry color information – red, green, and blue. These three signals can vary from 0V (black) to 0.7V (full brightness). To achieve this, I placed 470 ohm resistors in series with each, as shown in the schematic. The LCD terminates each color signal with a 75 ohm resistor, so the 470 ohm series resistors serve to convert the 5V output of the PIC to a 0.7V max signal for the LCD (using a resistive divider).
The other signal the LCD needs is CSYNC – an inverse TTL composite sync signal. This signal provides both the horizontal and vertical sync for the LCD. Without these sync signals the image rolls across the display because the LCD doesn’t know where the image starts or ends. Composite sync seems to be a little bit unusual – most RGB video signals have separate horizontal and vertical sync signals on separate wires.
A horizontal line of NTSC video is roughly 64μs long. (μs = microseconds) At the beginning of each line, CSYNC is held low for 4us, then set high again. The RGB lines are held low during sync and stay low for 8μs after sync, then set to the desired levels display the desired image on that line. 2μs before the end of the line, RGB are set low again to signal the end of the visible image. At 64μs, the program loops back to start another line.
You can generate video like this but there would be no vertical hold – the image would be stable in the horizontal direction because we are generating a horizontal sync but not in the vertical direction.
To create a stable image with vertical sync, you have to create a valid “field” of video. We’re creating non-interlaced video, so every field is the same (in contrast with ordinary interlaced video used in television.) A field is composed of 262 horizontal lines. The first line is blank, with CSYNC set low for the entire line. This is the vertical sync. The next 17 lines are called the “blanking interval” and occur above the visible image, during these lines horizontal sync is applied but no RGB signals. The next line is the first visible line and consists of both a horizontal sync and RGB signals as discussed earlier. 243 lines later, we write one blank line, then loop back and repeat the process all over again. Now we have a valid non-interlaced analog RGB video signal.
The blank lines are mostly due to compatibility with old television sets that needed time to reset the electron gun for the next field. The nice thing is that they give us time to do housekeeping before displaying the next field. For example, during the blanking interval I load the image to display into memory so it can be easily read back later.
The critical thing with regards to timing is that the PIC needs to execute the same number of instructions each time it loops, such that the sync signals always occur at the right time. This is why there are a lot of ‘nop’ instructions in my code – to pad the program execution in the right spots and maintain sync. I started by counting instructions to figure out where to put the ‘nop’s, but by the end of writing the program I was using MPLAB’s builtin “Stopwatch” feature instead.
That’s it for now! If you have any questions, make use of these routines in your own projects, or just find this interesting, please leave a comment!
Some interesting electronics/microcontroller/hacker themed blogs I’m reading these days:
NYC Resistor – Definitely the most interesting hackerspace in the US right now. Updates almost daily. I stand in awe of how prolific these guys/girls are, it seems like they are pushing the envelope of DIY every week. This blog alone makes me want to move to NYC sometimes. (Well, them and ITP.)
That’s it for now. If I missed any good ones, I’d love to hear about them, leave a comment or contact me. You can access all of the blogs I mentioned (and a couple more) from the blogroll to the right.
Update: For those who are interested in seeing how this is done, I have posted schematics and source along with some technical details about this project. Click here to learn more.
After two days of straight coding, this is the result – two more aliens and an animation routine that toggles between 2 images for each alien and changes aliens every n frames. The alien selection routine is implemented as a state machine and uses way too many instructions, but fit within a blank line of video so I called it good. The hardest part again was maintaining a constant number of clock cycles regardless of the program flow through the display loop. This is required to maintain the video timing and keep the LCD happy.
I also changed the image lookup table routine so that the image to be displayed is loaded during the blanking interval above the top of the visible screen. This makes the pixel display much more efficient during the field but at the cost of a bunch of memory. Now instead of execution time during the horizontal line limiting the max resolution, it’s memory instead… sometimes you can’t win. If I encoded color more efficiently the memory limitation would go away.
I need to clean up the code and add some more comments, but I’ll post the source in case anyone else is interested in learning how to generate RGB video with a PIC or wants to try a similar project.
Update: The LCD display I am using is a Sharp 4L-U4EB I bought surplus years ago. I haven’t been able to find any more of them since, does anyone have a source?
Here are some still images showing the animation sequence for each alien. Click on each for a bigger version.