Programming 128X64 graphic LCD with a PIC.

JJ Richard

New member
I'm trying to get a CFAG12864B-YYH-V working via programming a PIC16F877 uP (this is the 128X64 LCD w/the negative voltage converter).

I've been programming the 16X2 LCD's for a while, and have had success with them, but this 128X64 graphic LCD is giving me fits. After hours of trying tonight, I've yet to even get a single dot to display on the screen.

First of all, I'm not sure what the LCD operating voltage (on pin 3) is supposed to be.

Under section 5 of the data sheet, the "Supply Voltage for LCD" is listed as 7.5v minimum, 8.5V typical, and 9.5V max.

However, under section 4 of the same data sheet, it lists the "Supply Voltage for LCD" as variable from 0 to 16.7V.

Until I figure this out, I really don't know whether my code is bad, or if the LCD just isn't powered up, since I'm applying 5V (through a 2.2Kohm resistor) to LCD Voltage Pin (pin 3). I tried briefly touching 9V to pin 3, at which point the backlighting dimmed (as if in protest), and my logic high voltage (from the PIC) dropped from 5V to ~3.8V, so I quickly removed the 9V from pin 3.

So, that's the first question... what's the proper voltage that is to be applied to pin3?

The second question is this:
I'm using an 8-bit interface to communicate between the PIC and the LCD.

Referencing the datasheet, I wrote the following code:

CS1 CON E2 'Assign LCD constants to PIC pins

;========Main Program================
TRISE = $00 'Make portE pins outputs (E0 thru E2)
TRISC = $00 'Make portC pins outputs (C0 thru C7)
low E 'Put a low on enable pin
High RST 'De-select Reset Mode
portC = 0 'Send all zero's to ensure Reset mode off.
pulsout E,1 'Send high pulse to Enable pin (for 10us)
pause 1 'pause 1ms
low RW 'Put LCD in write mode
low CS1 'Select column 1 ~ 64
high CS2 'De-select columns 65 ~ 128


low DI 'Set LCD to receive instructions
PortC = %00111111 'Turn display on
pulsout E,1 'Send high pulse to Enable pin (for 10us)
pause 1 'pause 1ms
PortC = %01000000 'Set Y address to zero
pulsout E,1 'Send high pulse to Enable pin (for 10us)
pause 1 'pause 1ms
PortC = %10111000 'Set page address to page zero
pulsout E,1 'Send high pulse to Enable pin (for 10us)
pause 1 'pause 1ms
PortC = %11000000 'Set display start line to line zero
pulsout E,1 'Send high pulse to Enable pin (for 10us)
high DI 'Set LCD to receive characters

PortC = $41 'Character code for the letter A
pulsout E,1 'Send high pulse to Enable pin (for 10us)
pause 100 'pause 100ms
goto DisplaySomething 'Continue printing A's forever

This code produces zero output on the LCD.

If anyone is familiar with PICs and can offer some suggs, I'd appreciate it. Or if anyone knows of a basic "LCD Initialization" routine (if that's what I'm missing, etc..), all the better...

I've double checked my wiring interface (DBO is connected to C0, DB1 is connected to C1, etc..) and read the logic states with a DMM, and the PIC is producing the correct data output to the DB0...DB7 pins.

Looking for additional LCD resources? Check out our LCD blog for the latest developments in LCD technology.

CF Tech

Put a 10K pot between GND and the -5v output on the display, and run the wiper (center terminal) to pin 3 (Vlcd). This will allow you to control Vlcd from 0 to -5 (with respect to ground) or from -5 to -10 (with respect to Vcc). They measure Vlcd with respect to Vcc, then take the absolute value.

Set the pot to the middle and that should be a good starting point for the contrast.

JJ Richard

New member
Got it, thanks. The datasheet had me on the ropes there, but I should have known that the -5V was for contrast.

I also figured out how to program it with a PIC. Takes a lot of code, but it is definitely "doable". :)



New member
Just wondering, that code looks awfully like PBASIC for the Parallax Basic Stamp 2, how are you putting that on a PIC?

-special [k]

JJ Richard

New member
I'm using a program called MBasic from

It allows programming in Basic (or assembly), and creates the Hex file from your code and then programs the PIC. Pretty nice setup.

Generally, if I write a 500 line program, the assembly equivalent (created by the software) is 10 times longer...saves a ton of time and work.



New member
From their site: "Obtain access to thousands of programs and projects for the PICmicro® MCU via Basic Stamp® compatibility !" No wonder :p

-special [k]

JJ Richard

New member

Heh, yeah...It's a real sweet thing those guys have there (no, I don't work there). :)

They've also taken the basic code and added simplifications to it to further speed up programming time. Writing to an HD44780 controlled LCD used to take up a lot of code, but the MBasic program allows you to simply type:

lcdwrite[home,line1,"Hello There"]

and that's it...that single line will put the cursor on line 1, home position (first digit), and print the text in quotes on the LCD.

The MBasic software has other goodies such as that built into it as well, and they're always adding more things as new revisions come out.

Also, you can program assembly directly into it anywhere in your "basic" code (you start and end the assembly code with a {, } and it programs it in along with the basic code) to access the PIC more "directly". Adds power...

I talked my brother into writing a windoze app that will allow you to hand draw text/pictures onto a psuedo LCD screen, and then produce the data needed to cut and paste into the code to display it on the KS0108 graphic LCD screen. If you'd like a copy of the program, let me know, and I'll send it to you (or anyone else for that matter). The program itself is finished, I'm just adding a help file to it prior to "release". :)



New member
This may be about a year too late, but I'm interested in the code you speak of. Also, any other links you know of that deal with interfacing and programming PICs to graphical LCDs would be much helpful


New member
Well done JJ (or brother of)! Mind throwing a copy of the program over here as well? If the code is available, I'd like taking a look at that too!


New member
please help me

hi all,
Iam also trying to work 128x64 graphical lcd...after a terrific work in that ,i couldnt able to execute a single dot in it... can u please send me the sample simulated code for it in assembly or C, it will be a great help for completeing my this email id ..
Thanks in advance...
Best regards,


New member
I have started working on Graphics LCD 128x64 pixels. Any sample code will be a good starting for me and will speed up the development life cycle.

I would appreciate if any one can pass it on to my email id - tashya at

Thanks !