0% found this document useful (0 votes)
24 views26 pages

MP Lab 3

The document describes an experiment involving interfacing a LCD to a PIC microcontroller. It contains 3 activities: 1) Writing assembly code to display a name on the first line of the LCD in 4-bit mode. The code example and flowchart are provided. 2) Writing a C program to repeat Activity 1 and also display the year enrolled on the second LCD line. The code example is given. 3) Writing assembly code to continuously scroll the name displayed in Activity 1 across the LCD. The code example and flowchart are included. Results from executing each activity are also reported. The conclusion answers any questions about the experiment.

Uploaded by

Wong Wei Hao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views26 pages

MP Lab 3

The document describes an experiment involving interfacing a LCD to a PIC microcontroller. It contains 3 activities: 1) Writing assembly code to display a name on the first line of the LCD in 4-bit mode. The code example and flowchart are provided. 2) Writing a C program to repeat Activity 1 and also display the year enrolled on the second LCD line. The code example is given. 3) Writing assembly code to continuously scroll the name displayed in Activity 1 across the LCD. The code example and flowchart are included. Results from executing each activity are also reported. The conclusion answers any questions about the experiment.

Uploaded by

Wong Wei Hao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

FACULTY OF ENGINEERING

Experiment Interfacing a LCD to the PIC


No./Title:

Course Code/Name : EE317 Microprocessor Systems

Programme : B.Eng (Hons) Mechatronic Engineering (HYEK)

Name of Student : Wong Wei Hao

Student ID No : 1001746444

Semester : May 2020

Date of Experiment: 10th July 2020

Name of Lecturer : Mr. Manickam

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

Conclusion / Answering questions:.........................................................................................................26

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:

; The main program code is placed here.

Main:

; *** main code goes here ***

CLRF TRISD ; Define Port D as output

CLRF TRISB ; Define Port B as output

CLRF PORTB ; Set RS, R, E as low (0)

MOVLW 5 ;

CALL DELAY ; Calling delay subroutine

MOVLW 0x02 ; Initializing LCD display and cursor

CALL CMD ; Calling CMD subroutine

MOVLW 5 ;

CALL DELAY ; Calling delay subroutine

MOVLW 0x0C ; Display to ON, cursor to OFF

CALL CMD ; Calling CMD subroutine

MOVLW 5 ;

Page 3
CALL DELAY ; Calling delay subroutine

MOVLW 0x01 ; Clear the display

CALL CMD ; Calling CMD subroutine

MOVLW 5 ;

CALL DELAY ; Calling delay subroutine

MOVLW 0x28 ; Declare the LCD to 4-bit, 2-line size

CALL CMD ; Calling CMD subroutine

MOVLW 5 ;

CALL DELAY ; Calling delay subroutine

MOVLW 0x06 ; Defining auto-cursor increment

CALL CMD ; Calling CMD subroutine

MOVLW 5 ;

CALL DELAY ; Calling delay subroutine

MOVLW 0x80 ; Moving cursor to the start of the first line

CALL CMD ; Calling CMD subroutine

MOVLW 5 ;

CALL DELAY ; Calling delay subroutine

MOVLW 'W' ; Sending ASCII "W"

CALL LCDDATA ; Calling LCDDATA subroutine

MOVLW 'O' ; Sending ASCII "O"

CALL LCDDATA ; Calling LCDDATA subroutine

MOVLW 'N' ; Sending ASCII "N"

Page 4
CALL LCDDATA ; Calling LCDDATA subroutine

MOVLW 'G' ; Sending ASCII "G"

CALL LCDDATA ; Calling LCDDATA subroutine

MOVLW ' ' ; Sending ASCII " " (Space)

CALL LCDDATA ; Calling LCDDATA subroutine

MOVLW 'W' ; Sending ASCII "W"

CALL LCDDATA ; Calling LCDDATA subroutine

MOVLW 'E' ; Sending ASCII "E"

CALL LCDDATA ; Calling LCDDATA subroutine

MOVLW 'I' ; Sending ASCII "I"

CALL LCDDATA ; Calling LCDDATA subroutine

MOVLW ' ' ; Sending ASCII " " (Space)

CALL LCDDATA ; Calling LCDDATA subroutine

MOVLW 'H' ; Sending ASCII "H"

CALL LCDDATA ; Calling LCDDATA subroutine

MOVLW 'A' ; Sending ASCII "A"

CALL LCDDATA ; Calling LCDDATA subroutine

MOVLW 'O' ; Sending ASCII "O"

CALL LCDDATA ; Calling LCDDATA subroutine

CALL AGAIN

CMD: ; Declaring the CMD subroutine

MOVWF 0x40 ; Moving value in WREG into File Register with address 0x40

Page 5
ANDLW 0xF0 ; Mask lower nibble

MOVWF PORTD ; Moving the value in WREG into Port D

MOVLW 5 ; Adding a 10ms delay

CALL DELAY ; Calling delay subroutine

MOVLW 0x04 ; Setting RS and RW as low (0), and E to high (1)

MOVWF PORTB ; Moving the value in REG into Port B

MOVLW 5 ; Adding a 10ms delay

CALL DELAY ; Calling delay subroutine

CLRF PORTB ; Set RS, R, E as low (0)

SWAPF 0x40 ; Swap upper and lower nibble position

MOVF 0x40,W ; Moving the value in File Register to WREG

ANDLW 0xF0 ; Mask lower nibble

MOVWF PORTD ; Move the value in WREG into Port D

MOVLW 5 ; Adding a 10ms delay

CALL DELAY ; Calling delay subroutine

MOVLW 0x04 ; Setting RS and RW as low (0), and E to high (1)

MOVWF PORTB ; Move the value in WREG into Port B

MOVLW 5 ; Adding a 10ms delay

CALL DELAY ; Calling delay subroutine

CLRF PORTB ; Set RS, R, E as low (0)

RETURN

Page 6
LCDDATA:

MOVWF 0x50 ; Moving value in WREG into File Register with address 0x50

ANDLW 0xF0 ; Mask lower nibble

MOVWF PORTD ; Move the value in WREG into Port D

MOVLW 5 ; Adding a 10ms delay

CALL DELAY ; Calling delay subroutine

MOVLW 0x05 ; Setting RW as low (0), and RS and E to high (1)

MOVWF PORTB ; Move the value in WREG into Port B

MOVLW 5 ; Adding a 10ms delay

CALL DELAY ; Calling delay subroutine

MOVLW 0x01 ; Clearing RS, R, E to low (0)

MOVWF PORTB ; Move the value in WREG into Port B

SWAPF 0x50 ; Swap upper and lower nibble position

MOVF 0x50,W ; Moving the value in File Register to WREG

ANDLW 0xF0 ; Mask lower nibble

MOVWF PORTD ; Move the value in WREG into Port D

MOVLW 5 ; Adding a 10ms delay

CALL DELAY ; Calling delay subroutine

MOVLW 0x05 ; Setting RW as low (0), and RS and E to high (1)

MOVWF PORTB ; Move the value in WREG into Port B

MOVLW 5 ; Adding a 10ms delay

CALL DELAY ; Calling delay subroutine

Page 7
MOVLW 0x01 ; Setiing RS to high (1), and RW and E to low (0)

MOVWF PORTB ; Move the value in WREG into Port B

RETURN

DELAY:

MOVWF 0x20 ; Moving value in WREG into File Register with address 0x20

LOOP1:

MOVLW 255 ; 2 ms of delay with a count of 255

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,

skip the next line of code

GOTO LOOP2 ; go to Loop 2

DECFSZ 0x20 ; Decrease 1H of value from file register location 0X20, if the value is 0,

skip the next line of code

GOTO LOOP1 ; go to Loop 1

RETURN

AGAIN:

GOTO AGAIN ; Branch 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

* Author: Wong Wei Hao

* Created on July 10, 2020, 4:00 PM

*/

// CONFIG1H

#pragma config OSC = HS // Oscillator Selection bits (HS oscillator)

#pragma config OSCS = ON // Oscillator System Clock Switch Enable bit (Oscillator system

clock switch option is enabled (oscillator switching is enabled))

#include <xc.h> //Declaring xc8 library

#define _XTAL_FREQ 8000000 //Declaring LCD crystal frequency

Page 11
#define ldata PORTD //Declaring ldata as port D

#define rs PORTBbits.RB0 //Declaring RS as RB0 of port B

#define rw PORTBbits.RB1 //Declaring RW pin as RB1 of port B

#define en PORTBbits.RB2 //Declaring E pin as RB2 of port B

void lcdcmd(unsigned char value); // function prototype for LCD Commands

void lcddata(unsigned char value); // function prototype for LCD Data

void main() //Start main function

TRISD = 0; //Declare Port D as output

TRISB = 0; //Declare Port B as output

en = 0; //Clear enable = 0

__delay_ms(10); // Adding a 10ms delay

lcdcmd(0x02); //Initiate nibble mode in lcdcmd subroutine

__delay_ms(10); //Delay 10ms in lcdcmd subroutine

lcdcmd(0x28); //Declare LCD in 4-bit, 2-line mode

__delay_ms(10); // Adding a 10ms delay

lcdcmd(0x0C); //Initiatiation of display on,cursor off in lcdcmd subroutine

__delay_ms(10); // Adding a 10ms delay

lcdcmd(0x01); //Clear display screen

Page 12
__delay_ms(10); // Adding a 10ms delay

lcdcmd(0x06); //Cursor auto increment

__delay_ms(10); // Adding a 10ms delay

lcdcmd(0x80); //LCD cursor move to start of first line

__delay_ms(10); // Adding a 10ms delay

lcddata('W'); //Send ASCII “W” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('O'); //Send ASCII “O” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('N'); //Send ASCII “N” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('G'); //Send ASCII “G” (Space) in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata(' '); //Send ASCII “ ” (Space) in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('W'); //Send ASCII “W” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('E'); //Send ASCII “E” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('I'); //Send ASCII “I” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata(' '); //Send ASCII “ ” (Space) in lcddata subroutine

Page 13
__delay_ms(10); // Adding a 10ms delay

lcddata('H'); //Send ASCII “H” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('A'); //Send ASCII “A” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('O'); //Send ASCII “O" in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcdcmd(0xC0); //LCD cursor move to start of second line

__delay_ms(10); // Adding a 10ms delay

lcddata('E'); //Send ASCII “E” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('n'); //Send ASCII “n” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('r'); //Send ASCII “r” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('o'); //Send ASCII “o” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('l'); //Send ASCII “l” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('l'); //Send ASCII “l” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('e'); //Send ASCII “e” in lcddata subroutine

Page 14
__delay_ms(10); // Adding a 10ms delay

lcddata('d'); //Send ASCII “d” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata(' '); //Send ASCII “ ” (Space) in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('i'); //Send ASCII “i” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('n'); //Send ASCII “n” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata(' '); //Send ASCII “ ” (Space) in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('2'); //Send ASCII “2” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('0'); //Send ASCII “0” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('1'); //Send ASCII “1” in lcddata subroutine

__delay_ms(10); // Adding a 10ms delay

lcddata('7'); //Send ASCII “7” in lcddata subroutine

return; //End main function

void lcdcmd (unsigned char a ) //Declare lcdcmd subroutine

ldata = (ldata & 0x0F) |(0xF0 & a); //Send higher nibble of command first to PORT

Page 15
rs = 0; //Select resistor set to low

en = 1; //Enable pin set to high

__delay_ms(10); // Adding a 10ms delay

en = 0; //Enable pin set to low

__delay_ms(10); // Adding a 10ms delay

ldata = (ldata & 0x0F) | (a<<4); //Send lower nibble of command to PORT

en = 1; //Enable pin set to high

__delay_ms(10); // Adding a 10ms delay

en = 0; //Enable pin set to low

__delay_ms(10); // Adding a 10ms delay }

void lcddata (unsigned char a ) //Declare lcddata subroutine

ldata = (ldata & 0x0F) |(0xF0 & a); //Send higher nibble of command first to PORT

rs = 1; //Select resistor set to high

en = 1; //Enable pin set to high

__delay_ms(10); // Adding a 10ms delay

en = 0; //Enable pin set to low

__delay_ms(10); // Adding a 10ms delay

ldata = (ldata & 0x0F) | (a<<4); //Send lower nibble of command to PORT

en = 1; //Enable pin set to high

__delay_ms(10); // Adding a 10ms delay

en = 0; //Enable pin set to low

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

* Author: Wong Wei Hao

* Created on July 10, 2020, 6:00 PM

*/

// CONFIG1H

#pragma config OSC = HS // Oscillator Selection bits (HS oscillator)

#pragma config OSCS = ON // Oscillator System Clock Switch Enable bit (Oscillator

system clock switch option is enabled (oscillator switching is enabled))

#include <xc.h> //Declare xc8 header file

#define _XTAL_FREQ 8000000 //Declare LCD crystal frequency

#define ldata PORTD //Declare ldata as port D

#define rs PORTBbits.RB0 //Declare RS pin as RB0 of port B

#define rw PORTBbits.RB1 //Declare RW pin as RB1 of port B

#define en PORTBbits.RB2 //Declare E pin as RB2 of port B

Page 19
void lcdcmd(unsigned char value); // function prototype for LCD Commands

void lcddata(unsigned char value); // function prototype for LCD Data

void main() //Start main function

TRISD = 0; //Declare Port D as output

TRISB = 0; //Declare Port B as output

en = 0; //Clear enable = 0

__delay_ms(10); //Delay 10ms

lcdcmd(0x02); //Initiate nibble mode in lcdcmd subroutine

__delay_ms(10); //Delay 10ms in lcdcmd subroutine

lcdcmd(0x28); //Declare LCD 4 bit 2 line

__delay_ms(10); //Delay 10ms

lcdcmd(0x0C); //Initiation of display on,cursor off in lcdcmd subroutine

__delay_ms(10); //Delay 10ms

lcdcmd(0x01); //Clear display screen

__delay_ms(10); //Delay 10ms

lcdcmd(0x06); //Cursor auto increment

__delay_ms(10); //Delay 10ms

lcdcmd(0x86); //LCD cursor move to middle of first line

__delay_ms(10); //Delay 10ms

Page 20
lcddata('W'); //Send ASCII “W” in lcddata subroutine

__delay_ms(10); //Delay 10ms

lcddata('O'); //Send ASCII “O” in lcddata subroutine

__delay_ms(10); //Delay 10ms

lcddata('N'); //Send ASCII “N” in lcddata subroutine

__delay_ms(10); //Delay 10ms

lcddata('G'); //Send ASCII “G” in lcddata subroutine

__delay_ms(10); //Delay 10ms

lcdcmd(0xC6); //LCD cursor move to middle of second line

__delay_ms(10); //Delay 10ms

lcddata('2'); //Send ASCII “2” in lcddata subroutine

__delay_ms(10); //Delay 10ms

lcddata('0'); //Send ASCII “0” in lcddata subroutine

__delay_ms(10); //Delay 10ms

lcddata('2'); //Send ASCII “2” in lcddata subroutine

__delay_ms(10); //Delay 10ms

lcddata('0'); //Send ASCII “0” in lcddata subroutine

__delay_ms(10); //Delay 10ms

return; //End main function

void lcdcmd (unsigned char a ) //Declare lcdcmd subroutine

Page 21
{

ldata = (ldata & 0x0F) |(0xF0 & a); //Send higher nibble of command first to PORT

rs = 0; //Select resistor set to low

en = 1; //Enable pin set to high

__delay_ms(10); //Delay 10ms

en = 0; //Enable pin set to low

__delay_ms(10); //Delay 10ms

ldata = (ldata & 0x0F) | (a<<4); //Send lower nibble of command to PORT

en = 1; //Enable pin set to high

__delay_ms(10); //Delay 10ms

en = 0; //Enable pin set to low

__delay_ms(10); //Delay 10ms

void lcddata (unsigned char a ) //Declare lcddata subroutine

ldata = (ldata & 0x0F) |(0xF0 & a); //Send higher nibble of command first to PORT

rs = 1; //Select resistor set to high

en = 1; //Enable pin set to high

__delay_ms(10); //Delay 10ms

en = 0; //Enable pin set to low

__delay_ms(10); //Delay 10ms

ldata = (ldata & 0x0F) | (a<<4); //Send lower nibble of command to PORT

Page 22
en = 1; //Enable pin set to high

__delay_ms(10); //Delay 10ms

en = 0; //Enable pin set to low

__delay_ms(10); //Delay 10ms

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)

3. To send letter “A” to be displayed on the LCD, we must make RS = 1. (High)

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

You might also like