Lab 1
Lab 1
INDEX
SL No Experiment Name
1 To develop programming with Arithmetic
logic Instructions
2 To develop programming using Ports.
3 To develop programming for Delay
Generation using Timers.
4 To develop programming for interrupt
handling.
5 To develop programming for
implementation of standard UART
Communication
6 To develop programming for interfacing
with LCD display.
7 To develop programming for interfacing
with Keypad.
1
EXPERIMENT -1
2
EXPERIMENT-2
EXPERIMENT-3
3
EXPERIMENT 4
Output:- Thus, we have created a square wave of a 200microsecond period using the IE flag,
EXPERIMENT 5
4
PROGRAM Code:-
MOV TMOD, #20H
MOV TH1,#-6
MOV SCON,#50H
SETB TR1
AGAIN:- MOV SBUF, “#A”
HERE:- JNB TI,HERE
CLR TI
SJMP AGAIN
Output:- Thus, we have transmitted the letter “A” serially using UART communication.
EXPERIMENT-6
5
RS: RS is the register select pin. We need to set it to 1, if we are sending some data to be
displayed on LCD. And we will set it to 0 if we are sending some command instruction like
clear the screen (hex code 01).
RW: This is Read/write pin, we will set it to 0, if we are going to write some data on LCD.
And set it to 1, if we are reading from LCD module. Generally this is set to 0, because we do
not have need to read data from LCD. Only one instruction “Get LCD status”, need to be read
some times.
E: This pin is used to enable the module when a high to low pulse is given to it. A pulse of
450 ns should be given. That transition from HIGH to LOW makes the module ENABLE.
There are some preset command instructions in LCD, we have used them in our program
below to prepare the LCD (in lcd_init() function). Some important command instructions are
given below:
Program Code
6
// Program for LCD Interfacing with 8051 Microcontroller (AT89S52)
#include<reg51.h>
#define display_port P2 //Data pins connected to port 2 on microcontroller
sbit rs = P3^2; //RS pin connected to pin 2 of port 3
sbit rw = P3^3; // RW pin connected to pin 3 of port 3
sbit e = P3^4; //E pin connected to pin 4 of port 3
7
msdelay(1);
e=0;
}
CIRCUIT DIAGRAM
8
Circuit diagram for LCD interfacing with 8051 microcontroller is shown in the above figure.
If you have basic understanding of 8051 then you must know about EA(PIN 31), XTAL1 &
XTAL2, RST pin(PIN 9), Vcc and Ground Pin of 8051 microcontroller. I have used these
Pins in above circuit. If you don’t have any idea about that then I recommend you to read this
Article LED Interfacing with 8051 Microcontroller before going through LCD interfacing.
So besides these above pins we have connected the data pins (D0-D7) of LCD to the Port 2
(P2_0 – P2_7) microcontroller. And control pins RS, RW and E to the pin 12,13,14 (pin 2,3,4
of port 3) of microcontroller respectively.
PIN 2(VDD) and PIN 15(Backlight supply) of LCD are connected to voltage (5v), and PIN 1
(VSS) and PIN 16(Backlight ground) are connected to ground.
Pin 3(V0) is connected to voltage (Vcc) through a variable resistor of 10k to adjust the
contrast of LCD. Middle leg of the variable resistor is connected to PIN 3 and other two legs
are connected to voltage supply and Ground.
9
EXPERIMENT-7
Program Code:-
#include<reg52.h> //including sfr registers for ports of the controller
#include<lcd.h>
10
//LCD Module Connections
sbit RS = P0^0;
sbit EN = P0^1;
sbit D0 = P2^0;
sbit D1 = P2^1;
sbit D2 = P2^2;
sbit D3 = P2^3;
sbit D4 = P2^4;
sbit D5 = P2^5;
sbit D6 = P2^6;
sbit D7 = P2^7;
//End LCD Module Connections
//Keypad Connections
sbit R1 = P1^0;
sbit R2 = P1^1;
sbit R3 = P1^2;
sbit R4 = P1^3;
sbit C1 = P1^4;
sbit C2 = P1^5;
sbit C3 = P1^6;
sbit C4 = P1^7;
//End Keypad Connections
void Delay(int a)
{
int j;
int i;
for(i=0;i<a;i++)
{
for(j=0;j<100;j++)
{
}
}
}
char Read_Keypad()
{
C1=1;
C2=1;
C3=1;
C4=1;
R1=0;
R2=1;
R3=1;
R4=1;
if(C1==0){Delay(100);while(C1==0);return '7';}
if(C2==0){Delay(100);while(C2==0);return '8';}
if(C3==0){Delay(100);while(C3==0);return '9';}
11
if(C4==0){Delay(100);while(C4==0);return '/';}
R1=1;
R2=0;
R3=1;
R4=1;
if(C1==0){Delay(100);while(C1==0);return '4';}
if(C2==0){Delay(100);while(C2==0);return '5';}
if(C3==0){Delay(100);while(C3==0);return '6';}
if(C4==0){Delay(100);while(C4==0);return 'X';}
R1=1;
R2=1;
R3=0;
R4=1;
if(C1==0){Delay(100);while(C1==0);return '1';}
if(C2==0){Delay(100);while(C2==0);return '2';}
if(C3==0){Delay(100);while(C3==0);return '3';}
if(C4==0){Delay(100);while(C4==0);return '-';}
R1=1;
R2=1;
R3=1;
R4=0;
if(C1==0){Delay(100);while(C1==0);return 'C';}
if(C2==0){Delay(100);while(C2==0);return '0';}
if(C3==0){Delay(100);while(C3==0);return '=';}
if(C4==0){Delay(100);while(C4==0);return '+';}
return 0;
}
void main()
{
int i=0;
char c,p;
Lcd8_Init();
while(1)
{
Lcd8_Set_Cursor(1,1);
Lcd8_Write_String("Keys Pressed:");
Lcd8_Set_Cursor(2,1);
Lcd8_Write_String("Times:");
while(!(c = Read_Keypad()));
p=c;
while(p==c)
{
i++;
Lcd8_Set_Cursor(1,14);
Lcd8_Write_Char(c);
Lcd8_Set_Cursor(2,7);
Lcd8_Write_Char(i+48);
Delay(100);
while(!(c = Read_Keypad()));
12
}
i=0;
Lcd8_Clear();
}
}
CONCLUSION:- Thus, we have studied the key board interfacing with 8051.
13