Fa20-Bee-150 MP Lab Report - 04

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

1 Lab Report 4

MICROPROCESSORS
EEE-342

“Lab Report-04 LCD Interfacing with avr


microcontroller”

NAME: Muhammad Umar


CLASS: BEE-5c
REG No: FA20-BEE-150
INSTRUCTO
SIR RAHEEL AHMED
R:

Pre Lab-Tasks:
2 Lab Report 4

Find hexa-decimal codes for commands mentioned in LCD initializing.

Commands Hex Code


Clear Display 0x01
Return Home 0x02
Increment Cursor 0x06
Display on cursor blinking 0x0E
LCD On display on 0x0F
shift display left 0x07
8-bit 2-line 5x10 dots 0x3C

Table 1

In Lab Tasks:

Consider the basic wiring shown between an ATmega328P chip and an LCD in
figure 4.2.
Write and execute a C-program on Proteus that is able print Hello World on the
first row of the LCD.

Code:

#define F_CPU 16000000UL


#include <avr/io.h>

#include <util/delay.h>
#define RS PB0
#define RW PB1
#define EN PB2

int LCD_init(void);
void LCD_Send_Command(unsigned char);
void LCD_Send_Data(unsigned char);
int LCD_Send_Array(char * ptr);
int main(void)
{
3 Lab Report 4

LCD_init();
LCD_Send_Array("Hello World");
while (1)
{
}
return(0);
}
int LCD_init()
{
DDRD = 0b11111111;
DDRB = 0b00000111;
UCSR0B &= ~(1<<TXEN0);
UCSR0B &= ~(1<<RXEN0);
_delay_ms(100);
PORTB |=(1<<EN);
LCD_Send_Command(0x38);//lcd mode 8bit, 2 line, character size 5*7
_delay_ms(2);
LCD_Send_Command(0x0E);//display on, turn on cursor, no blinking
_delay_ms(2);
LCD_Send_Command(0x01);//clear display
_delay_ms(2);
LCD_Send_Command(0x06);//increament cursor, no display shift
_delay_ms(2);
return(0);
}
void LCD_Send_Command(unsigned char comm)
{
PORTB &= ~(1<<RS);//RS low command
PORTB &= ~(1<<RW);//RW low writing
PORTD = comm;//comm on port
PORTB &= ~(1<<EN);//short pulse on EN
_delay_ms(1);
PORTB |= (1<<EN);
}
void LCD_Send_Data(unsigned char data)
{
PORTB |= (1<<RS);//RS High command
PORTB &= ~(1<<RW);//RW low writing
4 Lab Report 4

PORTD = data;//comm on port


PORTB &= ~(1<<EN);//short pulse on EN
_delay_ms(1);
PORTB |= (1<<EN);
}
int LCD_Send_Array(char * ptr)
{
while(*ptr != '\0')
{
LCD_Send_Data(*ptr);
ptr++;
}
}

Proteus Simulation:

Figure 1

In Lab Task 2: Consider the basic wiring shown between an ATmega328P chip
and an LCD in figure 4.2.
5 Lab Report 4

Write and execute a C-program on Proteus that is able print your name on the
first row of the LCD and your roll-number on the second row of the LCD.

Code:

#define F_CPU 16000000UL


#include <avr/io.h>

#include <util/delay.h>
#define RS PB0
#define RW PB1
#define EN PB2

int LCD_init(void);
void LCD_Send_Command(unsigned char);
void LCD_Send_Data(unsigned char);
int LCD_Send_Array(char * ptr);
int main(void)
{
LCD_init();
LCD_Send_Array("Muhammad Umar ");
LCD_Send_Command(0xC0);
LCD_Send_Array("FA20-BEE-150");

/* Replace with your application code */


while (1)
{
}
return(0);
}
int LCD_init()
{
DDRD = 0b11111111;
DDRB = 0b00000111;
UCSR0B &= ~(1<<TXEN0);
UCSR0B &= ~(1<<RXEN0);
_delay_ms(100);
PORTB |=(1<<EN);
6 Lab Report 4

LCD_Send_Command(0x38);//lcd mode 8bit, 2 line, character size 5*7


_delay_ms(2);
LCD_Send_Command(0x0E);//display on, turn on cursor, no blinking
_delay_ms(2);
LCD_Send_Command(0x01);//clear display
_delay_ms(2);
LCD_Send_Command(0x06);//increament cursor, no display shift
_delay_ms(2);
return(0);
}
void LCD_Send_Command(unsigned char comm)
{
PORTB &= ~(1<<RS);//RS low command
PORTB &= ~(1<<RW);//RW low writing
PORTD = comm;//comm on port
PORTB &= ~(1<<EN);//short pulse on EN
_delay_ms(1);
PORTB |= (1<<EN);
}
void LCD_Send_Data(unsigned char data)
{
PORTB |= (1<<RS);//RS High command
PORTB &= ~(1<<RW);//RW low writing
PORTD = data;//comm on port
PORTB &= ~(1<<EN);//short pulse on EN
_delay_ms(1);
PORTB |= (1<<EN);
}
int LCD_Send_Array(char * ptr)
{
while(*ptr != '\0')
{
LCD_Send_Data(*ptr);
ptr++;
}
}

Output:
7 Lab Report 4

Figure 2

Hardware Implementation:

Critical Analysis / Conclusion:

Figure 3
8 Lab Report 4

The main objective of this lab was to interface LM016 16x2 LCD with ATmega328P
controller. I analyzed the basic configuration, data pins, register pins of the LCD. Made
the table for different instructions used for communication with LCD as shown in table1.
Wrote a C code to display some text on LCD my task was to display my name and
registration no on two different lines of LCD. Simulated it on proteus as shown in fig2
and then implemented it on hardware using Arduino nano as shown in fig3. After
interfacing LM016 with ATmega328P the outcome of this lab is that now I can use
LM016 LCD with ATmega328P according to my needs.

You might also like