Sunday, March 4, 2018

How to Interface 16x2 LCD with PIC Microcontroller (PIC16F877A)

Liquid Crystal Display











This component is specifically manufactured to be used with microcontrollers, which means that it cannot be activated by standard IC circuits. It is used for displaying different messages on a miniature liquid crystal display. The model described here is for its low price and great capabilities most frequently used in practice. It is based on the HD44780 microcontroller (Hitachi) and can display messages in two lines with 16 characters each. It displays all the letters of alphabet, Greek letters, punctuation marks, mathematical symbols etc. In addition, it is possible to display symbols made up by the user. Other useful features include automatic message shift (left and right), cursor

Inside the Display Controller
CG ROM stores segment patterns of 192 char
CG RAM stores segment patterns for 16-user designed characters.
An 8-bit instruction register
An 8-bit data register
DD RAM stores 80 numbers of 8-bit character codes 
11 instructions
To clear display
To write a character
To select a position
--To read information from the display, etc.

LCD Memory

LCD display contains three memory blocks:
DRAM – Display Data RAM;
CGRAM – Character Generator RAM; and
CGROM – Character Generator ROM.

DDRAM Memory








DDRAM memory is used for storing characters that should be displayed.
 The size of this memory is sufficient for storing 80 characters.
Some memory locations are directly connected to the characters on display.
It works quite simply: it is enough to configure the display to increment addresses
 automatically (shift right) and set the starting address for the message that should be
displayed (for example 00 hex).
After that, all characters sent through lines D0-D7 will be displayed as a message format
 we are used to- from left to right. In this very case, displaying starts from the first field
of the first line because the address is 00 hex. If more than 16 characters are sent then all
of them will be memorized, but only the first sixteen characters will be visible. In order to
 display the rest of them, a shift command should be used. Virtually, everything looks as if
 the LCD display is a window which shifts left-right over memory locations containing
 different characters. In reality, this is how the effect of message shifting on the screen
 has been created.

CGROM Memory

CGROM memory contains the default character map with all characters that can be displayed on the screen. Each character is assigned to one memory location:


What is Busy flag ?

Compared to the microcontroller, the LCD is an extremely slow component. Because of this, it was necessary to provide a signal which would, upon command execution, indicate that the display is ready for the next piece of data. That signal, called the busy flag, can be read from the line D7. When the voltage on this line is 0V (BF=0), the display is ready to receive new data.

         Pinout:












Commands:













Initializing the LCD for 8-bit operation:

To initialize for 2 lines, 5X7 matrix and 8bit operation the sequence of commandas
0x38,0x0E, and 0x01 should be executed.

If initialization is the first command in your code wait 15msec just after power up, if not
it is not necessary.

Sending Commands to the LCD

To send any of the commands, make pin RS and R/W both ‘0’ and  the command code in pin D0-D7
Then send a high-to-low pulse at pin E to enable the internal latch of the LCD 
For each of command you should wait at least 100 usec
But for clearing LCD (code=0x01) and Return home (code=0x02) you should wait for 2 msec

Sending Data to the LCD

To send any of the Data, make pin RS ‘1’ and R/W  ‘0’ and then put data in pin D0-D7
For each of command you should wait at least 100 usec
Then send a high-to-low pulse at pin E to enable the internal latch of the LCD

Sending Code or Data in 4-bit mode

        In 4-bit, we initialize LCD with 0x33, 0x32 and 0x28
The nibble 3, 3, 3 and 2 sets the LCD into 4-bit mode and 0x28 initialize the LCD for 5X7 matrix and 4-bit operation
select command register(RS=0)/Data register(RS=1)
Mask lower 4-bits
Send to the LCD port
Send enable signal
Mask higher 4-bits
Send to LCD port
Send enable signal

The Initial Setting Up for LCD

The LCD module must be connected to the port bits as follows:
 [LCD]             [AVR Port]
  RS (pin4) ------- bit 0
  RD (pin 5) ------ bit 1
  EN (pin 6) ------ bit 2
  DB4 (pin 11) --- bit 4
  DB5 (pin 12) --- bit 5
  DB6 (pin 13) --- bit 6
  DB7 (pin 14) --- bit 7
You must also connect the LCD power supply and contrast control voltage, 
according to the data sheet.

Some of the functions used for LCD

unsigned char lcd_init(unsigned char lcd_columns)
initializes the LCD module, clears the display and sets the printing character position 
at row 0 and column 0. The numbers of columns of the LCD must be specified (e.g. 16). 
No cursor is displayed.
void lcd_clear(void)
clears the LCD and sets the printing character position at row 0 and column 0.
void lcd_gotoxy(unsigned char x, unsigned char y)
sets the current display position at column x and row y. The row and column 
numbering starts from 0.
void lcd_putsf(char flash *str)
displays at the current display position the string str, located in FLASH

8-bit Connection












4-bit Connection
























Depending on how many lines are used for connecting an LCD to the microcontroller, there are 8-bit and 4-bit LCD modes. The appropriate mode is selected at the beginning of the operation in the process called ‘initialization’. The 8-bit LCD mode uses outputs D0- D7 to transfer data as explained on the previous page.
The main purpose of the 4-bit LCD mode is to save valuable I/O pins of the microcontroller. Only 4 higher bits (D4-D7) are used for communication, while others may be left unconnected. Each piece of data is sent to the LCD in two steps- four higher bits are sent first (normally through the lines D4-D7), then four lower bits. Initialization enables the LCD to link and interpret received bits correctly.
Advantages
LED consumes lot of power
Advantages of LCD
-Built in Data Controller
-Consume less power
-Able to display both Alphanumeric and custom Build characters.
-Can be used as a cheap alternative of debugging tools.


Circuit Diagram of Basic Language Program 



Circuit Diagram of C Language Program 






Code of Basic Language:

Define CONF_WORD = 0x3f72
Define CLOCK_FREQUENCY = 12
AllDigital

Define LCD_BITS = 8
Define LCD_DREG = PORTD
Define LCD_DBIT = 0
Define LCD_RSREG = PORTE
Define LCD_RSBIT = 0
Define LCD_RWREG = PORTE
Define LCD_RWBIT = 1
Define LCD_EREG = PORTE
Define LCD_EBIT = 2
Define LCD_READ_BUSY_FLAG = 1

Lcdinit

loop:
Lcdcmdout LcdClear
Lcdout "Hello World "
Lcdcmdout LcdLine2Home
Lcdout "ABCDEFGHIJ"
WaitMs 500

Goto loop

Code of C Language:

// LCD module connections
sbit LCD_RS at RD3_bit;
sbit LCD_EN at RD2_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_RS_Direction at TRISD3_bit;
sbit LCD_EN_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD7_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD4_bit;
// End LCD module connections


void main() {
TRISC=1;
TRISB=0;
lcd_init();
lcd_cmd(_LCD_CLEAR);
lcd_cmd(_LCD_CURSOR_OFF);
lcd_out(1,1," Hello World");
delay_ms(500);
lcd_out(2,1,"ABCDEFGHIJ");
delay_ms(500);

while(1)
        {}

}


1 comment:

  1. Casinos Near me - Hendon Mob - Hendon Mob
    Casinos Near Me - Find Casinos Near Me in Hendon Mob. 영천 출장샵 We have over 90 Casino, Sportsbook, 구리 출장샵 and Poker in 구미 출장안마 our 서울특별 출장샵 database for casino 서산 출장마사지 news,

    ReplyDelete