TekUT
Full Member level 6
pic lcd tutorial
Hi all,
I've see that many people have trouble about LCD interfacing, I've just finish to develop a library to do all the things needed to drive a HD44780 LCD controller based display with a PIC. My application is able to work with 4 bit interface (in order to save 4 pin of the microcontroller) and also isn't restricted to some PIC, just change the pin assignement on the header file and all is done.
This design was originally compiled with Hi-Tech for a PIC16F870 (20 MHz clock) but there is no restriction about what type of PIC you can use, simply change the pin assignement as I'll show later.
There are only three file:
- lcddisp.c (main file - just to show how to use the library)
- lcd-lib.c (the code to drive the LCD)
- lcd-lib.h (pin assignement)
Starting with the main file we've at first the inclusion of the header file regarding the microcontroller pin assignement:
Open the lcd-lib.h file, here below you can find the pin declaration for the Hi-Tech compiler, if you use another type of compiler simply change the pin syntax, just only a things, don't touch the pin name (RS, RW, EN, DB4, DB5, DB6, DB7) because these will be referred from the LCD routine inside the lcd-lib.c file:
Now you've available these functions:
If you are using a different compiler or other frequency clock simply change the delay routine to meet your need, just take care about the timing that I've written as remark into the source code.
About the display there is no restriction, the only things is about the controller it must be a HD44780 or compatible.
I've also attached a pic of the connection (the 2kOhm trimmer on the right of the connector is used to set the LCD contrast), anyway here the display connector signal name:
Pin 1: Vss (0 V)
Pin 2: Vcc (+5V)
Pin 3: Vee (contrast, just use a simple 2kOhm trimmer from Vcc to Vss, cursor to Vee)
Pin 4: RS
Pin 5: R/W
Pin 6: E
Pin 7: DB0 (not used for 4-bit interface)
Pin 8: DB1 (not used for 4-bit interface)
Pin 9: DB2 (not used for 4-bit interface)
Pin 10: DB3 (not used for 4-bit interface)
Pin 11: DB4 (used for 4-bit interface)
Pin 12: DB5 (used for 4-bit interface)
Pin 13: DB6 (used for 4-bit interface)
Pin 14: DB7 (used for 4-bit interface)
Pin 15: Led anode (+)
Pin 16: Led katode (-)
I've not used the led for back illumination, if you like to use it connect the Pin 15 trough a 680Ohm resistor to the +5V and the Pin 16 to the 0V.
Hope this can help a bit.
Bye
Pow
Hi all,
I've see that many people have trouble about LCD interfacing, I've just finish to develop a library to do all the things needed to drive a HD44780 LCD controller based display with a PIC. My application is able to work with 4 bit interface (in order to save 4 pin of the microcontroller) and also isn't restricted to some PIC, just change the pin assignement on the header file and all is done.
This design was originally compiled with Hi-Tech for a PIC16F870 (20 MHz clock) but there is no restriction about what type of PIC you can use, simply change the pin assignement as I'll show later.
There are only three file:
- lcddisp.c (main file - just to show how to use the library)
- lcd-lib.c (the code to drive the LCD)
- lcd-lib.h (pin assignement)
Starting with the main file we've at first the inclusion of the header file regarding the microcontroller pin assignement:
Code:
#include <pic.h>
#include <stdio.h>
#include "lcd-lib.h" // Pin definition for the LCD
Open the lcd-lib.h file, here below you can find the pin declaration for the Hi-Tech compiler, if you use another type of compiler simply change the pin syntax, just only a things, don't touch the pin name (RS, RW, EN, DB4, DB5, DB6, DB7) because these will be referred from the LCD routine inside the lcd-lib.c file:
Code:
// -------------------------------------------
// LCD type
// -------------------------------------------
#define MAXPOSX 16
#define MAXPOSY 2
// -------------------------------------------
// Definition of the LCD uP pin (Hi-Tech compiler)
// -------------------------------------------
static volatile bit RS @ (unsigned)&PORTA*8+0;
static volatile bit RW @ (unsigned)&PORTA*8+1;
static volatile bit EN @ (unsigned)&PORTA*8+2;
static volatile bit DB4 @ (unsigned)&PORTB*8+1;
static volatile bit DB5 @ (unsigned)&PORTB*8+2;
static volatile bit DB6 @ (unsigned)&PORTB*8+3;
static volatile bit DB7 @ (unsigned)&PORTB*8+4;
Now you've available these functions:
Code:
void GenDelay(int);// Generic delay
void LongDelay(long); // Long generic delay
void LCDInit(void); // LCD init routine
void ClearDisplay(void);// Clear the LCD display
void CursorAtHome(void);// Set cursor to home position
void Printxy(unsigned char, unsigned char, unsigned char); // Print a char
If you are using a different compiler or other frequency clock simply change the delay routine to meet your need, just take care about the timing that I've written as remark into the source code.
About the display there is no restriction, the only things is about the controller it must be a HD44780 or compatible.
I've also attached a pic of the connection (the 2kOhm trimmer on the right of the connector is used to set the LCD contrast), anyway here the display connector signal name:
Pin 1: Vss (0 V)
Pin 2: Vcc (+5V)
Pin 3: Vee (contrast, just use a simple 2kOhm trimmer from Vcc to Vss, cursor to Vee)
Pin 4: RS
Pin 5: R/W
Pin 6: E
Pin 7: DB0 (not used for 4-bit interface)
Pin 8: DB1 (not used for 4-bit interface)
Pin 9: DB2 (not used for 4-bit interface)
Pin 10: DB3 (not used for 4-bit interface)
Pin 11: DB4 (used for 4-bit interface)
Pin 12: DB5 (used for 4-bit interface)
Pin 13: DB6 (used for 4-bit interface)
Pin 14: DB7 (used for 4-bit interface)
Pin 15: Led anode (+)
Pin 16: Led katode (-)
I've not used the led for back illumination, if you like to use it connect the Pin 15 trough a 680Ohm resistor to the +5V and the Pin 16 to the 0V.
Hope this can help a bit.
Bye
Pow