You are on page 1of 11

Using a Microchip PIC18F4520 to program and

control a Crystalfontz 634 LCD screen

Brian Wendling
March 26,2007

Executive Summary
This paper will demonstrate the two most common ways to use a
Crystalfontz LCD display with a Microchip PIC18F4520. Both ways are
relatively easy to implement, differing only in the hardware required to
program the LCD. The first design uses the serial communication
capabilities of the PIC to communicate to the computer via the computers
serial communication port using some additional circuitry. The second
design configures the LCD to work specifically with the PIC by changing its
logic and power requirements. Each design uses the same library of
functions provided by Crystalfontz, which, makes it easy to write
customized programs for the LCD via the PIC18F4520. This application
note assumes that the reader has is familiar with the PIC18F450 and knows
how to use the MPLAB software, which, is used to program the PIC.

1
Introduction
The Crystalfontz 634 LCD display is a four-line display with a backlight and
serial communication port. The serial communication port makes the
Crystalfontz 634 a very popular display for it is easy to integrate with a
personal computer. Many hobbyists use the Crystalfontz 634 to display vital
functions on their computer such as over clocking, temperature and system
resources. However, there is another way that allows the user to connect the
LCD directly to a PIC via solder connections. Both procedures will be
discussed within this paper allowing the user to implement either of theses
designs for their particular use.

Hardware Components and Description


PIC18F4520

The PIC18F4520 is a 40 pin microcontroller that utilizes nanoWatt


technology. The PIC’s also features 13 possible 10-bit A/D conversion
inputs. The PIC pin out also includes separate transmit(TX) and receive
(RX) pins; these pins are 25 and 26 respectfully.

Figure1: PIC18F4520 Pin Diagram.

2
Basic Connections to PIC and R11 Header
Below are the connections that must be made to the PIC and R11 header.
These connections are identical for both designs.
PIC18f4520 Connections
 Pin 11 and pin 32 connected to VDD (+5V).
 10K resistor connecting pin 1 and VDD (+5V).
 Pin 12 and pin 31 connected to GND.

Pin 12 Pin 31

Pin 11 Pin 32 Pin 1

Figure 2: Connections for PIC

External Clock Connections


 Connect to pin 13 of PIC18F4520.
 Connect to VDD (+5V) & GND as shown below.

Pin 13
External
Clock

.
Figure 3: External Clock Diagram and Connections

3
R11 Headers Connections
The Winford R11 header is used to program the PIC via the USB connected
programming device and the MICROCHIP MPLAB software.

R11 Header PIC The connections to the R11 header


and the PIC are shown to the left.
Pin 1 -------------- Pin 1

Pin 2 -------------- +5V

Pin 3 -------------- Ground

Pin 4 -------------- Pin 40

Pin 5 -------------- Pin 39

Figure 4: R11 Header Connections

Crystalfontz 634 Layout and Connections

Figure 5: Backside Layout of Crystalfontz 634 LCD

4
Figure 6: Crystalfontz 634 DB-9 (Port J1) Pin Out

When using the J1 ports to communicate with the LCD you must also
connect two pins on the J2 connection to power the display. Pin 1
(GROUND) must be connected to ground and pin 3 (+5V LED) must be
connected to a 5V power supply.

Figure 7: Crystalfontz 634 Solder Connections (J2)

Design 1: Serial to PC Serial Connection

Necessary Hardware
 MAX232 chip
 Winford BC9F header
 4- 10µF capictors
 RS-232 Cable

Setup
The MAX232 chip must be connected to the PIC to allow
communication through the serial port. Pin 25 of the PIC must be
connected to pin 11 of the MAX232. Pin 26 of the PIC must be
connected to pin 12 of the MAX232. These connections are shown in the
figure below, along with the necessary connections to power and ground.

5
Figure 8: Connections to the MAXIM MAX 232 Chip
(COM1 DB9 in the Winford BC9F header)

The LCD is now controlled and programmed by sending single


commands to the LCD via the PIC. The “putcusart” function of the
PIC18F4520 facilitates this communication, thus the usart.h file must be
included in the header. Try and send the FormFeed command to the
LCD; this command will clear the LCD and set the baud rate for proper
communication. If the command does not work properly, check the baud
rate on the LCD by looking at the toggle switches.

OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF &


USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX
& USART_BRGH_LOW, 63);
putcUSART (0x0C)

Design 2: Direct Communication from LCD to PIC18F4520

Setup
To communicate directly with the LCD via the PIC some solder
connections must be closed on the back of the LCD itself. These
connections will invert the RS-232 logic and change to LCD voltage

6
level, so it operates on 0 to 5V.These connections are labeled JPB and
JPE respectively.
Wires must also be attached to the J2 solder connections on the LCD.
Pin 1 must be attached to GROUND. Pins 2 and 3 must be connected to a
+5V power supply. Pin 4 (Data_In) must be connected directly to pin 25
of the PIC, which is the PIC’s TX pin. Again try sending the FormFeed
command to the LCD using the same format as above. If it does not work
check all the solder connections and connections to the PIC itself.

Software and Programming

There is a library of C style functions that makes programming for the


LCD easy, as you do not have to remember each command in its
hexadecimal format. Try writing a program that sends a simple string to
the LCD, such as you name. You are now ready to start programming the
LCD for you particular application. The c style library of functions is
found in Appendix 1.

References

PIC18F4520 Data Sheet


http://ww1.microchip.com/downloads/en/DeviceDoc/39631B.pdf

Crystalfontz 364 Serial LCD Data Sheet


http://www.crystalfontz.com/products/634/data_sheets/CFA-
634_v2.0.pdf

7
Appendix 1
#include <usart.h>

#ifndef _CF634LIB_H_
#define _CF634LIB_H_

/* LCD Dimensions (characters) */


#define WIDTH 20
#define HEIGHT 4

/* CF634 Control Codes */


#define Cursor_Home 0x01
#define Hide_Display 0x02
#define Restore_Display 0x03
#define Hide_Cursor 0x04
#define Show_Underline_Cursor 0x05
#define Show_Block_Cursor 0x06
#define Show_Inverting_Block_Cursor 0x07
#define Backspace 0x08
#define Control_Boot_Screen 0x09
#define LineFeed 0x0A
#define Delete_InPlace 0x0B
#define FormFeed 0x0C
#define CarriageReturn 0x0D
#define Backlight_Control 0x0E
#define Contrast_Control 0x0F
#define Set_Cursor_Position 0x11
#define Horizontal_Bar_Graph 0x12
#define Scroll_On 0x13
#define Scroll_Off 0x14
#define Set_Scrolling_Marquee 0x15
#define Enable_Scrolling_Marquee 0x16
#define Wrap_On 0x17
#define Wrap_Off 0x18
#define Set_Custom_Char 0x19
#define Reboot 0x1A
#define Escape_Sequence_Prefix 0x1B
#define Large_Block_Number 0x1C
#define Send_Data_Directly_To_LCD 0x1E
#define Show_Information_Screen 0x1F

void lcd_write(char *buffer);


void lcd_goto_cursor(int x, int y);

void lcd_write(char *buffer)


{
int i = 0;

while(BusyUSART());

while(buffer[i] != '\0')
{
putcUSART(buffer[i]);
i++;
while(BusyUSART());
}

8
}

void lcd_writechar(char buffer)


{
while(BusyUSART());
putcUSART(buffer);
}

void lcd_reboot()
{
char out[] = {Reboot, Reboot};
lcd_write(out);
}

void lcd_goto_cursor(int x, int y)


{
char out[] = {Set_Cursor_Position, '0', '0'};

if ((x > 0) && (x <= WIDTH))


itoa((x - 1), out[1]);
if ((y > 0) && (y <= HEIGHT))
itoa((y - 1), out[2]);

lcd_write(out);
}

void lcd_cursor_home()
{
char out[] = {Cursor_Home};
lcd_write(out);
}

void lcd_num(int x, int num)


{
/* Big Num Support */
}

// Horizontal bar styles:


// 0 = thick bar
// 1 = striped
// 2 = medium bar centered
void lcd_hbar(int x, int x_max, int y, int style, int data)
{
char out[] = {Horizontal_Bar_Graph, 0, 0x55, 0, 0x13, 0x05,
0x03};

/*if ((x > 0) && (x <= WIDTH))


out[3] = (x - 1);
if ((x_max > 0) && (x <= WIDTH))
out[4] = (x - 1);
if ((y > 0) && (y <= HEIGHT))
out[6] = (y - 1);

if (style == 0)
out[2] = 0xFF;
if (style == 1)
out[2] = 0x55;

9
out[5] = data;*/

lcd_write(out);
}

void lcd_vbar(int x, int y, int length)


{
/* Vertical Bar Support */
}

/* Hides the cursor on the LCD */


void lcd_hidecursor()
{
char out[] = {Hide_Cursor};
lcd_write(out);
}

/* Contrast from 0 - 100 */


void lcd_setcontrast(int contrast)
{
char out[] = {Contrast_Control, 0};

if ((contrast >= 0) && (contrast <= 100))


out[1] = contrast;

lcd_write(out);
}

/* Toggle the built-in linewrap function */


void lcd_linewrap(int on)
{
char out[2];

out[0] = (on) ? Wrap_On : Wrap_Off;


lcd_write(out);
}

/* Toggle the built-in autoscroll function */


void lcd_autoscroll(int on)
{
char out[2];

out[0] = (on) ? Scroll_On : Scroll_Off;


lcd_write(out);
}

/* clears the LCD */


void lcd_clear()
{
char out[] = {FormFeed};
lcd_write(out);
}

#endif /* _CF634LIB_H_ */

10
11

You might also like