0% found this document useful (0 votes)
98 views36 pages

Lab Manual MIA

The document outlines a list of experiments for programming the 8051 microcontroller using Assembly and C, detailing various tasks such as arithmetic operations, interfacing with hardware, and implementing communication protocols. Each experiment includes an aim, required components, and example programs demonstrating the use of assembly language for specific functions like addition, subtraction, multiplication, and division. Additionally, it provides insights into the architecture and features of the 8051 microcontroller.

Uploaded by

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

Lab Manual MIA

The document outlines a list of experiments for programming the 8051 microcontroller using Assembly and C, detailing various tasks such as arithmetic operations, interfacing with hardware, and implementing communication protocols. Each experiment includes an aim, required components, and example programs demonstrating the use of assembly language for specific functions like addition, subtraction, multiplication, and division. Additionally, it provides insights into the architecture and features of the 8051 microcontroller.

Uploaded by

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

SUBJECT CODE - 2021406

NEW GOVERNMENT POYTECHNIC


PATNA - 13
LIST OF EXPERIMENTS

1. Programming 8051 Microcontroller using ASM and C, and


implementation in Flash 8051 Microcontroller.
2. Programming with Arithmetic logic instructions [ Assembly
and C ]
3. Program using constructs (Sorting an array) [Assembly and C ]
4. Programming using Ports [Assembly and C ]
5. Delay generation using Timer [Assembly and C]
6. Programming Interrupts [Assembly and C]
7. Implementation of standard UART communication (using
hyper terminal) [Assembly and C].
8. Interfacing LCD Display [Assembly and C]
9. Interfacing with Keypad [Assembly and C]
10. Programming ADC/DAC [Assembly and C]
11. Interfacing with stepper motor [Assembly and C]
12. Pulse Width Modulation [Assembly and C]
13. Interfacing with DC motor [Assembly and C]

1
EXPERIMENT 1
PROGRAMMING 8051 MICROCONTROLLER USING ASM AND C, AND
IMPLEMENTATION IN FLASH 8051 MICROCONTROLLER.

AIM: Programming 8051 microcontroller using asm and c, and implementation in flash 8051
microcontroller.

COMPONENTS REQUIRED:
 8051 Microcontroller Kit

THEORY:

The assembly language is a low-level programming language used to write program code in terms
of mnemonics. Even though there are many high-level languages that are currently in demand,
assembly programming language is popularly used in many applications .It can be used for direct
hardware manipulations. It is also used to write the 8051 programming code efficiently with less
number of clockcycles by consuming less memory compared to the other high-level languages.

8051 Programming in Assembly Language

The assembly language is a fully hardware related programming language. The embedded
designers must have sufficient knowledge on hardware of particular processor or controllers
before writing the program. The assembly language is developed by mnemonics; therefore, users
cannot understand it easily to modify the program.

Assembly programming language is developed by various compilers and the “keiluvison” is best
suitable for microcontroller programming development. Microcontrollers or processors can

2
understand only binary language in the form of ‘0s or 1s’. An assembler converts the assembly
language to binary language, and then stores it in the microcontroller memory to perform the
specific task.

1. Features of 8051 Microcontroller

An 8051 microcontroller comes bundled with the following features.

 64K bytes on-chip program memory (ROM)

 128 bytes on-chip data memory (RAM)

 Four register banks

 128 user defined software flags

 8-bit bidirectional data bus

 16-bit unidirectional address bus

 32 general purpose registers each of 8-bit

 16 bit Timers (usually 2, but may have more or less)

 Three internal and two external Interrupts

 Four 8-bit ports,(short model have two 8-bit ports)

 16-bit program counter and data pointer

3
2. Architecture of 8051 Microcontroller

Figure shows the architecture block diagram of 8051.

4
1. Pin Diagram of 8051 Microcontroller

The EA' (External Access) pin is used to control the internal or external memory access. The signal
0 is for external memory access and signal 1 for internal memory access. The PSEN' (Program
Store Enable) is for reading external code memory when it is low (0) and EA is also 0. The ALE
(Address Latch Enable) activates the port 0 joined with port 2 to provide 16 bit external address bus
to access the external memory. The ALE multiplexes the P0: 1 for latching address on P0 as A0-A7
in the 16 bit address buss, 0 for latching P0 as data I/O. P0.x is named ADx because P0 is
multiplexed for Address bus and Data bus at different clock time. WR' provides the signal to write
external data memory RD' provides the signal to read external data and code memory.

 PORT P1 (Pins 1 to 8): The port P1 is a port dedicated for general I/O purpose. The other ports
P0, P2 and P3 have dual roles in addition to their basic I/O function.

5
 PORT P0 (pins 32 to 39): When the external memory access is required then Port P0 is
multiplexed for address bus and data bus that can be used to access external memory in
conjunction with port P2. P0 acts as A0-A7 in address bus and D0-D7 for port data. It can be
used for general purpose I/O if no external memory presents.

 PORT P2 (pins 21 to 28): Similar to P0, the port P2 can also play a role (A8-A15) in the address
bus in conjunction with PORT P0 to access external memory.

 PORT P3 (Pins 10 to 17): In addition to acting as a normal I/O port,

 P3.0 can be used for serial receive input pin(RXD) • P3.1 can be used for serial

 transmit output pin(TXD) in a serial port,

 P3.2 and P3.3 can be used as external interrupt pins(INT0’ and INT1’),

 P3.4 and P3.5 are used for external counter input pins(T0 and T1),

 P3.6 and P3.7 can be used as external data memory write and read control signal

 Pins (WR’ and RD’) read and write pins for memory access.

6
EXPERIMENT 2
PROGRAMMING WITH ARITHMETIC LOGIC INSTRUCTIONS
[ASSEMBLY AND C]

AIM: - 2. (a). Write an ALP to find addition of two 8 bit numbers.

COMPONENTS REQUIRED:
 8051 Microcontroller Kit

PROGRAM:
MOV R0,#51H // Initialize input1 memory pointer
MOV R1,#61H /* Initialize input2 memory pointer and store output also same */
MOV R2,#02H // Initialize iteration count
CLR C
BACK: MOV A,@R0 /*Get lower bytes data in first iteration, upper bytes data in
second iteration, add them with carry and store in memory
pointer2.*/
ADDC A,@R1
MOV @R1,A
DEC R0 // Increment memory pointer1 & 2 to get upper bytes
DEC R1
DJNZ R2,BACK /* Decrement iteration count and if it is not zero, go to relative
address and repeat the same process until count become zero.*/
JNC FINISH
MOV
@R1,#01H
FINISH:SJMP $
END

MEMORY WINDOW
Before execution:
D:0x50H: FD 07 00 00 00 00
D:0x60H: FF 5F 00 00 00 00
After execution:
D:0x50H: FD 07 00 00 00 00
D:0x5FH: 01 FC 66 00 00 00

7
2. (b). Write an ALP to perform 16 bit subtraction
PROGRAM:
MOV R0,#51H //Initialize input1 memory pointer
MOV R1,#61H /* Initialize input2 memory pointer and store output also same */
MOV R2,#02H // Initialize iteration count
CLR C
BACK: MOV A,@R0 //Get lower bytes data in first iteration, upper bytes data in second
iteration, add them with carry and store in memory pointer2.
SUBB A,@R1
MOV @R1,A
DEC R0 // Increment memory pointer1 & 2 to get upper bytes
DEC R1
DJNZ R2,BACK /* Decrement iteration count and if it is not zero, go to relative
address and repeat the same process until count become zero.*/
JNC POSITIVE
MOV
@R1,#0FFH JMP
FINISH
POSITIVE: MOV @R1,#00H
FINISH: SJMP $
END

Eg. FAF4 - 02F5 = F7FF (ANSWER IS POSITIVE)


MEMORY WINDOW
Before execution:
D:0x50H: FA F4 00 00 00 00
D:0X60H: 02 F5 00 00 00 00
After execution:
D:0x50H: FA F4 00 00 00 00
D:0X60H: F7 FF 00 00 00 00

Eg. 0025 – 0AF6 = FFF52F (ANSWER IS NEGATIVE)


Before execution:
D:0x50H: 00 25 00 00 00 00
D:0X60H: 0A F6 00 00 00 00

8
After execution:
D:0x50H: 00 25 00 00 00 00
D:0x5FH: FF F5 2F 00 00 00

2. (c). Write an ALP to perform multiplication (16-bit by 16-bit)


First number will be in R6 and R7 while second number will be in R4 and R5. The result will
be in R0, R1, R2 and R3.
Eg:
R6 R7 = 15 FD
R4 R5 = A2 4B
R0 R1 R2 R3 = 0D F0 8B 1F
PROGRAM:
MOV R6,#0FFH //FFFF X FFFF = FFFE 0001
MOV R7,#0FFH // input the multiplicand
MOV R4,#0FFH // input the multiplier
MOV R5,#0FFH
//Multiply R5 by R7
MOV A,R5 // Move the R5 into the Accumulator
MOV B,R7 // Move R7 into B
MUL AB // Multiply the two values
MOV R2,B // Move B (the high-byte) into R2
MOV R3,A // Move A (the low-byte) into R3
//Multiply R5 by R6
MOV A,R5 // Move R5 back into the Accumulator
MOV B,R6 // Move R6 into B
MUL AB // Multiply the two values
ADD A,R2 // Add the low-byte into the value already in R2
MOV R2,A // Move the resulting value back into R2
MOV A,B // Move the high-byte into the accumulator
ADDC A,#00h // Add zero (plus the carry, if any)
MOV R1,A // Move the resulting answer into R1
MOV A,#00h // Load the accumulator with zero
ADDC A,#00h // Add zero (plus the carry, if any)
MOV R0,A // Move the resulting answer to R0.

9
//Multiply R4 by R7
MOV A,R4 // Move R4 into the Accumulator
MOV B,R7 // Move R7 into B
MUL AB // Multiply the two values
ADD A,R2 // Add the low-byte into the value already in R2
MOV R2,A // Move the resulting value back into R2
MOV A,B // Move the high-byte into the accumulator
ADDC A,R1 // Add the current value of R1 (plus any carry)
MOV R1,A // Move the resulting answer into R1.
MOV A,#00h // Load the accumulator with zero
ADDC A,R0 // Add the current value of R0 (plus any carry)
MOV R0,A // Move the resulting answer to R1.
//Multiply R4 by R6
MOV A,R4 // Move R4 back into the Accumulator
MOV B,R6 // Move R6 into B
MUL AB // Multiply the two values
ADD A,R1 // Add the low-byte into the value already in R1
MOV R1,A // Move the resulting value back into R1
MOV A,B // Move the high-byte into the accumulator
ADDC A,R0 // Add it to the value already in R0 (plus any carry)
MOV R0,A // Move the resulting answer back to R0
// answer is now in R0, R1, R2, and R3
SJMP $
END
RESULT
REGISTER VALUES:
R6 R7 = FF FF
R4 R5 = FF FF
R0 R1 R2 R3 = FF FE 00 01

2. (d). Write an ALP to perform division (16 -bit by 16-bit)

First number will be in R1 and R0 while second number will be in R3 and R2. The result will be in
R2 and R3. .
Eg:

10
R1 R0 = D7 4E
R3 R2 = 00 D9
R3 R2 = 00 FE PROGRAM:
MOV R1,0D7H
MOV R0,4EH
MOV R3,00H
MOV R2,0D9H
div16_16:
CLR C // Clear carry initially
MOV R4,#00h // Clear R4 working variable initially
MOV R5,#00h // Clear R5 working variable initially
MOV B,#00h /* Clear B since B will count the number of left-shifted bits*/ div1:
INC B // Increment counter for each left shift
MOV A,R2 // Move the current divisor low byte into the accumulator
RLC A /* Shift low-byte left, rotate through carry to apply highest bit to high-byte*/
MOV R2,A // Save the updated divisor low-byte
MOV A,R3 /* Move the current divisor high byte into the accumulator*/
RLC A // Shift high-byte left high, rotating in carry from low-byte
MOV R3,A // Save the updated divisor high-byte
JNC div1 // Repeat until carry flag is set from high-byte
div2: // Shift right the divisor
MOV A,R3 // Move high-byte of divisor into accumulator
RRC A // Rotate high-byte of divisor right and into carry
MOV R3,A // Save updated value of high-byte of divisor
MOV A,R2 // Move low-byte of divisor into accumulator
RRC A // Rotate low-byte of divisor right, with carry from high-byte
MOV R2,A // Save updated value of low-byte of divisor
CLR C // Clear carry, we don't need it anymore
MOV 07h,R1 // Make a safe copy of the dividend high-byte
MOV 06h,R0 // Make a safe copy of the dividend low-byte
MOV A,R0 // Move low-byte of dividend into accumulator
SUBB A,R2 /* Dividend - shifted divisor = result bit (no factor, only 0 or 1)*/

MOV R0,A // Save updated dividend


MOV A,R1 // Move high-byte of dividend into accumulator

11
SUBB A,R3 /*Subtract high-byte of divisor (all together 16-bit subtraction)*/
MOV R1,A // Save updated high-byte back in high-byte of divisor
JNC div3 // If carry flag is NOT set, result is 1
MOV R1,07h /* Otherwise result is 0, save copy of divisor to undo subtraction*/
MOV R0,06h
div3:
CPL C // Invert carry, so it can be directly copied into result
MOV A,R4
RLC A // Shift carry flag into temporary result
MOV R4,A
MOV A,R5
RLC A
MOV R5,A
DJNZ B,div2 // Now count backwards and repeat until "B" is zero MOV
R3,05h // Move result to R3/R2
MOV R2,04h // Move result to R3/R2
SJMP $
END
RESULT
REGISTER VALUES:
R1 R0 = D7 FE // D7FE/D9 = FE
R3 R2 = 00 D9
R3 R2 = 00 FE

12
EXPERIMENT 3
PROGRAM USING CONSTRUCTS (SORTING AN ARRAY)
[ASSEMBLY AND C]
AIM: To arrange n 8-bit numbers in ascending order.
COMPONENTS REQUIRED:
 8051 Microcontroller Kit

PROGRAM:
MOV R2, #05H // Initialize the iteration counter
DEC R2 // Decrement the iteration count
BACK1: MOV R0, #50H // Initialize memory pointer1
MOV R1, #51H // Initialize memory pointer2
MOV A, R2 // Store outer loop count
MOV R3, A // Store inner loop count
BACK: MOV A,@R0 // Get the data from memory pointer1
MOV B,@R1 // Get the data from memory pointer2
CJNE A, B, LOOP /* Compare if not equal go to relative address (LOOP)*/
LOOP: JC LOOP1 /* If carry generates, go to relative address (LOOP1)*/
MOV @R0,B // Exchange the data in memory pointer
MOV @R1, A
LOOP1: INC R0 // Increment the memory pointer1
INC R1 // Increment the memory pointer2
DJNZ R3, BACK // Decrement inner loop count if not zero go to back
DJNZ R2, BACK1 // Decrement outer loop count if not zero go to back1
END

MEMORY WINDOW:
Before execution:
D:0x50H: 06 04 03 07 02 01
After execution:
D:0x50H: 01 02 03 04 06 07

13
EXPERIMENT 4
Programming using Ports [Assembly and C]
AIM: Two understand the basics of serial port communication
COMPONENTS REQUIRED:
 8051 Microcontroller Kit

PROGRAM:
CR EQU 0DH //CLEAR LINE
LF EQU 0AH // LINE FEED
ORG 0 //Initialize the Special Function Registers Required
//for Serial Data Transmission
/*Clear the Serial Window(Screen) and return the curser to beginning of the line*/
MOV A,#0CH // clear screen
ACALL SENDsr
MOV A,#LF
ACALL SENDsr
/*Retrieve The Data to be transmitted from the code memory*/
MOV DPTR,#MYDATA0 // load pointer for message
H_1: CLR A
MOVC A,@A+DPTR // get the character
JZ HERE // if last character get out
ACALL SENDsr // otherwise call transfer
ACALL DELAY
INC DPTR // next one
SJMP H_1 //stay in loop
// Serial data transfer. ACC has the data
SENDsr: MOV TMOD, #20H // timer 1, mode 2(auto-reload)
MOV TH1, #-3 // 9600 baud rate
MOV SCON,#50H // 8-bit,1 stop, REN enabled
SETB TR1 // start timer 1
MOV SBUF,A // load the data
JNB TI,$ // stay here until last bit gone
CLR TI // get ready for next char
RET // return to caller
// Receive data serially in Acc

14
EXPERIMENT 5
Delay generation using Timer [Assembly and C]
AIM: Write an ALP to toggle the content of port 0 continuously using timer delay in between
COMPONENTS REQUIRED:
 8051 Microcontroller Kit

PROGRAM:
ORG 0000H
LJMP 8000H
ORG 8000H
MOV R0, #0AH // R0=0AH
MOV A, #55H // A=55H
ACALL DELAY // Call delay program
MOV A, #0AAH // A=AAh
MOV P0, A // Move the content of A in P0
ACALL DELAY // Call delay program
DJNZ R0, GO // R0=R0-1, if R0 is not equal to zero then jump to go
LCALL 0003H //End of main program
MOV TMOD, #01H // Load TMOD
MOV TL0, #00H // Load TL0
MOV TH0, #00H // Load TH0
SETB TR0 // TR0=1
JNB TF0, HERE // if TF0 is not equal 1 then jump to here
CLR TR0 // TR0=1
CLR TF0 // TF0=0
RET // Return to main program

15
EXPERIMENT 6
PROGRAMMING INTERRUPTS [ASSEMBLY AND C]
AIM: Programming interrupts in assembly and c.
COMPONENTS REQUIRED:
 8051 Microcontroller Kit
 LEDs
 Connecting Wires
THEORY:
• An interrupt is an external or internal event that interrupts the microcontroller to inform it that a
device needs its service.
• A set of program instructions written to service an interrupt is called the Interrupt Service Routine
• 8051 has six different sources of interrupts
External: Power-up reset, INT0, INT1
Internal: Timer0, Timer1, Serial Port
Interrupt Vector Table for 8051
Interrupt Source ROM Location Pin
Reset 0000H 9
INT0 0003H 12(P3.2)
Timer0 000BH
INT1 0013H 13(P3.3)
Timer1 001BH
Serial Port 0023H

Program

1. Assuming that INT1 is connected to a pulse generator. Write a program in which the
falling edge of the pulse will send a high to P 1.3, which is connected to an LED.

ORG 0000H
LJMP MAIN
; ISR for hardware interrupt INT1 to turn on the LED
ORG 0013H ; INT1 ISR
SETB P1.3 ; turn on the LED
MOV R3,#255
BACK:DJNZ R3,BACK ;keep the LED on for a while
CLR P1.3 ; turn off the LED

16
RETI ; return from ISR
; MAIN program for initialization
ORG 30H
MAIN: SETB TCON.2 ; make INT1 edge-trigger interrupt
MOV IE,#10000100B ;enable External INT1
HERE: SJMP HERE ; stay here until interrupted
END

2. A program to i) continuously read data from P1 and send it to P2


ii) receive data from serial port and send it to P0
ORG 0H
LJMP MAIN
ORG 0023H
LJMP SERIAL ;jump to aerial ISR
MAIN: MOV P1, #0FFH ;make P1 an input port
MOV TMOD, #20H ;timer 1, mode 2(auto-reload)
MOV TH1, #0FDH ;9600 baud rate
MOV SCON, #050H ;8-bite]. stop, REN enabled
MOV IE, #10010000B ;enable aerial interrupt
SETB TR1 ;start Timer 1
BACK: MOV A, P1 ;read data from port 1
MOV P2, A ;send it to P2
SJMP BACK ;stay in loop indefinitely
ORG 100H
SERIAL: JB TI, TRANS ;jump if TI is high
MOV A, SBUF ;otherwise due to receive
MOV P0, A ;send incoming data to P0
CLR RI ;clear RI since CPU doesn't
RETI ;return from ISR
TRANS: CLR TI ;clear TI since CPU doesn't
RETI ;return from ISR
END

17
EXPERIMENT 7
IMPLEMENTATION OF STANDARD UART COMMUNICATION (USING
HYPER TERMINAL) [ASSEMBLY AND C].
AIM: Write an ALP to transmit characters to a PC HyperTerminal using the serial port and display
on the serial window.
COMPONENTS REQUIRED:
 8051 Microcontroller Kit

Program:
ORG 0000H
LJMP 8000H
ORG 8000H
MOV TMOD, #20H ; TMOD= 20H
MOV TH1, #-3 ;TH1=-3H
MOV SCON, #50H ;SCON= 50H
SETB TR0 ;TR0=1;
MOV A, #’H’ ;Load letter ‘H’ in A
ACALL TRANS ;Call transmit program
MOV A, #’I’ ;Load letter ‘I’ in A
ACALL TRANS ;Call transmit program
MOV A, #’T’ ;Load letter ‘T’ in A
ACALL TRANS ;Call transmit program
SJMP AGAIN ;Short jump to again
MOV SBUF, A ; Load SBUF with letter stored in A
JNB TI, HERE ; if TI is not equal 1 jump to here
RET ; Return
END ; End

18
EXPERIMENT 8
Interfacing LCD Display [Assembly and C]
AIM: Write an assembly language program to display a message in LCD display
COMPONENTS REQUIRED:
 8051 Microcontroller Kit
 16×2 LCD module

THEORY:

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.

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.

19
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.
SCHEMATIC DIAGRAM:

PROGRAM:

ORG 0000H
MOV A,#38H
ACALL COMNWRT
ACALL DELAY
MOVA,#0EH

20
ACALL COMNWRT
ACALL DELAY
MOV A,#01
ACALL COMNWRT
ACALL DELAY
MOV A,#06H
ACALL COMNWRT
ACALL DELAY
MOV A,#86H
ACALL COMNWRT
ACALL DELAY
MOV A,#'Y'
ACALL DATAWRT
ACALL DELAY
MOV A,#'E'
ACALL DATAWRT
ACALL DELAY
MOV A,#'S'
ACALL DATAWRT
AGAIN:SJMP
AGAIN COMNWRT:
MOV P1,A
CLR P3.5
CLR P3.4
SETB P3.3
ACALL DELAY
CLR P3.3
RET DATAWRT:
MOV P1,A
SETB P3.5
CLR P3.4
SETB P3.3
ACALL DELAY
CLR P3.3
RET

21
EXPERIMENT 9
Interfacing with Keypad [Assembly and C]
AIM: Write an assembly language program to demonstrate the basic interface between an LCD
display and 4 x 4 matrix key board.
COMPONENTS REQUIRED:
 8051 Microcontroller Kit
 7 Segment LED display
 4 x 4 matrix key board.

THEORY:

The circuit diagram for demonstrating interfacing hex keypad to 8051 is shown below. The circuit
will display the character/numeric pressed on a seven segment LED display. The circuit is very
simple and it uses only two ports of the microcontroller, one for the hex keypad and the other for
the seven segment LED display.
The hex keypad is interfaced to port 1 and seven segment LED display is interfaced to port 0 of the
microcontroller. Resistors R1 to R8 limits the current through the corresponding segments of the
LED display. Capacitors C1, C2 and crystal X1 completes the clock circuitry for the
microcontroller. Capacitor C3, resistor R9 and push button switch S1 forms a debouncing reset
mechanism.

22
Program:
ORG 00H

MOV DPTR,#LUT ; MOVES START ADDR OF LUT TO DPTR


MOV A,#11111111B ; LOADS A WITH ALL 1'S
MOV P2,#00000000B ; INITIALIZES P0 AS OUTPUT PORT

BACK: MOV P1,#11111111B ; LOADS P1 WITH ALL 1'S


CLR P1.0 ; MAKES ROW 1 LOW
JB P1.4,NEXT1 ; CHECKS WHETHER COLUMN 1 IS LOW AND JUMPS
TO NEXT1 IF NOT LOW
MOV A,#0D ; LOADS A WITH 0D IF COL IS LOW (THAT MEANS
KEY 1 IS PRESSED)
ACALL DISPLAY ; CALLS DISPLAY SUBROUTINE
NEXT1: JB P1.5,NEXT2 ; CHECKS WHETHER COLUMN 2 IS LOW AND SO
ON...
MOV A,#1D
ACALL DISPLAY
NEXT2: JB P1.6,NEXT3
MOV A,#2D
ACALL DISPLAY
NEXT3: JB P1.7,NEXT4
MOV A,#3D
ACALL DISPLAY
NEXT4: SETB P1.0
CLR P1.1
JB P1.4,NEXT5
MOV A,#4D
ACALL DISPLAY
NEXT5: JB P1.5,NEXT6
MOV A,#5D
ACALL DISPLAY
NEXT6: JB P1.6,NEXT7
MOV A,#6D
ACALL DISPLAY

23
NEXT7: JB P1.7,NEXT8
MOV A,#7D
ACALL DISPLAY
NEXT8: SETB P1.1
CLR P1.2
JB P1.4,NEXT9
MOV A,#8D
ACALL DISPLAY
NEXT9: JB P1.5,NEXT10
MOV A,#9D
ACALL DISPLAY
NEXT10: JB P1.6,NEXT11
MOV A,#10D
ACALL DISPLAY
NEXT11: JB P1.7,NEXT12
MOV A,#11D
ACALL DISPLAY
NEXT12: SETB P1.2
CLR P1.3
JB P1.4,NEXT13
MOV A,#12D
ACALL DISPLAY
NEXT13: JB P1.5,NEXT14
MOV A,#13D
ACALL DISPLAY
NEXT14: JB P1.6,NEXT15
MOV A,#14D
ACALL DISPLAY
NEXT15: JB P1.7,BACK
MOV A,#15D
ACALL DISPLAY
LJMP BACK

DISPLAY: MOVC A,@A+DPTR ; GETS DIGIT DRIVE PATTERN FOR THE CURRENT
KEY FROM LUT

24
MOV P2,A ; PUTS CORRESPONDING DIGIT DRIVE PATTERN
INTO P0
RET

LUT:
DB 3FH, 06H, 5BH, 4FH, 66H, 6DH, 7DH, 07H, 7FH,
6FH,0F7H,0FCH,0B9H,0DEH,0F9H,0F1H,0
;00000000

END

25
EXPERIMENT 10
Programming ADC/DAC [Assembly and C]
10. (A) AIM: Write an assembly language program to interface ADC with 8051 microcontroller
COMPONENTS REQUIRED:
 8051 Microcontroller Kit
 LED display
 ADC0804
THEORY:
Pinout of ADC080

This is an 8-bit ADC that produces the 8-bit digital numbers corresponding to the analogue voltage
at its input.It is interfaced with microcontroller through its control lines i.e. WR, RD, INTR and
CS. The line CS must be logic low to perform any conversion with ADC. WR line should go low
to start new conversion and INTR signals the completion of conversion by placing logic 0 on it.
When conversion is completed, RD line must bepulled to logic 0 by the MCU to read the 8-bit
result from the data bus of ADC.

Example: Create an assembly language program the converts the analog voltage into digital value
and displays it on LED bar display.

ADC_WR EQU P2.0


ADC_RD EQU P2.1
ADC_INT EQU P2.2
LED EQU P1
ADC EQU P3
ORG 0000H
LJMP MAIN

26
ORG 0030H
MAIN:
CALL DELAY
CALL CONVERT ;RESULT IS IN A
MOV LED, A ;WRITE RESULT TO BAR DISPLAY
JMP MAIN
CONVERT:
SETB ADC_WR
SETB ADC_RD
NOP
CLR ADC_WR
NOP
SETB ADC_WR
JB ADC_INT,$ ;POLL FOR INTR = 0
CLR ADC_RD
NOP
MOV A, ADC ;READ ADC PORT IN A
SETB ADC_RD
RET
;SUBROUTINE GENERATES SOME DELAY
DELAY:
MOV R1, 250
LOOP1:MOV R0, #0FFH
DJNZ R0, $
MOV R0, #0FFH
DJNZ R0, $
MOV R0, #0FFH
DJNZ R0, $
MOV R0, #0FFH
DJNZ R0, $
DJNZ R1, LOOP1
RET
END

27
10. (B) AIM: Write an assembly language program to interface ADC with 8051 microcontroller
COMPONENTS REQUIRED:
 8051 Microcontroller Kit
 DAC0808
THEORY:
The Digital to Analog converter (DAC) is a device, that is widely used for converting digital pulses
to analog signals. There are two methods of converting digital signals to analog signals. These two
methods are binary weighted method and R/2R ladder method. In this article we will use the
MC1408 (DAC0808) Digital to Analog Converter. This chip uses R/2R ladder method.
Circuit Diagram:

PROGRAM:

ORG 0000H
MOV 1,#00H
REPEAT:ACALL
SQUARWAVE SJMP
REPEAT
SQUARWAVE:MOV
P1,#FFH ACALL DELAY
MOV P1,#00H
ACALL DELAY
RET

28
DELAY:MOV R0,#20
UP2:MOV R1,#250
UP1:MOV R2,#250
HERE:DJNZ R2,HERE
DJNZ R1,UP1
DJNZ
R0,UP2
RET
END

29
EXPERIMENT 11
INTERFACING WITH STEPPER MOTOR [ASSEMBLY AND C]
AIM: Write an assembly language program for interfacing stepper motor with 8051.
COMPONENTS REQUIRED:
 8051 Microcontroller Kit
 DC voltage source
 Stepper Motor
THEORY:

A Stepper Motor or a step motor is a brushless, synchronous motor which divides a full rotation
into a number of steps. Unlike a brushless DC motor which rotates continuously when a fixed DC
voltage is applied to it, a step motor rotates in discrete step angles. The Stepper Motors therefore
are manufactured with steps per revolution of 12, 24, 72, 144, 180, and 200, resulting in stepping
angles of 30, 15, 5, 2.5, 2, and 1.8 degrees per step. The stepper motor can be controlled with or
without feedback. Stepper motors work on the principle of electromagnetism. There is a soft iron
or magnetic rotor shaft surrounded by the electromagnetic stators. The rotor and stator have poles
which may be teethed or not depending upon the type of stepper. When the stators are energized
the rotor moves to align itself along with the stator (in case of a permanent magnet type stepper) or
moves to have a minimum gap with the stator (in case of a variable reluctance stepper). This way
the stators are energized in a sequence to rotate the stepper motor.

30
SCHEMATIC DIAGRAM:

PROGRAM:

ORG 0000H
MOV A,#66H
LOOP:MOV P2,A
ACALL DELAY
RR A
SJMP LOOP
DELAY:MOV R5,#0AH
AGAIN:MOV R3,#0FFH
BACK:DJNZ R3,BACK
DJNZ R5,AGAIN
RET
END

31
EXPERIMENT 12
PULSE WIDTH MODULATION [ASSEMBLY AND C]
AIM: Write an assembly language program to implement PWM on 8051 Microcontroller
COMPONENTS REQUIRED:
 8051 Microcontroller Kit
 DSO
THEORY:
Pulse width modulation is basically a square wave with a varying high and low time. A basic PWM
signal is shown in the figure below.

Pulse width modulation wave

Period:

As shown in the the figure, Ton denotes the on-time and Toff denotes the off time of signal. Period
is the sum of both on and off times and is calculated as shown in the equation below:

Duty Cycle:

Duty cycle is calculated as on-time to the period of time. Using the period calculated above, duty
cycle is calculated as:

The basic idea behind PWM implementation on 8051 is using timers and switching port pin high/low
at defined intervals. By changing the Ton time, we can vary the width of square wave keeping same
time period of the square wave.

32
We will be using 8051 Timer0 in Mode 0. Values for high and low level will be loaded in such a
way that total delay remains same. If for high level we load a value X in TH0 then for low level TH0
will be loaded with 255-X so that total remains as 255.

PROGRAM:
PWMPIN EQU P1.0 ; PWM output pin
PWM_FLAG EQU 0 ; Flag to indicate high/low signal
PWM_SETUP:
MOV TMOD,#00H ; Timer0 in Mode 0
MOV R7, #160 ; Set pulse width control
; The value loaded in R7 is value X as
; discussed above.
SETB EA ; Enable Interrupts
SETB ET0 ; Enable Timer 0 Interrupt
SETB TR0 ; Start Timer
RET

33
EXPERIMENT 13
INTERFACING WITH DC MOTOR [ASSEMBLY AND C]
AIM: Write an assembly language program for interfacing DC motor with 8051.
COMPONENTS REQUIRED:
 8051 Microcontroller Kit
 L293 Motor Driver
 DC Motor
THEORY:

DC motor converts electrical energy in the form of Direct Current into mechanical energy.

 The direction of rotation of the shaft of the motor can be reversed by reversing the direction
of Direct Current through the motor.
 The motor can be rotated at a certain speed by applying a fixed voltage to it. If the voltage
varies, the speed of the motor varies.
 Thus, the DC motor speed can be controlled by applying varying DC voltage; whereas the
direction of rotation of the motor can be changed by reversing the direction of current
through it.
 For reversing the current, we can make use of an H-Bridge circuit or motor driver ICs that
employ the H-Bridge technique or any other mechanisms.

L293 MOTOR DRIVER


L293 is a dedicated quadruple Half H Bridge motor driver IC available in 16 pin package. To know
more about H Bridge, check this link. H bridge motor driver circuit. L293 has a current capacity
of 600mA/channel and has supply voltage range from 4.5 to 36V DC. They are fitted with internal
high speed clamp diodes for inductive spike protection. Other good features of L293 are high noise
immunity, internal ESD protection, thermal shutdown, separate input supply for each channel etc.
The pinout of an L293 motor driver is shown in the figure below.

34
PROGRAM

ORG 00H // initial starting address


MAIN: MOV P1,#00000001B // motor runs clockwise
ACALL DELAY // calls the 1S DELAY
MOV P1,#00000010B // motor runs anti clockwise
ACALL DELAY // calls the 1S DELAY
SJMP MAIN // jumps to label MAIN for repaeting the cycle
DELAY: MOV R4,#0FH
WAIT1: MOV R3,#00H
WAIT2: MOV R2,#00H
WAIT3: DJNZ R2,WAIT3
DJNZ R3,WAIT2
DJNZ R4,WAIT1
RET
END

35

You might also like