Microcontroller Lab Manual
Microcontroller Lab Manual
ARTMENT OF ELECTRONICS
AND COMMNICATION
COMMNICATION
MICROCONTROLLER
MICROCONTROLLER LAB
LAB
FOURTH SEMESTER
SUBJECT CODE : 9EC45P
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
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
05
20
30
10
15
05
45
15
25
05
100
Page 2
Page 3
Page 4
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.
Page 5
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
Page 6
OUTPUT:
3. Program to convert two digit packed BCD to unpacked BCD & vice versa.
A]
B]
OUTPUT:
OUTPUT:
Page 7
A]
B]
OUTPUT:
OUTPUT:
Page 8
external RAM.
A]
Page 9
B]
OUTPUT:
OUTPUT:
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.
Page 10
Page 11
B]
OUTPUT:
Page 12
OUTPUT:
OUTPUT:
WATCH WINDOW:
Name : A
Value : 00 - FF
Page 13
OUTPUT:
WATCH WINDOW:
Name : A
Value : FF - 00
C]
Page 14
WATCH WINDOW:
Name : A
Value : 00 - 99
C]
OUTPUT:
WATCH WINDOW:
Name : A
Value : 99 - 00
Page 15
OUTPUT
OUTPUT:
OUTPUT
OUTPUT:
11. Program to clear all the bytes (stored in internal RAM) which have even number
of 1s.
Page 16
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
Page 17
OUTPUT:
Page 18
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.
Page 19
1.
2.
3.
4.
5.
6.
7.
8.
Note : Remove JP1 (Jumper 1) and Reset the Target Board, Before and after Loading
the HEX file.
Page 20
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);
Page 21
Calculation:
To calculate the Output voltage use the following formula:
Dout = ( + + + + + + + )
Where: (Step size or Resolution) =
= 0.019.
Page 22
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
Sine Table =
Page 23
Page 24
#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:
Page 25
#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]
#include <reg51.h>
idata unsigned char count;
void main ()
{
count = 0x0;
while(1)
{
P1 = count;
count++;
}
}
Page 26
Page 27
Page 28
Page 29
delay(void)
int i;
for(i=0;i<=30000;i++);
Page 30
Block Diagram:
Page 31
Program:
#include <reg51.h>
sbit P24= P2^4;
idata unsigned char off_time,on_time;
idata unsigned char i;
Page 32
Page 33
Page 34
Page 35
Page 36
A]
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;
}
Page 37
#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
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++);
}
Page 39
Page 40
Page 41