MP Lab 3
MP Lab 3
Student ID No : 1001746444
Page 1
TITLE: Interfacing a LCD to the PIC
ACTIVITY I...............................................................................................................................................3
1- Problems Statement:..............................................................................................................................3
2- Code Example:.......................................................................................................................................3
3- Flowchart................................................................................................................................................9
4- Results...................................................................................................................................................10
ACTIVITY II...........................................................................................................................................11
1- Problems Statement:............................................................................................................................11
2- Code Example:.....................................................................................................................................11
3- Flowchart .............................................................................................................................................17
4- Results...................................................................................................................................................18
ACTIVITY III..........................................................................................................................................19
1- Problems Statement:............................................................................................................................19
2- Code Example:.....................................................................................................................................19
3- Flowchart..............................................................................................................................................24
4- Results...................................................................................................................................................25
Page 2
Activity I
1- Problems Statement:
Write an assembly language program to display your name on line 1 of the LCD in 4-bit mode
(first name followed by last name with a space in between).
2- Code Example:
Main:
MOVLW 5 ;
MOVLW 5 ;
MOVLW 5 ;
Page 3
CALL DELAY ; Calling delay subroutine
MOVLW 5 ;
MOVLW 5 ;
MOVLW 5 ;
MOVLW 5 ;
Page 4
CALL LCDDATA ; Calling LCDDATA subroutine
CALL AGAIN
MOVWF 0x40 ; Moving value in WREG into File Register with address 0x40
Page 5
ANDLW 0xF0 ; Mask lower nibble
RETURN
Page 6
LCDDATA:
MOVWF 0x50 ; Moving value in WREG into File Register with address 0x50
Page 7
MOVLW 0x01 ; Setiing RS to high (1), and RW and E to low (0)
RETURN
DELAY:
MOVWF 0x20 ; Moving value in WREG into File Register with address 0x20
LOOP1:
MOVWF 0x21 ; Moving value in WREG into File Register with address 0x21
LOOP2:
DECFSZ 0X21 ; Decrease 1H of value from file register location 0X21, if the value is 0,
DECFSZ 0x20 ; Decrease 1H of value from file register location 0X20, if the value is 0,
RETURN
AGAIN:
END
Page 8
3- Flowchart
Start the process- First initialize the Port D and
Port B. In order to initialize the LCD for
operations, such as clearing the display, pin RS
has to be LOW, and R and E at LOW as well.
Then, start initializing LCD and cursor
followed by declaring LCD mode to be 4-bit,
and cursor increment after a value is entered.
Loops such as LCDDATA (for ASCII
entering), DELAY (to insert time delay), and
LOOP1 and 2 and AGAIN are all declared.
Then, the alphabets in the name can be started
entering using ASCII and calling loops
required (LCDDATA, DELAY) until the
process ends.
4- Results
Page 9
Page 10
Activity II
1- Problems Statement:
Write a C-program to repeat activity 1 while also putting the year you enrolled in UCSI on the
second line. When you run your program, the LCD in 4-bit mode should show (for example):
Alex Young
Enrolled In 2015
2- Code Example:
/*
* File: Lab_3_Activity_2.c
*/
// CONFIG1H
#pragma config OSCS = ON // Oscillator System Clock Switch Enable bit (Oscillator system
Page 11
#define ldata PORTD //Declaring ldata as port D
en = 0; //Clear enable = 0
Page 12
__delay_ms(10); // Adding a 10ms delay
Page 13
__delay_ms(10); // Adding a 10ms delay
Page 14
__delay_ms(10); // Adding a 10ms delay
ldata = (ldata & 0x0F) |(0xF0 & a); //Send higher nibble of command first to PORT
Page 15
rs = 0; //Select resistor set to low
ldata = (ldata & 0x0F) | (a<<4); //Send lower nibble of command to PORT
ldata = (ldata & 0x0F) |(0xF0 & a); //Send higher nibble of command first to PORT
ldata = (ldata & 0x0F) | (a<<4); //Send lower nibble of command to PORT
Page 16
__delay_ms(10); // Adding a 10ms delay
3- Flowchart
Start of the process- Declaring Port B and D as outputs and setting the Enable pin to LOW (0). Then,
The LCD is initialized to declare the 4-bit, 2-line mode, and calling lcdcmd subroutine to turn on the
display and turn off cursor. LCD is then cleared, and auto-increment for cursor. The cursor starts at
first line to input alphabets in ASCII to display using the loop lcddata (WREG to File Register). The
same loop is used when the command 0xc0 is used, where the cursor moves to the second line and
Page 17
continuing the input of the alphabets/digits.
4- Results
Page 18
Activity III
1- Problems Statement:
Write a C-program to display your last name on the first line and the current year on the second
line. Both should be in the middle of the line. Use 4-bit mode in LCD.
.
2- Code Example:
/*
* File: Lab_3_Activity_3.c
*/
// CONFIG1H
#pragma config OSCS = ON // Oscillator System Clock Switch Enable bit (Oscillator
Page 19
void lcdcmd(unsigned char value); // function prototype for LCD Commands
en = 0; //Clear enable = 0
Page 20
lcddata('W'); //Send ASCII “W” in lcddata subroutine
Page 21
{
ldata = (ldata & 0x0F) |(0xF0 & a); //Send higher nibble of command first to PORT
ldata = (ldata & 0x0F) | (a<<4); //Send lower nibble of command to PORT
ldata = (ldata & 0x0F) |(0xF0 & a); //Send higher nibble of command first to PORT
ldata = (ldata & 0x0F) | (a<<4); //Send lower nibble of command to PORT
Page 22
en = 1; //Enable pin set to high
Page 23
3- Flowchart
Start the program- In the C program, include
the xc8 library in header file, which is a 8-bit
xc8 compiler library. Then, the ports (Port B
and D and their bits) are declared with the pins
on the PIC (RW, RS and E) and ldata. Then,
the loops such as lcdcmd (command register)
and lcddata (data register) are declared to be
used in the process. With a short delay, the
LCD and cursor is initialized, such as
switching on the LCD, clearing the LCD, and
setting the LCD to 4-bit, 2-line mode with
auto-increment cursor. Then with a short delay
again, the alphabets/digits are entered into the
data register to be shown on the display using
the loops until the name and the current year is
fully shown on the display.
Page 24
4- Results
Page 25
Conclusion
1. How does the LCD distinguish data from instruction codes when receiving information at its data pin?
It distinguishes the data from instruction codes by selecting command register and data register, when command
register (cmd) is selected, instruction code received will be treated as command to initialize the LCD for
operations. If data register is selected, the code will be read as ASCII to be displayed on the LCD.
2. To send the instruction code 01 to clear the display, we must make RS = 0. (Low)
4. What is the purpose of the E line? Is it an input or an output as far as the LCD is connected?
E line is an input, as it is the enable line of an LCD, that latches data and send it to the LCD.
5. When is the information (code or data) on the LCD pin latched into the LCD?
When the Enable pin (E) is high (1).
Page 26