Sunday, October 23, 2011

pic 16f877 and LCD 16X2 character

discription :
HD 44780 based displays are very popular among hobbyists because they are cheap and they can display characters.Besides they are very easy to interface with microcontrollers and  most of the present day high_level compilers have in_built library routines for them.Today ,we will see how to inteerface an HD44780 based character LCD to a pic 16f877 microcontroller.The interface requires 6 input/output lines of the pic 16f877 : 4 data lines and 2 control lines.A blinking test message ,"hi Im charaf zouhair welcome to my blog ", will display on the LCD screen

Required Theory :
All HD 44780 based character LCD display are connected through 14 pins : 8 pins data pins (D0-D7), 3 control pins (RS,E,R/W), and three power lines (Vdd,Vss,Vee).some LCDs have LED backlight feature that helps to read the data on the display during low illumination conditions. so they have two additional connections (LED +and LED-), making altogether 16 pin. A -pin LCD module with its pin diagram is shown below
Control Pins :
The control pin RS determines if the data transfer between the LCD module and an external microcontroller are actual character data or command/status.when the microcontroller needs to send commands to LCD or to read the LCD status,it must be pulled low.Similarly,this must be pulled high if character data is to be sent to and from the LCD module.

The direction of data transfer is controlled by the R/W pin. if it is pulled low, the commands or character data is written to the LCD module. and when it is pulled high, the character data or status information from the LCD registers is read. here,we will use one way data transfer,from microcontrollerto LCD module, so the R/W will be grounded permanently.

The enable pin(E) initiates the actual data transfer.when wrting to the LCD display, the data is transferred only on the high to low transition of the E pin .

Power Supply Pins :
Altough most of the LCD module data sheets recommend +5V DC. Supply for operation,some LCDs may work well for a wider range(3 to5.5V).The Vdd pin should be connected to the positive power supply and Vss to ground.Pin 3 is Vee,which is used to adjust the contrast of the display.In the most of this cases, this pins is connected to a voltage between 0 and 2V by using a present potentiometer.

Data Pins :
Pins 7 to 14 are data lines (D0-d7) data transfer to and from the display can be achieved either in 8-bit or 4-bit mode . The 8-bit mode uses all eight data lines to transfer a byte,whereas ,in a 4-bit mode,a byte is transferd as two 4-bit nibbles .In the later case,only the upper 4 data lines (D4-D7) are used.This technique is beneficial as this saves 4 input/output pins of microcontroller.We will use the 4-bit mode.
For further details on LCDs please visite this site mybe helpfull http://lcd-linux.sourceforge.net/pdfdocs/lcd1.pdf
http://lcd-linux.sourceforge.net/pdfdocs/lcd2.pdf

Circuit Diagram:
Data transfer between the MCU and the LCD module will occur in the 4-bit. the R/W pin (5) of the LCD module is permanently grounded as there won't be any data read from the LCD module .RC0-RC3 serves the 4-bit data lines (d4-d7,pins 11-14) of the LCD module. control lines,RS and E ,are connected to RC4 and RC5 . Thus ,altogether 6 I/O pins of the 16f877 are used by the LCD module.The contrast adjustment is done with a 5K potentiometer as shown below  If your LCD module has backlight LED, use a 68 ohom resistance in series with pin 15 or 16 to limit the current through the LE .the detail of the circuit diagram is shown below.



Software :
The LCD data and control lines are driven through PORTB,so it must be defined as output port (TRISB=0).You need   select all I/O pins as digital (adcon1=0x07).The programming of an HD 44780 LCD is a little bit complex procedure as it requires accurate timing and proper sequence of various control signals and commands.But luckily, the MIKROC pro for pic has library routines to communicate with a standard HD44780 based LCD module using 4-bit mode.This makes programming a lot easier before using the built-in functions for LCD, the connections of LCD pins to the PIC microcontrollers must be defined. The comments are provided in the source code to understand how to use the library routines for LCDs in MIKROC pro for pic
/*
Lab 3: blink character message on LCD using 16f877
Internal Oscillator @ 8MHz, MCLR Enabled, PWRT Enabled, WDT OFF
Oct 23,      2011 by charaf zouhair
*/

sbit lcd_rs at rb0_bit;
sbit lcd_en at rb1_bit;
sbit lcd_d4 at rb2_bit;
sbit lcd_d5 at rb3_bit;
sbit lcd_d6 at rb4_bit;
sbit lcd_d7 at rb5_bit;
//***************************************************************************
sbit lcd_rs_direction at trisb0_bit;
sbit lcd_en_direction at trisb1_bit;
sbit lcd_d4_direction at trisb2_bit;
sbit lcd_d5_direction at trisb3_bit;
sbit lcd_d6_direction at trisb4_bit;
sbit lcd_d7_direction at trisb5_bit;
//**********************************************************************
char text1[]="CHARAF ZOUHAIR";
char text2[]="THIS IS MY BLOG";
char text3[]="MOROCCO";
char text4[]="ELECTRONICS";
//******************************************************************
void main() {
  adcon1=0x07;
trisb=0;
portb=0;
lcd_init();
lcd_cmd(_lcd_clear);
lcd_cmd(_lcd_cursor_off);
lcd_out(1,1,text1);
lcd_out(2,1,text2);
delay_ms(5000);
lcd_cmd(_lcd_clear);
lcd_out(1,6,text3);
lcd_out(2,4,text4);

}

i want to share with you this book : http://goo.gl/5Eqhi

For more information about code contact me : charaf_zohair@hotmail.com

No comments: