You are on page 1of 42

DEPARTMENT

ARTMENT OF ELECTRONICS
AND COMMNICATION
COMMNICATION

VET BVL POLYTECHNIC

14th Main, II Phase J.P.Nagar


Bangalore 560 078

MICROCONTROLLER
MICROCONTROLLER LAB
LAB
FOURTH SEMESTER
SUBJECT CODE : 9EC45P

Lab Manual Prepared By:


By
Ms. Pavithra S J
Lecturer
Dept of E&C
VET BVL Polytechnic

Head of the Department:


Ms.Geetha V
Dept of E&C
VET BVL Polytechnic
Polytechn

Microcontroller Lab 9EC45P


SYLLABUS
GRADED EXERCISES:
Section A:
I) This part should be done with a simulator by using an IDE (Integrated Development
Environment) such as the popular KEIL uVision.
II) The Students should be introduced to the options/menu/facilities of the IDE they are
using.
III) The student should write the program using assembly language.

1.
2.
3.
4.
5.
6.

Program to add three 8 bit numbers and to store the 16 bit result in internal RAM
Program to add an array of 8 bit numbers and to store the 16 bit result in internal RAM
Program to convert two digit packed BCD to to unpacked BCD & vice versa
Program to convert two digit packed BCD to to ASCII & vice versa
Program to transfer a block of data from External to internal RAM.
Program to find the largest/smallest number in an array of numbers stored in external
RAM.
7. Program to generate specified time delay with the use of timers.
8. Program to arrange a series of 8 bit numbers in ascending/descending order.
9. Program to illustrate an hex/bcd up/down counter(hex- 00 to FF, BCD 00 to 99)
10. Program to search for a given number in an array of numbers.
11. Program to clear all the bytes(stored in internal RAM) which have even number of 1s.
12. Program to count the number of ones and zeros in two consecutive memory locations.
Section B:
I) This part should be done with the use of standard interface modules available.
II)The student should write the program using C language..
III) A standard IDE like KEIL uVision(evaluation version can also be used with certain
limitations ),HEX file can be generated and has to be used to program the flash single chip
based boards with interface modules for testing.

1. Interfacing of ADC by connecting a variable voltage supply to the i/p of ADC and by
reading the 8 bit output of ADC and display it on 8 LEDs connected to port lines.
2. Interface DAC to generate different waveforms (like sine, square, triangular and
ramp).
3. Generate different tones using DAC (PWM technique)
4. Interface stepper motor and control its speed and direction.
5. Interface traffic light controller module for a junction of four roads.
6. Interface DC motor and control its speed by PWM technique.
Dept Of Electronics & Communication

Page 1

Microcontroller Lab 9EC45P


7. Interface push button switch & seven segment display, count the number (from 0 to 9)
of times the switch is pressed and display it on Seven segment display.
8. Interface LCD module &
(i) To output a message on the display.
(ii) To create a moving display
(iii) To display two messages alternately one on first line and another on 2nd line.
9. Interface DAC and to generate a square wave of programmable frequency(the
frequency should increase/decrease with the pressing of two separate push buttons)
10. Interface DAC and to generate a square wave of programmable amplitude(the
amplitude should increase/decrease with the pressing of two separate push buttons)

Scheme of Valuation
1
2
3

Record
Viva Voce
Section A
Writing one ALP ----------------------Simulation and Result ----------------Print Out -------------------------------Section - B
Writing One C Program --------------Flash Programming & Result -------Print Out --------------------------------Total

Dept Of Electronics & Communication

05
20
30
10
15
05
45
15
25
05
100

Page 2

Microcontroller Lab 9EC45P


Section A:
Procedure for KeiluVision3 :
1. Double click on the uVision3 icon on the Desktop
Deskto

2. Close any previous projects that


were opened using
Project Close Project.
Then Select New Project.
Project

3. Create the New folder with required name,


inside the folder give File Name with required
Project_Name then click on save button.
button

4. Select the CPU from the


device database:
Vendor Atmel
AT89C51ED2 OK
Or
Vendor Philips
P89C61X2 OK

Dept Of Electronics & Communication

Page 3

Microcontroller Lab 9EC45P


5. On the following pop up window
option is displayed.
Choose NO for Assembly Program.
Choose YES for C Program

6. Select New Text File and type


the Assembly or C Program.
Program

7. Save the File


ile Name with .asm
extension for Assembly
Program or .c extension for C
Program.

8. In Project Workspace window maximise


Target1,
Right click on Source Group1 then select
Add Files to Group Source Group1 .

9. Specify the File Type as ASM Source File for


Assembly Program or C Source File for C
Program then click on Add Close.

To Create the HEX file for C Program:


Right Click on Target1 Option for Target1 on the new pop up window:
Under Target Select Xtal as 11.0592.
Under Output enable Create HEX File option Ok.

Dept Of Electronics & Communication

Page 4

Microcontroller Lab 9EC45P


To Debug the Program:
1. Click on Translatee Current File Build Target Rebuild
All Target Files options.
2. At the Output Window check for Errors and Warnings then correct the errors if any
and warnings can be neglected.

3. Click on Start/Stop Debug Session Option, on the pop up window select Ok.
4. Memory window:
To access internal RAM D:zzzzH.
To access internal RAM X:zzzzH.
Where zzzz will be the required address.
5. Watch and call Stack Window:
Enter the Variable Name in the Value field.
6. Spectrum Analyzer Window:
Go to Setup,, on the pop up window Select New enter Port/Pin Address
Enter.
Enable Hexadecimal Display Close.
7. Press F5 for Single Step Execution or F10 for Step Through Execution.

Dept Of Electronics & Communication

Page 5

Microcontroller Lab 9EC45P


1. Program to add three 8 bit numbers and to store the 16 bit result in internal
RAM.
ORG 0000H
MOV R0,#30H
MOV R1,#00H
MOV R2,#03H
CLR A
UP: ADD A,@R0
JNC DOWN
INC R1
DOWN: INC R0
DJNZ R2,UP
MOV 34H,A
MOV 35H,R1
END
INPUT:

OUTPUT:

2. Program to add an array of 8 bit numbers and to store the 16 bit result in
internal RAM.
ORG 0000H
MOV R0,#30H
MOV R1,#00H
MOV R2,#05H
CLR A
UP: ADD A,@R0
JNC DOWN
INC R1
DOWN: INC R0
DJNZ R2,UP
MOV 40H,A
MOV 41H,R1
END

Dept Of Electronics & Communication

Page 6

Microcontroller Lab 9EC45P


INPUT:

OUTPUT:

3. Program to convert two digit packed BCD to unpacked BCD & vice versa.
A]

Packed BCD to Unpacked BCD:


ORG 0000H
MOV A,30H
SWAP A
ANL A,#0FH
MOV 31H,A
MOV A,30H
ANL A,#0FH
MOV 32H,A
END
INPUT:

B]

OUTPUT:

Unpacked BCD to Packed BCD:


ORG 0000H
MOV A,30H
SWAP A
ADD A,31H
MOV 32H,A
END
INPUT:

Dept Of Electronics & Communication

OUTPUT:

Page 7

Microcontroller Lab 9EC45P


4. Program to convert two digit packed BCD to ASCII & vice versa.

A]

Packed BCD to ASCII:


ORG 0000H
MOV A,30H
SWAP A
ANL A,#0FH
ADD A,#30H
MOV 31H,A
MOV A,30H
ANL A,#0FH
ADD A,#30H
MOV 32H,A
END
INPUT:

B]

OUTPUT:

ASCII to Packed BCD:


ORG 0000H
MOV A,30H
ANL A,#0FH
SWAP A
MOV B,31H
ANL B,#0FH
ADD A,B
MOV 32H,A
END
INPUT:

Dept Of Electronics & Communication

OUTPUT:

Page 8

Microcontroller Lab 9EC45P


5. Program to transfer a block of data from External to Internal RAM.

SRC EQU 8000H


DST EQU 30H
ORG 0000H
MOV R1,#05H
MOV DPTR,#SRC
MOV R0,#DST
BACK: MOVX A,@DPTR
MOV @R0,A
INC DPTR
INC R0
DJNZ R1,BACK
OVER: SJMP OVER
END
INPUT (External Memory):

OUTPUT (Internal Memory):

6. Program to find the largest/smallest number in an array of numbers stored in

external RAM.
A]

To find the largest number:


ARRAY EQU 0040H
LARGE EQU 0080H
ORG 0000H
MOV R1,#06H
DEC R1
MOV DPTR,#ARRAY
MOVX A,@DPTR
LOOP: MOV B,A
INC DPTR
MOVX A,@DPTR
CJNE A,B,GO
GO: JNC NEXT
MOV A,B
NEXT: DJNZ R1,LOOP
MOV DPTR,#LARGE
MOVX @DPTR,A
OVER: SJMP OVER
END

Dept Of Electronics & Communication

Page 9

Microcontroller Lab 9EC45P


INPUT:

B]

OUTPUT:

To find the smallest number:


ARRAY EQU 0040H
SMALL EQU 0080H
ORG 0000H
MOV R1,#06H
DEC R1
MOV DPTR,#ARRAY
MOVX A,@DPTR
LOOP: MOV B,A
INC DPTR
MOVX A,@DPTR
CJNE A,B,GO
GO: JC NEXT
MOV A,B
NEXT: DJNZ R1,LOOP
MOV DPTR,#SMALL
MOVX @DPTR,A
OVER: SJMP OVER
END
INPUT:

OUTPUT:

7. Program to generate specified time delay with the use of timers.

While designing delay programs in 8051, calculating the initial value that has to be loaded
into TH and TL registers forms a very important thing.
a.
b.
c.
d.

Assume the processor is clocked by a 12MHz crystal.


That means, the timer clock input will be 12MHz/12 = 1MHz.
That means, the time taken for the timer to make one increment = 1/1MHz = 1S.
For a time delay of X S the timer has to make X increments.

Dept Of Electronics & Communication

Page 10

Microcontroller Lab 9EC45P


e. 2^16 = 65536 is the maximum number of counts possible for a 16 bit timer.
f. Let TH be the value that has to be loaded to TH register and TL be the value that has
to be loaded to TL register.
g. Then, THTL = Hexadecimal equivalent of (65536-X) where (65536-X) is considered
in decimal.
Example:

Let the required delay be 1000S (ie; 1mS).


That means X = 1000
65536 X ; 65536 1000 = 64536.
64536 is considered in decimal and converting it to hexadecimal gives FC18
That means THTL = FC18
Therefore TH=FC and TL=18
Program:
ORG 0000H
GO: CLR P1.1
ACALL DELAY
SETB P1.1
ACALL DELAY
SJMP GO
DELAY: MOV TCON,#00H
MOV TMOD,#21H
MOV TH0,#0FCH
MOV TL0,#18H
SETB TR0
LOOP: JNB TF0,LOOP
CLR TF0
CLR TR0
RET
END
OUTPUT:

Dept Of Electronics & Communication

Page 11

Microcontroller Lab 9EC45P


8. Program to arrange a series of 8 bit numbers in ascending/descending order.
A]

Program to arrange in ascending order.


ORG 0000H
MOV R1,#08H
DEC R1
UP: MOV A,R1
MOV R2,A
MOV R0,#30H
ABOVE: MOV A,@R0
INC R0
MOV B,@R0
CJNE A,B,NEXT
SJMP BELOW
NEXT: JC BELOW
MOV @R0,A
DEC R0
MOV @R0,B
INC R0
BELOW: DJNZ R2,ABOVE
DJNZ R1,UP
END
INPUT:

B]

OUTPUT:

Program to arrange in descending order.


ORG 0000H
MOV R1,#08H
DEC R1
UP: MOV A,R1
MOV R2,A
MOV R0,#30H
ABOVE: MOV A,@R0
INC R0
MOV B,@R0
CJNE A,B,NEXT
SJMP BELOW
NEXT: JNC BELOW
MOV @R0,A
DEC R0

Dept Of Electronics & Communication

Page 12

Microcontroller Lab 9EC45P


MOV @R0,B
INC R0
BELOW: DJNZ R2,ABOVE
DJNZ R1,UP
END
INPUT:

OUTPUT:

9. Program to illustrate an HEX/BCD Up/Down counter (HEX 00 to FF, BCD 00 to


99)
A]

Program to illustrate an HEX Up counter (00 to FF):


ORG 0000H
MOV A,#00H
BACK: ACALL DELAY
INC A
JNZ BACK
HERE: SJMP HERE
DELAY: MOV R1,#99H
D1: MOV R2,#99H
D2: MOV R3,#99H
D3: DJNZ R3,D3
DJNZ R2,D2
DJNZ R1,D1
RET
END

OUTPUT:

WATCH WINDOW:
Name : A
Value : 00 - FF

Dept Of Electronics & Communication

Page 13

Microcontroller Lab 9EC45P


B]

Program to illustrate an HEX Down counter (FF to 00):


ORG 0000H
MOV A,#FFH
BACK: ACALL DELAY
DEC A
JNZ BACK
HERE: SJMP HERE
DELAY: MOV R1,#99H
D1: MOV R2,#99H
D2: MOV R3,#99H
D3: DJNZ R3,D3
DJNZ R2,D2
DJNZ R1,D1
RET
END

OUTPUT:

WATCH WINDOW:
Name : A
Value : FF - 00

C]

Program to illustrate an BCD Up counter (00 to 99):


ORG 0000H
MOV A,#00H
BACK: ACALL DELAY
ADD A,#01H
DA A
JNZ BACK
HERE: SJMP HERE
DELAY: MOV R1,#99H
D1: MOV R2,#99H
D2: MOV R3,#99H
D3: DJNZ R3,D3
DJNZ R2,D2
DJNZ R1,D1
RET
END

Dept Of Electronics & Communication

Page 14

Microcontroller Lab 9EC45P


OUTPUT:

WATCH WINDOW:
Name : A
Value : 00 - 99

C]

Program to illustrate an BCD Down counter (99 to 00):


ORG 0000H
MOV A,#00H
BACK: ACALL DELAY
ADD A,#99H
DA A
JNZ BACK
HERE: SJMP HERE
DELAY: MOV R1,#99H
D1: MOV R2,#99H
D2: MOV R3,#99H
D3: DJNZ R3,D3
DJNZ R2,D2
DJNZ R1,D1
RET
END

OUTPUT:

WATCH WINDOW:
Name : A
Value : 99 - 00

Dept Of Electronics & Communication

Page 15

Microcontroller Lab 9EC45P


10. Program to search for a given number in an array of numbers.
ORG 0000H
MOV R0,#30H
MOV R1,#05H
MOV A,#65H
UP: MOV B,@R0
CJNE A,B,DOWN
MOV A,#0EEH
MOV 50H,A
SJMP BELOW
DOWN: INC R0
DJNZ R5,UP
MOV A,#0FFH
MOV 40H,A
BELOW: SJMP BELOW
END
Trial 1:
INPUT:

OUTPUT
OUTPUT:

FF The Number is not present in the given array.


Trial 2:
INPUT:

OUTPUT
OUTPUT:

EE The Number is present in the given array.

11. Program to clear all the bytes (stored in internal RAM) which have even number
of 1s.

Dept Of Electronics & Communication

Page 16

Microcontroller Lab 9EC45P

ORG 0000H
MOV R0,#0H
MOV R1,#10H
UP: MOV A,@R0
JB PSW.0,DOWN
CLR A
MOV @R0,A
DOWN: INC R0
DJNZ R1,UP
END
INPUT:

OUTPUT:

12. Program to count the number of ones and zeros in two consecutive memory

locations.
ORG 0000H
MOV R0,#30H
MOV R1,#00H
MOV R2,#00H
MOV R3,#02H
UP: MOV R4,#08H
MOV A,@R0
GO: RRC A
JNC ZERO
INC R1
SJMP STOP
ZERO: INC R2
STOP: DJNZ R4,GO
INC R0
DJNZ R3,UP
MOV 40H,R1
MOV 41H,R2
END

Dept Of Electronics & Communication

Page 17

Microcontroller Lab 9EC45P


INPUT:

Dept Of Electronics & Communication

OUTPUT:

Page 18

Microcontroller Lab 9EC45P


Section B:
Procedure to load the HEX file:
Flash Magic for P89C61X2:
Step-1 Communication:
1. Select your target device.
device (P89C61X2)
2. Select your com port (Usually COM 1) make sure
that you willl select proper com port otherwise
other
you
cannot communicate.
3. Now select baud rate ideally it should be 9600
(recommended). Avoid higher than 9600 for proper
communication.
4. Now select your interface if you are using DB-9
DB then
it will be None (ISP).
Step-2 Erase:
Now here tick mark the Erase all Flash option.

Step-3 Hex file:


You need to set up a path of your Hex file which
is to be loaded on chip.
Step-4 Options:
In this always keep Verify after programming option
enable by tick mark. You can use another features as
well according to your need.

Step-5 Start:
Now you are all set to burn your code memory just click on start
but and it will start to load hex code in your chip. You can see the
process at the bottom.

Dept Of Electronics & Communication

Page 19

Microcontroller Lab 9EC45P


Atmel Flip for AT89C51ED2:

1.
2.
3.
4.
5.
6.
7.
8.

Start Atmel FLIP


Under the Device menu, Select AT89C51ED2.
Under the Communication menu, Select RS232 COM1 9600.
Click Open to establish communication with your board.
From the File menu select Load HEX File...
Find and Open the HEX file you want to upload
Select the Erase, Program, and Verify options and click Run
If successful all the tasks should have a green dot next to them.

Note : Remove JP1 (Jumper 1) and Reset the Target Board, Before and after Loading
the HEX file.

Dept Of Electronics & Communication

Page 20

Microcontroller Lab 9EC45P


1. Interfacing of ADC by connecting a variable voltage supply to the i/p of ADC
and by reading the 8 bit output of ADC and display it on 8 LEDs connected to
port lines.
Block Diagram:

Interface used: NIFC-07B


Target board : AT89C51ED2
1) Give external voltage to cahnnel - 0 and observe digital value on 8 leds provided on
interface board.
2) Output of ADC is connected to 8 LEDs which are connected to port P1
3) Port P0 is used to read converted digital values
4) P2.6 is used for OE line and
5) P2.7 is used for Start
6) P0.7 is used to monitor EOC

Program:
#include <reg51.h>
#include "intrins.h"
sbit OE = P2^6;
sbit START_ALE = P2^7;
unsigned char vtemp1 ,ch_sel,adc_value ;
void delay(unsigned int r);

Dept Of Electronics & Communication

Page 21

Microcontroller Lab 9EC45P


void main ()
{
P0 = 0xff;
delay(200);
ch_sel = 0x0;
while(1)
{
P2 = ch_sel;
OE=1;
START_ALE=1;
_nop_;
_nop_;
_nop_;
_nop_;
_nop_;
START_ALE = 0;
OE=0;
do
{
vtemp1=P0;
vtemp1=vtemp1 & 0x80;
}
while(vtemp1 != 0x80);
OE=1;
adc_value = P0 ;
delay(100);
P1= adc_value;
}
}
void delay(unsigned int r)
{
unsigned int r1;
for(r1=0;r1< r;r1++);
}

Calculation:
To calculate the Output voltage use the following formula:
Dout = ( + + + + + + + )
Where: (Step size or Resolution) =

Dept Of Electronics & Communication

= 0.019.

Page 22

Microcontroller Lab 9EC45P


2. Interface DAC to generate different waveforms
Block Diagram:

Interface used: NIFC 27


Target board : P89C61X2
A]

Generation of Sine Wave:

Sine Table:
Degree
0
7.5
15
22.5
30
37.5
45
52.5
60
67.5
75
82.5
90

Sine
0
0.13053
0.25882
0.3827
0.5
0.6087
0.7071
0.7934
0.866
0.9239
0.9659
0.9914
1

Decimal
(Sine*128)
0
16.7
33.13
49
64
77.9
90.5
101.5
110.8
118.25
123.63
126.8
128

DAC
value
in hex
0
10
21
31
40
4d
5a
65
6e
76
7b
7e
7f

+80h

80h

80
90
a1
b1
c0
cd
da
e5
ee
f6
fb
fe
ff

80
70
5f
4f
40
33
26
1b
12
0a
5
2
0

Formula to Generate the Sine Table:

Sine Table =

Where: Vout = 5 + 5 Sin


Resolution = 39mV

Dept Of Electronics & Communication

Page 23

Microcontroller Lab 9EC45P


Program:
#include <reg51.h>
xdata unsigned char sine_tab[49]=
{ 0x80,0x90,0xA1,0xB1,0xC0,0xCD,0xDA,0xE5,0xEE,0xF6,0xFB,0xFE,
0xFF,0xFE,0xFB,0xF6,0xEE,0xE5,0xDA,0xCD,0xC0,0xB1,0xA1,0x90,
0x80,0x70,0x5F,0x4F,0x40,0x33,0x26,0x1B,0x12,0x0A,0x05,0x02,
0x00,0x02,0x05,0x0A,0x12,0x1B,0x26,0x33,0x40,0x4F,0x5F,0x70,0x80};
idata int count;
void main ()
{
while(1)
{
For (count=0;count<49;count++)
{
P1 = sine_tab[count];
}
}
}
OUTPUT:

Dept Of Electronics & Communication

Page 24

Microcontroller Lab 9EC45P


B]

Generation of Square Wave:

#include <reg51.h>
void delay(void);
void main ()
{
while(1)
{
P1 = 0x0;
delay();
P1 = 0xff;
delay();
}
}
void delay(void)
{
int i;
for(i=0;i<=300;i++);
}
OUTPUT:

Dept Of Electronics & Communication

Page 25

Microcontroller Lab 9EC45P


C]

Generation of Triangle Wave:

#include <reg51.h>
idata unsigned char count;
void main ()
{
while(1)
{
for(count=0;count!=0xff;count++)
{
P1=count;
}
for(count=0xff; count>0;count--)
{
P1=count;
}
}
OUTPUT:

D]

Generation of Ramp Wave:

#include <reg51.h>
idata unsigned char count;
void main ()
{
count = 0x0;
while(1)
{
P1 = count;
count++;
}
}

Dept Of Electronics & Communication

Page 26

Microcontroller Lab 9EC45P


OUTPUT:

3. Generate different tones using DAC(PWM technique)


Block Diagram

Interface used: NIFC 42


Target board : P89C61X2
Program:
#include "REG51.H"
void tmr_isr(void);
void delay_2(void) ;
void delay_3(void) ;
idata unsigned char row, t;
sbit P20=P2^0;
sbit P21=P2^1;
idata unsigned char vall, valh, count, temp;
idata unsigned char i, j, k;
Dept Of Electronics & Communication

Page 27

Microcontroller Lab 9EC45P


unsigned char MUSIC_DATA[32]=
{0X29,0XF9,0xE1,0XF9,0X8A,0XFA,0XD8,0XFA,
0X68,0XFB,0XE9,0XFB,0X59,0XFC,0X8F,0XFC,
0X48,0XFE,0X2D,0XFE,0XF5,0XFD,0XB5,0XFD,
0X6D,0XFD,0X45,0XFD,0XF0,0XFC,0X90,0XFC};
void tmr_isr (void) interrupt 1
{
TR0 = 0;
TF0=0;
TCON = 0;
temp=P2;
temp = temp ^0x01;
P2=temp;
TL0 = vall;
TH0 = valh;
TR0 = 1;
}
void main ()
{
TMOD = 0x01;
TCON = 0;
ET0 = 1;
EA = 1;
P20=1;
while(1)
{
for(count=0;count<32;count+=2)
{
delay_3();
vall=MUSIC_DATA[count];
valh=MUSIC_DATA[count+1];
TL0 =vall;
TH0= valh;
TR0 = 1;
P21=1;
delay_2();
P21=0;
TR0 = 0;
}
}
}

Dept Of Electronics & Communication

Page 28

Microcontroller Lab 9EC45P


void delay_3(void)
{
for(i=0;i<2;i++)
for(j=0;j<0x7f;j++)
for(k=0;k<0x7f;k++);
}
void delay_2(void)
{
for(i=0;i<5;i++)
for(j=0;j<0x7f;j++)
for(k=0;k<0x7f;k++);
}

4. Interface stepper motor and control its speed and direction.


Block Diagram:

Interface used: NIFC 01A


Target board : P89C61X2

Dept Of Electronics & Communication

Page 29

Microcontroller Lab 9EC45P


Program:
#include <reg51.h>
void delay(void);
sbit sw = P2^7;
void main(void)
{
sw = 1;
while(1)
{
if (sw == 0)
{
P2=0x0e;
delay();
P2=0x0d;
delay();
P2=0x0b;
delay();
P2=0x07;
delay();
}
else
{
P2=0x07;
delay();
P2=0x0b;
delay();
P2=0x0d;
delay();
P2=0x0e;
delay();
}
}
}
void
{

delay(void)
int i;
for(i=0;i<=30000;i++);

Dept Of Electronics & Communication

Page 30

Microcontroller Lab 9EC45P


5.

Interface traffic light controller module for a junction of four roads.

Block Diagram:

Interface used: NIFC 11


Target board : P89C61X2
Program:
#include " reg51.h"
void delay(void);
void main()
{
P2=0xf3;
P0=0xff;
P1=0xff;
delay();
while(1)
{
P0=0xee;
P1=0xee;
delay();
P2=0xfc;
P0=0x7d;
P1=0x57;
delay();
P1=0xe7;
P0=0xfd;
P0=0xed;
delay();
P1=0xf7;
P2=0xf0;
P0=0xf1;
delay();
P0=0xfb;
P1=0xfb;
P2=0x50;
delay();
P0=0xfe;
P1=0xfe;
delay();

Dept Of Electronics & Communication

Page 31

Microcontroller Lab 9EC45P


P0=0xff;
P2=0xaf;
P0=0xef;
P1=0xef;
delay();
P0=0xbf;
P1=0xbf;
delay();
}
}
void delay(void)
{
int i;
for(i=0;i<=30000;i++);
for(i=0;i<=30000;i++);
for(i=0;i<=30000;i++);
for(i=0;i<=30000;i++);
for(i=0;i<=30000;i++);
for(i=0;i<=30000;i++);
for(i=0;i<=30000;i++);
for(i=0;i<=30000;i++);
for(i=0;i<=30000;i++);
for(i=0;i<=30000;i++);
}

6. Interface DC motor and control its speed by PWM technique.


Interface used: NIFC 53 / 55
Target board : P89C61X2
Program to test DC motor by varying off-time and on-time with different values user can
observe different speed on DC motor typically the
off values are 30,24,18 & 12
on values are 10,16,12 & 28
In this source off value is taken as 30 and on value is taken as 10.

Program:
#include <reg51.h>
sbit P24= P2^4;
idata unsigned char off_time,on_time;
idata unsigned char i;

Dept Of Electronics & Communication

Page 32

Microcontroller Lab 9EC45P


void main ()
{
TMOD |= 0x21;
TCON = 0;
TH1 = 0xfd;
SCON = 0x50;
TR1 = 1;
EA = 1;
ISDwait();
TCON = 0;
TMOD = 0x21;
off_time =30;
on_time = 10;
while(1)
{
P24 = 1;
for(i=0;i<on_time;i++)
{
TL0 = 0x66;
TH0 = 0xFC;
TR0 =1;
while(!TF0)
{
}
TF0 = 0;
TR0=0;
}
P24 = 0;
for(i=0;i<off_time;i++)
{
TL0 = 0x66;
TH0 = 0xFC;
TR0 =1;
while(!TF0)
{
}
TF0 = 0;
TR0=0;
}
}
}

Dept Of Electronics & Communication

Page 33

Microcontroller Lab 9EC45P


7. Interface push button switch & seven
sev segment
ment display, count the number (from
(
0
to 9) of times the switch is pressed and display it on Seven segment display.
Block Diagram:

Target board : P89C61X2


Display Table:

Dept Of Electronics & Communication

Page 34

Microcontroller Lab 9EC45P


Program:
#include <reg51.h>
sbit sw = P1^7;
void delay();
unsigned char LED_Code[10]= {0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
void main ()
{
unsigned char num;
sw = 1;
while(1)
{
num = 0;
while (num<10)
{
P0 = LED_Code[num];
delay();
if (sw !=1 )
{
num++;
}
}
}
}
void delay()
{
unsigned int i;
for(i=0;i<25000;i++);
for(i=0;i<25000;i++);
}

Dept Of Electronics & Communication

Page 35

Microcontroller Lab 9EC45P


8. Interface LCD module.
Block Diagram:

Target board : AT89C61ED2


162 LCD Commands:
162 LCD module has a set of preset command instructions. Each command will make the
module to do a particular task. The commonly used commands and their function are given in
the table below.
LCD Command Function:
0F
01
2
4
06
E
80
C0
38
83
3C
0C3
OC1

LCD ON, Cursor ON, Cursor blinking ON


Clear screen
Return home
Decrement cursor
Increment cursor
Display ON, Cursor ON
Force cursor to the beginning of 1st line
Force cursor to the beginning of 2nd line
Use 2 lines and 57 matrix
Cursor line 1 position 3
Activate second line
Jump to second line, position3
Jump to second line, position1

Dept Of Electronics & Communication

Page 36

Microcontroller Lab 9EC45P


LCD initialization:
The steps that have to be done for initializing the LCD display is given below and these steps
are common for almost all applications.
Send 38H to the 8 bit data line for initialization
Send 0FH for making LCD ON, cursor ON and cursor blinking ON.
Send 06H for incrementing cursor position.
Send 01H for clearing the display and return the cursor.
Sending data to the LCD:
The steps for sending data to the LCD module is given below. The LCD module has pins
namely RS, R/W and E. It is the logic state of these pins that make the module to determine
whether a given data input is a command or data to be displayed.
Make R/W low.
Make RS=0 if data byte is a command and make RS=1 if the data byte is a data to be
displayed.
Place data byte on the data register.
Pulse E from high to low.
Repeat above steps for sending another data.

A]

To output a message on the display.

Program:
#include <at89c51xd2.h>
void lcd_init(void);
void clear_lcd();
void maxdelay(void);
void lcd_outstr(unsigned char *str);
unsigned char xdata msg[]={"VET BANGALORE"};
unsigned char xdata *cptr;
idata unsigned char count;
void main()
{
lcd_init();
cptr = msg;
clear_lcd();
lcd_outstr(cptr);
maxdelay;
here: goto here;
}

Dept Of Electronics & Communication

Page 37

Microcontroller Lab 9EC45P


B]

To create a moving display:

#include <at89c51xd2.h>
void lcd_init(void);
void lcd_comm(unsigned char);
void clear_lcd();
void maxdelay(void);
void lcd_outstr(unsigned char *str);
void delay(void);
unsigned char xdata msg1[]={"MICROCONTROLLER LAB WELCOMES YOU"};
unsigned char xdata disp_msg[16];
unsigned char xdata *cptr;
idata unsigned int count;
idata unsigned char j, k;
void main()
{
clear_lcd();
lcd_init();
while(1)
{
delay();
lcd_comm(0x01);
for(k=0;k<32;k++)
{
lcd_comm(0x80);
for(j=0;j<16;j++)
disp_msg[j] = msg1[k+j];
disp_msg[16] = 0;
cptr = disp_msg;
lcd_outstr(cptr);
maxdelay();
maxdelay();
maxdelay();
delay();
maxdelay();
delay();
}
}
}
void delay(void)
{
for (count=0;count<5000;count++);
for (count=0;count<5000;count++);
for (count=0;count<5000;count++);
for (count=0;count<5000;count++);
}
Dept Of Electronics & Communication

Page 38

Microcontroller Lab 9EC45P


C]

To display two messages alternately one on first line and another on 2nd line.

Program:
#include <at89c51xd2.h>
void lcd_init(void);
void lcd_comm(unsigned char);
void clear_lcd();
void maxdelay(void);
void lcd_outstr(unsigned char *str);
void delay(void);
unsigned char xdata msg1[]={"LET GOOD LUCK BE"};
unsigned char xdata msg2[]={"ALWAYS WITH YOU"};
unsigned char xdata *cptr;
idata unsigned int count;
void main()
{
lcd_init();
while(1)
{
delay();
cptr = msg1;
clear_lcd();
lcd_outstr(cptr);
maxdelay();
delay();
clear_lcd();
lcd_comm(0xc0);
cptr = msg2;
lcd_outstr(cptr);
maxdelay();
delay();
}
}
void delay(void)
{
for (count=0;count<5000;count++);
for (count=0;count<5000;count++);
for (count=0;count<5000;count++);
for (count=0;count<5000;count++);
for (count=0;count<5000;count++);
for (count=0;count<5000;count++);
}

Dept Of Electronics & Communication

Page 39

Microcontroller Lab 9EC45P


9. Interface DAC and to generate a square wave of programmable frequency (The
frequency should increase/decrease with the pressing of two separate push
buttons)
Program:
#include <REG51.H>
sbit inc = P1^3;
sbit dec = P1^4;
void delay(unsigned int x)
{
for(;x>0;x--);
}
void main()
{
unsigned int freq = 100;
unsigned char on = 0x7f, off=0x00;
while (1)
{
if (!dec)
{
while (!dec);
if (freq>50)
freq -= 50;
}
if(!inc)
{
while(!inc);
if (freq<0xf0)
freq += 50;
}
P0=on;
delay(freq);
P0 = off;
delay(freq);
}
}

Dept Of Electronics & Communication

Page 40

Microcontroller Lab 9EC45P


10. Interface DAC and to generate a square wave of programmable amplitude (The
amplitude should increase/decrease with the pressing of two separate push
buttons)
Program:
#include <REG51.H>
sbit inr = P1^3;
sbit dcr = P1^4;
main()
{
unsigned char i = 0x80;
P0 = 0x7f;
while (1)
{
if (!inr)
{
while (!inr);
if (i>30)
i = i-30;
}
if(!dcr)
{
while(!dcr);
if (i<0xf0)
i = i+30;
}
P0=i;
}
}

Dept Of Electronics & Communication

Page 41