LCD Configuration and Programming
Interfacing and Programming with Input/Output Device: LCD LCD (liquid crystal display) 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. It is based on the HD44780 microcontroller (Hitachi) and can display messages in two lines with 16 characters each. It can display all the letters of alphabet, Greek letters, punctuation marks, mathematical symbols etc. It is also possible to display symbols made up by the user. Other useful features include automatic message shift (left and right), cursor appearance, LED backlight etc.
Liquid Crystal Display (LCD) Two types of LCD Alphanumeric Graphic Selection depends on What size and format is required to display the desired information What optical characteristics will look best in the package and attract the user to the product
Types of LCD Graphic LCD Alphanumeric LCD
Alphanumeric Liquid Crystal Display (LCD) Used to display numerical information, text massage and special symbol Many variety 16x2 (16 characters per lines, 2 lines) 16x4 (16 characters per lines, 4 lines) Two types of displays 5 x 8 dot character fonts with cursor 5 x 10 dot character fonts with cursor Can be controlled in two ways, depending on availability of IO pins 8 pins (8-bit interface) 4 pins (4-bit interface)
LCD
LCD PINS Pin Name Function 1 VSS Ground 2 VDD Supply (+5V) 3 VEE Contrast adjustment 4 RS 5 R/W Register Select Signal 0: Instruction Register (write), Address Counter (read) 1: Data Register Read/Write Signal 0: Write 1: Read 6 E Enable Signal (H L) : Start data read/write 7 DB0 8 DB1 9 DB2 10 DB3 11 DB4 12 DB5 13 DB6 14 DB7 15 LED+ Anode for LED backlighting Four low order bidirectional tristate data bus pins. Used for data transfer and receive between controller and LCD driver. These pins are not used during 4-bits operation Four high order bidirectional tristate data bus pins. Used for data transfer and receive between controller and LCD driver. DB7 can be used as a busy flag 16 LED- Cathode for LED backlighting
Program Writting for LCD Start up writing #include <htc.h> CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define_xtal_freq 20000000//20 MHz #define LCD_data PORTD //refer PTK40A schematic #define LCD_rs RA2//or can write PORTAbits.RA2 #define LCD_en RA3
Sending Command and Data to LCD Sending command Move data to LCD port Select command register Select write operation Send enable signal Wait for LCD to process the command Void LCD_sendcommand(unsigned char cmd) { LCD_data = cmd; LCD_rs = 0; //Selected command register LCD_rw = 0; //Writing in data register LCD_en = 1; //Enable H L delay_ms(1); //wait for a while LCD_en = 0; delay_ms(2); //wait for LCD to process the command }
Sending Command and Data to LCD Setting cursor position To set cursor position, send the DDRAM address Bit 7 always set to 1 and bit 0 to 7 are DDRAM address To put cursor on the first position in first line, the address will be 0b00000000 in binary and set the bit 7 to 1, so the address will be 0x80 or 0b10000000 To put cursor on the first position in second line, the address will be 0b01000000 in binary and set the bit 7 to 1, so the address will be 0xC0 or 0b11000000 Void LCD_gotoxy(unsigned char x, unsigned char y) { unsigned char pos; if(y==1) { pos=x 0x80; // or with 0x80 to set bit 7 = 1 }else { pos=x 0x80 0x40; // or with 0x80 to set bit 7 = 1 and //or with 0x40 because second line address start from 0x40 } LCD_sendcommand(pos); //call send command function }
Sending Command and Data to LCD Sending string data String is a set of data Sending string of data is actually sending multiple data one by one Void LCD_sendstringdata(unsigned char* data) { } while(*data!= \0 ) { } LCD_senddata(*data); data++; //till string ends //send characters one by one
LCD Initialization Before using the LCD for display purpose, LCD has to be initialized either by the internal reset circuit or sending set of commands. Initialize by internal reset circuit Automatically initializes when the power is turn on. The BF is kept in busy state until the initialize ends The following instructions are executed during the initialization Display Clear Function Set: DL = 1 ; 8-bit interface data N = 0; 1-line display F = 0; 5x8 dot character font Display on/off control: D = 0; Display off C = 0; Cursor off B = 0; Blinking off Entry mode set: I/D = 1; increment by 1 S = 0; No shift
LCD Initialization Conditions below have to be met if want to used initialization by internal reset Problem: highly dependent on power supply
LCD Initialization Initialization by instructions can be summarized as follow After power on, delay at least 15ms for proper operation Send Function Set command Send Display Control command Send set entry mode command
LCD Initialization description Void LCD_init() { delay_ms(15); //Delay for stability LCD_data = 0x38; //Function set: 2 line, 8-bit, 5x7 dots LCD_rs = 0; //Selected command register LCD_rw = 0; //Writing in data register LCD_en = 1; //Enable H L delay_ms(1); //wait for a while LCD_en = 0; delay_ms(2); //wait for LCD to process the command LCD_data = 0x0F; //Display on, Cursor blinking command LCD_rs = 0; //Selected command register LCD_rw = 0; //Writing in data register LCD_en = 1; //Enable H L delay_ms(1); //wait for a while LCD_en = 0; delay_ms(2); //wait for LCD to process the command
LCD Initialization } LCD_data = 0x01; //Clear LCD LCD_rs = 0; //Selected command register LCD_rw = 0; //Writing in data register LCD_en = 1; //Enable H L delay_ms(1); //wait for a while LCD_en = 0; delay_ms(2); //wait for LCD to process the command LCD_data = 0x06; //Entry Mode, auto increment with no shift LCD_rs = 0; //Selected command register LCD_rw = 0; //Writing in data register LCD_en = 1; //Enable H L delay_ms(1); //wait for a while LCD_en = 0; delay_ms(2); //wait for LCD to process the command
LCD Initialization void LCD_init() { } delay_ms(15); LCD_sendcommand(0x38); //4(?) bit mode, 1/16 duty, 5x8 font LCD_sendcommand(0x0F); //display off(?) LCD_sendcommand(0x00); //display on, blink curson on LCD_sendcommand(0x06); //entry mode
LCD instructions set - Main body example void main(void) { TRISB0 = 1; //This is to define all port B0 is input TRISB1 = 1; //This is to define all port B1 is input TRISC = 0x00; //PortC as o/p TRISD = 0x00; //PortD as o/p TRISA = 0x00; //PortA as o/p ADCON1 = 0x06; //configure ALL pins as digital i/p LCD_init(); while(1) { {LCD_sendcommand(0x80); LCD_send stringdata("electric"); delay_ms(300); LCD_sendcommand...continue
Shift word "Electric" from right to left { LCD_sendcommand(0xC0); LCD_sendstringdata("Electric"); delay_ms(300); LCD_sendcommand(0x18);//shift entire display left } Shift word "Electric" from left to right { LCD_sendcommand(0xC0); LCD_sendstringdata("Electric"); delay_ms(300); LCD_sendcommand(0x1C);//shift entire display right }
Interfacing and Programming with Input/Output Device: LCD Example2: LCD command code To send any of the command codes listed in Table 12-2 to the LCD, make pin RS = 0. For data, make RS = 1. Then send a high-to-low pulse to the E pin to enable the internal latch of the LCD.
Interfacing and Programming with Input/Output Device: LCD Example 1: LCD initialization command code sequence
On/Off letters on LCD screen { } RC2=1; //or use PORTC=0b00000100 LCD_sendcommand(0x80); LCD_sendstringdata("Alarm!!"); LCD_sendcommand(0x0C);//on display delay_ms(200); LCD_sendcommand(0x08);//off display delay_ms(200);
LCD Commands and Instruction Set *DDRAM address is given in Slide 24 **CGRAM address from 0x00 to 0x3F, 0x00 to 0x07 for char 1 and so on.
Program Writing using C language 1)Write a program to display text below a. Microcontroller at first line of thelcd b. System Design at second line of the LCD. 2)Write a program to display text a. If the switch 1 (SW1) is pressed will be displayed Microcontroller at first line of the LCD, and second line of the LCD is clear. b. If the switch 2 (SW2) is pressed will be displayed System Design at secon line of the LCD, and first line of the LCD is clear.
QUESTION 2 Need to include SW command #define SW1 RB0 #define SW2 RB1 TRISB0 = 1; //This is to define all port B0 as input TRISB1 = 1; //This is to define all port B1 as input
if (SW1==0) OR { LCD_sendcommand(0x80); LCD_sendstringdata("Microcontroller"); }
else if (SW2==0) OR { LCD_sendcommand(0xC0); LCD_sendstringdata("System Design"); }
REFER to LAB MODULE 5 (PIC Input and Output Interfacing -LCD)
Interfacing and Programming with Input/Output Device: LCD
Interfacing and Programming with Input/Output Device: LCD
Interfacing and Programming with Input/Output Device: LCD Connection of a 2x16 character LCD to PIC16F877A
LCD Controller Function Description Instruction Register (IR) Store instruction codes and address information for display data RAM (DDRAM) and character generator RAM (CGRAM) Can only be written Data Register (DR) Temporarily stores data to be written into or to be read from DDRAM or CGRAM Busy Flag (BF) 1: indicates the controller is in internal operation mode, no further instruction can be written BF is output to DB7 when RS = 0 and R/W = 1
LCD Controller Function Description Address Counter (AC) Assigns addresses to both DDRAM and CGRAM. The selection of either DDRAM or CGRAM is determined concurrently by the instruction AC automatically incremented by 1 after writing data into DDRAM or CGRAM AC automatically decremented by 1 after reading data from DDRAM or CGRAM AC contents are output to DB0 to DB6 when RS = 0 and R/W = 1
Interfacing and Programming with Input/Output Device: LCD LCD display contains three memory blocks: 1. DDRAM Display Data RAM; 2. CGRAM Character Generator RAM; and 3. CGROM Character Generator ROM.
LCD Controller Function Description Register Selection Table RS R/W Operation 0 0 Write to IR (write instruction) 0 1 Read busy flag (DB7) and address counter (DB0 DB6) 1 0 Write to DR (DDRAM or CGRAM) 1 1 Read from DR (DDRAM or CGRAM)
Interfacing and Programming with Input/Output Device: LCD DDRAM Memory DDRAM memory is used for storing characters to be displayed. The size of this memory is capable of storing 80 characters. Some memory locations are directly connected to the characters on display.
LCD Controller Function Description Character Generator ROM (CGROM) Generates 5x8 or 5x10 dots character patterns from 8-bit character codes. Can generate 208 5x8 dots character patterns and 32 5x10 dots character pattern Character Generator RAM (CGRAM) Used to rewrite (regenerate) character pattern by program 5x8 dots: max 8 character patterns can be written 5x10 dots: max 4 character patterns can be written
Character Code and Character Pattern
Character Code and Character Pattern
LCD Commands and Instruction Set MCU can only control IR and DR The internal operation of the LCD is determined by signals send from the MCU These signals, RS, R/W and data bus, make up the LCD instructions as shown in Table 3. There are four categories of instructions: Designate LCD functions such as display format, clear display, etc Set internal RAM addresses Perform data transfer with internal RAM Perform miscellaneous functions
The ASCII code table is shown in Table 4.4. A 250 ms delay is executed between each output to make the count visible.
LCD Commands and Instruction Set
Interfacing and Programming with Input/Output Device: LCD CGRAM Memory Apart from standard characters, the LCD display can also display symbols defined by the user itself. It can be any symbol in the size of 5x8 pixels. RAM memory called CGRAM in the size of 64 bytes enables it. Memory registers are 8 bits wide, but only 5 lower bits are used. Logic one (1) in every register represents a dimmed dot, while 8 locations grouped together represent one character. It is best illustrated in figure below:
Interfacing and Programming with Input/Output Device: LCD
Interfacing and Programming with Input/Output Device: LCD Writing a letter/character to the LCD display To write a letter/character on the LCD display we have to do the following: 1. Perform an initialization. 2. Send the desired position to IR (DDRAM Address). 3. Send ASCII code of the letter to DR. LCD display will show the letter that matches the code that was sent and the address counter AC will be updated (increment or decrement, depending on how it was initialized). You can write strings by sending characters in sequence.
Interfacing and Programming with Input/Output Device: LCD Before using the LCD for display purpose, LCD has to be initialized either by the internal reset circuit or sending the commands to initialize the LCD. Given is a flowchart that describes the step to follow to initialize the LCD. When you send out an instruction (command or information) to the LCD it takes some time to execute it, so it is important to make sure that the LCD is "ready" for the next instruction/operation. There are two ways to send characters (command/data) to the LCD: Create a delay subroutine to accommodate the minimum execution time. Scanning BF (busy flag) bit this bit gives an indication whether the LCD is finished working.
Interfacing and Programming with Input/Output Device: LCD LCD instruction set The LCD instruction set consists of the commands you can send to LCD. Remember that the RS line needs to be set to zero (RS = 0) to send instruction to the LCD. When the RS line is set to one (RS = 1), you are sending data to display memory or the character graphics (CG) memory. An X in any position means it does not matter what you enter there.
Interfacing and Programming with Input/Output Device: LCD Clear Display: This command clears the display and returns the cursor to the home position (address 0) and sets I/D to 1 in order to increment the cursor. Its line settings are as follows: RS R/W D7 D6 D5 D4 D3 D2 D1 D0 0 0 0 0 0 0 0 0 0 1 Home Cursor: This returns the cursor to the home position, returns a shifted display to the correct position, and sets the display data (DD) RAM address to 0. Its line settings are as follows: RS R/W D7 D6 D5 D4 D3 D2 D1 D0 0 0 0 0 0 0 0 0 1 X
Interfacing and Programming with Input/Output Device: LCD Entry Mode Set: This command sets the cursor move direction and specifies whether to shift the display or not. These operations are performed during the data write/read of the CG or DD RAM. Its line settings are as follows: RS R/W D7 D6 D5 D4 D3 D2 D1 D0 0 0 0 0 0 0 0 1 I/D S I/D=0 means the cursor position is decremented (moves right to left). I/D=1 means the cursor position is incremented (moves left to right). S=0 means normal operation, the display remains still, and the cursor moves. S=1 means the display moves with the cursor.
Interfacing and Programming with Input/Output Device: LCD Display On/Off Control: This command sets the ON/OFF display as well as the cursor and blinking capabilities (0 equals OFF; 1 equals ON). D controls whether the display is ON or OFF, C controls whether the cursor is ON or OFF, B controls whether the blinking is ON or OFF. The line settings are as follows: RS R/W D7 D6 D5 D4 D3 D2 D1 D0 0 0 0 0 0 0 1 D C B Cursor or Display Shift: This moves the cursor and shifts the display without changing DD RAM contents. The line settings are as follows: S/C=0 means move the cursor. S/C=1 means shift display. R/L= 0 means shift to the left. R/L= 1 means shift to the right. RS R/W D7 D6 D5 D4 D3 D2 D1 D0 0 0 0 0 0 1 S/C R/L X X
Interfacing and Programming with Input/Output Device: LCD Function Set: This sets the interface data length (DL), the number of display lines (N), and character font (F). The line settings are as follows: RS R/W D7 D6 D5 D4 D3 D2 D1 D0 0 0 0 0 1 DL N F X X DL=0 means 4 bits are being used (the standard) DL=1 means a full 8 bits being utilized N=0 means 1 line N=1 means 2 lines or more F=0 means that 5x7 dot characters are used (which is how 99% of all LCDs are set up) F=1 means 5x10 dot characters are used
Interfacing and Programming with Input/Output Device: LCD Set CG RAM Address: This command sets the custom graphics (CG) RAM address. Setting RS to 1 sends data to CG RAM instead of the DD RAM. Eight CG characters are available, and they reside in the ASCII codes 0 through 7. The is sent in the 8-bit bytes from the top row to the bottom row and is left justified, meaning that only the bottom 5 bits matter (it is a 5x7 dot matrix). The line settings are as follows: RS R/W D7 D6 D5 D4 D3 D2 D1 D0 0 0 0 1 MSB CG RAM ADDRESS LSB
Interfacing and Programming with Input/Output Device: LCD Set DD RAM Address: This sets the DD RAM address. Setting RS to 1 sends data to the display RAM, and the cursor advances in the direction where the I/D bit was set to. The line settings are as follows: RS R/W D7 D6 D5 D4 D3 D2 D1 D0 0 0 1 MSB DD RAM ADDRESS LSB Read Busy Flag and Address: This reads the busy flag (BF). If BF equals to 1, the LCD is busy and displays the location of the cursor. With the R/W line grounded, this command can not be used. The line settings are as follows: RS R/W D7 D6 D5 D4 D3 D2 D1 D0 0 1 0 0 0 0 0 0 0 1
Interfacing and Programming with Input/Output Device: LCD Write Data to CG or DD RAM: This command s line settings are as follows: RS R/W D7 D6 D5 D4 D3 D2 D1 D0 1 0 MSB ASCII code or CG bit pattern data LSB Read Data from CG or DD RAM: This command s line settings are as follows: RS R/W D7 D6 D5 D4 D3 D2 D1 D0 1 1 MSB ASCII code or CG bit pattern data LSB