Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

16x4 LCD initialization code for LCD in 4 bit mode using pic18f

Status
Not open for further replies.

Arrowspace

Banned
Joined
Jan 23, 2015
Messages
186
Helped
3
Reputation
6
Reaction score
3
Trophy points
18
Activity points
0
I am looking for 16x4 LCD initialization code for LCD in 4 bit mode using pic18f
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,510
If you are utilizing either the Microchip XC8 or C18 compilers, both provide the XLCD library which supports either the 8-bit or 4-bit interface to HD44780 compatible LCDs.

You can find the source code for the XLCD library in the:

<compiler installation directory>\sources or src\plib or pmc_common\XLCD

Depending on the compiler version.

The xlcd.h header file contains the appropriate #define macros to configure by comment/uncomment for 8-bit or 4-bit interface. Simply copy the header file to your project, make the appropriate changes to the xlcd.h header file and properly link the peripheral library (plib) or copy the XLCD source to your project.

The header file can be found in the:

<compiler installation directory>\include or h\plib or none\

Depending on the compiler version.

You will also need to ensure the required delays utilized by the XLCD library are properly configure and define the specific pin to LCD interface.

xlcd.h

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef __XLCD_H
#define __XLCD_H
#include "p18cxxx.h"
/* PIC18 XLCD peripheral routines.
 *
 *   Notes:
 *      - These libraries routines are written to support the
 *        Hitachi HD44780 LCD controller.
 *      - The user must define the following items:
 *          - The LCD interface type (4- or 8-bits)
 *          - If 4-bit mode
 *              - whether using the upper or lower nibble
 *          - The data port
 *              - The tris register for data port
 *              - The control signal ports and pins
 *              - The control signal port tris and pins
 *          - The user must provide three delay routines:
 *              - DelayFor18TCY() provides a 18 Tcy delay
 *              - DelayPORXLCD() provides at least 15ms delay
 *              - DelayXLCD() provides at least 5ms delay
 */
 
/* Interface type 8-bit or 4-bit
 * For 8-bit operation uncomment the #define BIT8
 */
/* #define BIT8 */
 
/* When in 4-bit interface define if the data is in the upper
 * or lower nibble.  For lower nibble, comment the #define UPPER
 */
/* #define UPPER */
 
/* DATA_PORT defines the port to which the LCD data lines are connected */
 #define DATA_PORT              PORTB
 #define TRIS_DATA_PORT         TRISB
 
/* CTRL_PORT defines the port where the control lines are connected.
 * These are just samples, change to match your application.
 */
 #define RW_PIN   LATBbits.LATB6        /* PORT for RW */
 #define TRIS_RW  TRISBbits.TRISB6      /* TRIS for RW */
 
 #define RS_PIN   LATBbits.LATB5        /* PORT for RS */
 #define TRIS_RS  TRISBbits.TRISB5      /* TRIS for RS */
 
 #define E_PIN    LATBbits.LATB4        /* PORT for D  */
 #define TRIS_E   TRISBbits.TRISB4      /* TRIS for E  */
 
/* Display ON/OFF Control defines */
#define DON         0b00001111  /* Display on      */
#define DOFF        0b00001011  /* Display off     */
#define CURSOR_ON   0b00001111  /* Cursor on       */
#define CURSOR_OFF  0b00001101  /* Cursor off      */
#define BLINK_ON    0b00001111  /* Cursor Blink    */
#define BLINK_OFF   0b00001110  /* Cursor No Blink */
 
/* Cursor or Display Shift defines */
#define SHIFT_CUR_LEFT    0b00000100  /* Cursor shifts to the left   */
#define SHIFT_CUR_RIGHT   0b00000101  /* Cursor shifts to the right  */
#define SHIFT_DISP_LEFT   0b00000110  /* Display shifts to the left  */
#define SHIFT_DISP_RIGHT  0b00000111  /* Display shifts to the right */
 
/* Function Set defines */
#define FOUR_BIT   0b00101100  /* 4-bit Interface               */
#define EIGHT_BIT  0b00111100  /* 8-bit Interface               */
#define LINE_5X7   0b00110000  /* 5x7 characters, single line   */
#define LINE_5X10  0b00110100  /* 5x10 characters               */
#define LINES_5X7  0b00111000  /* 5x7 characters, multiple line */
 
#ifdef _OMNI_CODE_
#define PARAM_SCLASS
#else
#define PARAM_SCLASS auto
#endif
 
#ifndef MEM_MODEL
#ifdef _OMNI_CODE_
#define MEM_MODEL
#else
#define MEM_MODEL far  /* Change this to near for small memory model */
#endif
#endif
 
/* OpenXLCD
 * Configures I/O pins for external LCD
 */
void OpenXLCD(PARAM_SCLASS unsigned char);
 
/* SetCGRamAddr
 * Sets the character generator address
 */
void SetCGRamAddr(PARAM_SCLASS unsigned char);
 
/* SetDDRamAddr
 * Sets the display data address
 */
void SetDDRamAddr(PARAM_SCLASS unsigned char);
 
/* BusyXLCD
 * Returns the busy status of the LCD
 */
unsigned char BusyXLCD(void);
 
/* ReadAddrXLCD
 * Reads the current address
 */
unsigned char ReadAddrXLCD(void);
 
/* ReadDataXLCD
 * Reads a byte of data
 */
char ReadDataXLCD(void);
 
/* WriteCmdXLCD
 * Writes a command to the LCD
 */
void WriteCmdXLCD(PARAM_SCLASS unsigned char);
 
/* WriteDataXLCD
 * Writes a data byte to the LCD
 */
void WriteDataXLCD(PARAM_SCLASS char);
 
/* putcXLCD
 * A putc is a write
 */
#define putcXLCD WriteDataXLCD
 
/* putsXLCD
 * Writes a string of characters to the LCD
 */
void putsXLCD(PARAM_SCLASS char *);
 
/* putrsXLCD
 * Writes a string of characters in ROM to the LCD
 */
void putrsXLCD(const char *);
 
/* User defines these routines according to the oscillator frequency */
extern void DelayFor18TCY(void);
extern void DelayPORXLCD(void);
extern void DelayXLCD(void);
 
#endif



I would also recommend examining the Microchip Peripheral Libraries documentation which I've attached below, as they contain many routines you might find useful to manage various commonly used peripherals. The Windows Help file (.chm), contained in the compiler docs directory, discuss the XLCD configuration setup.

BigDog
 

Attachments

  • MPLAB_XC8_Peripheral_Libraries.pdf
    9.7 MB · Views: 21

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,510
The compiler in use is the relevant issue, not the IDE.

What compiler are you utilizing for your project?

XC8, C18, CCS, etc?


BigDog
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top