Lab Manual MIA
Lab Manual MIA
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.
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.
3
2. Architecture of 8051 Microcontroller
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.
P3.0 can be used for serial receive input pin(RXD) • P3.1 can be used for serial
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]
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
8
After execution:
D:0x50H: 00 25 00 00 00 00
D:0x5FH: FF F5 2F 00 00 00
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
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)*/
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
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.
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
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.
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.
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.
34
PROGRAM
35