0% found this document useful (0 votes)
26 views86 pages

M Cmanual 2020-UPDATED

Uploaded by

msahi2558
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views86 pages

M Cmanual 2020-UPDATED

Uploaded by

msahi2558
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 86

IV Sem E&C Microcontrollers lab[18ECL47]

MICROCONTROLLER 8051

Dept., of Electronics & Communication 1 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

Sl NO Program Name CO’S Page


NO
Introduction to Keil Software 5-6
1 Data Transfer: Block Move, Exchange, Sorting, Finding largest CO1
element in an array.
1 Write a program to transfer a block of data from a given source to CO1
destination. Without overlap. Use external memory
2 Write a program to transfer a block of data from a given source to CO1
destination. Without overlap. Use internal memory
3 Write a program to transfer a block of data from a given source to CO1
destination. With overlap. Use external memory 7-14
4 Write a program to interchange ‘N’ bytes of data stored at location x with CO1
‘N’ bytes of data stored at location y. Use external memory
5 Write a program to interchange ‘N’ bytes of data stored at location x with CO1
‘N’ bytes of data stored at location y. use internal memory
6 Write a program to arrange a set of 8 bit numbers starting at location x in CO1
ascending order/ descending order using bubble sort.
7 Write a program to find largest/ smallest number in data array. CO1
2 Arithmetic Instructions - Addition/subtraction, multiplication and CO1
division, square, Cube – (16 bits Arithmetic operations – bit
addressable).
8 Write an ALP illustrating addition, subtraction, multiplication and CO1
division
9 Write a program to multiply 16 bit by 8 bit data. CO1 15-22
10 Write an ALP to add two multi byte binary numbers. CO1
11 Write an ALP to subtract two multi byte binary numbers. CO1
12 Write an ALP to find square and cube of a number CO1
13 Write an ALP to add ‘N’ number of 1-byte binary numbers CO1
14 Write an ALP to add ‘N’ number of 1-byte BCD number CO1
3 Counters. CO1
15 Write an ALP to implement a binary (Hex) up/down counter. CO1 23-25
16 Write an ALP to implement decimal (BCD) up/down counter. CO1

Dept., of Electronics & Communication 2 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

4 Boolean & Logical Instructions (Bit manipulations). CO1

17 Write an ALP to realize the following Boolean expression. CO1

18 Write an ALP to compute the following. CO1 26-30


IF X=0; THEN NUM1 (AND) NUM2, IF X=1; THEN NUM1 (OR)
NUM2,
IF X=2; THEN NUM1 (XOR) NUM2, ELSE RES =00, RES IS 23H
LOCATION
19 Write an ALP to compute the following. CO1
IF X=0; THEN LSB OF NUM1 (AND) LSB OF NUM2,
IF X=1; THEN MSB OF NUM1 (OR)MSB OF NUM2 ,
IF X=2; THEN COMPLEMENT MSB OF NUM1
STORE THE BIT RESULT IN RES, WHERE RES IS MSB OF 23H
LOCATION
5 Conditional CALL & RETURN CO2
20 Write an ALP to demonstrate call and return instruction using a program CO2 31-32
to find factorial of a number.
6 Code conversion: BCD – ASCII; ASCII – Decimal; Decimal - CO2
ASCII; HEX - Decimal and Decimal - HEX.
21 Write an ALP to implement decimal to hex conversion CO2
22 Write an ALP to implement hex to decimal conversion CO2
23 Write an ALP to implement BCD to ASCII conversion CO2
24 Write an ALP to implement hexadecimal to ASCII conversion CO2
33-43
25 Write an ALP to implement ASCII to hexadecimal conversion CO2
26 Write an ALP to convert 8 bit BCD to its equivalent ASCII. CO2
27 Write an ALP to convert 8 bit binary (hex) to its equivalent ASCII. CO2
28 Write an ALP to convert ASCII to Binary (hex). CO2
29 Write an ALP to convert HEX to Decimal. CO2
30 Write an ALP to convert Decimal to HEX CO2
7 Programs to generate delay, Programs using serial port and on-Chip CO2
timer/counter
44-49
31 Conduct an experiment to configure 8051 microcontroller to transmit characters CO2
(yE) to a PC using the serial port and display on the serial window.

Dept., of Electronics & Communication 3 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

32 Generate a 1second delay continuously using the on chip timer in CO2


interrupt mode

33 Write an ALP to generate delay of 1 sec using timer. CO2


34 Write an ALP to transfer the message “WELCOME” using serial port CO2
with a baud rate of 9600.
8 PART-B: INTERFACING
1 Interface a simple toggle switch to 8051 and write an ALP to generate an CO3
interrupt which switches on an LED (i) continuously as long as switch is
on and (ii) only once for a small time when the switch is turned on.

2 Write a C program to (i) transmit and (ii) to receive a set of characters CO4
50-80
serially by interfacing 8051 to a terminal.
3 Write ALPs to generate waveforms using ADC interface CO3
4 Write ALP to interface an LCD display and to display a message on it. CO3
5 Write ALP to interface a Stepper Motor to 8051 to rotate the motor. CO3
6 Write ALP to interface ADC-0804 and convert an analog input connected CO3
to it.

Dept., of Electronics & Communication 4 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

Creating and compiling a μVision2 project

1. Double Click on the Vision3 icon on the desktop.


2. Close any previous projects that were opened using – Project->Close.
3. Start Project – New Project, and select the CPU from the device database (Database-Atmel-
AT89C51ED2). (Select AT89C51ED2 or AT89C51RD2 as per the board).On clicking ‘OK’,
the following option is displayed. Choose Yes.

4. Create a source file (using File->New), type in the assembly or C program and save this
(filename.asm/ filename.c) and add this source file to the project using either one of the
following two methods. (i) Project-Components,Environmentand Books->addfiles-> browse to
the required file -> OK “OR”
(ii) right click on the Source Group in the Project Window and the Add Files to Group

option.
5. Set the Target options using -> Project – Options for Target opens the Vision2 Options
for Target – Target configuration dialog. Set the Xtal frequency as 11.0592 Mhz, and also the
Options for Target – Debug – use either Simulator / Keil Monitor- 51 driver.

If Keil Monitor- 51 driver is used click on Settings -> COM Port settings select the COM
Port to which the board is connected and select the baud rate as 19200 or 9600 (recommended).
Enable Serial Interrupt option if the user application is not using on-chip UART, to stop
program execution.
6. Build the project; using Project -> Build Project. Vision translates all the user application
and links. Any errors in the code are indicated by – “Target not created” in the Build window,
along with the error line. Debug the errors. After an error free build, goto Debug mode

Dept., of Electronics & Communication 5 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

7. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog. Or by
clicking in the icon.
8.The program is run using the Debug-Run command & halted using Debug-Stop Running.

Also the (reset, run, halt) icons can be used. Additional icons are
(step, step over, step into, run till cursor).
9. If it is an interface program the outputs can be seen on the LCD, CRO, motor, led status, etc.
If it is a part A program, the appropriate memory window is opened using View -> memory
window (for data RAM & XRAM locations), Watch window (for timer program), serial
window, etc.

Note: To access data RAM area type address as D:0020h.


Similarly to access the DPTR region (XRAM-present on chip in AT89C51ED2) say 8000h
location type in X: 08000H.

Dept., of Electronics & Communication 6 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

Part-A: Assembly language programs


Data Transfer: Block Move, Exchange, Sorting, Finding largest element in
an array.
1. Write a program to transfer a block of data from a given source to destination.
Without overlap. Use external memory

(SL) Source Locations: 8100H; (DL) Destination location: 8120H; (BS) Block size: 03H

ORG 0000H
LJMP 8000H
ORG 8000H
MOV R7, #03H
MOV DPH, #81H
MOV R2, #20H
MOV R3, #00H
L1: MOV DPL, R3
MOVX A, @DPTR
MOV DPL, R2
MOVX @DPTR, A
INC R2
INC R3
DJNZ R7, L1
HERE: SJMP HERE
END

RESULT:

Before Execution After Execution


Memory loc Data Memory loc Data
X:8100H X:8120H

Dept., of Electronics & Communication 7 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

2. Write a program to transfer a block of data from a given source to destination.


Without overlap. Use internal memory

SL: 40H DL: 50H (Block Size) BS: 05H

ORG 0000H
LJMP 8000H
ORG 8000H
MOV R5, #05h
MOV R0, #40H
MOV R1, #50H
LOOP: MOV A, @R0
MOV @R1, A
INC R0
INC R1
DJNZ R5, LOOP
HERE: SJMP HERE
END

Before Execution After Execution


Memory loc Data Memory loc Data
D:40H D:50H

Dept., of Electronics & Communication 8 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

3. Write a program to transfer a block of data from a given source to destination.


With overlap. Use external memory

SL: 8100H DL: 8102H BS: 05H

ORG 0000H
LJMP 8000H
ORG 8000H
MOV DPH, #81H
MOV R2, #00H
MOV R3, #02H
MOV R1, #05H
MOV A, R2
ADD A, R1
DEC A
MOV R2, A
MOV A, R3
ADD A, R1
DEC A
MOV R3, A
L2: MOV DPL, R2
MOVX A, @DPTR
MOV DPL, R3
MOVX @DPTR, A
DEC R2
DEC R3
DJNZ R1, L2
HERE: SJMP HERE
END

RESULT:

Before Execution After Execution


Memory loc Data Memory loc Data
X:8100H X:8102H

Note: choose the DL within the range of SL.


Dept., of Electronics & Communication 9 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

4. Write a program to interchange ‘N’ bytes of data stored at location x with ‘N’
bytes of data stored at location y. Use external memory

SL: 8100H DL: 8200H Buffer Loc: 8300H BS: 03H

ORG 00H
LJMP 8000H
ORG 8000H
MOV R7, #03H
MOV R1, #81H
MOV R2, #82H
MOV R3, #83H
MOV R4, #00H
L1: MOV DPH, R1
MOV DPL,R4
MOVX A,@DPTR
MOV DPH,R3
MOVX @DPTR,A
MOV DPH,R2
MOVX A, @DPTR
MOV DPH, R1
MOVX @DPTR, A
MOV DPH, R3
MOVX A,@DPTR
MOV DPH, R2
MOVX @DPTR, A
INC R4
DJNZ R7, L1
HERE: SJMP HERE
END

RESULT:

Before Execution After Execution


Memory loc Data Memory loc Data
X:8100H X:8100H

X:8200H X:8200H

Dept., of Electronics & Communication 10 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

5. Write a program to interchange ‘N’ bytes of data stored at location x with


‘N’ bytes of data stored at location y. use internal memory

SL: 30H DL: 40H BS: 05H

ORG 00H
LJMP 8000H
ORG 8000H
MOV R7, #05H
MOV R0, #30H
MOV R1, #40H
NEXT: MOV A, @R0
MOV R2, A
MOV A,@R1
MOV @R0, A
MOV A, R2
MOV @R1, A
INC R0
INC R1
DJNZ R7, NEXT
HERE: SJMP HERE
END

Before Execution After Execution


Memory loc Data Memory loc Data
D:30H D:30H

D:40H D:40H

Dept., of Electronics & Communication 11 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

6. Write a program to arrange a set of 8 bit numbers starting at location X in


Ascending order/ Descending order using bubble sort.

ORG 0000H
LJMP 8000H
ORG 8000H
MOV R0, #09h (no. of comparisons)
L1: MOV DPTR, #9000h
MOV A, R0
MOV R1, A
L2: MOVX A, @DPTR
MOV B, A
INC DPTR
MOVX A, @DPTR
CLR C
MOV R2, A
SUBB A, B
JNC NOEXCHG //JC for descending
MOV A, B
MOVX @DPTR, A
DEC DPL
MOV A,R2
MOVX @DPTR,A
INC DPTR
NOEXCHG: DJNZ R1, L2
DJNZ R0, L1
HERE: SJMP HERE
END

NOTE: Enter 10 elements.

RESULT:
Before Execution After Execution
Memory loc Data Memory loc Data
X:9000h X:9000h

Dept., of Electronics & Communication 12 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

7. Write a program to find largest/ smallest number in data array.

ORG 0000H
LJMP 8000H
ORG 8000H
MOV R3, #06H (no. of comparisons)
MOV DPTR, #9000H
MOVX A,@DPTR
MOV R1,A
NEXTBYTE: INC DPTR
MOVX A,@DPTR
CLR C
MOV R2, A
SUBB A, R1
JC skip // JNC for smallest element
MOV A, R2
MOV R1,A
skip: DJNZ R3,NEXTBYTE
MOV A, R1
INC DPTR
MOVX @DPTR, A
HERE: SJMP HERE
END

RESULT: Largest
Before Execution After Execution
Memory Data Memory Data
loc loc
X:900H X:900H

Smallest
Before Execution After Execution
Memory Data Memory Data
loc loc
X:900H X:900H

Dept., of Electronics & Communication 13 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

VIVA QUESTIONS
1. Which are the most common 8051 series of microcontrollers
Atmel series AT89C2051 and Philips family P89C51RD2 are the two most common
microcontrollers of 8051 families.
2. What is the internal RAM size of 8051
128 bytes
3. When 8051 wakes up then 0x00 is loaded to which register?
Program Counter
4. How many bytes of bit addressable memory is present in 8051 based microcontrollers?
16 bytes
5. List Interrupts available in 8051 microcontroller
 External interrupt 0 (IE0) has highest priority among interrupts.
 Timer interrupt 0 (TF0)
 External interrupt 1 (IE1)
 Timer interrupt 1 (TF1) has lowest priority among other interrupts.
 Serial port Interrupt
 Reset
6. What is the meaning of the instruction MOV A,05H?
Address 05H is stored in the accumulator

Dept., of Electronics & Communication 14 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

Arithmetic Instructions - Addition/subtraction, multiplication and division,


square, Cube – (16 bits Arithmetic operations – bit addressable).

8. Write an ALP illustrating addition, subtraction, multiplication and division.

ORG 0000H
LJMP 8000H
ORG 8000H
MOV R0, #40H
MOVX A,@R0
MOV R1, A
INC R0
MOVX A,@R0
MOV B, A
INC R0
MOVX A,@R0
CJNE R1, #00H, CKSUB
ADD A, B
MOV B, #00H
JNC SKIP
MOV B, #01H
SKIP: SJMP LAST
CKSUB: CJNE R1, #01H, CKMUL
CLR C
SUBB A, B
MOV B, #00H
JNC SKIP1
MOV B, #0FFH
SKIP1: SJMP LAST
CKMUL: CJNE R1, #02H, CKDIV
MUL AB
SJMP LAST
CKDIV: CJNE R1, #03H, OTHER
DIV AB
SJMP LAST
OTHER: MOV A, #00H
MOV B, #00H
LAST: INC R0
MOVX @R0, A
INC R0
MOV A, B Memory Data to be operation
MOVX @R0, A location entered
HERE: SJMP HERE X:40h 00 ADD
END X:40h 01 SUB
X:40h 02 MUL
OBSERVATION & RESULT: X:40h 03 DIV
Enter the data in location 41h & 42h. Check the results in 43h& 44h

Dept., of Electronics & Communication 15 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

9. Write a program to multiply 16 bit by 8 bit data.

ORG 0000H
LJMP 8000H
ORG 8000H
MOV DPTR, #8050H
MOVX A,@DPTR
MOV B,A
PUSH B
INC DPTR
MOVX A,@DPTR
MUL AB
MOV R1,A
MOV R2,B
INC DPTR
MOVX A,@DPTR
POP B
MUL AB
ADD A,R2
PUSH 0E0H
MOV DPTR, #8055H
MOV A,R1
MOVX @DPTR,A
INC DPTR
POP 0E0H
MOVX @DPTR,A
MOV A,B
ADDC A, #00H
INC DPTR
MOVX @DPTR,A
HERE: SJMP HERE
END

OBSERVATION & RESULT:


Example:
78EF x 24=11019C
Before After
Mem data Mem data
Loc Loc
X:805 24 X:805 9C
0 5
X:805 EF X:805 01
1 6
X:805 78 X:805 11
2 7

Dept., of Electronics & Communication 16 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

10. Write an ALP to add two multi byte binary numbers.

ORG 0000H
LJMP 8000H
ORG 8000H
MOV DPH, #81H
MOV R2, #00H
MOV R3, #20H
MOV R4, #50H
MOV R5, #02H (data size)
L2: MOV DPL,R2
MOVX A,@DPTR
MOV B,A
MOV DPL,R3
MOVX A, @DPTR
ADDC A,B
MOV DPL,R4
MOVX @DPTR,A
INC R2
INC R3
INC R4
DJNZ R5,L2
INC DPTR
JC L3
MOV A,#00H
MOVX @DPTR,A
SJMP HERE
L3: M0V A,#01H
MOVX @DPTR,A
HERE: SJMP HERE
END

OBSERVATION & RESULT:


Memory Data Memory Data Memory Data
loc loc loc
X:8100h X:8120h X:8150h
X:8101h X:8121h X:8151h

Change data size to 4 bytes (32 bits)


Memory Data Memory Data Memory Data
loc loc loc
X:8100h X:8120h X:8150h
X:8101h X:8121h X:8151h
X:8102h X:8122h X:8152h
Dept., of Electronics
X:8103h & Communication X:8123h 17 X:8153h S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

11. Write an ALP to subtract two multi byte binary numbers.

ORG 0000H
LJMP 8000H
ORG 8000H
MOV DPH, #81H
MOV R2, #00H
MOV R3, #20H
MOV R4, #50H
MOV R5, #02H ( data size) (2 bytes=16 bits)
L2: MOV DPL,R2
MOVX A, @DPTR
MOV B, A
MOV DPL, R3
MOVX A, @DPTR
SUBB A, B
MOV DPL, R4
MOVX @DPTR, A
INC R2
INC R3
INC R4
DJNZ R5, L2
INC DPTR
CLR A
SUBB A,#00H
MOVX @DPTR, A
HERE: SJMP HERE
END

NOTE:-Contents of 8100h are subtracted from 8120h

OBSERVATION & RESULT:

Memory Data Memory Data Memory Data


loc loc loc
X:8100h X:8120h X:8150h
X:8101h X:8121h X:8151h

Change data size to 4 bytes (32 bits)


Memory Data Memory Data Memory Data
loc loc loc
X:8100h X:8120h X:8150h
X:8101h X:8121h X:8151h
X:8102h X:8122h X:8152h
X:8103h X:8123h X:8153h
Dept., of Electronics & Communication 18 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

Dept., of Electronics & Communication 19 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

12. Write an ALP to find square and cube of a number.

ORG 0000H
LJMP 8000H
ORG 8000H
MOV DPTR, #9000H
MOVX A, @DPTR
MOV R0, A Bit addressable location
MOV B, A 20H 07 06 05 04 03 02 01 00
MUL AB
CLR C
JB 01, LAST
PUSH B
MOV B, A
MOV A, R0
MUL AB
INC DPTR
MOVX @DPTR, A
MOV A, B
MOV R2, A
POP B
MOV A, R0
MUL AB
ADD A, R2
LAST: INC DPTR
MOVX @DPTR, A
MOV A, B
ADDC A, #00H
INC DPTR
MOVX @DPTR, A
HERE: SJMP HERE
END

NOTE: If bit 01 =1, square operation is done


If bit 01=0, cube operation is done

D:20H OPERATION
02/03/0f/ff square
00/05/08 cube

OBSERVATION & RESULT:

Mem Loc Data entered operation Mem Loc Result


X:9000H X:9001H
D:20H X:9002H
D:20H X:9001H
X:9002H

Dept., of Electronics & Communication 20 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

13. Write an ALP to add ‘N’ number of 1-byte binary numbers.

ORG 0000H
LJMP 8000H
ORG 8000H
MOV DPTR, #8100H
MOV R5, #00H
MOVX A, @DPTR
MOV R7, A
DEC R7
CLR C
INC DPTR
MOVX A,@DPTR
L1: MOV B, A
INC DPTR
MOVX A, @DPTR
ADD A, B
JNC L2
INC R5
L2: DJNZ R7, L1
MOV DPTR, #9100H
MOVX @DPTR, A
INC DPTR
MOV A, R5
MOVX @DPTR, A
HERE: SJMP HERE
END

RESULT:
Note: N- size of the array. Store N in location 8100h and elements from 8101h onwards

Mem Loc Data


X:8100H Size of
array
X:8101H
X:8102H RESULTS
X:8103H Mem Loc Data
X:8104H X:9100H
X:8105H X:9101H

Dept., of Electronics & Communication 21 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

14. Write an ALP to add ‘N’ number of 1-byte BCD numbers.

ORG 0000H
LJMP 8000H
ORG 8000H
MOV DPTR, #8100H
MOV R5, #00H
MOVX A,@DPTR
MOV R7, A
DEC R7
CLR C
INC DPTR
MOVX A,@DPTR
L1: MOV B, A
INC DPTR
MOVX A, @DPTR
ADD A, B
DA A
JNC L2
INC R5
L2: DJNZ R7, L1
MOV DPTR, #9100H
MOVX @DPTR, A
INC DPTR
MOV A, R5
ADD A, #00H
DA A
MOVX @DPTR, A
HERE: SJMP HERE
END

RESULT:
Note: N- size of the array. Store ‘N’ in location 8100h and elements from 8101h onwards.
Enter only BCD numbers.
Mem Loc Data
X:8100H Size of
array
X:8101H
X:8102H
X:8103H
Result
Mem Loc Data
X:9100H
X:9101H

Dept., of Electronics & Communication 22 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

VIVA QUESTIONS

1. How are the status of the carry, auxiliary carry and parity flag affected if the write
instruction
MOVA,#9C
ADD A,#64H
CY=1,AC=1,P=0
2. When the microcontroller executes some arithmetic operations, then the flag bits of
which register are affected?
PSW
3. How are the bits of the register PSW affected if we select Bank2 of 8051?
PSW.3=0 and PSW.4=1
4. DJNZ R0, label is how many bit instructions?
2
5. JZ, JNZ, DJNZ, JC, JNC instructions monitor the bits of which register?
PSW
6. DAA command adds 6 to the nibble if:
Either CY or AC is 1

Dept., of Electronics & Communication 23 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

Counters.

15. Write an ALP to implement a binary (Hex) up/down counter.

ORG 0000H
LJMP 8000H
ORG 8000H
MOV A, #00H
BACK: ACALL DELAY
INC A //DEC A for binary down counter
JNZ BACK
HERE: SJMP HERE

DELAY: MOV r1, #0FFH


DECR1: MOV r2, #0FFH
DECR: MOV r3, #0FFH
DJNZ r3, $
DJNZ r2, DECR
DJNZ r1, DECR1
RET
END

Note: Check the result in watch 1window.


$ indicates self loop.

OBSERVATION & RESULT:

Dept., of Electronics & Communication 24 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

16. Write an ALP to implement decimal (BCD) up/down counter.

ORG 00H
SJMP 30H
ORG 30H
MOV A, #00H
BACK: ACALL DELAY
ADD A, #01H //ADD A, #99H for BCD up counter,
DA A
JNZ BACK
HERE: SJMP HERE

DELAY: MOV r1, #0FFH


DECR1: MOV r2,#0FFH
DECR: MOV r3, #0FFH
DJNZ r3, $
DJNZ r2, DECR
DJNZ r1, DECR1
RET
END

Note: Check the result in watch 1window

OBSERVATION & RESULT:

Dept., of Electronics & Communication 25 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

VIVA QUESTIONS

1. What is the clock source for the timers?


From the crystal applied to the micro-controller
2. What is the function of the TMOD register?
TMOD register is used to set different timer’s or counter’s to their appropriate modes
3. Auto reload mode is allowed in which mode of the timer?
Mode 2
4. A counter is fundamentally a _________ sequential circuit that proceeds through the
predetermined sequence of states only when input pulses are applied to it.
Register
5. Which special function register play a vital role in the timer/counter mode selection
process by allocating the bits in it?
TMOD

Dept., of Electronics & Communication 26 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

Boolean & Logical Instructions (Bit manipulations).

17. Write an ALP to realize the following Boolean expression.

Y=A’BC+AB’C+ABC’
ORG 0000H
LJMP 8000H
ORG 8000H
SETB 00H
CLR 01H
SETB 02H
MOV C,01H
ANL C,02H
ANL C,/00H
MOV 03H,C
MOV C,00H
ANL C,/01H
ANL C,02H
MOV 04H,C
MOV C,00H
ANL C,01H
ANL C,/02H
ORL C,04H
ORL C,03H
MOV 08H,C
HERE: SJMP HERE
END

OBSERVATION & RESULT:

Bit addressable area:

21H 0F 0E 0D 0C 0B 0A 09 08
20H 07 06 05 04 03 02 01 00

C B A
Results:
Mem.Loc Result
D:21H
D:21H

Dept., of Electronics & Communication 27 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

LOGICAL INSTRUCTIONS

18. ASSEMBLY PROGRAM ILLUSTRATING LOGICAL INSTRUCTIONS


(BYTE LEVEL)
eight bit numbers X, NUM1 & NUM2 are stored in internal data RAM locations 20h,
21h & 22H respectively.
Write an ALP to compute the following.
IF X=0; THEN NUM1 (AND) NUM2, IF X=1; THEN NUM1 (OR) NUM2,
IF X=2; THEN NUM1 (XOR) NUM2, ELSE RES =00, RES IS 23H LOCATION
ORG 0000H
SJMP 30H
ORG 30H
MOV A, 20h //donot use #, as data ram 20h is to be accessed
MOV R1,A //X IN R1
MOV A,21H //A -NUM1
CJNE R1,#0,CKOR
ANL A, 22H
SJMP END1
CKOR:CJNE R1,#01,CKXOR
ORL A, 22H
SJMP END1
CKXOR:CJNE R1,#02,OTHER
XRL A, 22H
SJMP END1
OTHER: CLR A
END1: MOV 23H,A //STORE RESULT
HERE: SJMP HERE
END

Algorithm:
1. Point to the data RAM register 20h and store the condition x.
2. Point to 21h and 22h and move the first number to A register.
3. Compare the contents of r1 and perform the operations accordingly.
4. The result will be stored in 23H register.
RESULT:
1)Before Execution: D:020H =00, 21=0f, 22 = 12
After Execution D:023H = 02
2)Before Execution: D:020H =01, 21=0f, 22 = 12
After Execution D:023H = 1F
3)Before Execution: D:020H =02, 21=0f, 22 = 12
After Execution D:023H = 1D
4)Before Execution: D:020H =34, 21=0f, 22 = 12
After Execution D:023H = 00

Dept., of Electronics & Communication 28 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

The above program can also be written as shown below (using indirect addressing)
ORG 0000H
SJMP 30H
ORG 30H
MOV R0,#20h
MOV A,@R0 //ON CHIP DATA RAM-DONOT USE MOVX
MOV R1,A //X IN R1
INC R0
MOV A,@R0 //A -NUM1
INC R0 // R0 POINTS TO NUM2
CJNE R1,#0,CKOR
ANL A, @R0
SJMP END1
CKOR:CJNE R1,#01,CKXOR
ORL A, @R0
SJMP END1
CKXOR:CJNE R1,#02,OTHER
XRL A, @R0
SJMP END1
OTHER: CLR A
END1:INC R0
MOV @R0,A //STORE RESULT
HERE:SJMP HERE
END

Dept., of Electronics & Communication 29 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

19. Boolean variable instructions are also called as bit level logical instructions
3 eight bit numbers X, NUM1 & NUM2 are stored in internal data RAM locations 20h, 21h &
22H respectively.
Write an ALP to compute the following.
IF X=0; THEN LSB OF NUM1 (AND) LSB OF NUM2,
IF X=1; THEN MSB OF NUM1 (OR)MSB OF NUM2 ,
IF X=2; THEN COMPLEMENT MSB OF NUM1
STORE THE BIT RESULT IN RES, WHERE RES IS MSB OF 23H LOCATION
ORG 00H
SJMP 30h
ORG 30h
MOV R0,20H //R0-X
CJNE R0,#0,CK1
MOV C,08H //LSB OF NUM1 (21H) - BIT ADDRESS -08
ANL C,10H //LSB OF NUM2 (22H) - BIT ADDRESS -10
SJMP LAST
CK1:CJNE R0,#1,CK2
MOV C,0FH //MSB OF NUM1 (21H) - BIT ADDRESS -0F
ANL C,17H //MSB OF NUM2 (22H) - BIT ADDRESS -17
SJMP LAST
CK2:CJNE R0,#2,CK3
CPL 0FH
MOV C,0FH //MSB OF NUM1 (21H) - BIT ADDRESS -0F
SJMP LAST
CK3:CLR C
LAST:MOV 1FH,C //RES IS MSB OF 23H LOCATION -1FH
HERE:SJMP HERE
END
RESULT: 20h = 00 => AND OF LSBs=1 (hence 80 in 23h location)

20h = 01 => OR of MSBs = 0 (hence 00 in 23h location)

20h = 01 =>complement of MSB of 21h location. Hence 21h is changed to A1 and 23h
location has 80h
Before Execution After Execution

Dept., of Electronics & Communication 30 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

Algorithm:
1. Move the condition X (from 20h location) into R0 register.
2. If X=0; then move LSB bit of 21h to carry flag and ‘AND’ Carry flag with LSB bit of
22h. Goto step5
3. If X=1; then move MSB bit of 21h to carry flag and ‘OR’ Carry flag with MSB bit of
22h. Goto step5
4. If X=0; then complement MSB bit of 21h and move it to carry flag. Goto step5
5. Store Carry flag at MSB bit of 23h location.

VIVA QUESTIONS

1. ANL instruction is used _______


To AND the contents of the two registers and to mask the status of the bits
2. CJNE instruction makes _______
In CJNE command, the pointer jumps if the values of the two registers are not equal and
it resets CY if the destination address is larger then the source address and sets CY if the
destination address is smaller then the source address.
3. XRL, ORL, ANL commands have _______
accumulator as the destination address and any register, memory or any immediate data
as the source address
4. In unsigned number addition, the status of which bit is important?
CY
5. Which of these instructions have no effect on the flags of PSW?
ANL , ORL and XRL.These instructions are the arithmetic operations and the flags are
affected by the data copy instructions, so all these instructions don’t affect the bits of the
flag.

Dept., of Electronics & Communication 31 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

Conditional CALL & RETURN

20. Write an ALP to demonstrate call and return instruction using a program to find
factorial of a number.

Address Label Mnemonic Comment


8000 MOV R0,#05 Input number
MOV A,R0
LCALL 9000 LCALL FACT
LCALL 0003

9000 FACT CJNE R0,#01,9005 CJNE R0,#01,LOOP


RET
9005 LOOP DEC R0
MOV F0,R0
MUL AB
LJMP 9000 LJMP FACT

ORG 0000 /* starting of the programe


MOV R1,#04 /* load r1 with 04
MOV R7,#01 /* load r7 with 01
LCALL FACT /*longcalltothefact subroutine
MOV R7,A /* load R7 WITH 00

FACT: MOV A,R7 /* LOAD A WITH 01


CJNE R1,#00,UP /*compare jump in not equal
SJMP UP1 /* inttrupt(condition togo up1)
UP: MOV B,R1 /* b= 00
MUL AB /* result in A
DJNZ R1,UP /* decrement jump if r1 zero
UP1: RET / return
END

Dept., of Electronics & Communication 32 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

VIVA QUESTIONS

1. LCALL instruction takes how many bytes


3 bytes
2. Are PUSH and POP instructions are a type of CALL instructions?
No
3. Find the number of times the following loop will be executed
MOV R6,#200
BACK:MOV R5,#100
HERE:DJNZ R5, HERE
DJNZ R6,BACK
END
20000
4. Calculate the jump code for again and here if code starts at 0000H
MOV R1,#0
MOV A,#0
MOV R0,#25H
AGAIN:ADD A,#0ECH
JNC HERE
HERE: INC R1
DJNZ R0,AGAIN
MOV R0,A
END
E9, 01. Loop address is calculated by subtracting destination address and the address
next
to the source address. So here if we start with 0000H then source address is 0008H and
the destination address is 0004H So loop address is 04-0A=E9
5. What is the time taken by one machine cycle if crystal frequency is 20MHz?
0.60 micro seconds. Time taken by one machine cycle is calculated by the inverse of a
(crystal frequency) /12

Dept., of Electronics & Communication 33 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

Code conversion: : BCD – ASCII; ASCII – Decimal; Decimal - ASCII;


HEX - Decimal and Decimal - HEX.

21. Write an ALP to implement decimal to hex conversion

ORG 0000H
SJMP 30h
ORG 30h
MOV DPTR,#40H //2-digit decimal number to be converted is given in data memory
40h
MOVX A, @DPTR
ANL A, #0F0H //obtain upper decimal digit
SWAP A //bring to the units place
MOV B,#0AH //MULTIPLY tens digit with #0A-toget tens in hex
MUL AB
MOV R1,A //temporarily store the converted tens value
MOVX A,@DPTR //get the decimal number again
ANL A,#0FH //obtain the units digit
ADD A,R1 //add to the converted tens value
INC DPTR //increment data address
MOVX @DPTR,A //converted hexadecimal number in next location
HERE:SJMP HERE
END

RESULT: before execution- X:0040H = 45 (Decimal/BCD)


After Execution: X:0041h = 2D (hex value)

Algorithm
1. Move the decimal data to be converted from external memory 40h to accumulator.
2. AND A reg with 0f0h and obtain the upper MSB of the decimal digit and swap the LSB
and MSB of accumulator to bring the same to units place.
3. Move 0ah to B register and multiply with A reg to convert to hex value, store the
converted tens value in r1
4. Get the LSB of the decimal number and add to the converted tens value
5. point to the next memory location and store the result (hexadecimal).

Dept., of Electronics & Communication 34 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

22. Write an ALP to implement hex to decimal conversion


ORG 0000H
SJMP 30h
ORG 30h
MOV DPTR,#9000H
MOVX A,@DPTR //Get hex number
MOV B,#10
DIV AB //divide by 10 (0AH)
INC DPTR
XCH A,B
MOVX @DPTR,A //Store the remainder (in B) In units place
XCH A,B
MOV B,#10 //Divide the quotient in A by 10
DIV AB
INC DPTR
XCH A,B
MOVX @DPTR,A //Store the remainder (in B) In tens place
XCH A,B
INC DPTR
MOVX @DPTR,A //Store the quotient (in A) in hundreds place
HERE:SJMP HERE
End

RESULT: 9000H – FF (HEX NUMBER)


9001 to 9003 – unpacked BCD number (decimal)- 5,5,2 (i.e., 255 stored Lower digit first)

Algorithm
1. Move the hex data to be converted to accumulator.
2. Move 10 to B register and divide with A reg to convert to ascii value
3. Store the converted LSB value in r7
4. Repeat the step 2 to obtain the converted MSB value
5. Store the same in r6

Dept., of Electronics & Communication 35 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

23. Write an ALP to implement BCD to ASCII conversion

ORG 0000H
SJMP 30h
ORG 30h
MOV R1,#50H
MOV A,@R1 //get BCD data byte from RAM location 50h
MOV R2,A //Store in R2
ANL A,#0FH //Get the lower nibble
ORL A,#30H //Add/or with 30h i.e., 0-9 converted to 30-39h
INC R1
MOV @R1,A //Store the lower digit's ASCII code
MOV A,R2 //Get back the number
SWAP A //Swap nibbles in A
ANL A,#0FH //Get the upper BCD digit
ORL A,#30H //Convert to ASCII
INC R1
MOV @R1,A //Store the upper digit's ASCII code
here: sjmp here
END

RESULT: The BCD code 28 at D:0050h is converted to 2 ASCII codes-38h 32h

Algorithm :
//Converts the BCD byte in A into two ASCII characters.
1. Move the BCD data to be converted to accumulator.
2. Get the lower nibble(BCD digit) & ADD (or ORL) with 30h
3. Store the converted ASCII value
4. Get the higher nibble(tens BCD digit) & ADD (or ORL) with 30h
5. Store the converted ASCII value

Dept., of Electronics & Communication 36 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

24. Write an ALP to implement hexadecimal to ASCII conversion


//This program also illustrates conditional branching (JNC), call and return instructions.

ORG 0000H
SJMP 30h
ORG 30h
MOV R1,#50H
MOV A,@R1 //get hexadecimal data byte from RAM location 50h
MOV R2,A //Store in R2
ANL A,#0FH //Get the lower nibble
ACALL ASCII //Convert to ASCII
INC R1
MOV @R1,A //Store the lower digit's ASCII code
MOV A,R2 //Get back the number
SWAP A //Swap nibbles in A
ANL A,#0FH //Get the upper BCD digit
ACALL ASCII
INC R1
MOV @R1,A //Store the upper digit's ASCII code
HERE: SJMP HERE
ASCII:MOV R4,A //Store a
CLR C
SUBB A,#0AH //Check if digit >=0A
MOV A,R4
JNC SKIP
ADD A,#07H //Add 07 if >09
SKIP:ADD A,#30H //Else add only 30h for 0-9
RET
END
RESULT: The BCD code 2C at D:0050h is converted to 2 ASCII codes-43h(for 0B) & 32h
(for 02) Another Example-BA

Algorithm :
//Converts the hexadecimal byte in A into two ASCII characters.
1. Move the hexadecimal data to be converted to accumulator.
2. Get the lower nibble & call ASCII routine
3. Store the converted ASCII value
4. Get the higher nibble & call ASCII routine
5. Store the converted ASCII value
ASCII subroutine
1. If digit greater than 09,(for A-F) add 07h & 30h
2. Else (i.e., for 0-9) add only 30h
3. return
Dept., of Electronics & Communication 37 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

25. Write an ALP to implement ASCII to hexadecimal conversion


ORG 0000H
SJMP 30h
ORG 30h
MOV R1,#50H
MOV A,@R1 //get ascii byte from RAM location 50h
CLR C
SUBB A,#41H
MOV A,@R1
JC SKIP
CLR C
SUBB A,#07H
SKIP:CLR C
SUBB A,#30H
INC R1
MOV @R1,A //Store the hex code
here: sjmp here
END

RESULT: The ASCII code 45 at D:0050h is converted to hexadecimal -0E at 51h

Note: For this program the input data should be only in the range 30h-39h & 41h to 46h.

Algorithm :
//Converts the ASCII characters into hexadecimal number.
1. Move the ASCII character to be converted to accumulator.
2. If character is greater than 41h,(for A-F), then subtract 07h & 30h
3. Else (i.e., for 0-9) subtract only 30h
4. Store the converted hexadecimal number.

Dept., of Electronics & Communication 38 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

26. Write an ALP to convert 8 bit BCD to its equivalent ASCII.

ORG 0000H
LJMP 8000h
ORG 8000h
MOV R1, #50H
MOV A,@R1
MOV R2, A
ANL A, #0FH
ORL A, #30H
INC R1
MOV @R1, A
MOV A, R2
SWAP A
ANL A, #0FH
ORL A, #30H
INC R1
MOV @R1, A
HERE: SJMP HERE
END

Note: Enter the input data ranging from 00 to 99.


Do not enter hexadecimal numbers (A-F)

OBSERVATION & RESULT:

BCD ASCII
0 30
1 31
2 32
3 33
4 34
5 35
6 36
7 37
8 38
9 39

EXAMPLE:
D:50H 65
D:51H 35
D:52H 36

Results:

Mem Data entered


Loc
D:50H
Dept., of Electronics & Communication Results 39 S JBIT, B’lore
D:51H
D:52H
IV Sem E&C Microcontrollers lab[18ECL47]

27. Write an ALP to convert 8 bit binary (hex) to its equivalent ASCII.

ORG 0000H
SJMP 30h Binar Equivalent
ORG 30h y ASCII
MOV R1,#50H 0 30
MOV A,@R1 1 31
MOV R2, A 2 32
ANL A, #0FH 3 33
ACALL ASCII
4 34
INC R1
5 35
MOV @R1, A
6 36
MOV A, R2
7 37
SWAP A
ANL A, #0FH 8 38
ACALL ASCII 9 39
INC R1 A 41
MOV @R1, A B 42
HERE: SJMP HERE C 43
D 44
ASCII: MOV R4, A E 45
CLR C F 46
SUBB A, #0AH
MOV A, R4
JC SKIP
ADD A, #07H
SKIP: ADD A, #30H
RET
END

OBSERVATION & RESULT:

Mem Data entered


Loc
Dept., of Electronics & Communication 40 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

D:50H
Results
D:51H
D:52H

28. Write an ALP to convert ASCII to Binary (hex).

ORG 0000H
SJMP 30h
ORG 30h
MOV R1,#50H
MOV A,@R1 //get ASCII byte from RAM location 50h
CLR C
SUBB A,#41H
MOV A,@R1
JC SKIP
SUBB A,#07H
SKIP: CLR C
SUBB A,#30H
INC R1
MOV @R1, A
HERE: SJMP HERE
END

Note: this program works for ASCII to BCD conversion, if the input data is between 30-39

OBSERVATION & RESULT:


Examples:
Result for ASCII to binary
D:50H D:51H
37 07
42 0B

D:50H D:51H
Dept., of Electronics & Communication 41 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

37 07
42 0B

Result for ASCII to BCD

D:50H D:51H
37 07
42 0B
D:50H D:51H
37 07
39 09

D:50H D:51H
37 07
42 0B

D:50H D:51H
37 07
42 0B

29. Write an ALP to convert HEX to Decimal.

ORG 0000H
LJMP 8000h
ORG 8000h
MOV DPTR,#9000H
MOVX A,@DPTR
MOV B, #10
DIV AB
Dept., of Electronics & Communication 42 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

XCH A, B
MOV r2, A
XCH A, B
MOV B, #10
DIV AB
INC DPTR
XCH A, B
SWAP A
ADD A, R2
MOVX @DPTR, A
INC DPTR
MOV A, B
MOVX @DPTR, A

HERE: SJMP HERE


End

OBSERVATION & RESULT:

Mem loc Data entered


X:9000h
Results
X:9001h
X:9002h
X:9003h

30. Write an ALP to convert Decimal to HEX.

Dept., of Electronics & Communication 43 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

ORG 0000H
LJMP 8000h
ORG 8000h
MOV DPTR, #40H
MOVX A, @DPTR
ANL A, #0F0H
SWAP A
MOV B, #0AH
MUL AB
MOV R1, A
MOVX A,@DPTR
ANL A, #0FH
ADD A, R1
INC DPTR
MOVX @DPTR, A
HERE: SJMP HERE
END

OBSERVATION & RESULT:

Mem loc Data entered


X:0040h
Results
X:0041h
X:0042h
X:0043h

Dept., of Electronics & Communication 44 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

VIVA QUESTIONS

1. What does ASCII stand for?


American Standard Code for Information Interchange. The ASCII codes are used to
represent the bits into symbols and vice versa. ASCII is the American Standard Code
which is used to exchange information.
2. Binary Coding for the letter X is ______________
01011000

3. Binary coded decimal is a combination of __________


Four binary digits. Binary coded decimal is a combination of 4 binary digits. For
example-8421.
4. Add the two BCD numbers: 1001 + 0100 = ?
00010011. Firstly, Add the 1001 and 0100. We get 1101 as output but it’s not in BCD
form. So, we add 0110 (i.e. 6) with 1101. As a result we get 10011 and it’s BCD form is
0001 0011.
5. Which is the number system that uses numbers as well as alphabets?
Hexadecimal System

Dept., of Electronics & Communication 45 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

6. SERIAL DATA TRANSMISSION


Program illustrating serial ASCII data transmission (data-yE) Note-to use result of this
program, after selecting DEBUG session in the main menu use View-> serial window #1. On
running & halting the program, the data is seen in the serial window.
31. Conduct an experiment to configure 8051 microcontroller to transmit characters
(yE) to a PC using the serial port and display on the serial window.

ORG 0H
SJMP 30H
ORG 30H
MOV TMOD,#20H //timer 1; mode 2
MOV TH1,#-3 //-3=FD loaded into TH1 for 9600 baud, 11.0592MHz.
MOV SCON,#50H //8-bit, 1 stop bit, REN enabled
SETB TR1 //Start timer 1
AGAIN:MOV A,#’y’ //transfer “y”
ACALL TRANS
MOV A,#’E’ //transfer “E”
ACALL TRANS
AGAIN1:SJMP AGAIN1
TRANS: MOV SBUF,A //load SBUF
HERE:JNB TI,HERE //Wait for last bit to transfer
CLR TI //get ready for next byte
RET
END
RESULT: yE is printed on the serial window each time the program is executed.
Theory: In serial transmission as opposed to parallel transmission, one bit at a time is
transmitted. In serial asynchronous transmission, the data consists of a Start bit (high), followed
by 8 bits of data to be transmitted and finally the stop bit. The byte character to be transmitted is
written into the SBUF register. It transmits the start bit. The 8-bit character is transferred one bit
at a time. The stop bit is transferred. After the transmission, the TI flag = 1 indicating the
completion of transmission. Hence in the subroutine wait until TI is set. Later clear the TI flag
and continue with transmission of the next byte by writing into the SBUF register. (The
program can also be written in interrupt mode). The speed of the serial transmission is set by the
baud rate which is done with the help of timer 1. (Refer Ayala). Timer1 must be programmed in
mode 2 (that is, 8-bit, auto reloads).
Baud rate Calculation: Crystal freq/ (12*32) = (11.0592MHz)/(12*32) = 28800.
Serial communication circuitry divides the machine cycle frequency (11.0592MHz)/(12) by 32
before it is being used by the timer to set the baud rate.
To get 9600, 28800/3 is obtained by loading timer1 with -3 (i.e., FF – 3 = FD) for further clock
division. For 2400 baud rate, 28800/12 => -12 = F4 in TH1.
Algorithm:
1. Initialize timer 1 to operate in mode 2 by loading TMOD register.
2. load TH1 with -3 to obtain 9600 baud.
3. Initialize the asynchronous serial communication transmission (SCON) register.
4. Start timer1 to generate the baud rate clock.
5. Transmit the characters “y” & “E” by writing into the SBUF register and waiting for the
TI flag.
Dept., of Electronics & Communication 46 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

32. Generate a 1second delay continuously using the on chip timer in interrupt mode.
ORG 0H //Reset Vector
SJMP 30H
ORG 0BH //TF0 vector
SJMP ISR
ORG 30H
MOV A,#00
MOV R0,#0
MOV R1,#0
MOV TMOD,#02H //00000010-Run timer0 in mode 2
MOV TH0,#118 //Set up timer 0 to overflow in 0.05msec
MOV IE,#82H //%10000010 – Enable timer0 interrupt
SETB TCON.4 //Start the timer0
HERE:SJMP HERE
ISR: CLR TCON.4 //Disable timer0
INC r1 //r1*r2 = 100*200 = 20000 * 0.05msec = 1sec
CJNE R1,#100,SKIP
MOV R1,#00
INC R0
CJNE R0,#200,SKIP
MOV R0,#00H
INC A
SKIP: SETB TCON.4 //Enable Timer
RETI //Return from interrupt subroutine
END

RESULT: Accumulator A is incremented in binary from 00, 01,02…09,0A, 0B, …, 0F, 10, 11,
…FF every 1 second (for 33MHz clock setting & every 3 seconds for 11.0598MHz)

Algorithm:
1. Set up timer0 in mode 2 operation
2. Load TH1 with 118 to generate an interrupt every 0.05msec.
3. Reset registers a, r1 & r0.
4. Repeat step 4 continuously
5. On interrupt; ISR at 000B loaction goes to step 6
6. disable timer0
7. Update r1 & r0
8. Check if 20000 interrupts (=1 sec) over. Yes –increment accumulator a.
9. enable timer & return from ISR.

Dept., of Electronics & Communication 47 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

Timerdelay = 12*(257-delay)/frequency
Timerdelay=0.05msec
Delay=256-((timerdelay * frequency)/12) =256-(0.05*10 -3 * 33*106)/12
=256-137.5 =118.5 //loaded in TH0
To get 1sec delay
1/0.05msec = 200*100 in the ISR
(assuming 33 MHZ crystal frequency. For 11 MHz, the calculations change).

Dept., of Electronics & Communication 48 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

33. Write an ALP to generate delay of 1 sec using timer.

ORG 0000H
LJMP 8000H
ORG 0BH
LJMP ISR
ORG 8000H
MOV A, #00H
MOV R0, #00H
MOV R1, #00H
MOV TMOD, #02H
MOV TH0, #118
MOV IE, #82H
SETB TCON.4
HERE: SJMP HERE
ISR: CLR TCON.4
INC R1
CJNE R1, #100, SKIP
MOV R1, #00
INC R0
CJNE R0, #200, SKIP
MOV R0, #00H
INC A
SKIP: SETB TCON.4
RETI
END

Refer Page No.: 43 for TMOD & TCON Register Format

OBSERVATION & RESULT:

Dept., of Electronics & Communication 49 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

34. Write an ALP to transfer the message “WELCOME” using serial port with a baud
rate of 9600.

ORG 0000H
MOV TMOD, #20H
MOV TH1, #0FDH (count for given baud rate)
MOV SCON, #50H
SETB TR1
MOV DPTR, #MESSG
NEXT: CLR A
MOVC A,@A+DPTR
JZ HERE
ACALL SEND
INC DPTR
SJMP NEXT
HERE: SJMP HERE

SEND : MOV SBUF, A


BACK: JNB TI, BACK
CLR TI
RET
END

MESSG: DB “WELCOME”, 0

RESULT:
Note: Check the result in serial window.
Change the baud rate and the message to note the second result.

Dept., of Electronics & Communication 50 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

VIVA QUESTIONS

1. Which devices are specifically being used for converting serial to parallel and from
parallel to serial respectively?
Registers
2. What is the difference between UART and USART communication?
One uses asynchronous means of communication and the other uses asynchronous and
synchronous means of communication
3. Which of the following best describes the use of framing in asynchronous means of
communication?
It tells us about the start and stops of the data to be transmitted or received
4. Which of the following is the logic level understood by the micro-controller/micro-
processor?
TTL logic level
5. What is a null modem connection?
The RxD of one is the TxD for the other
6. Which of the following best states the reason that why baud rate is mentioned in serial
communication?
To make the two devices compatible with each other, so that the transmission becomes
easy and error free

Dept., of Electronics & Communication 51 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

PART-B: INTERFACING:
1. Interface a simple toggle switch to 8051 and write an ALP to generate an
interrupt which switches on an LED (i) continuously as long as switch is on
and (ii) only once for a small time when the switch is turned on.

a).LED ON/OFF

ORG 000H
CLR A
MOV P1,0FFH
READSW: MOV A,P1 // Moving the port value to Accumulator.
;MOV A,R0
XRL A,#81H
JZ NXT // If switch 1 is OFF then jump to NXT to check if switch 2 is ON
CLR P0.0 // Turn ON LED because Switch 1 is ON
//MOV P0,#00H
CLR A
SJMP READSW // Read switch status again.
NXT:
SETB P0.0 // Turning OFF LED because Switch 2 is ON
//MOV P0,#0FFH
SJMP READSW // Jumping to READSW to read status of switch 1 again.
END

Dept., of Electronics & Communication 52 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

b) LED ON/OFF

ORG 000H
CLR A
MOV P1,0FFH
READSW: MOV A,P1 // Moving the port value to Accumulator.
;MOV A,R0
XRL A,#81H
JZ NXT // If switch 1 is OFF then jump to NXT to check if switch 2 is ON
//CLR P0.0 // Turn ON LED because Switch 1 is ON
MOV P0,#00H
CLR A
SJMP READSW // Read switch status again.
NXT:
//SETB P0.0 // Turning OFF LED because Switch 2 is ON
MOV P0,#0FFH
SJMP READSW // Jumping to READSW to read status of switch 1 again.

Dept., of Electronics & Communication 53 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

END
c) BLINK LED CONTINOUSLY
ORG 000H
CLR A
MOV P1,0FFH
READSW: MOV A,P1 // Moving the port value to Accumulator.
;MOV A,R0
XRL A,#81H
;CLR P0.0
JZ NXT // If switch 1 is OFF then jump to NXT to check if switch 2 is ON
CLR P0.0 // Turn ON LED because Switch 1 is ON
CLR A
SJMP READSW // Read switch status again.
NXT:
//ACALL DELAY
SETB P0.0 // Turning OFF LED because Switch 2 is ON
ACALL DELAY
CLR P0.0
//SJMP READSW // Jumping to READSW to read status of switch 1 again.
DELAY:
MOV R7,#0C8H
MOV TMOD,#01H
L2: MOV TH0,#0DBH
MOV TL0,#0FFH
SETB TCON.4
L3: JNB TCON.5,L3
CLR TCON.4
CLR TCON.5
DJNZ R7,L2
RET
END

Dept., of Electronics & Communication 54 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

d) BLINK LED ONCE

ORG 000H
CLR A
MOV P1,0FFH
READSW: MOV A,P1 // Moving the port value to Accumulator.
;MOV A,R0
XRL A,#81H
;CLR P0.0
JZ NXT // If switch 1 is OFF then jump to NXT to check if switch 2 is ON
CLR P0.0 // Turn ON LED because Switch 1 is ON
CLR A
SJMP READSW // Read switch status again.
NXT:
//ACALL DELAY
SETB P0.0 // Turning OFF LED because Switch 2 is ON
ACALL DELAY
CLR P0.0
RET
//SJMP READSW // Jumping to READSW to read status of switch 1 again.
DELAY:
MOV R7,#0FFH
MOV TMOD,#01H
L2: MOV TH0,#0DBH
MOV TL0,#0FFH
SETB TCON.4
L3: JNB TCON.5,L3
CLR TCON.4
CLR TCON.5
DJNZ R7,L2
RET
END

Dept., of Electronics & Communication 55 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

2. Write a C program to (i) transmit and (ii) to receive a set of characters serially
by interfacing 8051 to a terminal.

#include <REG51.h>
#include <stdio.h>
#include <intrins.h>
//#include "lcd.h"
main()
{
char chr; /* variable to hold characters in */
SCON = 0x52; /* mode 1, 8-bit uart, enable receiver */
TMOD = 0x20; /* timer 1, mode 2, 8-bit reload */
TH1 = 0xFD; /* reload value for 2400 baud *//* Setting TR1 will start the timer, and
serial communications */
TR1 = 1; /** Set the Transmit Interrupt flag to send the character in* the serial
buffer, clearing it for use by the program.*/
TI = 1;
while (1==1)
{ /* read the next character from the serial port */
chr = getchar(); /* send it back to the original sender */
//putchar("\n");
printf("\n");
putchar(chr);
//putchar("\n");
printf("\n"); //Print the digital value
//sendCharacter (chr);}
}
}

Dept., of Electronics & Communication 56 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

3.Write ALPs to generate waveforms using ADC interface.

********************* DUAL DAC INTERFACE ***************************


OBJECT : To demonstrate the working of Dual Dac Interface.

CONNECTIONS: Connect J1connector of Dual Dac Interface & J7connector of ESA


MCB51 with the 26pin FRC connector cable given by ESA.

OUTPUT : Obseve the output at Xout&Yout pins of interface. Press


P3.3(INT1*) >Triangular wave
P3.2(INT0*)->Square wave

//SIRI 02.09//
************************************************************************

/*Lcd Monitor Routines*/


#define lcdinit 0x0E267
#define lcdstatus 0x0E2D3
#define lcdstr 0x0E39E
#define gotoxy 0x0E380

ORG 0x00
LJMP 0x30
ORG 0x13 /* External inerrupt1 */
MOV R4,#0x00
LCALL DISPLAY
RETI /* Return from inerrupt1 */
ORG 0x03 /* External inerrupt0 */
MOV R4,#0xFF
LCALL DISPLAY
RETI /* Return from inerrupt0 */
ORG 0x30
LCALL lcdstatus /* Read LCD busy flag and wait */
LCALL lcdinit /* Initialize the LCD */
MOV IE,#0x85
MOV R4,#0x00
MOV P0,#0x00 /* PO & p1 as output */
MOV P1,#0x00
ACALL DISPLAY

START: CJNE R4,#0x00,SQUARE


/* Triangualr wave */
MOV A,#0x00
UP: MOV P0,A
MOV P1,A
INC A
CJNE A,#0xFF,UP
Dept., of Electronics & Communication 57 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

DOWN: MOV P0,A


MOV P1,A
DEC A
CJNE A,#0x00,DOWN
SJMP START
/* Square wave */
SQUARE: MOV TMOD,#0x01
MOV TL0,#0x8A
MOV TH0,#0xEA
MOV P0,#0x00
MOV P1,#0x00
ACALL DELAY
MOV TL0,#0x8A
MOV TH0,#0xEA
MOV P0,#0xFF
MOV P1,#0xFF
ACALL DELAY
SJMP START

DELAY: SETB TR0 /* Generate required delay using TIMER */


AGAIN: JNB TF0,AGAIN
CLR TR0
CLR TF0
RET

DISPLAY: MOV 0xF0,#0x00


MOV R0,#0x00
LCALL gotoxy
CJNE R4,#0x00,SQ
MOV DPTR,#MTRI /* Point to message MTRI */
DIS: LCALL lcdstr /* Display string on LCD */
RET
SQ: MOV DPTR,#MSQ /* Point to message MSQ */
sjmp DIS

MSQ: DB 'Square Wave ',0x00


MTRI: DB 'Triangular Wave',0x00
END

Dept., of Electronics & Communication 58 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

DUAL DAC INTERFACE

This is a Sample Program to demonstrate the working of Dual DAC Interface.


Connect the external DC power supplies +12V,-12V & GND to the pins marked as +12V,-12V
& GND respectively on the interface through the 4pin connector provided on the interface.
Connect J1connector of Dual Dac Interface & J7connector of ESA MCB 51 with the 26pin FRC
connector cable given by ESA.
Observe the output at Xout &Yout pins of the interface.
Press P3.3(INT1*) -> Triangular wave
P3.2(INT0*) -> Square wave
Also the wave form name will be displayed on LCD of ESA MCB 51 Board.

Dept., of Electronics & Communication 59 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

4.8051 LCD INTERFACE

LEDs (lights), interfaced with 8051 microcontroller, can be used to display binary numbers or
on/off states while Seven Segment Displays (SSD) can display digits but that's not enough.
What if you wanted to display a proper message consisting of numbers, letters, characters,
symbols e.t.c.? That's where LCD Screens are better. LCDs make it convenient to display
anything to the user.

LCD
Stands for Liquid Crystal Display and is definitely an output device. It comes in many popular
shapes and sizes but here is the one that you will most probably face at the beginner's level.

COMMON LCD
It is basically called 16x2 LCD because it has sixteen columns and two rows thus it can
display thirty-two (32) characters at a time (sixteen characters in each row). There are sixteen
hardware pins as shown.

LCD HARDWARE PINS

Dept., of Electronics & Communication 60 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

Number Pin Function


1 GND Ground
2 Vcc Power Source (+5V)
3 Vee Contrast Control
Register Select
4 RS 0=Command Register
1=Data Register
Read/Write
5 R/W 0=Write
1=Read
6 E Enable
D0 –
7-14 8-bit data pins
D7
15 BL+ Backlight Control
16 BL- Ground

LCD comes with two important registers :

1. Control Register Used for instructing LCD on what to do next. It's like talking to your
LCD using this register.
2. Data Register Used for displaying data on LCD.
Control Register is used for sending commands to LCD and when it's ready, only then you can
use Data Register to display your data on the LCD. In other words, first you have to talk to your
LCD (by sending commands) about what you want it to do and then send all the useful data for
display.
PINS
The RS (Register Select) control pin is used to select either command or data register as
described in the pin configuration table. Once selected, all data sent on the 8-bit data lines will
be latched to that register.

The R/W (Read/Write) control pin is used to determine the flow of data. You have to select

 Write Mode when you're sending something to the LCD (data or command)
 Read Mode when you're reading from the LCD

The E (Enable) control pin acts as a guard to allow exchange of data. This pin is very important
in this whole process and must be handled precisely. The following animation explains its
function

Dept., of Electronics & Communication 61 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

SENDING DATA TO LCD


It must be used in the following manner

 HIGH to LOW transition for Write Operation


 LOW to HIGH transition for Read Operation

COMMANDS

LCDs have some useful handful of commands that can be used for different operations. You can
check a comprehensive list of commands here. commands required to initialize an LCD because
they are fairly sufficient to get things done and they are as follows

Instruction Decimal Hexa-Decimal


Enable 8-bit interface, 2 lines, 5*7
56 0x38
Pixels
Display ON, Cursor Blinking 14 0x0E
Clear Screen 1 0x01

HARDWARE CONNECTIONS

Here is the optimized version of LCD hardware Connections. It takes the minimum possible
time and works most of the time

Dept., of Electronics & Communication 62 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

LCD Hardware Connections


However it is assumed that we are

 Always writing to LCD (R/W = 0)


 Constant Contrast (through 1K ohm resistor)
 Full Brightness (no resistor in the BL connections)

The rest of the pins are connected to microcontroller. For instance,


connect potentiometers to Vee or BL controls to control the Contrast or Brightness .

LCD INTERFACE CODE

In the Download Package below, it is included the Assembly Language Code as well as the C
language Code for the very same program. The LCD program code in a stepwise fashion. Let's
get started.

 For convenience, a unique name to each pin is given on the LCD


using #define directive so I can mention them easily.

Naming each pin using #define

Dept., of Electronics & Communication 63 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

 To differentiate between command and data , two separate functions. As you can see,
the only difference is that of using RS pin according to requirement.

LCD Command Function

LCD Data Function

 The delay function is a general one and can use it safely at this stage but remember that
it's not accurate.

Delay Function

 Here is main function

Dept., of Electronics & Communication 64 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

LCD Main Function

 To send a command to LCD, use the lcdcmd function with the command as input.
 To send data to LCD, use the lcddata function with the data as input.

The LCD has to be initialized using the appropriate commands as it can be seen in the main
function. Once initialized, you can send data for display. It is very important to note that the
LCD accepts only ASCII characters which means that every character you send for display must
be in ASCII. It can be checked here for a complete list of ASCII characters and their
corresponding values.

There are many ways in which you can send an ASCII character to LCD. For instance, you can
display A in the following ways

 x='A' lcddata ( x )
 x=65 //decimal value lcddata ( x )
 x=0x41 //hexa decimal value lcd data ( x )
 or directly as lcddata ( 'A' )

Where x can be of type char or int whether signed or unsigned.

As discussed above, the enable pin (e) is given a high-to-low transition for data transfer.

Here is the Proteus Simulation Diagram for LCD interface used a BUS in this simulation for
simplicity.

Dept., of Electronics & Communication 65 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

Proteus Simulation

Dept., of Electronics & Communication 66 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

16×2 LCD module.

16×2 LCD module is a very common type of LCD module that is used in 8051 based embedded
projects. It consists of 16 rows and 2 columns of 5×7 or 5×8 LCD dot matrices. The module
were are talking about here is type number JHD162A which is a very popular one. It is available
in a 16 pin package with back light, contrast adjustment function and each dot matrix has 5×8
dot resolution. The pin numbers, their name and corresponding functions are shown in the table
below.

Pin Name Function


No:
1 VSS This pin must be connected to the ground
2 VCC Positive supply voltage pin (5V DC)
3 VEE Contrast adjustment
4 RS Register selection
5 R/W Read or write
6 E Enable
7 DB0 Data
8 DB1 Data
9 DB2 Data
10 DB3 Data
11 DB4 Data
12 DB5 Data
13 DB6 Data
14 DB7 Data
15 LED+ Back light LED+
16 LED- Back light LED-

VEE pin is meant for adjusting the contrast of the LCD display and the contrast can be adjusted
by varying the voltage at this pin. This is done by connecting one end of a POT to the Vcc (5V),
other end to the Ground and connecting the center terminal (wiper) of of the POT to the VEE
pin.

The JHD162A has two built in registers namely data register and command register. Data
register is for placing the data to be displayed, and the command register is to place the
commands. The 16×2 LCD module has a set of commands each meant for doing a particular job
with the display. High logic at the RS pin will select the data register and Low logic at the RS
pin will select the command register. If we make the RS pin high and the put a data in the 8 bit
data line (DB0 to DB7), the LCD module will recognize it as a data to be displayed. If we make
RS pin low and put a data on the data line, the module will recognize it as a command.

Dept., of Electronics & Communication 67 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

R/W pin is meant for selecting between read and write modes. High level at this pin enables
read mode and low level at this pin enables write mode.

E pin is for enabling the module. A high to low transition at this pin will enable the module.

DB0 to DB7 are the data pins. The data to be displayed and the command instructions are
placed on these pins.

LED+ is the anode of the back light LED and this pin must be connected to Vcc through a
suitable series current limiting resistor. LED- is the cathode of the back light LED and this pin
must be connected to ground.

16×2 LCD module commands.

16×2 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.

Command Function
0F LCD ON, Cursor ON, Cursor Blinking ON
01 Clear screen
02 Return home
04 Decrement cursor
06 Increment cursor
0E Display ON ,Cursor blinking OFF
80 Force cursor to the beginning of 1st line
C0 Force cursor to the beginning of 2nd line
38 Use 2 lines and 5×7 matrix
83 Cursor line 1 position 3
3C Activate second line
08 Display OFF, Cursor OFF
C1 Jump to second line, position1
OC Display ON, Cursor OFF
C1 Jump to second line, position1
C2 Jump to second line, position2

LCD initialization.

The steps that has 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.
Dept., of Electronics & Communication 68 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

Sending data to the LCD.

The steps for sending data to the LCD module is given below. I have already said that 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.

Circuit diagram.

Interfacing 16×2 LCD module to 8051

The circuit diagram given above shows how to interface a 16×2 LCD module with AT89S1
microcontroller. Capacitor C3, resistor R3 and push button switch S1 forms the reset circuitry.
Ceramic capacitors C1,C2 and crystal X1 is related to the clock circuitry which produces the
system clock frequency. P1.0 to P1.7 pins of the microcontroller is connected to the DB0 to
DB7 pins of the module respectively and through this route the data goes to the LCD module.
P3.3, P3.4 and P3.5 are connected to the E, R/W, RS pins of the microcontroller and through
Dept., of Electronics & Communication 69 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

this route the control signals are transffered to the LCD module. Resistor R1 limits the current
through the back light LED and so do the back light intensity. POT R2 is used for adjusting the
contrast of the display. Program for interfacing LCD to 8051 microcontroller is shown below.

Write ALP to interface an LCD display and to display a message on it.

/*Program to initialize and display some characters on LCD*/


/*The program tells the user , how to initialize and display characters on LCD*/
/*Refer the user manual for details on the LCD interface , to find to which port it is connected*/

#define RS P3.7 *LCD Register select (0)- Instruction reg & (1) Data Reg*/
#define RW P3.6 /*Read (1) or Write (0)*/
#define E P3.5 /*LCD Enable*/

org 0h
sjmp 30h
org 30h
anl P3,#1fh /*Make P3 & P2 as output*/
anl P2,#00h
acall status /*Routine to read Busy Flag of LCD*/
acallinit /*Routine initialize the LCD*/
mov dptr,#msg /*Point to message to be displayed*/
disp: clr a /*clear acc*/
movca,@a+dptr /*Get the character to be displayed*/
jz here /*if it is last char terminate the routine*/
acalldat /*Routine to write into LCD display RAM*/
incdptr /*increment the pointer*/
mov a,dpl
cjne a,#10h,disp /*check for first line completion*/
mov a,#0c0h /*Select the second line of LCD*/
acall cmd
sjmp disp /* display again*/
here: sjmp $

init: /*LCD initialization Routine*/


mov a,#38h /* func set command word, selects 2 data lines, 2 disp
lines, font 5x7*/
acallcmd /*routine to write into command reg*/
mov a,#01h /*clear LCD display*/
acallcmd
mov a,#0ch /*Command, selects the Display ON & No cursor & Blink off*/
acall cmd
mov a,#80h /*Sets the display RAM address*/
acall cmd /*i.e. selects the first line*/
ret

cmd: nop /*1 cycle delay*/


Dept., of Electronics & Communication 70 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

clr RS /* Select Insruction Register, by making RS low*/


nop /*1 cycle delay for settling*/
clr RW /*Select LCD Write operation, by clearing RW*/
nop

mov P2,a /*move Command DATA into P2 , i.e. LCD Data BUS*/
nop
setbE /*Enable the LCD by making high */
nop /*1-Cycle Delay for enable pulse*/
clr E /*Disable the LCD*/
mov r2,#03h /*1500usec(approx) delay for data settling*/
acall delay
acall status /*Read the Busy flag of LCD & wait if busy*/
ret

dat: nop /*1-Cycle delay */


setb RS /* Select DATA register of LCD*/
nop
clr RW /*Select Read operation*/
nop
mov P2,a /*Move the DATA to P2*/
nop
setb E /*Enable LCD*/
nop
clr E /*Disable LCD*/
mov r2,#03h /*1500usec(approx) delay for LCD settling*/
acall delay
acall status /*Read Busy flag & wait if busy*/
ret
status: clr RS /*Select the Instruction register*/
nop
setb RW /*Select Read operation*/
nop
setbE /*Enable LCD*/
nop
mov a,P2 /*Read the LCD Data Bus*/
clr E /*Disable LCD*/
jb acc.7,$-4 /*Check Busy flag bit, if high loop back*/
ret

delay: mov r1,#0ffh /*0usec delay, when clock 11.0592MHz*/


djnz r1,$
djnz r2,delay
ret

org 100h
msg: db 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',00h
end
Dept., of Electronics & Communication 71 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

5.Interfacing stepper motor to 8051

Stepper motor.

Stepper motor is a brush less motor which converts electrical pulses into mechanical rotation.
As the name indicates it rotates in steps according to the input pulses. A stepper motor usually
have a number of field coils (phases) and a toothed rotor. The step size of the motor is
determined by the number of phases and the number of teeth on the rotor. Step size is the
angular displacement of the rotor in one step. If a stepper motor has 4 phases and 50 teeth, it
takes 50×4=200 steps to make one complete rotation. So step angle will be 360/200=1.8°.

The stepper motor we are using has 4 poles and a 1/64 reduction gear mechanism for increasing
torque. The step angle of the motor is 5.64°. But when considering the reduction gear, the step
angle of the output shaft is 5.64/64°. The internal schematic of the stepper motor is given below.

The stepper motor is rotated by switching individual phases ON for a given time one by one.
The sequence is given in the graph below.

Dept., of Electronics & Communication 72 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

Circuit diagram.

The circuit diagram for interfacing stepper motor to 8051 is shown above. P1.0, P1.1,
P1.2 and P1.3 pins are used for controlling the phases A1, A2, A3 and A4 of the stepper motor
respectively. ULN2003 is used for driving the individual phases of the stepper motor.
ULN2003 is a darlington transistor array used for driving high current loads such as relays and
motors. ULN2003 has 8 individual channels each with 1A capacity. The channels can be
paralleled to increase the current capacity. Each channels are fitted with individual freewheeling
diodes. The ULN2003 is operated in current sinking mode. Each channel is activated by giving
a logic LOW at the corresponding input. For example if we make pin 1 of ULN2003 LOW,
phase A1 of the stepper motor gets switched ON.

Program.

OBJECT :To demonstrate the working of Stepper Motor Interface.

CONNECTIONS : Connect J1connector of Stepper Motor Interface & J7connector of ESA


MCB 51 with the 26pin FRC connector cable given by ESA.

OUTPUT :Initially motor will rotate in clock wise direction,


To change direction Press,
P3.3(INT1*) -> Clockwise direction
P3.2(INT0*) -> Anticlockwise direction

//SIRI 02.09// SELECT THE DEVICE “89C51RDXX”


Dept., of Electronics & Communication 73 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

*****************************************************************************
/*Lcd Monitor Routines*/
#define lcdinit 0x0E267
#define lcdstatus 0x0E2D3
#define lcdstr 0x0E39E
#define gotoxy 0x0E380

ORG 0x00
LJMP 0x30
ORG 0x13 /* External inerrupt1 */
MOV R4,#0x00
RETI /* Return from inerrupt1 */
ORG 0x03 /* External inerrupt0 */
MOV R4,#0xFF
RETI /* Return from inerrupt0 */

ORG 0x30
LCALL lcdstatus /* Read LCD busy flag and wait */
LCALL lcdinit /* Initialize the LCD */
MOV IE,#0x85
MOV R4,#0x00
MOV P0,#0x00 /* PO as output */

START: CJNE R4,#0x00,ACLK


CLK: MOV 0xF0,#0x00
MOV R0,#0x00
LCALL gotoxy
MOV DPTR,#mclk /* Point to message mclk */
LCALL lcdstr /* Display string on LCD */
MOV R5,#0x05
MOV A,#0x11 /* Data to activate one winding at a time */
LOP: MOV P0,A /* Move it to P0 */
ACALL DELAY /* Delay */
ACALL DELAY
RR A * Rotate the bit pattern*/
DJNZ R5,LOP
SJMP START

ACLK: MOV 0xF0,#0x00


MOV R0,#0x00
LCALL gotoxy
MOV DPTR,#maclk /* Point to message maclk */
LCALL lcdstr /* Display string on LCD */
MOV R5,#0x05 /* PO as output */
MOV A,#0x88 /* Data to activate one winding at a time */

LOOP1: MOV P0,A /* Move it to P0 */


Dept., of Electronics & Communication 74 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

ACALL DELAY /* Delay */


ACALL DELAY
RL A /* Rotate the bit pattern */
DJNZ R5,LOOP1
SJMP START
DELAY: MOV R1,#0x07
DECR: MOV R2,#0xFF
DJNZ R2,$
DJNZ R1,DECR
RET
mclk: DB 'Clock Wise ',0x00
maclk: DB 'Anticlock Wise',0x00
END

~ STEPPER MOTOR INTERFACE ~

-> This is a Sample Program to demonstrate the working of Stepper Motor Interface.

-> Plug in 4pin polarised connector of the motor to interface & 3 pin connector of the motor
to the 3 pin connector of interface marked as "WHT BLK".

-> Connect the 3 pin female connector of the stepper motor power supply to the connector on
the interface marked as "GND +5/12V".

-> Connect J1connector of Stepper Motor Interface & J7connector of ESA MCB 51 with the
26pin
FRC connector cable given by ESA.

-> Initially the motor will rotate in Clock wise direction.

-> If you want to change the direction press,


P3.3(INT1*) -> Clockwise direction
P3.2(INT0*) -> Anticlockwise direction

-> The direction of the motor will be displayed on LCD of ESA MCB 51 Board.

Dept., of Electronics & Communication 75 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

3. ADC 0804.

ADC0804 is an 8 bit successive approximation analogue to digital converter from National


semiconductors. The features of ADC0804 are differential analogue voltage inputs, 0-5V input
voltage range, no zero adjustment, built in clock generator, reference voltage can be externally
adjusted to convert smaller analogue voltage span to 8 bit resolution etc. The pin out diagram of
ADC0804 is shown in the figure below.

ADC0804 pinout

The voltage at Vref/2 (pin9) of ADC0804 can be externally adjusted to convert smaller input
voltage spans to full 8 bit resolution. Vref/2 (pin9) left open means input voltage span is 0-5V
and step size is 5/255=19.6V. Have a look at the table below for different Vref/2 voltages and
corresponding analogue input voltage spans.

Vref/2 (pin9) (volts) Input voltage span (volts) Step size (mV)
Left open 0–5 5/255 = 19.6
2 0–4 4/255 = 15.69
1.5 0–3 3/255 = 11.76
1.28 0 – 2.56 2.56/255 = 10.04
1.0 0–2 2/255 = 7.84
0.5 0–1 1/255 = 3.92

Dept., of Electronics & Communication 76 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

6. Write ALP to interface ADC-0804 and convert an analog input


connected to it
 Make CS=0 and send a low to high pulse to WR pin to start the conversion.
 Now keep checking the INTR pin. INTR will be 1 if conversion is not finished and
INTR will be 0 if conversion is finished.
 If conversion is not finished (INTR=1) , poll until it is finished.
 If conversion is finished (INTR=0), go to the next step.
 Make CS=0 and send a high to low pulse to RD pin to read the data from the ADC.

Circuit diagram.

Interfacing ADC to 8051

The figure above shows the schematic for interfacing ADC0804 to 8051. The circuit initiates
the ADC to convert a given analogue input , then accepts the corresponding digital data and
displays it on the LED array connected at P0. For example, if the analogue input voltage Vin is
5V then all LEDs will glow indicating 11111111 in binary which is the equivalent of 255 in
decimal. AT89s51 is the microcontroller used here. Data out pins (D0 to D7) of the ADC0804
Dept., of Electronics & Communication 77 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

are connected to the port pins P1.0 to P1.7 respectively. LEDs D1 to D8 are connected to the
port pins P0.0 to P0.7 respectively. Resistors R1 to R8 are current limiting resistors. In simple
words P1 of the microcontroller is the input port and P0 is the output port. Control signals for
the ADC (INTR, WR, RD and CS) are available at port pins P3.4 to P3.7 respectively. Resistor
R9 and capacitor C1 are associated with the internal clock circuitry of the ADC. Preset resistor
R10 forms a voltage divider which can be used to apply a particular input analogue voltage to
the ADC. Push button S1, resistor R11 and capacitor C4 forms a debouncing reset mechanism.
Crystal X1 and capacitors C2,C3 are associated with the clock circuitry of the microcontroller.

Program.

Write ALP to interface ADC-0804 and convert an analog input connected to it.

***********************16 CHANNEL ADC INTERFACE *****************

OBJECT :To demonstrate the working of 16Channel ADC Interface.

CONNECTIONS : Connect J1connector of 16Channel Adc Interface & J7connector


of ESA MCB 51 with the 26pin FRC connector cable given by
ESA.

OUTPUT : Press ',' to Increment the Channel number from PC keyboard.


Press '-' to Decrement the Channel number from PC keyboard.
Observe the output on Serial Window of KEIL or on
Hyperterminal.

//SIRI 02.09//
*************************************************************************

#define hex2ascii 0x0E3B0


#define delay 0x0E2EE

ORG 0x00
SJMP 0x30

ORG 0x30

/* serial mode initialization */


MOV TMOD,#0x20 /* Timer1 mode2 */
MOV TH1,#-3 /* To set baud rate at 9600 */
MOV SCON,#0x50 /* 8 Bit , 1 stop, ren enabled */
SETB TR1

MOV P0,#0x00 /* Initialize P0 as output & P1 &p2 as input*/


MOV P1,#0xFF

Dept., of Electronics & Communication 78 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

MOV P2,#0xFF
MOV R3,#0x00
NXTCHANL: LCALL CONVERT
LCALL DISPLAY
LCALL UPDATE
LJMP NXTCHANL

CONVERT: MOV A,#0x00


ORL A,R3
MOV P0,A

MOV A,#0x20
ORL A,R3
MOV P0,A

MOV A,#0x00
ORL A,R3
MOV P0,A

WAIT1: MOV A,P2


CJNE A,#0x00,D2
SJMP WAIT1
D2: NOP

WAIT2: MOV A,P2


ANL A,#0x01
JZ WAIT2

D3: MOV A,#0x40


ORL A,R3
MOV P0,A
MOV A,P1
PUSH 0xE0
MOV A,#0x00
ORL A,R3
MOV P0,A
POP 0xE0
RET

DISPLAY: PUSH 0xE0


MOV DPTR,#CHANL
LCALL DISPSTR

MOV A,R3
CLR C
DA A
MOV R4,A
LCALL LL1
Dept., of Electronics & Communication 79 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

MOV DPTR,#DIGITAL
CLR PSW.5
LCALL DISPSTR

POP 0xE0
MOV R4,A
LCALL LL1

MOV DPTR,#CR
LCALL DISPSTR
RET

UPDATE:
S2: LCALL GETCH /* get the key */
LCALL PUTCH /* display the pressed key */
CJNE A,#0x2C,S1 /* check whether it is ',' */
SJMP CHINR

S1: CJNE A,#0x2D,NXTCHANL /* check whether it is '-' */


SJMP CHDCR

CHDCR: MOV A,R3


CJNE A,#0x00,UP
SJMP CHAN15

UP: DEC R3
RET

CHINR: MOV A,R3


CJNE A,#15,DOWN
SJMP CHAN0

DOWN: INC R3
RET

CHAN0: MOV R3,#0x00


RET

CHAN15: MOV R3,#15


RET

DISPSTR: CLR A /* Routine to display a string */


MOVC A,@A+DPTR
JZ FINISH
MOV SBUF,A/* Transfer the letter in Accumulator to UART*/

Dept., of Electronics & Communication 80 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

HERE: JNB TI,HERE /* Wait for the last bit */


CLR TI /* clear TI for next character */
INC DPTR
SJMP DISPSTR
FINISH: RET

DIGIT: MOV SBUF,A /* Transfer the letter in Accumulator to


UART */
LOP: JNB TI,LOP /* Wait for the last bit */
CLR TI
RET

LL1: SWAP A /* Swap to get the highernibble into lower nibble*/


LCALL hex2ascii /* Convert higher nibble to ASCII*/
LCALL DIGIT
MOV A,R4
LCALL hex2ascii /*convert lower nibble to ASCII*/
LCALL DIGIT
RET

GETCH: CLR RI \* Routine to get a key serially */


RPT: JNB RI,RPT
MOV A,SBUF
RET

PUTCH: CLR TI /* Routine to display the key */


MOV SBUF,A
RPT1: JNB TI,RPT1
CLR TI
RET

CHANL: DB 0x0A,0x0D,"CHANNEL NUMBER = ",0x00


DIGITAL: DB " DIGITAL VALUE = ",0x00
CR: DB 0x0A,0x0D,"",0x00
END

~ 16 CHANNEL ADC INTERFACE ~

-> This is a Sample Program to demonstrate the working of 16 Channel ADC Interface.

-> Connect the external DC power supplies +12V & GND to the pins marked as +12V & GND

respectively on the interface through the connector provided on the interface.

-> Connect J1connector of 16 Channel ADC Interface & J7connector of ESA MCB 51 with the
26pin FRC connector cable given by ESA.

Dept., of Electronics & Communication 81 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

-> Apply Analog input at the screw terminals(IN0 - IN15).

-> The euqivalent digital value for the applyed analog input will be displayed on
Serial Window of KEIL or HyperTerminal of PC as shown below:

CHANNEL NUMBER = ## DIGITAL VALUE = ##

-> Press ',' to Increment the Channel number from PC keyboard.


Press '-' to Decrement the channel number from PC keyboard.

NOTE: Dont apply input at screw( named as GND) terminals on the interface.

VIVA QUESTIONS
1. How many rows and columns are present in a 16*2 alphanumeric LCD?
rows=2, columns=16
2. How many data lines are there in a 16*2 alphanumeric LCD?
8
3. For writing commands on an LCD, RS bit is
Reset
4. Mention the steps to detect the key in a 4*4 keyboard matrix about the key that is being
pressed?
Masking of bits, ensuring that initially, all keys are open and checking that whether the key is
actually pressed or not
5. To detect that in which column, the key is placed?
We can mask the bits and then check it , we can rotate the bits and then check that particular bit
which is set or reset(according to the particular condition)
6. To identify that which key is being pressed, we need to
Ground pins of the port one at a time
7. Why two pins for ground are available in ADC0804?
For controlling the analog and the digital pins of the controller. There are two pins, that are
available for ground in the ADC0804. one is for analog and the other is for digital pins
8. Step size is selected by which two bits?
Vref/2. Step size is selected by the Vref/2 bit as its value is Vref/256.
9. INTR, WR signal is an input/output signal pin?
One is input and the other is output. INTR pin tells about the end of the conversion (output) and
WR pin tells us to start the conversion (input).
10. Why MOVX instruction is being used to access the ports of the 8255?
8255 is used to access the memory mapped I/O. MOVX instruction is used to access the external
memory, so it is used to access the memory mapped I/O.

Dept., of Electronics & Communication 82 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

Viva Questions

1. List few differences between a microcontroller and a microprocessor.


2. Why ROM less versions of microcontrollers exist.
3. Name the ways to speed up digital computers.
4. What is the size of internal RAM?
5. What is the size of internal ROM in 8031.
6. What is the Execution time of a single cycle instruction for a 6MHz crystal.
7. Name few registers that can do division.
8. Name the ports used for external memory access.
9. What is the address of the stack when 8051 is reset.
10. How many register banks are available in 8051.
11. How may I/O pin can be both input and output.
12. Which bits in which register must be set to give the serial data interrupt the highest
priority.
13. Why a low address byte latch for external memory is needed.
14. What is the baud rate for the serial port in mode 0 for a 6MHz crystal.
15. Which bits in which register must be set to make INTO level generated interrupts.
16. Name the register containing GFO and GF1.
17. What is the address of PCON register.
18. What is the address of the interrupt program for the INTO level generated interrupt.
19. Which bits in which registers must be set to have timer 0 count input pulses on pin TO
in timer mode.
20. Name 2 conditions under which program opcodes are fetched from external rather than
internal memory.
21. Name the signal that reads external ROM
22. When used in multiprocessing which bit in which register is used by a transmitting 8051
to signal receiving 8051s that an interrupt should be generated.
23. What is instruction syntax?
24. What is the significance of a testing program?
25. Why are high level languages machine independent.
26. What is software?
27. Why are high level languages machine independent.
28. Write an instruction sequence to rotate DPTR one place left.
29. What is the difference between signed and unsigned operations?
30. How do you perform signed subtraction
31. Why mode 0 not suitable for 8051 communications.
32. How is memory organized in 8051?
33. Write the interrupt priority sequence.
34. What are the flags stored in PSW.
35. Which register holds the serial data interrupt bits T1 and R1.
36. What are the bits that determine timer modes and the register that holds these bits.
37. What are the different ports of 8051?
38. What is the advantage in connecting external memory to 8051?
39. What is TCON register
40. What is SCON register?
Dept., of Electronics & Communication 83 S JBIT, B’lore
IV Sem E&C Microcontrollers lab[18ECL47]

41. What is PCON register?


42. Name the different Time modes of operation
43. What is SCON register?
44. How is Transmission and reception of data done in 8051
45. When is it required use the control signals WR and RD?
46. How do you compare the flash PROM, EPROM & ROM versions of 8051
microcontrollers why selecting device for product device?
47. What are the constraints on frequency counting, pulse width measurements using
89C2051?
48. What is power-up of reset and what is watch dog timer reset?
49. Compare b/w LJMP, AJMP & SJMP?
50. Compare b/w LCALL, ACALL & SCALL?
51. Define addressing mode. List them?
52. How RETI & RET instruction differ.
53. Why is the coding done in assembly language of being written in the machine codes
directly by a programmer?
54. What are the different components in an instruction?
55. How do you compare program, routine and interrupt service routine?
56. What are the limitation in designing frequency counter using 89C51 as per as the
operating frequency concerned?
57. List the instructions of 8051 in which use of accumulator and not any register for the
accumulating the result must?

Dept., of Electronics & Communication 84 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]
||Jai Sri Gurudev||
Sri AdichunchanagiriShikshana Trust ®

S J B Institute of Technology
No. 67, BGS Health & Education City, Dr. VishnuvardhanRoadKengeri,Bengaluru -560060
Department of Electronics & Communication Engg.
COURSE EXIT SURVEY:2019-20

Student Name: _________________________ USN: _____________


Semester: IV Subject Name:Microcontroller Lab Subject Code: 18ECL47
Faculty Members Name:
Dr Ravi Kumar A V, Sowmya B J, Uma S, Anushree R, Rahul R Rai
Dear Student,
Congratulations for completing this course / subject. Please provide your prompt opinion on the
potentiality of the course in terms of knowledge gained by you
Please rate attainment level appropriately as applicable in the below Form.
Attainment Level: 1 to 5 (1-Poor, 2-Satisfactory, 3-Good, 4-Very Good and 5-Excellent)
Sl. CO Level of
Description PO Mapped
No. Mapped attainment
Demonstrate ability to handle data
transfer, arithmetic operations,
1 CO1 counters and Boolean and logical PO1,PO2,PO5,PSO1
instructions using assembly
language programming
Understand and design of
experiments using instructions call
2 CO2 PO1,PO2,PO5,PSO1
and return, code conversion , delay
programs and serial port programs
Interface different input and output
devices to 8051 and control them PO1, PO2,
3 CO3
using Assembly language PO3,PO5,PSO1
programs
Interface the serial devices to 8051
4 CO4 and do the serial transfer using C PO1,PO2,PO3,PO5,PSO1
programming.

Signature of Student

Dept., of Electronics & Communication 85 S JBIT, B’lore


IV Sem E&C Microcontrollers lab[18ECL47]

||Jai Sri Gurudev||


Sri AdichunchanagiriShikshana Trust(R)

SJB Institute of Technology


No. 67, BGS Health & Education City, Dr. Vishnuvardhan Road
Kengeri, Bangalore – 560 060

Department of Electronics & Communication


Engineering

LABORATORY USAGE AGREEMENT:


I have read and agree to observe all laboratory safety rules. I understand these rules are to

ensure my own safety, and that of my fellow students, instructors, and staff members. I will

cooperate to the fullest extent to maintain a safe laboratory environment. I will follow all

written and oral instructions provided by my instructor. I am aware any violation of these rules

may result in not being allowed to participate in the laboratory experiment, receiving a failing

grade, and/or dismissal from the course.

Student Name:

USN:

Section: Batch:

Signature and Date:_____________________________________________

Dept., of Electronics & Communication 86 S JBIT, B’lore

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy