Interfacing PIC18F46K22 MCU with VGA Monitor – Graphics Test Example

This post shows how to generate VGA signal using PIC18F46K22 8-bit microcontroller.
In this example the PIC18F46K22 MCU is used to draw shapes (lines, circles …) and print texts on a VGA monitor with black and white colors.
VGA: Video Graphics Array

Hardware required:

  • PIC18F46K22 microcontroller
  • VGA monitor
  • 2 x 68 ohm resistor
  • 220 ohm resistor
  • VGA connector
  • 5V source
  • Breadboard
  • Jumper wires

PIC18F46K22 with VGA monitor graphics example

Interfacing PIC18F46K22 MCU with VGA monitor circuit:
Example circuit diagram is shown below.

PIC18F46K22 VGA interfacing circuit

All the grounded terminals are connected together, don’t forget the negative of the 5V source!

HSYNC (horizontal synchronization) pin of the VGA connector (pin #13) is connected to PIC18F46K22 microcontroller pin RC2 (#17) through 68 ohm resistor.
VSYNC (vertical synchronization) pin of the VGA connector (#14) is connected to PIC18F46K22 microcontroller pin RC0 (#15) through 68 ohm resistor.

Color pins (R: red, G: green, B: blue) of the VGA connector (respectively #1, #2 and #3) are connected together in order to get white and black colors, they are connected to PIC18F46K22 microcontroller pin RD7 (#30) through 220 ohm resistor.

In this project the PIC18F46K22 microcontroller runs with its internal oscillator @ 64 MHz (16 MIPS), MCLR pin is configured as input.

Interfacing PIC18F46K22 MCU with VGA monitor code:
The C code below is for CCS C compiler, it was tested with version 5.083.

The following CCS C code requires two libraries:
The first library is a simple and small library for generating the required VGA signals (VSYNC, HSYNC and video).
The HSYNC (horizontal synchronization) signal is generated on pin RC2 (fixed).
The VSYNC (vertical synchronization) signal is generated on pin RC0 (default), other pin may be used.
The video signal is generated on pin RD7 (fixed).

Timer2 Module is configured to generate an interrupt every 31.75 microseconds (31.25 kHz), the horizontal synchronous pulses are generated as a PWM signal with frequency of 31.496 kHz. Within this interrupt the PIC18F46K22 outputs vertical synchronization pulse and video signal.
So, to get a PWM signal of 31.496 kHz, CCP1 module is configured to work in PWM mode using Timer2 module where the duty cycle is set to 88%. You can find this setting with comments in function named VGA_begin().

The VGA display has total of 525 lines, the VSYNC signal is generated with frequency of about 60Hz (31.25 kHz/525) with duty cycle of about 99%.

Note that this library uses all latch D register (PORTD), so connecting another device to PORTD may give some wrong results, but pins RD0~6 may still be used as inputs!

The actual displayed width of the VGA display is fixed to 160 pixel and the height may be: 64, 96, 128 or 160 pixel where the default height is 64 pixel.

The VGA library is configured to work with microcontroller frequency of 64MHz (16 MIPS) and PIC18F46K22.
An image of the display is stored in the microcontroller RAM space as a buffer (vga_buffer). The size of this buffer depends on the pre-defined height of the display (default is 160×64/8 = .1280).
The default VGA buffer is filled with an image (house) as shown in the above picture, I got it from MikroElektronika!

The VGA library is designed to work with graphics library which helps us to easily draw many shapes and write texts with different sizes. This library generates the video signal in 2 colors, connecting the three color pins of the VGA connector together as shown in the circuit diagram above will give black and white colors.

The VGA library download link is below, its full name (with extension) is VGA.c:
VGA Library for CCS C compiler

Graphics library full name is GFX_Library.c, download link is the one below:
Graphics library for CCS C compiler:

After the download of the 2 library files, add both of them to the project folder.

Rest of code is described through comments.

Full CCS C code:

The video below shows my breadboard test circuit:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.