Professional Documents
Culture Documents
M I C RO C O N T R O L L E R L A B . ( 1 5 E E L 5 7 )
S y l l a b u s : [ U s e o f 8 0 5 1 M i c r o c o n t r o l l e r a n d i t s a d va n c e d ve r s i o n AT 8 9 S 5 2 ]
A. Use of Simulation Software for writing programs . .
1. D a t a Tr a n s f e r – W r i t i n g A L P s f o r :
Block data move ment , bl ock data e xc h a n g e , sorting of data, finding
l a r g e s t / s m a l l e s t e l e m e n t i n a g i ve n a r r a y e t c , e t c , .
3. Wr i t i n g A L P s f o r :
Counters: Binary Counters, Decimal Counters etc.,
4. Wr i t i n g A L P s f o r :
Boolean and Logical Instructions (bit manipulations).
5. Wr i t i n g A L P s f o r :
Understanding conditional CALL and RETURN instructions.
6. Wr i t i n g A L P s f o r :
Unde rsta ndi ng Code conver si on program s – BC D to ASC II, AS CII to BCD, AS CII to
decimal, Dec imal to ASCII, Hexa to dec imal and Decimal to Hexa
7. Wr i t i n g A L P s f o r :
Generation of time-delay (delay programs), Generation of time delay(use of
on chip timer/counter), serial port communication.
8. Wr i t i n g ‘ c ’ p r o g r a m f o r : S t e p p e r m o t o r s p e e d a n d d i r e c t i o n c o n t r o l .
9. Wr i t i n g ‘ c ’ p r o g r a m f o r : D C M o t o r s p e e d a n d d i r e c t i o n c o n t r o l ( P W M o d u l a t i o n ) .
1 1 . W r i t i n g ‘ c ’ p r o g r a m f o r : G e n . o f Wa v e f o r m s ( S i n e , T r i a n g u l a r, R a m p ) u s i n g DA C .
1 2 . W r i t i n g ‘ c ’ p r o g r a m f o r : E x t e r n a l A D C a n d Te m p e r a t u r e c o n t r o l .
**************
INTRODUCTION
MCS 8051 is an 8-bit single chip microcontroller with many built-in functions and
is the core for all MCS-51 devices.
P1.0 1 40 Vcc
P1.1 2 39 P0.0(AD0
P1.2 3 38 )P0.1(AD1)
P1.3 4 37 P0.2(AD2
P1.4 5 8051 36 )P0.3(AD3)
P1.5 6 35 P0.4(AD4)
P1.6 7
(8031) 34 P0.5(AD5)
P1.7 8 33 P0.6(AD6)
RST 9 32 P0.7(AD7)
(RXD)P3.0 10 31 EA/VPP
(TXD)P3.1 11 30 ALE/PROG
(INT0)P3.2 12 29 PSEN
(INT1)P3.3 13 28 P2.7(A15)
(T0)P3.4 14 27 P2.6(A14
(T1)P3.5 15 26 )P2.5(A13
(WR)P3.6 16 25 )P2.4(A12
(RD)P3.7 17 24 )P2.3(A11)
XTAL2 18 23 P2.2(A10)
XTAL1 19 22 P2.1(A9)
GND 20 21 P2.0(A8)
SAMPLE PROGRAMS:
Example: 01
org 0000h
mov r0,#88h ; R0 = ;Memory Location(D:0x00)=
mov a,#24h ; A = ;Memory Location(D:0xE0)=
mov a,r0 ; Check the change in the content of A and R0 if any
end
Example: 02
org 0000h
mov a,#88h ; (A)= and (R0)=
mov r0,#24h
mov 24h,#20h ; contents of RAM location 24h=
mov a,@r0 ; Check the change in the content of A and R0 if any
end
Example: 03
org 0000h
mov a,#40h ; (A)=
mov DPTR,#34h ; (DPTR)=
movx @DPTR, a ; Comments on this instruction
mov a,#00h
movx a,@DPTR
end
Example: 04
org 0000h
mov DPTR,#1234h ; (DPTR)=
mov a,#00h ; (a)=
movc a,@a+DPTR ; Comment on this instruction and check out the
end ; respective register and memory location content
Addition operation:
Example 5
org 0000h
mov r1,#27h ; (r1)=
mov a,#45h ; (a) =
add a,#34h ; (a) =
add a,r1 ; (a) = (r1)=
add a,01h ; (a) = (01)=
add a,@r1 ; Comment on this instruction
addc a,#34h ; (PSW) = (a)= (r1)=
addc a,r1 ; (PSW) = (a)= (r1)=
addc a,01h ; (PSW) = (a)= (r1)=
addc a,@r1 ; Comment on this instruction
end
Example 06
org 0000h
mov r1,#27h ; (r1)=
mov a,#45h ; (a) =
subb a,#34h ; (a) = (psw)
subb a,r1 ; (a) = (r1)=
subb a,01h ; (a) = (psw)=
subb a,@r1 ; Comment on this instruction and check the
end ; content of the required registers
Example 8
org 0000h
mov b,#27h ; (b)=
mov a,#45h ; (a) =
div ab ; (a) = (b)=
end
Rotate Operation:
Example 9
org 0000h
mov a,#66h ; (a)=
clr c
rl a ; (a)= flag C=
rlc a ; (a)= flag C=
rr a ; (a)= flag C=
rrc a ; (a)= flag C=
end
Boolean Operations
Example 10
AND Operations
org 0000h
mov a,#23h ; (a)=
mov r1,#45h ; (r1)=
anl a,#56h ; (a)=
anl a,r1 ; (a)= (r1)=
anl a,01h ; Comment on this instruction and check out of the
end ; respective registers
Example 11
OR Operations
org 0000h
mov a,#23h ; (a)=
mov r1,#41h ; (r1)=
orl a,#56h ; (a)=
orl a,r1 ; (a)= (r1)=
orl a,@r1 ; Comments on this instruction
orl a,01h ; (a)= (r1)=
end ;
Example 12
Ex-OR Operations
org 0000h
mov a,#23h ; (a)=
mov r1,#45h ; (r1)=
xrl a,#56h ; (a)=
xrl a,r1 ; (a)= (r1)=
xrl a,@r1 ; (a)= Comments on this instruction, check the registers
xrl a,01h ; (a)= (r1)=
end ;
Department of Electrical & Electronics Engineering
6
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual
Example 13
Compliment Instruction
org 0000h
mov a,#23h ; (a)=
cpl a ; (a)=
end
Example 14
Clear Instruction
org 0000h
mov a,#34h ; (a)=
clr a ; (a)=
end
Example 15
Bit level Operations
org 0000h
mov 0d0h,#0ffh ; (0d0h)=
anl c,08h ; c flag= (080)=
anl c,/08h ; c flag= (080)=
orl c,08h ; c flag= (080)=
orl c,/08h ; c flag= (080)=
cpl c ; c flag=
cpl 08h ; (o8oh)=
clr c ; c flag =
clr 08h ; (080h)=
clr acc.7 ; (a)=
setb c ; c flag =
setb 08h ; (08h)=
mov c,08h ; c flag = (080)=
mov 08h,c ; c flag = (080)=
end.
Example 16
Swap Operations
org 0000h
mov a,#23h
swap a
end
Example 17
Exchange Operations
org 0000h
mov a,#23h
mov r1,#45h
xch a,@r1
xch a,01h
xch a,@r1
end
Example 18
Increment & Decrement Operations
org 0000h
mov a,#45h ; (a)
mov r1,#27h ; (r1)
dec r1 ; (r1)
dec a ; (a)
dec@r1
inc r1 ; (r1)=
inc a ; (a)=
inc@r1 ;
inc dptr ; (dptr)
end
Example 19
Decimal Adjustment Operations
org 0000h
mov b,#27h ; (b)
mov a,#45h ; (a)
add ab ; (a)
da a ; (a)
end
Example 20
Call Operation
main: mov 81h,#30h ; (81h)=
Lcall sub ; (pc)=
Nop ; comment on this
sub: mov a,#45h ; (a) =
Ret ; (pc) =
end
Example 21
Jump Operations
org 0000h
mov a,#55h ; (a)=
mov r1,#0ffh ; (r1)=
add a,r1 ; (a)= (r1)=
jc loop ;
Main: jnc loop1 ;
jbc 08h,loop ;
Example 21
Delay Program:
org 0000h
start: mov a,#00h
main: mov P0,a
acall delay
inc a
jnz main
sjmp start
delay: mov r0,#0fh
d0: mov r1,#0ffh
d1: mov r2,#0ffh
d2: djnz r2,d2
djnz r1,d1
djnz r0,d0
ret
1 . D a t a Tr a n s f e r – Wr i t i n g A L P s f o r :
B l o c k d a t a m o v e m e n t , b l o c k d a t a e xc h a n g e , s o r t i n g o f d a t a , f i n d i n g
l a r g e s t / s m a l l e s t e l e m e n t i n a g i ve n a r r a y e t c ,
org 0000 ; Defining the code location to start from location 0000
mov r0,#0x30 ; Initializing r0 as a memory pointer to location X
mov r1,#0x40 ; Initializing r1 as a memory pointer to location Y
mov r5,#0x05 ; Number of elements in the array is defined as a count
bck: mov a,@r0 ; Reads a data from location X
mov @r1,a ; Transfers data to location Y
inc r0 ; Points to next location in X
inc r1 ; Points to next location Y
djnz r5,bck ; Repeats data transfer until all the data are transferred
exit: sjmp exit ; Specifies logical end of the program
end ; Specifies physical end of the program
32 - 03 42 – 00 32 - 03 42 - 03
33 - 04 43 – 00 33 - 04 43 - 04
34 - 05 44 – 00 34 - 05 44 - 05
Note: It is assumed that location X starts from RAM location 30H & location Y
starts from 40H
org 0000 ; Defining the code location to start from location 0000
mov r0,#0x30 ; Initializing r0 as a memory pointer to location X
mov r1,#0x40 ; Initializing r1 as a memory pointer to location Y
mov r2,#0x05 ; Number of elements in the array is defined as a count
bck: mov a,@r0 ; Reads a data from location X
mov r3,a ; Copies data to a temporary register
mov a,@r1 ; Reads a data from location Y
mov @r0,a ; Transfers data to location X
mov a,r3
mov @r1,a ; Transfers data to location Y
inc r0 ; Points to next location in X
inc r1 ; Points to next location Y
djnz r2,bck ; Repeats data transfer until all the data are transferred
exit: sjmp exit ; Specifies logical end of the program
end ; Specifies physical end of the program
34- 05 44 – 0E 34 – 0E 44 - 05
c. ALP to perform ascending order sort operation using "Bubble Sort" Technique
org 0000
mov r0,#0x30 ; initializing array to point location specified
mov 01,r0 ; copying array location address.
mov r2,#0x05 ; initializing number of elements in the array 'n'
dec r2 ; performs n-1 operation
bck2: mov 03,r2 ; copying count value for number of pass & comparison value
mov a,@r0
bck1: mov r6,a ; reads first element in the array
inc r0 ; points to next location in the array
mov a,@r0
mov r7,a ; reads second element in the array
mov a,r6
djnz r3,bck1 ; decrements the number of comparison count value & checks
for zero
mov r0,01 ; if yes, initialize array to starting addr. to perform next pass
djnz r2,bck2 ; decrement number of pass count value & checks for zero
exit: sjmp exit ; if yes exits the program
end
; This program sorts the entered data elements in the array in ascending order using
"bubble sort" Technique
30h - 0x05
31h - 0x04
32h - 0x03
33h - 0x02
34h - 0x01
After Execution
30h - 0x01
31h - 0x02
32h - 0x03
33h - 0x04
34h - 0x05
1. First element in the array is read and compared with the second element
2. If first element < second element
yes: make second element as first element & go to next step
No: exchange first & second element
3. Continue this process until all the elements in the array are compared or until the end
of current pass
4. Decrement the comparison pointer value & check for end of comparison
If end of comparison is reached
yes: go to step 5
no: go to step 2
5. Decrement the number of pass value & check for end of pass
If end of pass value is reached
yes: exit the program
no: initialize sort for starting address of array for next pass
org 0000
end
; This program sorts the entered data elements in the array in descending order using
"bubble sort" mechanism
30h - 0x01
31h - 0x02
32h - 0x03
33h - 0x04
34h - 0x05
After Execution
30h - 0x05
31h - 0x04
32h - 0x03
33h - 0x02
34h - 0x01
1. First element in the array is read and compared with the second element
2. If first element > second element
yes: make second element as first element & go to next step
No: exchange first & second element
3. Continue this process until all the elements in the array are compared or until the end
of current pass
4. Decrement the comparison pointer value & check for end of comparison
If end of comparison is reached
yes: go to step 5
no: go to step 2
5. Decrement the number of pass value & check for end of pass
If end of pass value is reached
yes: exit the program
no: Initialize sort for starting address of array for next pass
; In 8051 we don’t have simple compare instruction, at first the numbers are checked for
equality if both the elements in the array are equal no task is performed.
; First element is copied to R3 & second element to R4, initially the value in R3 is
assumed largest and it is compared with next number in R4
; If R3 > R4
yes: continue comparing with next elements
no: copy r4 to r3 and r3 is treated as largest, comparing with next elements done
; In 8051 we don’t have simple compare instruction, at first the numbers are checked for
equality if both the elements in the array are equal no task is performed.
; "cjne" instruction is used to check for equality of both the numbers i.e.
"cjne a, directaddr., location"
; First element is copied to R3 & second element to R4, initially the value in R3 is
assumed as smallest and it is compared with next number in R4
; If R3 < R4
Yes: continue comparing with next elements
No: copy r4 to r3 and r3 is treated as smallest, comparing with next elements done
Alternate method:
org 0000h
mov dptr,#9000h
mov r0,#05h
dec r0
movx a,@dptr
mov 7fh,a
back2: inc dptr
movx a,@dptr
cjne a,7fh,back1
sjmp back3
back1: jnc back3
mov 7fh,a
back3: djnz r0,back2
mov a,7fh
end
42h - 4A ; sum
43h - 00 ; carry
b. ALP to perform 8-bit subtraction and specifying both negative & positive
difference value
org 0000
end
R5 - 00H : Borrow
Input: 30h - 21H ; first number (LSB) Result:34h -32H ; Sum (LSB)
d. ALP to perform 16-bit subtraction and specifying both negative & positive
difference value
org 0000
mov a,@r0 ; Reads lower order byte value of 16-bit subtrahend value
mov a,@r0 ; Reads higher order byte value of 16-bit subtrahend value
mov a,r2
subb a,@r0 ; Subtracts the lower order byte value of minuend from lower
order subtrahend
mov a,r3
inc r0
subb a,@r0 ; Subtracts the higher order byte value of minuend from higher
; order subtrahend with borrow
inc r0
mov a,r5
end
; This program performs subtraction of two 8-bit numbers. The subtraction operation is
done by using "Subb" instruction
Eg:
43 21
12 11
31 10
ORG 0000h ;
MOV A, 30h ; Take one of the numbers in A. (p x q = q x p)
MOV B, 31h ; Take the other number in B. Use of A and B is a must.
MOV R1, B ; R1 B , i t i s r e q u i r e d l a t e r.
Stage 3 multiplication.
MOV A, R0 ;
MOV B, 40h ;
MUL AB ;
ADD A, R1 ;
MOV 48h, A ;
JNC next ; (during above addition, to account for the carry.,)
INC B ; ie., add carry if resulted. Carry is always 1h in hex.
Next: NOP ;
MOV 47h, B ;
here: SJMP here ; END ;
ORG 0000h ;
MOV A, 50h ; rule: Dividend is taken in A.
MOV B, 51h ; rule: Divisor is taken in B.
4. Wri ti ng ALPs for : C ou nte rs: Bi na ry C ou nte rs, Decim al C ou nters e tc.,
5. Wri ti ng ALPs for : Unde rsta ndi ng conditi ona l CA LL and RETURN i nstr uctions.
b. C O U N T E R S : BCD UP COUNTER / BCD DOWN COUNTER*
ORG 0000h ;
MOV A, #00h ;
COUNT: MOV P0, A ;
ACALL DELAY ;
ADD A, #01h ; ADD A, #99h for down counter.
DA A ; decimal adjust the value
JNZ COUNT ;
HERE: SJMP HERE ;
; Time Del ay su brouti ne for a pprox. 1 0 sec. del ay
DELAY: MOV R1, #0AAh ; (use #0FF for longer time delays).
LOOP3: MOV R2, #0AAh ;
LOOP2: MOV R3, #0AAh ;
LOOP1: DJNZ R3, LOOP1 ; complete innermost loop first.
DJNZ R2, LOOP2 ; complete the middle loop incl. the first for every R2
DJNZ R1, LOOP3 ;
RET ; RETURN TO THE MAIN PROGRAM.
END
call delay
cjne r6,#00h,down
sjmp up
org 0000h
mov a,#76h
mov r0,#00h
mov r1,#00h
mov r2,#08h
clr c
back: rrc a
jc ones
inc r1
djnz r2, back
Department of Electrical & Electronics Engineering
29
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual
c . ALP to find number of even numbers and odd numbers from given array
org 0000h
mov r0,#40h
mov r1,#00h
mov r2,#00h
mov r3,#08h
back: mov a,@r0
clr c
rlc a
jc odd
inc r1
inc r0
djnz r3, back
axis1: sjmp axis1
odd: inc r2
inc r0
djnz r3,back
axis: sjmp axis
end
6. Wr i t i n g A L P s f o r :
Unde rsta ndi ng Code conver si on program s – BC D to ASC II, AS CII to BCD, AS CII to
decimal, Dec imal to ASCII, Hexa to dec imal and Decimal to Hexa
org 0000
orl a,#30h ;
mov a.r1 ;
anl a,#0f0h
rr a
rr a
rr a
orl a,#30h
mov r1,a
end
input: Output
A=16H R7=36h
R1=31
Note: Instead of rotate right (RR ) instruction, we can use SWAP instruction. (swap a)
org 0000h
rl a
rl a
rl a
mov P1,a
end
org 0000
mov @r0,b ;
mov b,#10
div ab
dec r0
mov @r0,b
dec r0
mov @r0,a
end
Input Output
Department of Electrical & Electronics Engineering
33
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual
mov a,b
rl a
rl a
rl a
orl a,r4
mov r4,a
mov a,r5
swap a
orl a,r4
mov P1,a
here: sjmp here
end
org 0000h
mov P2,r6
end
Interfacing Programs
PROGRAM 01:
A. Blinking of LED
org 0000h
mov P2,#00h ; P0—initialize as output port
back: mov a,#0aah
mov P2,a ; send 0xaa to P0
acall delay ; call delay
mov a,#55h
mov P2,a ; send 0x55 to P0
acall delay
sjmp back
org 0000h
mov P2,#00h
mov a,#00h
next: mov P2,a
acall dealy
inc A
sjmp next
delay: mov r1,#0ffh
C. ALP to perform down counter program using i/p and o/p devices
org 0000h
mov P2,#00h
mov a,#0ffh
next: mov P2,a
acall dealy
dec a
sjmp next
delay: mov r1,#0ffh
d3: mov r2,#0ffh
d2: djnz r2,d2
djnz r1,d3
ret
end
PROGRAM 02:
Aim: Interfacing of 8051 and stepper motor.
/*Program to control Stepper Motor Direction*/
#include <reg52.h>
#include <intrins.h>
sbit START = P1^0;
sbit STOP = P1^4;
#define MOTOR P2
unsigned char run=1;
unsigned char del=2;
/*------------------------------------------------
Stepper delay
------------------------------------------------*/
void stp_delay(unsigned char dly)
{
unsigned int i,j;
for (j = 0;j<dly;j++)
for(i = 0;i<100;i++);
}
/*------------------------------------------------
Key functions
------------------------------------------------*/
void chk_key(void)
{
if (START == 0) //Start key pressed
{
run = 1;
Department of Electrical & Electronics Engineering
39
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual
goto rotate;
}
if (STOP == 0) //Stop key pressed
{
run = 0;
goto rotate;
}
rotate:
_nop_();
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char stpval=1;
MOTOR = stpval;
stp_delay(del);
}
}
}
PROGRAM 03:
Aim: Interfacing of 8051 and DC Motor
/* Progaram to control DC Motor Direction */
#include <reg52.h>
#include <intrins.h>
sbit DIR1 = P2^0;
sbit DIR2 = P2^1;
sbit PWM = P3^0;
while (1)
{
Department of Electrical & Electronics Engineering
41
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual
goto quit;
}
}
}
void lcdcmd (unsigned char value)
{
ldata = value; //issue command code
rs = 0; //RS=0 for command
en = 1; //E=1 for H-to-L pulse
delay(1);
en = 0; //E=0 ,latch in
return;
}
void lcddata (unsigned char value)
{
ldata = value; //issue data
rs = 1; //RS=1 for data
en = 1; //E=1 for H-to-L pulse
delay(1);
en = 0; //E=0, latch in
return;
}
void delay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++); }
PROGRAM 05:
Aim: Interfacing of 8051 and DAC to generate different types of waveforms.
A. Generation of Ramp wave using DAC
/* Program to generate ramp wave using DAC*/
#include <reg52.h>
#include <intrins.h>
#define DAC P2 // DAC Data Output
sbit DWR = P3^0; //DAC Write Pin
for (j=0;j<tim;j++)
for (i=0; i<10;i++);
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char dval;
P3=0x00;
DAC = 0x00;
DWR = 0;
_nop_();
DWR = 1;
Department of Electrical & Electronics Engineering
43
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual
dval = 0x00;
while (1)
{
DAC = dval;
DWR = 0;
_nop_();
DWR = 1;
delay(1); //varying this value will wary the frequency of the waveform
dval++;
}
}
#include <reg52.h>
#include <intrins.h>
#define DAC P2 // DAC Data Output
sbit DWR =P3^0; //DAC Write Pin
for (j=0;j<tim;j++)
for (i=0; i<200;i++);
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char amp;
P3 = 0x00;
DAC = 0x00;
amp = 0x7F; //vary the value to change the amplitude of the waveform
while (1)
{
Department of Electrical & Electronics Engineering
44
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual
DAC = amp;
DWR = 0;
_nop_();
DWR = 1;
delay(1); //vary the value to change the frequency of the waveform
DAC = 0x00;
DWR = 0;
_nop_();
DWR = 1;
delay(1); // vary the value to change the frequency of the waveform
}
}
#include <reg52.h>
#include <intrins.h>
#define DAC P2 // DAC Data
sbit DWR =P3^0; //DAC Write Pin
for (j=0;j<tim;j++)
for (i=0; i<10;i++);
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char dval, amp;
P3=0x00;
DAC = 0x00;
DWR = 0;
_nop_();
DWR = 1;
amp= 0X7F; // vary the value to change the amplitude of the waveform
dval = 0x00;
Department of Electrical & Electronics Engineering
45
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual
while (1)
{
while(dval != amp)
{
DAC = dval;
DWR = 0;
_nop_();
DWR = 1;
delay(1); // vary the value to change the frequency of the waveform
dval++;
}
while(dval != 0x00)
{
DAC = dval;
DWR = 0;
_nop_();
DWR = 1;
delay(1); // vary the value to change the frequency of the waveform
dval--;
}
}
}
/*Program to generate Rectangular wave using DAC with Variable Amplitude /Frequency */
#include <reg52.h>
#include <intrins.h>
#define DAC P2 // DAC Data Output
sbit DWR =P3^0; //DAC Write Pin
for (j=0;j<tim;j++)
for (i=0; i<200;i++);
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char amp;
P3 = 0x00;
DAC = 0x00;
amp = 0x7F; //vary the value to change the amplitude of the waveform
while (1)
{
DAC = amp;
DWR = 0;
_nop_();
DWR = 1;
delay(2); //vary the value to change the frequency of the waveform
DAC = 0x00;
DWR = 0;
_nop_();
DWR = 1;
delay(1); // vary the value to change the frequency of the waveform
}
}
#include <reg52.h>
#include <intrins.h>
#define DAC P2 // DAC Data Output
sbit DWR =P3^0; //DAC Write Pin
for (j=0;j<tim;j++)
for (i=0; i<10;i++);
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char dval;
P3=0x00;
DAC = 0x00;
DWR = 0;
_nop_();
DWR = 1;
dval = 0x7F; //vary the value to change the amplitude of the waveform
while (1)
{
DAC = dval;
DWR = 0;
_nop_();
DWR = 1;
delay(1); // vary the value to change the frequency of the waveform
dval--;
}
}
#include <reg52.h>
#include <intrins.h>
#define DAC P2 // DAC Data Output
sbit DWR =P3^0; //DAC Write Pin
unsigned char code tab[] =
{128,131,134,137,140,144,147,150,153,156,159,162,165,168,171,174,177,179,
182,185,188,191,193,196,199,201,204,206,209,211,213,216,218,220,222,224,
226,228,230,232,234,235,237,239,240,241,243,244,245,246,248,249,250,250,
251,252,253,253,254,254,254,255,255,255,255,255,255,255,254,254,254,253,
253,252,251,250,250,249,248,246,245,244,243,241,240,239,237,235,234,232,
230,228,226,224,222,220,218,216,213,211,209,206,204,201,199,196,193,191,
188,185,182,179,177,174,171,168,165,162,159,156,153,150,147,144,140,137,
134,131,128,125,122,119,116,112,109,106,103,100,97,94,91,88,85,82,79,77,74,7
1,68,65,63,60,57,55,52,50,47,45,43,40,38,36,34,32,30,28,26,24,22,21,19,17,16,15
,13,12,11,10,8,7,6,6,5,4,3,3,2,2,2,1,1,1,1,1,1,1,2,2,2,3,3,4,5,6,6,7,8,10,11,12,13,1
5,16,17,19,21,22,24,26,28,30,32,34,36,38,40,43,45,47,50,52,55,57,60,63,65,68,71
,74,77,79,82,85,88,91,94,97,100,103,106,109, 112,116,119,122,125};
for (j=0;j<tim;j++)
for (i=0; i<10;i++);
Department of Electrical & Electronics Engineering
48
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char dval;
IE = 0x00;
DAC = 0x00;
P3=0x00;
dval = 0x00;
while (1)
{
DAC = tab[dval];
DWR = 0;
_nop_();
DWR = 1;
delay(1); //varying this value will vary the frequency of the wave
dval++;
}
}
PROGRAM 07:
#include <reg52.h>
#include <intrins.h>
sbit DSP1 = P3^4; //display 1 enable port line
sbit DSP2 = P3^5; //display 2 enable port line
sbit DSP3 = P3^6; //display 3 enable port line
sbit DSP4 = P3^7; //display 4 enable port line
/*------MAIN C function--------*/
void main (void)
{
unsigned int cnt = 1234; //value to be displayed
unsigned char dig1, dig2, dig3, dig4;
unsigned int temp;
DIGDAT = 0x00;
temp=cnt/10;
cnt=temp;
dig3= cnt%10;
temp=cnt/10;
cnt=temp;
dig4= cnt%10;
while(1)
{
DIGDAT = digit[dig1];
DSP1 = 1;
DSP1 = 0;
DIGDAT = digit[dig2];
DSP2 = 1;
DSP2 = 0;
DIGDAT = digit[dig3];
DSP3 = 1;
DSP3 = 0;
DIGDAT = digit[dig4];
DSP4 = 1;
DSP4 = 0;
}
}
PROGRAM 08:
Aim: Interfacing of 8051 and ADC.
#include <reg52.h>
#include <intrins.h>
sbit DSP1 = P3^4; //display 1 enable port line
sbit DSP2 = P3^5; //display 2 enable port line
sbit DSP3 = P3^6; //display 3 enable port line
sbit DSP4 = P3^7; //display 4 enable port line
_nop_();
ASTART = 1;
_nop_();
_nop_();
_nop_();
while(AEOC == 1);
return(ADATA);
}
void delay10ms() //05 msec
{
unsigned int m,n;
for(n=0;n<2;n++)
for(m=0;m<600;m++);
}
void ssegdisp(unsigned char dig1,dig2)
{
DIGDAT = digit[dig1];
DSP1 = 1;
delay10ms();
DSP1 = 0;
DIGDAT = digit[6];
DSP2 = 1;
delay10ms();
DSP2 = 0;
DIGDAT = digit[dig2];
DSP3 = 1;
delay10ms();
DSP3 = 0;
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned char aval, dig1,dig2;
P3=0x00;
ADATA=0xFF;
ASTART=0;
AEOC=0;
while (1)
{
aval=rdadc();
delay10ms();
delay10ms();
if((aval<0x19))
Department of Electrical & Electronics Engineering
51
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual
{
dig1=0;
dig2=0;
ssegdisp(dig1,dig2);
}
if((aval>0X19)&&(aval<0X32))
{
dig1=5;
dig2=0;
ssegdisp(dig1,dig2);
}
if((aval>0X32)&&(aval<0X4b))
{
dig1=0;
dig2=1;
ssegdisp(dig1,dig2);
}
if((aval>0X4b)&&(aval<0X64))
{
dig1=5;
dig2=1;
ssegdisp(dig1,dig2);
}
if((aval>0X64)&&(aval<0X7d))
{
dig1=0;
dig2=2;
ssegdisp(dig1,dig2);
}
if((aval>0X7d)&&(aval<0X96))
{
dig1=5;
dig2=2;
ssegdisp(dig1,dig2);
}
if((aval>0X96)&&(aval<0Xaf))
{
dig1=0;
dig2=3;
ssegdisp(dig1,dig2);
}
if((aval>0Xaf)&&(aval<0Xc8))
{
dig1=5;
dig2=3;
ssegdisp(dig1,dig2);
}
if((aval>0Xc8)&&(aval<0Xe1))
{
dig1=0;
Department of Electrical & Electronics Engineering
52
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual
dig2=4;
ssegdisp(dig1,dig2);
}
if((aval>0Xe1)&&(aval<0Xfa))
{
dig1=5;
dig2=4;
ssegdisp(dig1,dig2);
}
if((aval>0Xfa))
{
dig1=0;
dig2=5;
ssegdisp(dig1,dig2);
}
}
}
ACSII CHART
ACSII VALUE CHARACTER ACSII VALUE CHARECTER ACSII VALUE CHAREC
TER
48(30) 0 65(41) A 97(61) a
49(31) 1 66(42) B 98(62) b
50(32) 2 67(43) C 99(63) c
51(33) 3 68(44) D 100(64) d
52(34) 4 69(45) E 101(65) e
53(35) 5 70(46) F 102(66) f
54(36) 6 71(47) G 103(67) g
55(37) 7 72(48) H 104(68) h
Department of Electrical & Electronics Engineering
54
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual
Special function
S.No. Address Description
Register
01. 80* Port-0 Port-0 Latch
02. 90* Port-1 Port-1 Latch
03. A0* Port-2 Port-2 Latch
04. B0* Port-3 Port-3 Latch
05. C0*
06. D0* PSW Program Status Word
07. E0* A Register A
08. F0* B Register B
09. 81 SP Stack Pointer
10. 82 DPL Data pointer Lower byte
11. 83 DPH Data pointer higher byte
12. 87 PCON Power Control Register
Department of Electrical & Electronics Engineering
55
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual
2A 57 56 55 54 53 52 51 50
29 4F 4E 4D 4C 4B 4A 49 48
28 47 46 45 44 43 42 41 40
27 3F 3E 3D 3C 3B 3A 39 38
26 37 36 35 34 33 32 31 30
25 2F 2E 2D 2C 2B 2A 29 28
24 27 26 25 24 23 22 21 20
23 1F 1E 1D 1C 1B 1A 19 18
22 17 16 15 14 13 12 11 10
21 0F 0E 0D 0C 0B 0A 09 08
20 07 06 05 04 03 02 01 00
18 – 1F Register Bank 3
10 – 17 Register Bank 2
08 – 0F Register Bank 1
00 – 07 Register Bank 0
F8 FF
F0 B F7
E8 EF
E0 ACC E7
D8 DF
D0 PSW D7
C8 CF
C0 C7
B8 IP BF
B0 P3 B7
A8 IE AF
A0 P2 A7
98 SCON SBUF 9F
90 P1 97
Viva Questions
Augmented Programs
PROGRAM 09:
Aim: Interfacing of 8051 and Relay/ Buzzer.
A. Program to interface Buzzer.
#include <reg52.h>
#include <intrins.h>
sbit Buzzer = P3^0; //BUZZER CONTROL Pin
sbit ON = P1^0; //ON KEY
Department of Electrical & Electronics Engineering
65
Acharya Institute of technology
5th Semester Microcontroller Lab (15EEL57) Manual
IE=0x00;
P3=0x00;
lcdcmd(0x38); //LCD command
lcdcmd(0x0e);
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x80);
for(j=0;j<16;j++)
{
lcddata( msg1[j]);
}
lcdcmd(0xC0);
for(j=0;j<16;j++)
{
lcddata( msg2[j]);
}
while (1)
{
key = 0;
for( i = 0; i < 4; i++ )
{
// turn on row output
ROWPORT = rval[i];
key++;
if( COL2 == 0)
break;
key++;
if( COL3 == 0)
break;
key++;
if( COL4 == 0)
break;
key++;
}
lcdcmd(0xCF);
lcddata(keyPadMatrix[key]);
}
}
void lcdcmd(unsigned char value)
{
ldata = value; //issue command code
rs = 0; //RS=0 for command
// rw = 0; //R/W=0 to write to LCD
en = 1; //E=1 for H-to-L pulse
delay(1);
en = 0; //E=0 ,latch in
return;
}
void lcddata (unsigned char value)
{
ldata = value; //issue data
rs = 1; //RS=1 for data
// rw = 0; //R/W=0 to write to LCD
en = 1; //E=1 for H-to-L pulse
delay(1);
en = 0; //E=0, latch in
return;
}
void delay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}