Professional Documents
Culture Documents
8051 - Lab Manual - Old
8051 - Lab Manual - Old
MICROCONTROLLER
&
EMBEDDED SYSTEM
LABMANUAL
Microcontroller & embedded system
OUTPUT:-
OUTPUT:-
OUTPUT:-
OUTPUT:-
OUTPUT:-
[0123H]=00H [0123H]=
[02BCH]=00H [02BCH]=
OUTPUT:-
7. Write a program to SWAP the bytes in timer 0: i.e. put TL0 in TH0 and TH0 in
TL0.
OUTPUT:-
8. Write a program to Store the content of register R3 at the internal RAM address
contained in R2.
OUTPUT:-
1. Write a program to add the bytes in RAM locations 34h & 35h, put the result in
registers R5(LSB) & R6(MSB).
OUTPUT:-
2. Write a program to add the bytes in external RAM locations 02CDH to internal
RAM location 19h, put the result into external RAM location 00C0h(LSB) &
00C1H(MSB).
OUTPUT:-
3. Write a program to subtract the contents of R2 from the number F3h, put the result in
external RAM location 028Bh.
OUTPUT:-
Before execution After execution
R2=55H R2=
[028BH]=00H [028BH]=
4. Write a program to subtract the contents of RAM location 13h from RAM location
2Bh, put the result in RAM location 3Ch.
OUTPUT:-
[3CH]=00H [3CH]=
5. Write a program to add a 1 to every external RAM address from 00h to 06h.
[02H]=03H [02H]=
[03H]=22H [03H]=
[04H]=23H [04H]=
[05H]=F0H [05H]=
[06H]=FFH [06H]=
OUTPUT:-
7. Write a program to square the content of R5, put the result in R0(Higher byte) &
R1(Lower byte).
OUTPUT:-
8. Write a program to divide the number in RAM 15h by the data in RAM location 16h,
put the resulting quotient in external RAM locations 7Ch.
OUTPUT:-
1. Write a program to set port 0,bits 1,3,5 & 7 to 1;set the rest to 0.
OUTPUT:-
2. Write a program to clear bit 3 of RAM location 22h without affecting any other bit.
OUTPUT:-
Microcontroller & embedded system
3. Write a program to invert the data on port 0 pins and write the data to port 1.
OUTPUT:-
4. Write a program to swap the nibbles of R0 & R1 so that the low nibble of R0 swaps with
the high nible of R1 & the high nibble of R0 swaps with the low nibble of R1.
R1
2 3
R0
5 8
Before execution
R1
8 5
R0
3 2
After execution
Microcontroller & embedded system
OUTPUT:-
5. Write a program to complement the lower nibble of internal RAM location 2Ah.
OUTPUT:-
OUTPUT:-
Microcontroller & embedded system
OUTPUT:-
8. Write a program to store the least significant nibble of A in both nibbles of RAM
address 3Ch; for example if A=36h,then [3Ch]=66h.
OUTPUT:-
9. Write a program to Set the carry flag to 1 if the number in A is even; set the carry flag
to 0 if the number in A is odd.
RRC A
MOV A,R0
HERE SJMP HERE
OUTPUT:-
10. Write a program to rotate the DPTR one place to the left; bit 15 becomes bit 0.
MOV A,DPL
RLC A
MOV A,DPH
RLC A
MOV DPH,A
MOV A,DPL
RLC A
MOV DPL,A
OUTPUT:-
OUTPUT:-
OUTPUT:-
3. Write a program to decrement the numbers stored in internal RAM locations 20h(LSB)
& 21h(MSB) & decrement them as if they were a single 16-bit counter until they equal
numbers in R2(LSB) & R3(MSB).
SUBB A,R3
JZ HERE
SKIP DEC 20H
CJNE 20H,#0FFH, ABC
DEC 21H
ABC SJMP START
HERE SJMP HERE
OUTPUT:-
4. Write a program to set the carry flag to 1 if the lower nibble of any number placed in A
is larger than the upper nibble.
OUTPUT:-
5. Write a program to count the number of 1s in any number in register B & put the count
in R5
OUTPUT:-
6. Write a program to decrement the DPTR from any initialize value to 0033h as a 16 bit
register.
OUTPUT:-
OUTPUT:-
8. Write a program to put one random number in R2 and another in R5. Increment R2 &
decrement R5 until they are equal.
OUTPUT:-
9. Write a program to fill external RAM locations 100h to 200h with the number AAh.
OUTPUT:-
Theory:-
7 segment LED display is very popular and it can display digits from 0 to 9 and quite a few
characters like A, b, C, ., H, E, e, F, n, o,t,u,y, etc. Knowledge about how to interface a seven
segment display to a micro controller is very essential in designing embedded systems. A
seven segment display consists of seven LEDs arranged in the form of a squarish ’8′ slightly
inclined to the right and a single LED as the dot character. Different characters can be
displayed by selectively glowing the required LED segments. Seven segment displays are of
two types, common cathode and common anode. In common cathode type , the cathode of all
LEDs are tied together to a single terminal which is usually labeled as ‘com‘ and the anode of
all LEDs are left alone as individual pins labeled as a, b, c, d, e, f, g & h (or dot) . In common
anode type, the anode of all LEDs are tied together as a single terminal and cathodes are left
alone as individual pins. The pin out scheme and picture of a typical 7 segment LED display is
shown in the image below.
Program:-
ORG 100h
START: MOV A,#00001001B // initial value of accumulator
MOV B,A
MOV R0,#0AH //Register R0 initialized as counter which
counts from 10 to 0
LABEL: MOV A,B
INC A
MOV B,A
MOVC A,@A+PC // adds the byte in A to the
program counters
address
MOV P1,A
ACALL DELAY // calls the delay of the timer
DEC R0//Counter R0 decremented by 1
MOV A,R0 // R0 moved to accumulator to check if
it is zero in next instruction.
JZ START //Checks accumulator for zero and
jumps to START. Done to check if counting has been
finished.
SJMP LABEL
DB 3FH // digit drive pattern for 0
DB 06H // digit drive pattern for 1
DB 5BH // digit drive pattern for 2
DB 4FH // digit drive pattern for 3
DB 66H // digit drive pattern for 4
DB 6DH // digit drive pattern for 5
DB 7DH // digit drive pattern for 6
DB 07H // digit drive pattern for 7
DB 7FH // digit drive pattern for 8
DB 6FH // digit drive pattern for 9
DELAY: MOV R4,#05H // subroutine for delay
WAIT1: MOV R3,#00H
WAIT2: MOV R2,#00H
WAIT3: DJNZ R2,WAIT3
DJNZ R3,WAIT2
DJNZ R4,WAIT1
RET
END
Microcontroller & embedded system
Questions:-
1. Which segments should be on to display 5?
2. Explain Common cathode 7-segment display.
3. Explain Common anode 7-segment display.
4. Why series registers are connected in series with LED 7-segment display?
Conclusion:-
Microcontroller & embedded system
Theory:
LCD display is an inevitable part in almost all embedded projects and this article is about
interfacing 16×2 LCD with 8051 microcontroller. Many guys find it hard to interface LCD
module with the 8051 but the fact is that if you learn it properly, its a very easy job and by
knowing it you can easily design embedded projects like digital voltmeter / ammeter, digital
clock, home automation displays, status indicator display, digital code locks, digital
speedometer/ odometer, display for music players etc etc. Thoroughly going through this
article will make you able to display any text (including the extended characters) on any part
of the 16×2 display screen. In order to understand the interfacing first you have to know about
the 16×2 LCD module.
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. See the circuit diagram for better understanding.
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. We will discuss in detail about the commands later. 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.
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.
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.
Circuit diagram:-
Program:-
Subroutine CMND sets the logic of the RS, R/W, E pins of the LCD module so that the
module recognizes the input data ( given to DB0 to DB7) as a command.
Subroutine DISP sets the logic of the RS, R/W, E pins of the module so that the module
recognizes the input data as a data to be displayed .
ORG 100h
MOV A,#38H // Use 2 lines and 5x7 matrix
ACALL CMND
MOV A,#0FH // LCD ON, cursor ON, cursor blinking ON
ACALL CMND
MOV A,#01H //Clear screen
ACALL CMND
MOV A,#06H //Increment cursor
ACALL CMND
MOV A,#82H //Cursor line one , position 2
ACALL CMND
MOV A,#3CH //Activate second line
ACALL CMND
MOV A,#49D
ACALL DISP
MOV A,#54D
ACALL DISP
MOV A,#88D
ACALL DISP
MOV A,#50D
ACALL DISP
MOV A,#32D
ACALL DISP
MOV A,#76D
ACALL DISP
MOV A,#67D
ACALL DISP
MOV A,#68D
ACALL DISP
MOV A,#0C1H //Jump to second line, position 1
ACALL CMND
MOV A,#67D
ACALL DISP
MOV A,#73D
Microcontroller & embedded system
ACALL DISP
MOV A,#82D
ACALL DISP
MOV A,#67D
ACALL DISP
MOV A,#85D
ACALL DISP
MOV A,#73D
ACALL DISP
MOV A,#84D
ACALL DISP
MOV A,#83D
ACALL DISP
MOV A,#84D
ACALL DISP
MOV A,#79D
ACALL DISP
MOV A,#68D
ACALL DISP
MOV A,#65D
ACALL DISP
MOV A,#89D
ACALL DISP
DISP:MOV P1,A
SETB P3.5
CLR P3.4
SETB P3.3
CLR P3.3
ACALL DELY
RET;
Microcontroller & embedded system
CLR P3.3
CLR P3.4
RET;
END
Questions:-
Conclusion:-
Theory:-
Of all motors, step motor is the easiest to control. It's handling simplicity is really hard to deny
- all there is to do is to bring the sequence of rectangle impulses to one input of step controller
and direction information to another input. Direction information is very simple and comes
down to "left" for logical one on that pin and "right" for logical zero. Motor control is also
very simple - every impulse makes the motor operating for one step and if there is no impulse
the motor won't start. Pause between impulses can be shorter or longer and it defines
revolution rate. This rate cannot be infinite because the motor won't be able to "catch up" with
all the impulses (documentation on specific motor should contain such information). The
picture below represents the scheme for connecting the step motor to microcontroller and
appropriate program code follows.
The key to driving a stepper is realizing how the motor is constructed. A diagram shows the
representation of a 4 coil motor, so named because 4 coils are used to cause the revolution of
the drive shaft. Each coil must be energized in the correct order for the motor to spin.
As with drive circuitry for variable reluctance motors, we must deal with the inductive kick
produced when each of these switches is turned off. Again, we may shunt the inductive kick
using diodes, but now, 4 diodes are required.
Step angle
It is angle through which motor shaft rotates in one step. step angle is different for different
Microcontroller & embedded system
motor . selection of motor according to step angle depends on the application , simply if you
require small increments in rottion choose motor having smaller step angle.
No of steps require to rotate one complete rotation = 360 deg. / step angle in deg.
Steps/second
The relation between RPM and steps per sec. is given by ,
steps or impulses /sec. =(RPM X Steps /revolution ) /60
Pause between impulses can be shorter or longer and it defines revolution rate. This rate
cannot be infinite because the motor won't be able to "catch up" with all the impulses
(documentation on specific motor should contain such information). So referring to RPM
value in datasheet you can calculate steps/sec and from it delay or pause between impulses
Interfacing Circuit:-
Step Sequence:-
Q4 Q2 Q3 Q1 Step
Microcontroller & embedded system
1 0 1 0 1
0 1 1 0 2
0 1 0 1 3
1 0 0 1 4
➢ Connect stepper motor interfacing module to 8255-I of 8051 trainer kit through 26 pin FRC
cable.
➢ Be sure about the direction of the cable i.e. pin no-1 of module should be connected to pin no.1
of 8255 connector.
➢ Connect +5V, GND from the trainer kit .
➢ Connect motor supply to +12V from the trainer kit.
➢ 8255 port address:-
Port A- FF00h
Port B-FF01H
Port C-FF02H
Control Word- FF03H
Program:-
ORG 3000h
MOV DPTR, #0FF03H
MOV A, #80H
MOVX @DPTR,A
START: MOV DPTR, #0FF00H
MOV A, #0FAH
MOVX @DPTR, A
ACALL DELAY
MOV A, #0F6H
MOVX @DPTR, A
ACALL DELAY
Microcontroller & embedded system
MOV A, #0F5H
MOVX @DPTR, A
ACALL DELAY
MOV A, #0F9H
MOVX @DPTR, A
ACALL DELAY
SJMP START
ORG 3050H
DELAY: MOV R7, #03FH ; DECREASE THIS NUMBER TO INCREASE SPEED
DELAY1: MOV R6, #03FH ; DECREASE THIS NUMBER TO INCREASE SPEED
DELAY2: DJNZ R6, DELAY2
DJNZ R7, DELAY1
RET
Questions:-
2. Write the formula for relation between RPM and steps per sec for stepper motor.
Conclusion:-
Microcontroller & embedded system
Theory:-
Basics of Opto-Coupler:
An opto-isolator, also known as an optical coupler or optocoupler, is a semiconductor device
that allows signals to be transferred between circuits or systems, while keeping those circuits
or systems electrically isolated from each other. Optoisolators are used in a wide variety of
communications, control, and monitoring systems.
The opto-coupler consists of two parts: 1. The LED Side and 2. Photo-Transistor side. Both
sides are separated and are not electrically connected. Only light can travel between the
LED and the photo-transistor. When an electrical signal is applied to the input of the opto-
isolator, its LED lights. This light falls on the photo transistor and it then activates.
A typical usage of an opto-coupler is as follows:
Microcontroller & embedded system
In the above circuit, a forward biasing voltage is applied to the LED making it light up. The R1
resistor is a current limiting resistor - to provide sufficient voltage drop required by the LED.
The LED triggers the Q1 Photo-transistor inside the opto-coupler. This can be used to produce
an output in common emmitter configuration.
If the opto's LED is off, the photo-transistor is turned off. The Collector will be in High-
impedence, that is, in a disconnected state. Hence, the week pull-up resistor will increase the
Microcontroller & embedded system
voltage in the microcontroller's pin and make it logical high. The Microcontroller's internal
circuits will sense this as logical LOW.
If the LED is turned on, the photo-transistor will conduct current from the collector to emitter.
This will cause the microcontroller pin to 'drain' and reduce its voltage to zero. This is because
the resistance of the collector-emitter path is too low comapred to the pull-up resistor. The
pull-up being week, will not be able to raise the voltage level of the pin sufficiently. The
microcontroller's internal circuits will sense this as logical HIGH.
when the LED is turned off again, the collector will become hi-impedence (disconnected) and
the pull-up will increase the value of the voltage to logical high levels. The Microcontroller's
internal circuits will sense this as logical LOW. If the pull-up resistor is too large in value, it
will take more time to pull up the pin to high. If it is too low, it will pull-up the pin very fast
but it will consume more power when the LED is turned on due higher current being drained.
Theory:-
DAC0800 introduction
The DAC0800 series are monolithic 8-bit high-speed current-output digital-to-analog
converters (DAC) featuring typical settling times of 100 ns. The DAC0800 series also features
high compliance complementary current outputs to allow differential output voltages of 20 Vp-
Microcontroller & embedded system
Features of DAC0800
analog voltage get amplified and isolated from the controller circuit hence the Opamp circuit is
used to provide the compatibility with the alarming device. In our circuit we have used a
speaker as an alarming device which is operable on 5V.
The DAC0800 provides high speed digital to analog conversion. When very low conversion
rates can be used, another method of digital to analog conversion may suffice. Consider a
square wave which is input to a low pass filter, whose cutoff frequency is far below the
repetition rate of the square wave. From Fourier analysis, only the average value of the square
wave should pass through the filter. By varying the duty cycle for the square wave, the average
value o f the wave can be changed. Therefore, a low pass filter and the output compare
functions can be used together to form a simple D/A converter for slowly varying signals.
Write a program that uses the low pass filter in Figure 2 as a D/A converter. Accept a voltage,
between 0V and 5.0V, and use a 10KHz wave with variable duty cycle to produce the proper
output voltage. The output compare functions should be used to generate the square wave.
Measure Rref with an ohmmeter. Connect the circuit in Figure 1. Measure the voltage dropped
across Vref and calculate IFS. Run the program for several voltages and analyze the results.
Programs:-
(1) Program to generate sawtooth wave.
ORG 3000H
MOV DPTR, #0FF03H
MOV A, #80H
MOVX @DPTR, A
MOV A,#00H
AGAIN: MOV DPTR, #0FF00H
MOVX @DPTR,A
INC A
SJMP AGAIN
END
(2) Program to generate staircase waveform.
ORG 3000H
MOV DPTR, #0FF03H
MOV A, #80H
MOVX @DPTR, A
MOV A, #00H
Microcontroller & embedded system
ORG 3050H
DELAY: MOV R0,#35H ;DECIDE WIDTH OF STEP
MOV R3, #FFH
ABC: DJNZ R3, ABC
XYZ: DJNZ R0,XYZ
RET
END
(3) Program to generate square wave.
ORG 3000H
MOV DPTR, #0FF03H
MOV A, #80H
MOVX @DPTR, A
AGAIN: MOV A, #00H
MOV DPTR, #0FF00H
MOVX @DPTR,A
LCALL DELAY
MOV A, #0FFH
MOV DPTR, #0FF00H
MOVX @DPTR, A
LCALL DELAY
SJMP AGAIN
ORG 3050H
DELAY: MOV R0,#35H
MOV R3, #FFH
ABC: DJNZ R3, ABC
XYZ: DJNZ R0,XYZ
RET
END
(4) Program to generate triangle wave.
ORG 3000H
MOV DPTR, #0FF03H
MOV A, #80H
Microcontroller & embedded system
MOVX @DPTR, A
MOV A, #00H
MOV DPTR, #0FF00H
AGAIN: MOVX @DPTR, A
INC A
CJNE A, #0FFH, AGAIN
REPEAT: DEC A
MOVX @DPTR, A
CJNE A, #00H, REPEAT
SJMP AGAIN
END
Questions:-
(1) Enlist the name of different types of D to A Converters.Draw circuits of any one.
Conclusion:-
Microcontroller & embedded system
Theory:-
ADC 0809:-
The ADC0808 and ADC0809 are monolithic CMOS devices with an 8-channel multiplexer,
an 8-bit analog-to-digital (A/D) converter, and microprocessor-compatible control logic. The
8-channel multiplexer can be controlled by a microprocessor through a 3-bit address decoder
with address load to select any one of eight single-ended analog switches connected directly
to the comparator. The 8-bit A/D converter uses the successive-approximation conversion
technique featuring a high-impedance threshold detector, a switched-capacitor array, a sample
and-hold, and a successive-approximation register (SAR). Detailed information on interfacing
tomost popular microprocessors is readily available from the factory. The comparison and
converting methods used eliminate the possibility of missing codes, non-monotonicity, and
the need for zero or full-scale adjustment. Also featured are
latched 3-state outputs from the SAR and latched inputs to the
multiplexer address decoder. The single 5-V supply and low power
requirements make the ADC0808 and ADC0809 especially useful for a
wide variety of applications. Ratiometric conversion is made
possible by access to the reference voltage input terminals. The
ADC0808 and ADC0809 are characterized for operation from −40°C to
85°C.
Microcontroller & embedded system
Microcontroller & embedded system
Interfacing:-
➢ Connect ADC-0809 interfacing module to 8255-I of 8051 trainer kit through 26-pin
FRC cable.
➢ Be sure about the direction of the cable i.e. pin no.1 of module should be connected to
pin no. 1 of 8255 connector.
➢ Connect +5V, GND from the trainer kit( +5V & GND signals are available in the 25 &
26 pin of FRC 8255-I connector).
➢ D0-TO D7 PIN of ADC0809 is connected port A of 8255.
➢ OE pin (output enable) is connected to PC2, EOC(End of conversion) pin is connected
to PC4, START(start of conversion) pin is connected to PC1 & A0,A1,A2( Input line
selection)pin is connected to PB0, PB1 & PB2 pin respectively.
Program:-
Write a program to convert a DC voltage applied at IN0 of ADC0809 & store the digital value at
memory location 3050H.
ORG 3000H
MOV DPTR, #0FF03H
Microcontroller & embedded system
MOV A, #98H
MOVX @DPTR, A
START: MOV DPTR, #0FF01H
MOV A, #00H
MOVX @DPTR, A
MOV DPTR, #0FF02H
MOV A, #00H
MOVX @DPTR, A
MOV A, #03H
MOVX @DPTR, A
MOV A, #00H
MOVX @DPTR, A
DL1: MOVX A, @DPTR
ANL A, #10H
JZ DL1
MOV A, #04H
MOVX @DPTR, A
MOV DPTR, #0FF00H
MOVX A, @DPTR
MOV DPTR, #3050H
MOVX @DPTR, A
HERE: SJMP HERE
Questions:-
(1) What is the function WR/ SOC pin in A-to D converter?
(2) What is the function of RD/OE pin in A-to-D converter?
(3) What is the function of INTR pin in A-to-D converter?
Conclusion:-
Microcontroller & embedded system