Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views

Lab Handout#05 Solved

The document describes interfacing a character LCD with an AVR ATMEGA16 microcontroller. It provides details on the LCD pinout and commands. The objective is to display characters on the LCD by programming the microcontroller. It describes initializing the LCD, creating subroutines for sending commands and data, and provides an example main program to initialize and send data to the LCD.

Uploaded by

hk2359140
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Lab Handout#05 Solved

The document describes interfacing a character LCD with an AVR ATMEGA16 microcontroller. It provides details on the LCD pinout and commands. The objective is to display characters on the LCD by programming the microcontroller. It describes initializing the LCD, creating subroutines for sending commands and data, and provides an example main program to initialize and send data to the LCD.

Uploaded by

hk2359140
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

MEHRAN UNIVERSITY OF ENGINEERING AND TECHNOLOGY, JAMSHORO

DEPARTMENT OF ELECTRONIC ENGINEERING

INTRODUCTION TO EMBEDDED SYSTEMS (ES-314)


Batch: 21ES (5th Semester)

Lab Experiment #5
Interfacing Character LCD with AVR ATMEGA 16
Name Hitesh Kumar Roll # 21ES048
Signature of Lab Tutor Date

RUBRICS:

USE OF MODERN

DATA ANALYSIS
Performance Metric

PARTICIPATION

OBSERVATION/
CALCULATION
ENGINEERING
CONDUCTING

AND CODING
EXPERIMENT
TEAMWORK

Total Score
PROGRAM
RESULTS
TOOLS
0.5 0.5 1 1 1 1 05
Obtained

OBJECTIVE(S)
The purpose of this lab is to:
# Topic # Of Lectures CLO Taxonomy level
Become familiar with Character LCD and its
1
working.
3 4,5 P3, A2
Display the characters on LCD through C
2
programming of ATMEGA16

OUTCOME(S)
a. An ability to use the techniques, skills, and modern PLO5: Modern Tool Usage
engineering tools necessary for engineering practice.
b. An ability to function on multi-disciplinary teams PLO9: Individual and Teamwork

REQUIRED TOOLS AND SOFTWARES:


 WinAVR (GCC Compiler)
 AVR Studio 4 or any latest version - (for Programming)
 Proteus 8 or any latest version - (for Simulation)
 Extreme Burner – AVR (for Programming through ICSP)
 AVR ATMEGA16/ ATMEGA32 Chip
 USBAsp Programmer
 16x2 Character LCD
INTRODUCTION:
Various display devices are usually used to show information to users, like LED is used as
indicator to show the indication like the Power is On, Device is working, Internet is working fine
on DSL Modems etc. LCDs are used to show messages and information to users, like in cell
phones, it displays messages and help us to connect to a particular contact from various contacts
stored etc.
In this lab, we will learn to interface with any microcontroller, we will also work with various
options of LCD.
LCDs are of two types:
1. Graphics LCD
2. Character / Matrix LCD
Here, we will work with 16x2 Character LCD.

Character LCDs:
With various LCD Manufacturers, the pin positions also vary. In most of character LCDs the pin
1 starts from Left side of LCD and ends at either pin number 14 or 16. Pin description is same
for all character LCDs, the pin number 15 and 16 are for Backlight, positive supply and GND.
Pin Description of Character LCD is given in table 1.

Table 1: Pin Description for LCD Table 2: LCD Command Codes

Working of LCD:
The LCD works in two modes, command mode and data mode. RS pin of LCD is responsible for
selecting Mode. When RS =0 then command mode is selected and when RS=1 data mode is
selected.
When we instruct LCD to do some operations like clear LCD, set cursor position, increment
cursor etc, we will use command mode (RS=0).
When we want to display data on screen of LCD then we will use Data Mode (RS=1).

Procedure to work with Character LCD:


1. Initialize LCD (RS=0 – Command Mode)
2. Display Data (RS=1 Data Mode)

Initializing LCD:
There are various commands by which LCD is initialized; the list of commands is given in Table
2:
The list of necessary commands for initializing LCD is given below:
1. 8-Bit – 2 lines 5x7 matrix
2. Display ON
3. Increment Cursor
4. Clear Display Screen
5. Set Starting Cursor Position

Schematic Diagram:
We will connect LCD with Port C and D. Simplified connections are shown below:
Data lines of LCD are connected to Port C and Control Pins RS and EN of LCD are connected to
Port D Pin 7 and 6 respectively.
Programming:
Delay subroutine:
There is a built-in delay subroutine, which can be added to program by including header file
<delay.h>, and delay can be generated by calling.
_delay_ms(time in milliseconds);
Anywhere in program, where <time in milliseconds> will be replaced by a number.
For example, if we want to generate delay of 1 second (1000 milliseconds):
_delay_ms(1000);

Programming LCD:

As Port C is connected to Data lines of LCD and RS pin is connected to Port D-pin6, EN of LCD
is connected to Port D-pin7.
To write any command to LCD:
First make Port C and Port D as output ports:
DDRC=0xFF;
DDRD=0xFF;
To send any command to LCD, make RS=0, by writing logic LOW to Port D-pin 6:
PORTD &=~(1 << 6);
Now, send command, for example we are sending 8-bit mode command, which is 0x38 from
Table 2:
PORTC = 0x38;
After writing any command or sending any data, we must make EN signal of LCD HIGH for a
little time and then make it off, such that data available on data lines can be stored in LCD’s
buffer. Hence, the code will be:

PORTD &= ~(1<<6); // RS=0


PORTC=0x38; // Command
PORTD |=(1<<7); // EN=1
_delay_ms(1); // delay of 1 millisecond
PORTD &= ~(1 << 7); // EN=0

To initialize LCD, we will send multiple commands and between each command we will create
some delay say 10 milliseconds. To send multiple commands to LCD, we will repeat above code,
by just changing command code, like for LCD on, Cursor Blinking, we have to write: 0x0E, the
code will be:

PORTD &= ~(1<<6); // RS=0


PORTC=0x0E; // Command for Display ON, Cursor Blinking
PORTD |=(1<<7); // EN=1
_delay_ms(1); // delay of 1 millisecond
PORTD &= ~(1 << 7); // EN=0

Now this code will be repeated for every command. Why not to make a subroutine for this code.

Making a subroutine:
To make a function or subroutine, the basic structure is:
void name (datatype& variable)
{
// Code here
}

Whenever you call name, it will execute program above.

To make a subroutine for LCD commands:

void lcd_cmd(unsigned char data)


{
PORTD &= ~(1<<6); // RS=0
PORTC=data; // Command
PORTD |=(1<<7); // EN=1
_delay_ms(1); // delay of 1 millisecond
PORTD &= ~(1 << 7); // EN=0
}

Now whenever we call lcd_cmd (any data); it will execute subroutine define above, but it will
send data that is placed in brackets (here it is <any data>).

For example, we are calling:


lcd_cmd(0x38);

it will execute full subroutine of lcd_cmd but now data variable has value of 0x38. And 0x38 will
write to Port C.

The main program to write multiple commands to LCD will look like:

#include "avr/io.h"
#include "util/delay.h"
voidlcd_cmd(unsigned char cmnd)
{
PORTD &= ~(1 <<rs);
data = cmnd;
PORTD |= (1 << en);
_delay_ms(1);
PORTD &= ~(1<< en);
}
int main(void)
{
DDRD=0xFF;
DDRC=0xFF;
lcd_cmd(0x38); // 8-bit Mode
_delay_ms(1);
lcd_cmd(0x0E); // Display on, Cursor Blinking
….
return 0;
}
Similarly, if we want to make data subroutine (RS=1):

void lcd_data (unsigned char data2)


{
PORTD |= (1<<6); // RS=1
PORTC=data2; // character data
PORTD |=(1<<7); // EN=1
_delay_ms(1); // delay of 1 millisecond
PORTD &= ~(1 << 7); // EN=0
}

But the above subroutine will only print one character to LCD per call.

Now, we have to execute multiple commands on LCD and have to send more than one character
to display on LCD.So the main program will be:

#include "avr/io.h"
#include "util/delay.h"

void lcd_cmd (unsigned char cmnd)


{
PORTD &= ~(1 <<6); // RS=0
PORTC= cmnd;
PORTD |= (1 <<7); // EN=1
_delay_ms(1);
PORTD &= ~(1<< 7); // EN=0
}

void lcd_data (unsigned char data)


{
PORTD |= (1<<6); // RS=1
PORTC = data;
PORTD |= (1 << 7); // EN=1
delay(1);
PORTD &= ~(1<< 7); // EN=0
}

int main(void)
{
DDRD=0xFF; // Port D as output
DDRC=0xFF; // Port C as output (LCD Data)
lcd_cmd(0x38); // 8-bit Mode
_delay_ms(1);

lcd_cmd (0x0E); // Display On, Cursor Blinking


_delay_ms(1);
lcd_cmd (0x06); // Increment Cursor
_delay_ms(1);

lcd_cmd (0x01); // Clear Display


_delay_ms(1);

lcd_cmd(0x80); // Cursor Position at 1st row, 1st col


_delay_ms(1);

lcd_data ('A'); // Data Display, char A


_delay_ms(1);

While (1);
return 0;
}

This program will write just one character ‘A’ to LCD screen, if we want to display multiple
characters to LCD, we have to repeatlcd_data(); for each letter. Hence, to avoid that we can use
arrays to write multiple bytes to LCD screen.

Using Arrays:
Using Arrays, we can store data which is to be displayed on LCD screen:

unsigned char <array-name>[<no. of characters>]=“<text-here>”;

for example: if we want to store word “Welcome” to an Array:

unsigned char data[7]=”welcome”;

in square-brackets we will write size of word that is stored in array. Here word “Welcome”
occupies 7 bytes, hence we have written 7 in square brackets.

Now if want to write welcome on LCD, we will use loop and repeat lcd_data(); command, like:

unsigned char data[7]= “Welcome”;

for(int i=0;i<7;i++)
{
lcd_data(data[i]);
_delay_ms(1);
}

This will repeat lcd_data(); command and each time it will take a byte from array “data” and
display it on LCD.

Setting Position on LCD to display Data:


The LCD command list includes two commands for two rows of 16x2 LCD.
0x80 command is used to force cursor to be beginning of first line, and 0xC0 is used to force
cursor to be beginning of 2nd line.
80 81 ……… …. .. …. .. .. . .. .. . . ….. . . ..

C0 C1 ……… …. .. …. .. .. . .. .. . . ….. . .
...............CF
To display data starting from row 1, column 2, 0x81 command will be sent to LCD.

Lab Tasks:
Simulate on Proteus and execute the following programs on AVR Chip. Attach the C program
files and simulation results snapshots along with the handouts for the following tasks.

1. Display your name and Roll No. at Center of LCD


2. Make that text blink with delay of 1 seconds.
3. Consider text written in task 1, make that text rotate left and right, the text
should completely disappear towards right and then returns and disappears to
left and this process continues infinite times.
4. In task 1, make your Name visible all the time, whereas your roll number should
blink continuously.
5. Write your name and roll number on LCD such that letters appear one by one.

You might also like