Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Experiment No.

2
Aim: Write a program to generate square wave of 50% duty cycle having frequency 5 KHz
at port pin P2.1 using timer 1 in mode 2. Modify program to generate pulse waveform
of 70% duty cycle using timer on the same pin.
Apparatus required: 89s5x trainer Kit, CRO, FG, and BNC probe etc.
Circuit Diagram:

Fig.1 Circuit diagram for generating the 50% duty cycle pulse having 5 kHz frequency
on 2.1 pin

Calculation of delay:
Time = 1/Frequency = 1/ (5x1000) ms = 200 S
For 50% duty cycle ON time and OFF time are equal
Therefore, T_ON = T_OFF = 100 S
Time delay required is 100 S

If we consider crystal frequency 11.0592 MHz, time to execute one cycle is


If
TH0= 255-100 S /1.085 S
TH0=162 (Decimal value)
TH0=A2 (Hex value)

Program:
#include <reg52.h>
#include<stdio.h>
void Delay(void);
sbit mybit=P2^1;
void main(void)
{
while (1)
{
mybit=~mybit;
Delay();
}
}
void Delay(void)
{
TMOD=0x02;
TL0=0x00;
TH0=0xA2;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}

Output-

For 70% Duty Cycle

Calculation of delay:
Time = 1/Frequency = 1/ (5x1000) ms = 200 S
For 80% duty cycle ON time and OFF time will be different.
Therefore,
T_ON = 140 S
T_OFF = 60 S
ON Time delay required is 140 S
If we consider crystal frequency 11.0592 MHz, time to execute one cycle is
Value = 65536 140 S / 1.085 S
= 65407 (Decimal value) = FF7F (Hex value)
Therefore, TH0= FF, TL0=7F
OFF Time delay required is 60 S
Value = 65536 60 S / 1.085 S
= 65481 (Decimal value) = FFC9 (Hex value)
Therefore, TH0= FF, TL0=C9
Program-

#include <reg51.h>
#include<stdio.h>
void T0M1Delay1(void);
void T0M1Delay2(void);
sbit mybit=P2^1;
void main(void)
{
while (1)
{
mybit=1;
T0M1Delay1();
mybit=0;
T0M1Delay2();
}
}
void T0M1Delay1(void)
{
TMOD=0x01;
TL0=0x7F;
TH0=0xFF;
TR0=1;
while (TF0==0);
{
TR0=0;
TF0=0;
}
}
void T0M1Delay2(void)
{
TMOD=0x01;
TL0=0xC9;
TH0=0xFF;
TR0=1;
while (TF0==0);
{
TR0=0;
TF0=0;
}
}
Output-

Worksheet

1) Modify program for 80% duty cycle.


Hint: Use separate delay loop for ON time and OFF time. Total time is 200 s, so use 160 s
for ON time and 40 s for OFF time. You can also prepare delay subroutine of 10 s. Call
delay subroutine 16 times for ON time and 4 times for OFF time.
Calculation of delay:
Time = 1/Frequency = 1/ (5x1000) ms = 200 S
For 80% duty cycle ON time and OFF time will be different.
Therefore,
T_ON = 160 S
T_OFF = 40 S
ON Time delay required is 160 S
If we consider crystal frequency 11.0592 MHz, time to execute one cycle is
Value = 65536 160 S / 1.085 S
= 65389 (Decimal value) = FF6D (Hex value)
Therefore, TH0= FF, TL0=6D
OFF Time delay required is 40 S

Value = 65536 40 S / 1.085 S


= 65499 (Decimal value) = FFDB (Hex value)
Therefore, TH0= FF, TL0=DB

Program#include <reg51.h>
#include<stdio.h>
void Delay1(void);
void Delay2(void);
sbit mybit=P1^0;
void main(void)
{
while (1)
{
mybit=1;
Delay1();
mybit=0;
Delay2();
}
}
void Delay1(void)
{
TMOD=0x01;
TL0=0x6D;
TH0=0xFF;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
void Delay2(void)
{
TMOD=0x01;
TL0=0xDB;
TH0=0xFF;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
Output-

Ton = (76.67826-76.67808) s = 0.176 ms


Toff = (76.67831-76.67826) s = 0.05 ms
2) Execute following C program in Keil and measure frequency of the square wave.
Program# include <reg52.h>
#include<stdio.h>
sbit wave_pin = P1^0;
void sqdelay(unsigned int);
void main(void)
{
while(1)
{
wave_pin=1;
sqdelay(100);
wave_pin=0;
sqdelay(100);
}
}
void sqdelay(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<1000;j++);
}

Output-

Thus, frequency of the square wave is:


1 / (9.52502-8.331425) = 0.83781 Hz

3) What modifications you suggest in above program to reduce frequency of square


wave to half?

Program# include <reg51.h>


#include<stdio.h>
sbit wave_pin= P1^0;
void sqdelay(unsigned int);
void main(void)
{
while(1)
{
wave_pin=1;
sqdelay(200);
wave_pin=0;
sqdelay(200);
}
}
void sqdelay(unsigned int t)

{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<1000;j++);
}

Output -

Thus, frequency of square wave is:


1 / (85.692-83.309) = 0.41964 Hz

4) Suggest modification in the program to achieve duty cycle 60% without changing
frequency of the square wave.
Program# include <reg51.h>
#include<stdio.h>
sbit wave_pin= P1^0;
void sqdelay1(unsigned int);
void sqdelay2(unsigned int);
void main(void)
{
while(1)
{
wave_pin=1;
sqdelay1(100);
wave_pin=0;
sqdelay2(100);
}
}

void sqdelay1(unsigned int t)


{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<600;j++);
}
void sqdelay2(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<400;j++);
}
Output-

8) WAP to toggle all bits of port P1 continuously with 10 ms delays in between.


Calculation of DelayValue = 65536 5000 S / 1.085 S
= 60430 (Decimal value) = EE02 (Hex value)
Therefore, TH0= EE, TL0=02

Program#include<reg52.h>
#include<stdio.h>
void Delay(void)
{
TMOD=0x02;
TL0=0x02;
TH0=0xEE;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
void main(void)
{
P1=0x00;
while(1)
{
P1=0xff;
Delay();
P1=0x00;
Delay();
}
}
Output-

9) Why crystal frequency in divided by 12 in 8051?


Crystal frequency in divided by 12 because 1 machine cycle needs to execute 12 oscillator
periods in 8051. It is a presale which is used to execute 1 machine cycle instruction.
10) Explain TMOD register?
TMOD is an 8 bit register, in which lower 4 bits are for Timer 0 and upper 4 bits are for
Timer 1.

GATE

C/T

M1

Timer1

M0

GATE

C/T

M1

Timer0

11) A switch is connected to pin P2.2.WAP to monitor the switch and create the
following frequency on P3.3:
SW=100 Hz
SW=1 KHz
Calculation for delay
1) 1 kHz
ON time and OFF time are equal TON = TOFF = 0.001s
Time delay required is 10 ms
If we consider crystal frequency 11.0598 MHz, time to execute one cycle is:
delay= 65536-0.001 s /1.085 s
64615 (Decimal value) FC67 (Hex value)
TH0=FC
TL0=67
2) 100 Hz
ON time and OFF time are equal TON = TOFF = 0.01s
Time delay required is 10 ms
If we consider crystal frequency 11.0598 MHz, time to execute one cycle is:
delay= 65536-0.01 s /1.085 s
56320 (Decimal value) FC67 (Hex value)
TH0=DC
TL0=00
Program# include <reg51.h>
#include<stdio.h>
sbit pin = P2^2;
sbit sw = P3^3;
void delay();
void delay1();
void main(void)
{
while(1)
{
if(sw==1)
{
pin = 1;

M0

delay1();
pin = 0;
delay1();
}
else if(sw==0)
{
pin = 1;
delay();
pin = 0;
delay();
}
}
}
void delay()
{
TMOD=0x01;
TL0=0x00;
TH0=0xDC;
TR0=1;
while(TF0==0);
TF0=0;
}
void delay1()
{
TMOD=0x01;
TL0=0x67;
TH0=0xFC;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}
TR0=0;

EXPERIMENT -3
AIM: Interface LCD with the microcontroller. Display your name on the LCD.
APPARATUS REQUIRED: 89s5x trainer kit, CRO etc.
CIRCUIT DIAGRAM:

PROGRAM:
#include <reg51.h>
#include <stdio.h>
sfr ldata = 0x80;
sbit rs = P1^0;
sbit rw = P1^1;
sbit en = P1^2;
void lcdcmd(unsigned char value1);
void lcddata(unsigned char value2);
void msdelay(unsigned int itime);
void main()
{
lcdcmd(0x38);

msdelay(100);
lcdcmd(0x01);
msdelay(100);
lcdcmd(0x80);
msdelay(100);
lcdcmd(0x06);
msdelay(100);
lcddata('C');
msdelay(100);
lcddata('H');
msdelay(100);
lcddata('I');
msdelay(100);
lcddata('N');
msdelay(100);
lcddata('N');
msdelay(100);
lcddata('U');
msdelay(100);
}
void lcdcmd(unsigned char value1)
{
ldata = value1;
rs = 0;
rw = 0;
en = 1;
msdelay(1);
en = 0;
}
void lcddata(unsigned char value2)
{
ldata = value2;
rs = 1;
rw = 0;
en = 1;
msdelay(1);
en = 0;
}
void msdelay(unsigned int itime)
{
unsigned int i, j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}

WOKSHEET
1)Modify LCD program to display two lines: WELCOME TO E.C. on first line and
Embedded lab on the second line. Execute program in your hardware.
#include <reg51.h>
#include <stdio.h>
sfr ldata = 0x80;
sbit rs = P1^0;
sbit rw = P1^1;
sbit en = P1^2;
void lcdcmd(unsigned char value1);
void lcddata(unsigned char value2);
void msdelay(unsigned int itime);
void main()
{
lcdcmd(0x38);
msdelay(100);
lcdcmd(0x01);
msdelay(100);
lcdcmd(0x80);
msdelay(100);
lcdcmd(0x06);
msdelay(100);
lcddata('W');
msdelay(100);
lcddata('E');
msdelay(100);
lcddata('L');
msdelay(100);
lcddata(C);
msdelay(100);
lcddata('O');
msdelay(100);
lcddata('M');
msdelay(100);
lcddata('E');
msdelay(100);
lcddata(' ');
msdelay(100);
lcddata('T');
msdelay(100);
lcddata('O');
msdelay(100);
lcddata(' ');
msdelay(100);
lcddata('E');
msdelay(100);

lcddata(' ');
msdelay(100);
lcddata('C');
msdelay(100);
lcdcmd(0xC0);
msdelay(100);
lcddata('E');
msdelay(100);
lcddata('M');
msdelay(100);
lcddata('B');
msdelay(100);
lcddata('E');
msdelay(100);
lcddata('D');
msdelay(100);
lcddata('D');
msdelay(100);
lcddata('E');
msdelay(100);
lcddata('D');
msdelay(100);
lcddata(' ');
msdelay(100);
lcddata('L');
msdelay(100);
lcddata('A');
msdelay(100);
lcddata('B');
msdelay(100);
}
void lcdcmd(unsigned char value1)
{
ldata = value1;
rs = 0;
rw = 0;
en = 1;
msdelay(1);
en = 0;
}
void lcddata(unsigned char value2)
{
ldata = value2;
rs = 1;
rw = 0;
en = 1;
msdelay(1);
en = 0;
}

void msdelay(unsigned int itime)


{
unsigned int i, j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}

2) Explain the pin out diagram of LCD 16x2.


PIN NO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

NAME
Vss
Vcc
Vee
RS
RW
En
DH0
DH1
DH2
DH3
DH4
DH5
DH6
DH7
LED+
LED-

DESCRIPTION
This pin must be connected to the ground.
Positive supply voltage pin (5V DC).
Contrast adjustment
Register selection
Read or write
Enable
Data
Data
Data
Data
Data
Data
Data
Data
Back light LED+
Back light LED-

3) How does the LCD distinguish between data and command ?


Their are two registers in every character lcd Data and Command. When we want to send
data we have to send it to Data register. When we want to send Commands we have to send it
to command register.
lcdcmd function is used to send commands to lcd.
lcddata function is used to send data to lcd.
Example:
LCD COMMANDS
void lcdcmd(unsigned char value)
{
P1 = value;
rs = 0;
rw = 0;
en = 1;
MSDelay(50);
en = 0;

MSDelay(50);
}
LCD DATA
void lcddata(unsigned char value)
{
P1 = value;
rs = 1;
rw = 0;
MSDelay(50);
en = 0;
MSDelay(50);
}
5) How does the busy flag aid in making the LCD program more efficient?
The busy flag is D7 . If D7=1 the LCD is woking on internal operation and will not accept
any new information. If D7=0 the LCD is free and is available to receive as well as work on
new information.
6) For a 16*2 LCD, the location of the last character of line 1 is 8FH (its command code).
Show how this value was calculated?

Line1(min)
Line1(max)

DB7
1
1

DB6
0
0

DB5
0
0

DB4
0
0

DB3
0
1

DB2
0
1

DB1
0
1

DB0
0
1

The upper address range can go as high as 1111(binary) = 15 decimal , which corresponds to
locations 0 to 15 for the LCDs of 16*2 size.
7) Draw the circuit diagram of interfacing of 20x2 LCD with 8051?

EXPERIMENT NO -4
AIM: Write a program to interface the DC motor with 8051.
a) If sw=0, DC motor will rotate clockwise
b) If sw=1, DC motor will rotate anticlockwise
APPARATUS REQUIRED: 89s5x trainer kit
CIRCUIT DIAGRAM:

PROGRAM:
#include <reg51.h>
#include <stdio.h>
sbit sw=P2^7;
sbit A1=P1^0;
sbit A2=P1^1;
void main()
{
while(1)
{

if(sw==1)
{
A1=1;
A2=0;
}
else
{
A1=0;
A2=1;
}}}

WORKSHEET
1) Interface DC motor with 89C51 microcontroller. Write a program to rotate motor with
different speed using PWM.
a) 5% duty cycle
#include <reg51.h>
#include <stdio.h>
sbit sw=P2^7;
sbit A1=P1^0;
sbit A2=P1^1;
void msdelay(unsigned int itime);
void main()
{
while(1)
{
if(sw==1)
{
A1=1;
msdelay(5);
A2=0;
msdelay(95);
}
else

{
A1=0;
msdelay(5);
A2=1;
msdelay(95);
}}}
void msdelay(unsigned int itime)
{
int i,j;
for(i=0;i<itime;i++);
for(j=0;j<1275;j++);
}

b) 10% duty cycle

#include <reg51.h>
#include <stdio.h>
sbit sw=P2^7;
sbit A1=P1^0;
sbit A2=P1^1;
void msdelay(unsigned int itime);
void main()
{
while(1)
{
if(sw==1)
{
A1=1;
msdelay(10);
A2=0;
msdelay(90);
}
else

{
A1=0;
msdelay(10);
A2=1;
msdelay(90);
}}}
void msdelay(unsigned int itime)
{
int i,j;
for(i=0;i<itime;i++);
for(j=0;j<1275;j++);
}

c) 25% duty cycle


#include <reg51.h>
#include <stdio.h>
sbit sw=P2^7;
sbit A1=P1^0;
sbit A2=P1^1;
void msdelay(unsigned int itime);
void main()
{
while(1)
{
if(sw==1)
{
A1=1;
msdelay(25);
A2=0;
msdelay(75);
}
else

{
A1=0;
msdelay(25);
A2=1;
msdelay(75);
}}}
void msdelay(unsigned int itime)
{
int i,j;
for(i=0;i<itime;i++);
for(j=0;j<1275;j++);
}

d) 50% duty cycle


#include <reg51.h>
#include <stdio.h>
sbit sw=P2^7;
sbit A1=P1^0;
sbit A2=P1^1;
void msdelay(unsigned int itime);
void main()
{
while(1)
{
if(sw==1)
{
A1=1;
msdelay(50);
A2=0;
msdelay(50);
}
else

{
A1=0;
msdelay(50);
A2=1;
msdelay(50);
}}}
void msdelay(unsigned int itime)
{
int i,j;
for(i=0;i<itime;i++);
for(j=0;j<1275;j++);
}

e) 75% duty cycle


#include <reg51.h>
#include <stdio.h>
sbit sw=P2^7;
sbit A1=P1^0;
sbit A2=P1^1;
void msdelay(unsigned int itime);
void main()
{
while(1)
{
if(sw==1)
{
A1=1;
msdelay(75);
A2=0;
msdelay(25);
}
else
{

A1=0;
msdelay(75);
A2=1;
msdelay(25);
}}}
void msdelay(unsigned int itime)
{
int i,j;
for(i=0;i<itime;i++);
for(j=0;j<1275;j++);
}

f) 100% duty cycle


#include <reg51.h>
#include <stdio.h>
sbit sw=P2^7;
sbit A1=P1^0;
sbit A2=P1^1;
void msdelay(unsigned int itime);
void main()
{
while(1)
{
if(sw==1)
{
A1=1;
msdelay(100);
A2=0;
msdelay(0);
}
else
{
A1=0;

msdelay(100);
A2=1;
msdelay(0);
}}}
void msdelay(unsigned int itime)
{
int i,j;
for(i=0;i<itime;i++);
for(j=0;j<1275;j++);
}

2)Why driver circuit is needed for motor driving?


Normal DC motor requires current greater than 250 milliAmpere . 8051 microcontroller IC
cannot supply this amount of current . If we directly connect motors to the output of any IC ,
they might get damaged. There is need of circuitry that can act as a bridge between the above
mentioned ICs and the motors . There are several ways of making it .
Using transistor
Using relays.

3)What is the advantage of DC motor over AC motor ?


Advantages of DC motor
Speed control over a wide range both above and below the rated speed . This can be
achieved in DC shunt motors by methods such as armature control method and field
control method.
High starting torque: It can handle heavy loads . DC series motors will has a starting
torque as high as 500% compared to normal operating torque.
Accurate steep less speed with constant torque.
Quick starting, stopping , reversing and acceleration.
Free from harmonics , reactive power consumption and many factors which makes
DC motors more advantageous compared to AC induction motors.

4) How can be microcontroller be used to control automatically speed of DC motor?


Microcontroller can provide easy control of DC motor. The microcontroller has been
programmed to automatically vary the duty cycle of the chopper depending upon the required
speed of motor . By changing the voltage or current we can also change the speed of DC
motor.

EXPERIMENT -5
AIM: Interface seven segment displays with Port P2. Write program to display number 0 to 9
on the seven segment display at the interval of 1 second.
APPARATUS REQUIRED : 89s5x trainer kit .
CIRCUIT DIAGRAM:

PROGRAM
#include <reg51.h>
#include <stdio.h>
void delay(void);
void main(void)
{
while (1)
{
P1==0x00;
delay();

P1=0x01;
delay();
P1=0x02;
delay();
P1=0x03;
delay();
P1=0x04;
delay();
P1=0x05;
delay();
P1=0x06;
delay();
P1=0x07;
delay();
}
}
void delay(void)
{
TMOD=0x01;
TL0=0x00;
TH0=0xA2;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}

WORKSHEET

1) How many way the seven segment display can interface with 8051?
Two ways are used to interface seven segment with 8051.
Direct
Indirect

The use of decoder reduces the number of pins of 8051 . The conversion from BCD to seven
segment is performed internally in the decoder but in direct connection we have to perform
the conversion manually.
2)What is seven segment display?
A seven segment display is a set of seven bar shaped LED elements arranged to form a
squared off figure below . By activating all or some of the elements , any single digit numeral
from 0 to 9 , as well as most upper case and lower case letters of English alphabets can be
portrayed. They are used in digital clocks , clock radio ,timers , write watches and calculators.

3)Comparison between 7 segment display and LCD.

Most LCD has built in processing power including a character generator , whereas
with 7 segment display , whatever is used to control them supply the processing
power to generate the characters to be displayed.
LCD interface through a 4 bit data bus with 3 wires for control. Whereas seven
segment displays are just LEDs . So we can drive them directly or using a decoder.
Seven segment display have limited text capabilities , they are best suited for
numbers.
The advantages of LED displays is the size of the character and the ability to be seen
in dimlight.

4)Write a program to display number 10 to 20 on the 7 segment display at the interval of 0.5
second.
a) direct interfacing
#include <reg51.h>
#include <stdio.h>
void delay(unsigned char d);
void main(void)
{
while (1)
{
P1==0x10;
delay(50);
P1=0x11;

delay(50);
P1=0x12;
delay(50);
P1=0x13;
delay(50);
P1=0x14;
delay(50);
P1=0x15;
delay(50);
P1=0x16;
delay(50);
P1=0x17;
delay(50);
P1=0x18;
delay(50);
P1=0x19;
delay(50);
P1=0x20;
delay(50);
}
}
void delay(unsigned char d )
{
TMOD=0x01;
TL0=0x00;
TH0=0xA2;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}

You might also like