Sie sind auf Seite 1von 63

GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

SEMESTER-5 (EC DEPT.)

MICRO-CONTROLLER (8051)

LAB MANUAL

1
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

Sr. Experiment Page Sign


No. No.
1 To study the architecture of 8051 µC
2 Basic Tutorial for Keil Software & Edsim
3 Introduction to .ASM programming.
4 Write and Run Programs using MOV Instruction- Direct Register,
Immediate addressing mode & Indirect addressing mode.
5 Write & Run Programs using PUSH & POP Instructions.
6 Write & Run Programs using Logical Instructions.
7 Write & Run Programs using Arithmetic Instructions.
8 Write & Run Programs using Looping Techniques.
9 Write & Run Programs using Timers in different mode.
10 To study the interfacing of LED with µC 89C51
11 To study the interfacing of relay with 8051 microcontroller.
12 To study the interfacing of LCD with µC 89C51
13 To study the interfacing of STEPPER MOTOR with µC 89C51
14 To interface LDR and seven segment with microcontroller 89C51.
15 To study the interfacing of ADC-0804 with µC 89C51

2
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-1
AIM: To study the architecture of 8051 µC

THEORY:

Introduction:
The Intel MCS-51 (commonly referred to as 8051) is a
Harvard architecture, single chip microcontroller (µC)
series which was developed by Intel in 1980 for use in
embedded systems. Intel's original versions were popular
in the 1980s and early 1990s. While Intel no longer
manufactures the MCS-51, binary compatible derivatives
remain popular today.
Intel's original MCS-51 family was developed using NMOS technology, but later versions, identified
by a letter C in their name (e.g., 80C51) used CMOS technology and consumed less power than their
NMOS predecessors. This made them more suitable for battery-powered devices.

Simplified Block diagram:

Features:
 8-bit ALU, Accumulator and 8-bit Registers; hence it is an 8-bit microcontroller
 8-bit data bus – It can access 8 bits of data in one operation

3
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

 16-bit address bus – 64 KB (65536 locations) each of RAM and ROM


 On-chip RAM – 128 bytes (data memory)
 On-chip ROM – 4 kByte (program memory)
 Four byte bi-directional input/output port
 UART (serial port)
 Two 16-bit Counter/timers with Two-level interrupt priority
 Power saving mode (on some derivatives)

Pin diagram:
Pins 1-8: Port 1 Each of these pins can be configured as an input or an output.

Pin 9: RS A logic one on this pin disables the microcontroller and clears the contents of most
registers. In other words, the positive voltage on this pin resets the microcontroller. By applying logic
zero to this pin, the program starts execution from the beginning.
Pins10-17: Port 3 Similar to port 1, each of these pins can serve as general input or output. Besides, all
of them have alternative functions:
Pin 10: RXD Serial asynchronous communication input or Serial synchronous communication
output.
Pin 11: TXD Serial asynchronous communication output or Serial synchronous
communication clock output.
Pin 12: INT0 Interrupt 0 input.
Pin 13: INT1 Interrupt 1 input.
Pin 14: T0 Counter 0 clock input.
Pin 15: T1 Counter 1 clock input.
Pin 16: WR Write to external (additional) RAM.
Pin 17: RD Read from external RAM.

Pin 18, 19: X2, X1 Internal oscillator input and output. A quartz crystal which specifies operating
frequency is usually connected to these pins. Instead of it, miniature ceramics resonators can also be
used for frequency stability. Later versions of microcontrollers operate at a frequency of 0 Hz up to
over 50 Hz.

Pin 20: GND Ground.

4
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

Pin 21-28: Port 2 If there is no intention to use external memory then these port pins are configured as
general inputs/outputs. In case external memory is used, the higher address byte, i.e. addresses A8-
A15 will appear on this port. Even though memory with capacity of 64Kb is not used, which means
that not all eight port bits are used for its addressing, the rest of them are not available as
inputs/outputs.

Pin 29: PSEN If external ROM is used for storing program then a logic zero (0) appears on it every
time the microcontroller reads a byte from memory.

Pin 30: ALE Prior to reading from external memory, the microcontroller puts the lower address byte
(A0-A7) on P0 and activates the ALE output. After receiving signal from the ALE pin, the external
register (usually 74HCT373 or 74HCT375 add-on chip) memorizes the state of P0 and uses it as a
memory chip address. Immediately after that, the ALU pin is returned its previous logic state and P0 is
now used as a Data Bus. As seen, port data multiplexing is performed by means of only one additional
(and cheap) integrated circuit. In other words, this port is used for both data and address transmission.

Pin 31: EA By applying logic zero to this pin, P2 and P3 are used for data and address transmission
with no regard to whether there is internal memory or not. It means that even there is a program
written to the microcontroller, it will not be executed. Instead, the program written to external ROM
will be executed. By applying logic one to the EA pin, the microcontroller will use both memories,
first internal then external (if exists).

Pin 32-39: Port 0 Similar to P2, if external memory is not used, these pins can be used as general
inputs/outputs. Otherwise, P0 is configured as address output (A0-A7) when the ALE pin is driven
high (1) or as data output (Data Bus) when the ALE pin is driven low (0).

Pin 40: VCC +5V power supply.

Memory Organization
The 8051 has two types of memory and these are Program Memory and Data Memory. Program
Memory (ROM) is used to permanently save the program being executed, while Data Memory (RAM)
is used for temporarily storing data and intermediate results created and used during the operation of
the microcontroller. Depending on the model in use (we are still talking about the 8051
microcontroller family in general) at most a few Kb of ROM and 128 or 256 bytes of RAM is used.
However…

5
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

All 8051 microcontrollers have a 16-bit addressing bus and are capable of addressing 64 kb memory.
It is neither a mistake nor a big ambition of engineers who were working on basic core development. It
is a matter of smart memory organization which makes these microcontrollers a real “programmers’
goody“.

EA=0 In this case, the microcontroller completely ignores internal program memory and executes
only the program stored in external memory.
EA=1 In this case, the microcontroller executes first the program from built-in ROM, then the
program stored in external memory.
In both cases, P0 and P2 are not available for use since being used for data and address transmission.
Besides, the ALE and PSEN pins are also used.

Special Function Registers (SFRs)


Special Function Registers (SFRs) are a sort of control table used for running and monitoring the
operation of the microcontroller. Each of these registers as well as each bit they include, has its name,
address in the scope of RAM and precisely defined purpose such as timer control, interrupt control,
serial communication control etc. Even though there are 128 memory locations intended to be
occupied by them, the basic core, shared by all types of 8051 microcontrollers, has only 21 such
registers. Rest of locations are intensionally left unoccupied in order to enable the manufacturers to
further develop microcontrollers keeping them compatible with the previous versions.

6
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

Counters and Timers


As you already know, the microcontroller oscillator uses quartz crystal for its operation. As the
frequency of this oscillator is precisely defined and very stable, pulses it generates are always of the
same width, which makes them ideal for time measurement. Such crystals are also used in quartz
watches. In order to measure time between two events it is sufficient to count up pulses coming from
this oscillator. That is exactly what the timer does. If the timer is properly programmed, the value
stored in its register will be incremented (or decremented) with each coming pulse, i.e. once per each
machine cycle. A single machine-cycle instruction lasts for 12 quartz oscillator periods, which means
that by embedding quartz with oscillator frequency of 12MHz, a number stored in the timer register
will be changed million times per second, i.e. each microsecond.
The 8051 microcontroller has 2 timers/counters called T0 and T1. As their names suggest, their main
purpose is to measure time and count external events. Besides, they can be used for generating clock
pulses to be used in serial communication, so called Baud Rate.

Interrupts
There are five interrupt sources for the 8051, which means that they can recognize 5 different events
that can interrupt regular program execution. Each interrupt can be enabled or disabled by setting bits
of the IE register. Likewise, the whole interrupt system can be disabled by clearing the EA bit of the
same register. Refer to figure below.
Now, it is necessary to explain a few details referring to external interrupts- INT0 and INT1. If the IT0
and IT1 bits of the TCON register are set, an interrupt will be generated on high to low transition, i.e.
on the falling pulse edge (only in that moment). If these bits are cleared, an interrupt will be
continuously executed as far as the pins are held low.

CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________

LAB IN CHARGE: DATE:

7
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-2
AIM: Basic Tutorial for Keil Software

APPARTUS:
 Minimum PC requirement:
Windows XP SP2, Windows Vista or Windows 7
Mouse or Similar Pointing Device
Available Hard Disk Space
C51: 200Mbytes
C251: 100Mbytes
C166: 100Mbytes
MDK: 1.4GBytes
1GBytes of RAM (2GB recommended)
Pentium Class PC

THEORY:
The sample program included in the tutorial toggles Ports 1 and 2 on the 8051
Basic Keil Tutorial
Open Keil from the Start menu
The Figure below shows the basic names of the windows referred in this document

Starting a new Assembler Project


1. Select New Project from the Project Menu.
2. Name the project ‘Toggle.a51’
3. Click on the Save Button.
4. The device window will be displayed.
5. Select the part you will be using to test with.
For now we will use the Dallas Semiconductor part DS89C420.
6. Double Click on the Dallas Semiconductor

8
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

In case of Atmel AT89C51 select it from


7. Click OK.

Creating Source File


1. Click File Menu and select New.

9
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

2. A new pop-up window will open in the Keil IDE.

3. Write a program to toggle the Port.

4. Click on File menu and select Save As… .ASM and SAVE it

10
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

Adding File to the Project


1. Expand Target 1 in the Tree Menu

2. Click on Project and select Targets, Groups, Files…

3. Click on Groups/Add Files tab

4. Under Available Groups select Source Group 1

5. Click Add Files to Group… button

6. Add created .ASM file

7. Click OK when return to target menu

8. Expand the Source Group 1 in the Tree menu to ensure that the file was added to the project

11
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

Creating HEX file


1. Click on Target 1 in Tree menu
2. Click on Project Menu and select
Options for Target 1
3. Select Target Tab Change Xtal
(MHz) to 11.059 MHz (or as your
target board crystal frequency)

4. Select an Output tab and click on Creating Hex file, with Hex file name and target folder.
(Note: Here make sure to rename Hex file name for different .ASM files for same Project .uv2
file, otherwise same Hex file will be replaced by new rebuild program)
5. Click on Project Menu and select

6. Rebuild all Target Files

12
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

7. In the Build Window it should report


‘0 Errors (s), 0 Warnings’
8. Now your Hex file is ready to burn into
controller

Edsim Snapshot:

CONCLUSION:

LAB IN CHARGE: DATE:

13
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-3
AIM: Introduction to .ASM programming.

Programme 1: exchange the content of FFh and FF00h

Solution: - here one is internal memory location and other is memory external location. so first the
content of ext memory location FF00h is loaded in acc. then the content of int memory location FFh is
saved first and then content of acc is transferred to FFh. now saved content of FFh is loaded in acc and
then it is transferred to FF00h.

Mov dptr, #0FF00h ; take the address in dptr


Movx a, @dptr ; get the content of 0050h in a
Mov r0, 0FFh ; save the content of 50h in r0
Mov 0FFh, a ; move a to 50h
Mov a, r0 ; get content of 50h in a
Movx @dptr, a ; move it to 0050h

Programme 2: store the higher nibble of r7 in to both nibbles of r6

Solution: –first we shall get the upper nibble of r7 in r6. Then we swap nibbles of r7 and make OR
operation with r6 so the upper and lower nibbles are duplicated

Mov a, r7 ; get the content in acc


Anl a, #0F0h ; mask lower bit
Mov r6, a ; send it to r6
Swap a ; xchange upper and lower nibbles of acc
Orl a, r6 ; OR operation
Mov r6, a ; finally load content in r6
Programme 3: treat r6-r7 and r4-r5 as two 16 bit registers. Perform subtraction between them. Store
the result in 20h (lower byte) and 21h (higher byte).

Solution: - first we shall clear the carry. Then subtract the lower bytes afterward then subtract higher
bytes.

Clr c ; clear carry


Mov a, r4 ; get first lower byte
Subb a, r6 ; subtract it with other
Mov 20h, a ; store the result
Mov a, r5 ; get the first higher byte
Subb a, r7 ; subtract from other
Mov 21h, a ; store the higher byte

14
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

Programme 4: divide the content of r0 by r1. Store the result in r2 (answer) and r3 (reminder). Then
restore the original content of r0.

Solution:-after getting answer to restore original content we have to multiply answer with divider and
then add reminder in that.

Mov a, r0 ; get the content of r0 and r1


Mov b, r1 ; in register A and B
Div ab ; divide A by B
Mov r2, a ; store result in r2
Mov r3, b ; and reminder in r3
Mov b, r1 ; again get content of r1 in B
Mul ab ; multiply it by answer
Add a, r3 ; add reminder in new answer
Mov r0, a ; finally restore the content of r0
Programme 5: transfer the block of data from 20h to 30h to external location 1020h to 1030h.

Solution: - here we have to transfer 10 data bytes from internal to external RAM. So first, we need
one counter. Then we need two pointers one for source second for destination.

Mov r7, #0Ah ; initialize counter by 10d


Mov r0, #20h ; get initial source location
Mov dptr, #1020h ; get initial destination location
Nxt: Mov a, @r0 ; get first content in acc
Movx @dptr, a ; move it to external location
Inc r0 ; increment source location
Inc dptr ; increase destination location
Djnz r7, nxt ; decrease r7. if zero then over otherwise move next

Programme 6: find out how many equal bytes between two memory blocks 10h to 20h and 20h to
30h.

Solution: - here we shall compare each byte one by one from both blocks. Increase the count every
time when equal bytes are found

Mov r7, #0Ah ; initialize counter by 10d


Mov r0, #10h ; get initial location of block1
Mov r1, #20h ; get initial location of block2
Mov r6, #00h ; equal byte counter. Starts from zero
Nxt: Mov a, @r0 ; get content of block 1 in acc
Mov b, a ; move it to B
Mov a, @r1 ; get content of block 2 in acc
Cjne a, b, nomatch ; compare both if equal

15
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

Inc r6 ; increment the counter


Nomatch: inc r0 ; otherwise go for second number
Inc r1
djnz r7, nxt ; decrease r7. if zero then over otherwise move next

Ex: To Practice 8051 program based on instructions like arithmetic and logical operation.

CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________

LAB IN CHARGE: DATE:

16
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-4
AIM: Write & run programs using MOV instructions- Direct Register and Immediate addressing
mode and Indirect addressing mode.

THEORY :

The 8051 provide a total of five distinct addressing modes. They are as follows.
1. Immediate addressing mode.
2. Register addressing mode.
3. Direct addressing mode.
4. Register addressing mode.
5. Indirect addressing mode.

 Immediate Addressing Mode.

In this addressing mode, the source operand is a constant, i.e. immediate data. Note that
the immediate data must be preceded by the pound sing “ # “ .

Example
MOV A,#82H ; load 82H into A.
MOV R4,#100 ; load the decimal value 100 into R4.
MOV P1,#55H ; load 55H directly to port 1.

 Register Addressing Mode.

Register addressing mode involves the use of register to hold


Data to be manipulated.

Example
MOV A,RO ; Copy the contents of R0 into A.
MOV R7,DPL ; Load R7 with contents of DPL,

 Direct Addressing Mode.

In direct addressing mode, the data is in a RAM memory


Location address is known, and this address is given as part of the
instruction.
Although the entire 128 bytes of RAM can be accessed using
direct addressing mode.
It is most often used to access RAM locations 30-7FH.

Example :
MOV R0,40H : save data of RAM location 40H in R0,
MOV 03H,04H : it copies data in R4 to R3.

 Indirect Addressing Mode.

In the register addressing mode, a register is used as pointer to

17
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

The data.
It is symbol is “@”,
Example :
MOV @R1,B : move data of B into RAM location whose
Address is held by R1,

 Indexed Addressing Mode.

Index addressing mode is widely used in accessing data element


Of look-up table entries located in the program ROM space of the
8051.

Example :
MOV A,@A+DPTR
MOV A,@A+PC

Program 01 : Place the number 3Bh in internal RAM location 40h to 41h.

Solution :

MOV A,#3BH : Data at A = 3Bh


MOV 40H,A : Result at (40)h = 3Bh.
MOV 41H,A : Result at (41)h = 3Bh.
MOV 42H,A : Result at (42)h = 3Bh.

Result :
RAM Address Data
40h 3Bh
41h 3Bh
42h 3Bh

Program 02 : Set timer 1 at A25ER.

Solution :

MOV TL1,#5EH : Timer 1 lower byte.


MOV TH1,#OA2H : Timer 1 Higher byte.
Result :
[a] After Execution
Special Function Register Contents
TL1 5Eh
TH1 A2h

Program 03 : Copy the data in Internal RAM location 10h to 13h to Internal
RAM location 20h to 23h : Copy from 10h to 20h, 11h to 21h,
And so on.
Solution :
MOV 20H,10H : Copy data from RAM location 10h to 20h.

18
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

MOV 21H,11H : Copy data from RAM location 11h to 21h.


MOV 22H,12H : Copy data from RAM location 12h to 22h.
MOV 23H,13H : Copy data from RAM location 13h to 23h.

Result :

[A] Before Execution [B] After Execution


Internal RAM Addr. Data Internal RAM Addr. Data
10h 55h 20h 55h
11h 66h 21h 66h
12h 77h 22h 77h
13h 88h 23h 88h

Program 04 : Load R7 by data from external location A397H.

Solution :
MOV DPTR,#0A397h : DPTR pointer at ext RAM Loc. A397H
MOVX A,@DPTR : Get data in accumulator.
MOV R7,A : Load data at R7 register.

Result:
[A] Before Execution [B] After Execution.
External RAM addr. Data Register Data
(A397)h 75h R5 75h

Program 05 : Copy the byte at Internal RAM address 27h to external RAM
Address 27h.

Solution :
MOV A, 27H : get the data from 27h.
MOV R0,#27H : R0 used as a pointer for RAM address 27h.
MOVX @R0,A : store the data at ext. RAM address 27h.

Result :
[A] Before Execution [B] After Execution
Internal RAM addr. Data External RAM addr. Data
(27)h 99h (27)h 99h
Program 06 : Copy the data in external RAM Location 0123h to TL0 and
Data external RAM location 0234h to TH0.

Solution :
MOV DPTR,#0123H : DPTR pointer at 0123H
MOVX A,DPTR : get data in register A
MOV TL0,A : put result at TL0
MOV DPTR,#0234H : DPTR pointer at 0234H
MOVX A,@DPTR : get data in register A
MOV TH0,A : put result at TH0

Result :

19
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

[A] Before Execution [B] After Execution


External RAM addr. Data Register Data
(0123)h 55h TL0 55h
(0234)h 88h TH0 88h

Program 07 : copy the data byte at address 2050h to external RAM address
2050h.

Solution :
MOV A, #00H : clear accumulator
MOV DPTR,#2050H : DPTR pointer at 2050h
MOVC A,@A+DPTR : get data external ROM
MOVX @DPTR,A : Put the data at external RAM

Result :
[A] Before Execution [B] After Execution
Internal RAM addr. Data External RAM addr. Data
(2050)h 3Eh (2050)h 3Eh

Program 08 : copy the data of external code memory address 0030h to IE

Solution :
MOV A,#00H : Clear accumulator
MOV DPTR,#0030H : DPTR pointer at ext ROM addr 0040h
MOVC A,@A+DPTR : Get data ext ROM addr 0040h
MOV IE,A : Put the result at IE register
Result :
[A] Before Execution [B] After Execution
External ROM addr. Data Register Data
(0030)h 78h IE 78h

Program 09 : Exchange the contents of R0 and the R1.

Solution :
MOV A,R0 : get the data from Ro
MOV A,R1 : exchange between A and R1
MOV R0,A : store the result in R0

Result :
[A] Before Execution [B] After Execution
Register Data Register Data
R0 34h R0 5Ah
R1 5Ah R1 34h

20
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

Program 10 : Exchange both low nibbles of register R5 and R6: put the low
Nibble of R5,and the low nibble of R6 and R5.

Solution :
MOV A,R5 : Get the data from R5
MOV R0,#06H : R0 used for indirect operation for R6
XCHD A,@R0 : exchange the low nibbles of A and R6
MOV R5,A : put the result in R5

Result :
[A] Before Execution [B] After Execution
Register Data Register Data
R5 75h R5 79h
R6 89h R6 85h

CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________

LAB IN CHARGE: DATE:

21
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-5
AIM: Write & Run program using PUSH and POP instruction.

THEORY:

A push opcode copies data from the source address to the stack. SP is in cerement by 1 before
the data is copied to the internal RAM location contain in to the Internal RAM. The stack grows up in
memory as it is push. Excessive pushing can make the stack exceed 7FH, after which PUSH hed data
is lost

A push opcode copies data from the stack to the destination address SP is decremented by 1
after data is copied from the stack RAM location to the direct destination to ensure that data placed on
the stack is retrived in the sane order as it was stored.

The push and pop opcodes.

MNEMONIC OPERATION

PUSH ADD Increment SP; copy the data to the


Internal RAM address contain in SP.

POP ADD copy the data from the internal RAM


Address contain in SP to add
Decrement the SP.

The SP register is set to 07h when the 8051 is reset, which is the same direct address
RAM as register R7 in bank 0. The first push opcode would write data to R0 of bank 1.

Program 01 : copy the byte in TCON to register R2 using at least fore different method.

Method 01 : use to direct address for TCON (88H) and register R2

MOV R2,88H : copy TCON to R2

Method 02 : use the direct address for TCON and R2

MOV 02H,88H : copy TCON to direct address 02h(R2)

Method 03 : use R1 as a point to R2 and use the address of TCON.

MOV R1,#02H : use R1 as a pointer to R2


MOV @R1,88H : copy TCON byte to address in R1
(02h=R2)

Method 04 : push the contents of TCON into direct address 02h(R2)

MOV 81H,#01H : set the SP to address 01 in RAM location


PUSH 88H : push TCON (88H) to address 02H(R2)

Program 02 : put the number 8Dh in RAM location 30h to 34h.

22
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

MOV 30H,#8DH : Copy the num 8d to RAM addr 30h


MOV 81H,#30H : set the SP to 30h
PUSH 30H : push the contents of 30h to address 31h
PUSH 30H : continues pushing to address 34h
PUSH 30H :
PUSH 30H : total : 14 bytes . 6 lines

Program 03 : swap the contain of register R7 and R6 in register bank 0.

PUSH 07H : push contain of R7 on the stack


PUSH 06H : push contain of R6 on the stack
POP 07H : pop contain of R6 to R7
POP 06H : pop contain of R7 to R6

TOTAL 8 BYTES , 4 LINES

CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________

LAB IN CHARGE: DATE:

23
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-6
AIM: Write & Run Programs using Logical Instructions.
THEORY:

[ A. AND LOGIC ]

1. ANL A,#DATA
2. ANL A,Rn
3. ANL A,direct
4. ANL A,@Rp
5. ANL direct,A
6. ANL drict,#DATA

[B. OR LOGIC]

1. ORL A, #DATA
2. ORL A,Rn
3. ORL A, direct
4. ORL A.@Rp
5. ORL driect,A
6. ORL driect,#Data

[C. EX-OR LOGIC ]

1. XRL A,#DATA
2. XRL A,Rn
3. XRL A,direct
4. XRL A,@Rp
5. XRL direct,A
6. XRL dricet,#data

[D. CLEAR & COMPLEMENT]

1. CLR A
2. CPL A

[E. BIT LEVAL LOGIC INSTRCTION ]

1. ANL C,BIT
2. ANL C,/BIT
3. ORL C,BIT
4. ORL C,/BIT
5. CPL C
6. CPL BIT
7. CLR C
8. CLR BIT
9. STEB C
10. STEB BIT

24
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

[F. ROTATE & SAWP ]

1. RL A
2. RR A
3. RLC A
4. RRC A
5. SWAP A

Program 01 : Invert the data on the port 0 pins & write the data to port 1.

MOV A,P0 : get data at A=1AH


CPL A : complement data (A=E5H)
MOV P1,A :result at p1=E5h

Program 02 : swap the nibbles of R0 and R1 , so that the lower nibble of R0 swaps with higher
nibble of R1 & vice-versa.

MOV A,R0 : get data at A=81h


SWAP A : exchange nibbles ( A=18h)
MOV R0,A : store at R0
MOV A,R1 : get data at A=2Ah
SWAP A : exchange nibbles (A=A2h)
XCH A,R0 : exchange between two register
MOV R1,A : store at R1

Program 03 : make the low nibble of R5 the complement of the high nibble of R6.

MOV A,R6 : get data at A=25h


XRL A,#0F0H :complement higher nibble
SWAP A : exchange nibble
MOV R1,#05H :use R1 as pointer
XCHD A,@R1 : exchange nibbles (R5=8DH)

Program 04 : find a number that when xo red to A register , result in the number 3fh in A.

MOV R0,A : get data at R0


XRL A,#3FH : A .XOR. 3FH ; Forming N
XRL A,R0 : A .XOR. N ; Result 3Fh

RESULT :
REGISTER DATA
A 3FH

Program 05 : move bit 4 of RAM location 30h to bit 2 of A.

MOV 20H,30H : get data at bit addr, location


MOV C,04H : copy to carry flag
MOV ACC.2,C : move to 2nd bit of A

25
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

RESULT :
REGISTER DATA
A 11000100

program 06 : rotate the contents of four times left and add it in the content of R1 and store the
result in internal RAM location 2AH.

MOV A,R0 : get the data from R0 to accumulator.


RL A : rotate acc 1-bit to left position
RL A : rotate acc 1-bit to left position
RL A : rotate acc 1-bit to left position
RL A : rotate acc 1-bit to left position
ADD A,R1 : add the contents with R1
MOV 2AH,A :store the result at RAM address 2AH

RESULT :
INTENAL RAM DATA
2AH 5CH

Program 07 : double the number in reg. R2 ; and put the result in register R3 (high byte)

CLR C : clear carry flag


MOV A, R2 : get data to acc.
RLC A : rotate acc 1-bit to left with carry.
MOV R4,A : store low byte at R4
CLR A : clear accumulator
RLC A : the carry bit is now other bit of A
MOV R3,A : store high byte at R3

RESULT :
REGISTER DATA
R3 01H
R4 28H

Program 08 : rotate DPTR 1-bit left.

CLR C : clear carry flag


MOV A,DPL : get LSB of DPTR to Acc.
RLC A : rotate acc 1-bit to left with carry
MOV DPL,A : store at DPL
MOV A,DPH : get MSB of DPTR to Acc.
RLC A : rotate acc 1-bit to left with carry
MOV DPH,A : store at DPH
CLR A : clear Accumulator
RLC A : rotate Acc 1-bit to left with carry
ORL A,DPL :OR with DPL
MOV DPL,A : store result at DPL

26
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

RESULT :
REGISTER VALUE
DPTR 2468H

CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________

LAB IN CHARGE: DATE:

27
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-7
AIM: Write & Run programs using Arithmetic Instructions.
THEORE:

AIRTHMETIC INSTRUCTION

[ A. INCREMENT]
1. Inc A
2. Inc Rn
3. Inc add
4. Inc DPTR

[B. DECREMENT]
1. DEC A
2. DEC Rn
3. DEC add
4. DEC @Rp

[C. ADDITION]
1. ADD A,#DATA
2. ADD A,Rn
3. ADD A,DRIECT
4. ADD A,@Rp

[D. ADDITION WITH CARRY BIT]


1. ADDC A,#DATA
2. ADDC A,Rn
3. ADDC A,DIRECT
4. ADDC A,@Rp

[E. BCD ARITHMETIC]


1. DA A

[F. SUBTRACTION WITH BORROW]


1. SUBB A.#DATA
2. SUBB A,Rn
3. SUBB A,DIRECT

[G. MULTIPLICATION]
1. MUL AB

[H. DIVISION]
1. DIV AB

Program o1 : Add the num found in Ram location 25h,26h and 27h put the result in ram
location 31h (LSB) and (MSB)

MOV 31H,#00H : clear MSB of the result


MOV A,25H : get first no in Acc.
ADD A,26H :Add with second num.
MOV RO,A :store result at R0

28
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

MOV A,#00H :clear Acc.


ADDC A,#31H :Add the carry to MSB
MOV 31H,A :store MSB
MOV A,Ro :get previous result from R0
ADD A,27H :Add third number
MOV A,#00H :store LSB
ADDC A,31H :clear Acc.
END

RESULT :
RAM ADDR. DATA
30H(LSB) C2H
31H(MSB) O1H

Program 02 : Add the byte in register R0 and R1 : put the resulet in external RAM

MOV A,R0 : get first no in Acc.


ADD A, R1 : Add with second number
MOV DPTR,#2050H : DPTR point to external memory
MOVX @DPTR,A : store LSB
MOV A,#00H : clear Acc.
ADDC A,00H : get carry flag to A
INC DPTR : DPTR pointer to next location
MOVX @DPTR,A: store MSB

RESULT :
EXTENAL RAM ADDR DATA
2050 (LSB) F2H
2051 (MSB) 00H

Program 03 : subtract the connect of RAM location 13h from RAM location 2Bh; put result in
RAM location 3Ch.

CLR C : clear carry flag


MOV A,2BH : get first no in Acc
SUUB A,13H : subtract
MOV 3CH,A : store result

RESULT :
RAM ADDR DATA
3CH 5FH

Program 04 : subtract the connect of R2 from the number F3h; put result in external RAM
location 028Bh

CLR A : clear carry flag


MOV A,#F3H : get first no. in Acc.
SUBB A,R2 :subtract
MOV DPTR,#028BH : DPTR pointer to external memory
MOVX @DPTR,A : store result

29
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

RESULET :
EXTENAL RAM ADDERSS DATA
028B H 2EH

Program 05 : Increment the content of RAM location 13h,14h and 15h using increment
addressing only.

MOV R0,#13H : R0 pointer RAM location 13h


INC @R0 :now ram location 13h=c6h
INC R0 : now R0 pointer to RAM location 14h
INC @R0 : now ram location 14h=51h
INC R0 : now R0 pointer to RAM location 15h
INC @R0 : now ram location 15h=4Fh

RESULT :
RAM LOCATION DATA
13H C6H
14H 51H
15H 4FH

Program 06 : Decrement contents of TL0,TH0 TL1 and TH1 by 1.

DEC TL0 : decrement contents of TL0 by 1


DEC TH0 : decrement contents of TH0 by 1
DEC TL1 : decrement contents of TL1 by 1
DEC TH1 : decrement contents of TH1 by 1

RESULT :
Then TL0=1oh, TH0=21H, TL1=32H AND TH1=43H

Program 07 : multiply the using number in register R3 by the unigened number on port 2 and
put the result in external RAM location 10H(MSB) and 11H(LSB)

MOV A,R3 : get first num from R3 to A


MOV B,0A0H : get second num from P2 to B
MUL AB : multiply
MOV R0,#0011H : R0 point to external memory
MOV A,B :get result to A
DEC R0 : decrement R0
MOVX R0,A : store high byte at external

RESULT :
EXTENAL RAM ADDERSS DATA
0010H (MSB) 02H
0011H (LSB) A0H

Program 08 : divide data in internal RAM location 45H by data 12H put quotient in external
RAM location 4545H and reminder in external RAM location 4546H.

MOV A,45H : get the num in A


MOV B,#12H : get the division in B

30
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

DIV AB : divide
MOV DPTR,#4545H : DPTR pointer to 4545h
MOV @DPTR ,A : store result at external memory
INC DPTR : increment DPTR
MOV A,B : get remainder at A
MOV @DPTR,A : store remainder at next memory location

RESULT :
EXTENAL RAM ADDR. DATA
4545H 01H
4546H 03H

CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________

LAB IN CHARGE: DATE:

31
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-8
AIM: Write & run Programs using Looping Techniques.
THEORY:

LOOP AND JUMP INSTRUCTIONS

Let us start with a simple example that will help you to learn how to create loops in assembly. In the
following code the instruction DJNZ is used to reduce the counter and is repeated till the counter
becomes zero.

Eg-1:

ORG 0H
MOV A, #0 ; clear A
MOV R1, #10 ; load counter R1 =10
AGAIN: ADD A, # 05 ; add five to register A
DJNZ R1, AGAIN ; repeat until R1=0 (10 times)
MOV R3, A ; save A in R3
END

In this code R1 acts as a counter. The counter value is initialized i.e. 10 HEX is loaded to R1. In each
iteration, the instruction DJNZ decrements R1 by one until it becomes zero. This loop adds 5 HEX to
A every time it runs. After ten iterations R1 becomes zero and the instructions below it are executed.

Note: - Some Jump statements can only be performed on some special register A (or bit CY) as
mentioned in the table below.

32
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

Nested loops:

ORG 0H
MOV A, #55H ; A= 55 hex
MOV R1, #100 ; the outer counter R1 =100
NEXT: MOV R2, # 20 ; the inner counter
AGAIN: CPL A, # 05 ; add five to register A
DJNZ R2, AGAIN ; repeat until R1=0 (100 times)
DJNZ R1, NEXT ; repeat till 20 times (outer loop)
END

SJMP refers to short jump and LJMP refers to long jump. All the conditional jumps are short jumps.

SJMP: This instruction is of two bytes in which first one is opcode & second is the address. The
relative address of the instruction called should be in between -127 to 127 bytes from the current
program counter (PC).

LJMP: This instruction is of three bytes in which the first is the opcode and the second & third are for
address. The relative address of the instruction can be anywhere on the ROM.

So it is clear from the above examples that we can use different jump instructions with a
condition or counter called conditional loop. And when we create loop inside an existing loop it
is called nested loop.

CALL INSTRUCTIONS:

Example:

LCALL (long call)

ORG 0H
BACK : MOV A, #55H ; load A= 55 hex value
MOV P1, A ; issue value of register A to port1
LCALL DELAY ; to call DELAY function created below
MOV A, #0AAH ;load AAH hex value to A
MOV P1,A ;issue value of register A to port 1
LCALL DELAY ; to call DELAY function as created below
SJMP BACK ; keep doing this
; ________ this is the delay subroutine

DELAY:
MOV R5, #0FFH ; R5= 255 hex, the counter
AGAIN: DJNZ R5, AGAIN ; stay here until R5 becomes zero
RET ; return to caller
END

33
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

In this code we keep on toggling the value of the register of port 1 with two different hex values and a
DELAY subroutine is used to control how fast the value is changing. Here in DELAY subroutine the
program is kept busy by running an idle loop and counting 256 counts. After the DELAY subroutine
is executed once the value of port 1 is toggled and this process goes on infinitely.

CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________

LAB IN CHARGE: DATE:

34
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-9
AIM: Write & Run Programs using Timers in different mode.
THEORY:
For the CPU to execute an instruction takes a certain number of clocks cycle 8051 family.
These clock cycles are referred to as machine cycles. To calculate a time delay.

We use this list. In the 8051 family the length of the machine cycle depends on the frequency
of the crystal oscillator connected the 8051 system. The crystal oscillator along with on-chip circuitry
provides the clock source for 8051 CPU.

The frequency of the craystal connects to the 8051 family can very from 4mhz to 30mhz
depanding on the chip rating and manufacture very often the11.092 MHz crystal oscillator is used to
make the 8051 base system compatible with the serial part of the IBM pc.

TIKMER DELAY USING REGISTER :

SOFTWER TIME DELAY


Pure softwer delay can be written by losding count value in some register and loading count
velue in same register and decrement them until they become zero.
Example
DELAY : MOV R3,#0FFH
HERE : DJNZ R3 HERE
RET
For an 8051 microcontroller a signal instruction cycle is execute for every 12 clock cycle of the
processor clock. The time to execute the instruction is found multiplaing c by 12 and dividing product
by the crystal frequency.
T=[C x 12d] / crystal frequency

Thus. For an 8051 clocked at 12 MHz. the instruction. Cycle time is one microsecond.

Example
Find the size of the delay in the following program. If the crystal frequency is 11.0592 MHz

MOV A,#55H
AGAIN: MOV P1,A
ACALL DELAY
CPL A
SJMP AGAIN
;--------------------- TIME DELAY
DELAY: MOV R3, #200
HERE : DJNZ R3, HEAR
RET

Ans:
We have the following machine cycle for each instruction of the DELAY subroutine
machine cycle

DELAY : MOV R3, #200 1


HERE : DJNZ R3, HERE 2
RET

35
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

We have a time delay of [(200*2)11+1]*1.085us = 436.17

LOOP INSIDE LOOP DELAY

Example
For a machine cycle of 1 Us find the time delay in following subroutine.

Machine cycle
DELAY : MOV R2, #200 1
AGAIN : MOV R3,#250 1
HERE : NOP 1
NOP 1
DJNZ R3,HERE 2
DJNZ R2,AGAIN 2
RET

Ans:
For the here loop we have
(4*250)*1 us = 1000us = 1Ms

The AGAIN loop repeat the HERE loop 200 times =200*100us
=200 Ms

AGAIN loop add (3*200*1us)=600 us

TIMER DELAY USING TIMER

It is a 16-bit timer : there for. It allows values of 0000 to FFFFH to be loaded in to the timer
register TL and TH.

Example

Indicate which mode and which timer are selected for each of the following
(A) MOV TOMD, #01H
(B) MOV TMOD,#20H
(C) MOC TMOD,#12H

Solution :

(A) TMOD = 0000 0001 : mode 1 of timer 0 is selected


(B) TMOD = 0010 0000 : mode 2 of timer 1 is selected
(C) TMOD = 0001 0010 : mode 2 of timer 1 is selected

TIMER DELAY USING CONTER

Program : write a program for counter 1 in mode 2 to count the pulses and display the state of TL1
and count on P2 . assume that clock pulses are fed in to pin T1.

MOV TMOD,#01100000B
MOV TH1,#00H
SETB P3.5

36
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

HERE: SETB TRL


BACK : MOV A,TL1
MOV P2,A
JNB TF1,BACK
CLR TR1
CRL TF1
SJMP HERE
END

CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________

LAB IN CHARGE: DATE:

37
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-10
AIM: To study the interfacing of LED with µC 89C51

APPARATUS:
 Micro controller 89C51
 LED
 12 MHz crystal
 7805 voltage regulator IC
 Bridge rectifier IC
 33 PF capacitor
 10 microf/63V capacitor
 8K2 resistor
 1K resistors

THEORY:
LEDs are by far the most widely used means of taking output. They find huge application as indicators
during experimentations to check the validity of results at different stages. They are very cheap and
easily available in a variety of shape, size and colors.

The principle of operation of LEDs is simple. The commonly available LEDs have a drop voltage of
1.7 V and need 10mA to glow at full intensity. The following circuit describes “how to glow a led”.

The value of resistance R can be calculated using the equation, R= (V-1.7)/10 mA. Since most of the
controllers work on 5V, so substituting V= 5V, the value of resistance comes out to be 330 ohm. The
resistance 220 ohm, 470 ohm is commonly used substitute in case 330 ohm is not available.

CIRCUIT DIAGRAM:
LEDs are connected to the port P0. LEDs need approximately 10mA current to flow through them in
order to glow at maximum intensity. However the output of the
controller is not sufficient enough to drive the LEDs, so if the
positive leg of the LED is connected to the pin and the negative
to ground as shown in the figure, the LED will not glow at full
illumination.
To overcome this problem LEDs are connected in the reverse
order and they run on negate.ve logic i.e., whenever 1 is given

38
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

on any pin of the port, the LED will switch off and when logic 0 is provided the LED will glow at full
intensity.
As soon as we provide supply to the controller, the LEDs start blinking i.e., they become on for a
certain time duration and then become off for the same time duration. This delay is provided by calling
the delay function.

ALGORITHM:
Start

Turn ON LED

Wait for some time (delay)

Turn OFF LED

Wait for some time (delay)

Go to start

You can see in the modified algorithm that after turning ON the LED the controller waits for the delay
period & then turns OFF the led & again waits for the delay period & then goes back to the start.

39
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

PROGRAM:
ORG 0000h
loop: CLR P1.0
CALL DELAY
SETB P1.0
CALL DELAY
JMP loop

delay: mov R7,#100


l1_delay: djnz r7,l1_delay
ret

PROGRAM :- To ON – OFF two LED simultaneously using Arduino.

void setup()
{
pinMode(13, OUTPUT); // initialize digital pin 13 as an output.
pinMode(12, OUTPUT); // initialize digital pin 12 as an output.
}

// the loop function runs over and over again forever


void loop()
{
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

40
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

CIRCUIT DAIGRAM :-
Interfacing of Two Different LED’S with Arduino Uno R3 Board.

CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________

LAB IN CHARGE: DATE:

41
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-11
AIM: To study the interfacing of relay with 8051 microcontroller.

APPARTUS:
 Micro controller 89C51
 Relay
 12 MHz crystal
 7805 voltage regulator IC
 Bridge rectifier IC
 33 PF capacitor
 10 µF/63V capacitor
 8K2 resistor
 1K resistors

THEORY:

A relay is an electrically operated switch. Many relays use an electromagnet


to operate a switching mechanism mechanically, but other operating
principles are also used. Relays are used where it is necessary to control a
circuit by a low-power signal (with complete electrical isolation between
control and controlled circuits), or where several circuits must be controlled
by one signal. The first relays were used in long distance telegraph circuits,
repeating the signal coming in from one circuit and re-transmitting it to another. Relays were used
extensively in telephone exchanges and early computers to perform logical operations.

A type of relay that can handle the high power required to directly control an electric motor is called a
contractor. Solid-state relays control power circuits with no moving parts, instead using a
semiconductor device to perform switching. Relays with calibrated operating characteristics and
sometimes multiple operating coils are used to protect electrical circuits from overload or faults; in
modern electric power systems these functions are performed by digital instruments still called
"protective relays".

A simple electromagnetic relay consists of a coil of wire surrounding a soft iron core, an iron yoke
which provides a low reluctance path for magnetic flux, a movable iron armature, and one or more sets
of contacts (there are two in the relay pictured). The armature is hinged to the yoke and mechanically
linked to one or more sets of moving contacts. It is held in place by a spring so that when the relay is
de-energized there is an air gap in the magnetic circuit. In this condition, one of the two sets of

42
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

contacts in the relay pictured is closed, and the other set is open. Other relays may have more or fewer
sets of contacts depending on their function. The relay in the picture also has a wire connecting the
armature to the yoke. This ensures continuity of the circuit between the moving contacts on the
armature, and the circuit track on the printed circuit board (PCB) via the yoke, which is soldered to the
PCB.

When an electric current is passed through the coil it generates a magnetic field that attracts the
armature and the consequent movement of the movable contact either makes or breaks (depending
upon construction) a connection with a fixed contact. If the set of contacts was closed when the relay
was de-energized, then the movement opens the contacts and breaks the connection, and vice versa if
the contacts were open. When the current to the coil is switched off, the armature is returned by a
force, approximately half as strong as the magnetic force, to its relaxed position. Usually this force is
provided by a spring, but gravity is also used commonly in industrial motor starters. Most relays are
manufactured to operate quickly. In a low-voltage application this reduces noise; in a high voltage or
current application it reduces arcing.When the coil is energized with direct current, a diode is often
placed across the coil to dissipate the energy from the collapsing magnetic field at deactivation, which
would otherwise generate a voltage spike dangerous to semiconductor circuit components. Some
automotive relays include a diode inside the relay case. Alternatively, a contact protection network
consisting of a capacitor and resistor in series (snubber circuit) may absorb the surge. If the coil is
designed to be energized with alternating current (AC), a small copper "shading ring" can be crimped
to the end of the solenoid, creating a small out-of-phase current which increases the minimum pull on
the armature during the AC cycle.

CIRCUIT DIAGRAM:

43
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

WORKING:
Microcontrollers have internal pull up
resistors hence when a port pin is HIGH
the output current flows through this
internal pull up resistor. 8051
microcontrollers have an internal pull up
of 10KΩ. Hence the maximum output
current will be 5v/10k = 0.5ma. This
current is not sufficient to drive the
transistor into saturation and turn ON the
relay. Hence an external pull up resistor
R3 is used. Let us now calculate the value of R3. Normally a relay requires a pull in current of 70ma
to be turned ON. So our BC547 transistor will require enough bases current to make sure it remains
saturated and provide the necessary collector current i.e. 70ma. The gain (hfe) of BC547 is 100 so we
need to provide at least 70ma/100 = 0.7ma of base current. In practice you require roughly double the
value of this current so we will calculate for 1.4ma of base current.
From the above equation the value of R3 comes out to be 5.55KΩ. Typically I use 4.7KΩ resistor.
Whenever 8051 microcontroller is turned ON initially the controller is in reset state and all the
controller pins are HIGH which would result in TURNING ON the relay every time power is turned
ON or if there is a power fluctuation. This may also damage the device connected to relay so as to
avoid this problem another transistor Q2 has been added between the controller & the previous
transistor. This transistor acts as an inverter. Figure 2 shows the updated Relay Driver Circuit. So now
when a High is applied from the controller the TRANSISTOR Q2 turns ON so the base of transistor
Q1 gets 0 so the transistor Q1 turns OFF so the relay turns OFF. And when a LOW is applied from the
controller the TRANSISTOR Q2 turns OFF so the base of transistor Q1 gets high voltage through the
resistor R2 so the transistor Q1 turns ON which turns ON the relay.

PROGRAME:
relay equ P1.0
sw equ P2.0
org 0000h

Main:clr relay ;Configure inp and outp


setb sw

44
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

up: jnb sw,on ;wait for switch to be pressed


clr relay
acall delay
sjmp up
on: setb relay ;Turn ON relay
acall delay
acall delay
here:jb sw,here ;wait for switch to be released
clr relay ;Turn OFF relay
acall delay
acall delay
sjmp up ;Loop

delay:mov r7,#0ffh ;delay subroutine


again:mov r6,#0ffh
djnz r6,$
djnz r7,again
ret

CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________

LAB IN CHARGE: DATE:

45
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-12
AIM: To study the interfacing of LCD with µC 89C51

APPARTUS:
 Micro controller 89C51
 LCD
 12 MHz crystal
 7805 voltage regulator IC
 Bridge rectifier IC
 33 PF capacitor
 10 µF/63V capacitor
 8K2 resistor
 1K resistors

THEORY:

Liquid Crystal Display also called as LCD is very helpful in providing user interface as well as for
debugging purpose.
These LCD's are very simple to interface with the controller as well as are cost effective.

The most commonly used ALPHANUMERIC displays are


1*16(Single line & 16 characters),
2*16(Double line & 16 characters per line),
4*20(Four lines & 20 characters per line).
The LCD requires 3 control lines (RS, R/W & EN) & 8 (or 4) data lines. The number on data lines
depends on the mode then 8 data lines + 3 control lines i.e. total 11 lines are required and if operated
in 4-bit mode then 4 data line required.

Now how do we decide which mode to use?


It’s simple if you have sufficient data lines you can go for 8 bit mode & if the display should be faster
then we have to use 8-bit mode because basically 4-bit mode takes twice as more time.

46
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

PIN DISCRIPTION:

Pin No. Symbol Function


1 Vss Ground
2 Vdd Supply Voltage
3 V0 Contrast Setting
4 RS Register Select
5 R/W Read/Write Select
6 En Chip Enable Signal
7 to 14 DB0-DB7 Data Line
15 A/Vee GND for the backlight
16 K Vcc for backlight

When RS is low (0), the data is to be created as a command. When RS is high (1), the data being sent
is considered as text data which should be displayed on the screen.

When R/W is low (0), the information on the data bus is being written to the LCD. When RW is high
(1), the program is effectively reading from the LCD. Most of the times there is no need to read from
the LCD so this line can directly be connected to GND thus saving one controller line.

The ENABLE pin is used to latch the data present on the data pins. A HIGH - LOW signal is required
to latch the data. The LCD interprets and executes our command at the instant the EN line is brought
low. If you never bring EN low, your instruction will never be executed.

For Contrast setting a 10K pot should be used as shown in the figure.

In this interfacing we used the port 1 & port 3. The data line D0 to D7 of LCD is connected to the port
1.

The chip enable signal, RS signal and R/W signal is connected to the port number 3.

COMMAND OF LCD:

Code Command to Instruction Register Code Command to Instruction Register


(Hex) (Hex)
1 clear display screen C Display on, cursor off
2 Return home E Display on, cursor blinking
4 Decrement cursor (shift cursor left) F Display on, cursor blinking
6 Increment cursor (shift cursor right) 10 Shift cursor position to left
5 Shift display right 14 Shift cursor position to right
7 Shift display left 18 Shift the entire display to the left
8 Display off, cursor off 80 Force cursor to beginning to 1st line
A Display off, cursor on C0 Force cursor to beginning to 2nd line

47
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

CIRCUIT DIAGRAM:

PROGRAME:
ORG 0000H ;
MOV A, #38H ; INITIATE LCD
ACALL COMMWRT ;
ACALL DELAY ;

MOV A, #0EH ; DISPLAY ON CURSOR ON


ACALL COMMWRT ;
ACALL DELAY ;

MOV A, #01H ; CLEAR LCD


ACALL COMMWRT ;
ACALL DELAY ;

MOV A, #84H ; CURSOR AT LINE 1 POSITION 4


ACALL COMMWRT ;
ACALL DELAY ;

MOV A, #'A' ; SEND ASCII DATA


ACALL DATAWRT ;
ACALL DELAY ;

AGAIN :
SJMP AGAIN ;

COMMWRT:
MOV P1, A ;
CLR P2.0 ; RS = 0 FOR COMMAND REGISTER
CLR P2.1 ; R/W = 0 FOR WRITE
SETB P2.2 ; E = 1 FOR HIGH PULSE
ACALL DELAY ;
CLR P2.2 ; E = 0 FOR LOW PULSE
RET

DATAWRT:
MOV P1, A ;
SETB P2.0 ; RS = 1 FOR DATA REGISTER
CLR P2.1 ; R/W = 0 FOR WRITE
SETB P2.2 ; E = 1 FOR HIGH PULSE
ACALL DELAY ;
CLR P2.2 ; E = 0 FOR LOW PULSE
RET

48
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

DELAY :
MOV R3, #50H ;
BACK:
MOV R4, #255H ;
HERE:
DJNZ R4, HERE ;
DJNZ R3, BACK ;
RET
END

PROGRAM :- To Display “ EC GPG ‘’ on 16 * 2 LCD using Arduino.

#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// RS , E , D4 , D5 , D6 , D7 Connected on 12 , 11 , 5 , 4 , 3 , 2 digital pins of Arduino respectively.

void setup ( )
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.print("EC GPG"); // Print a message to the LCD.

void loop ( )
{
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0,1);

49
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

CIRCUIT DIAGRAM :-
Interfacing of 16 * 2 LCD with Arduino Uno R3 Board.

CONCLUSION:

LAB IN CHARGE: DATE:

50
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-13
AIM: To study the interfacing of STEPPER MOTOR with µC 89C51

APPARTUS:
 Micro controller 89C51
 Stepper motor
 ULN2003A
 12 MHz crystal
 5V regulated supply
 33 PF capacitor
 10 µF/63V capacitor
 8K2 resistor
 1K resistors

THEORY:
Uni-polar stepper motor
The unipolar stepper motor has five or six
wires and four coils (actually two coils
divided by center connections on each
coil). The center connections of the coils
are tied together and used as the power
connection. They are called uni-polar
steppers because power always comes in
on this one pole.
Bipolar stepper motor
The bipolar stepper motor usually has four wires coming out of it. Unlike unipolar steppers, bipolar
steppers have no common center connection. They have two independent sets of coils instead. You
can distinguish them from unipolar steppers by measuring the resistance between the wires. You
should find two pairs of wires with equal resistance. If you've got the leads of your meter connected to
two wires that are not connected (i.e. not attached to the same coil), you should see infinite resistance
(or no continuity).

51
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

CIRCUIT DIAGRAM:

WORKING:
A Unipolar Stepper Motor is rotated by
energizing the stator coils in a sequence. In
unipolar stepper, the direction of current in
stator coils is not required to be controlled by
the driving circuit. Just applying the voltage
signals across the motor coils or motor leads in a
sequence is sufficient to drive the motor.
A two phase unipolar stepper motor has a total of six wires/leads of which four are end wires
(connected to coils) and two are common wires. The color of common wires in the stepper motor used
here is Green. Each common wire is connected to two end leads thus forming two phases. The end
leads corresponding to each phase have to be identified.
In some cases, when the leads cannot be directly identified in the motor, the identification of endpoints
and common points can be done by measuring the resistance between the leads. The leads of different
phase will show open circuited condition with respect to each other. This way the leads corresponding
to different phase can be separated. The resistance between any two end points of same phase will be
twice the resistance between a common point and an end point. This way the common and end points
of both the phases can be identified.

52
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

In the circuit, Port P2 is defined as output


port to provide the input sequence for the
stepper motor. Four transistors are used as
switches to drive the motor. The motor
leads are not directly interfaced with the
microcontroller pins because stepper motor
requires 60mA current while AT89C51
has the maximum current rating of 50mA.
So the transistors switches are used to
transfer signals from the microcontroller to
the stepper wires.
The sequence of signals mentioned in the tables above rotates the motor in clockwise direction.
Reversing these input sequences would rotate the motor in counter clockwise direction.
PROGRAME:
org 0H
stepper equ P1
main: mov stepper, #0CH
acall delay
mov stepper, #06H
acall delay
mov stepper, #03H
acall delay
mov stepper, #09H
acall delay
sjmp main
delay: mov r7,#4
wait2: mov r6,#0FFH
wait1: mov r5,#0FFH
wait: djnz r5,wait
djnz r6,wait1
djnz r7,wait2
ret
end

CONCLUSION:

LAB IN CHARGE: DATE:

53
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-14
AIM: To interface LDR and seven segment with microcontroller 89C51.

APPARTUS:
 Micro controller 89C51
 LDR
 Seven segment display
 SN7447A
 12 MHz crystal
 5V regulated power supply
 33 PF capacitor
 330Ω resistor

THEORY:
LDR
A photoresistor or light dependent resistor is a resistor
whose resistance decreases with increasing incident light
intensity; in other words, it exhibits photoconductivity.
A photoresistor is made of a high resistance
semiconductor. If light falling on the device is of high
enough frequency, photons absorbed by the semiconductor
give bound electrons enough energy to jump into the
conduction band. The resulting free electron (and its hole
partner) conduct electricity, thereby lowering resistance.
A photoelectric device can be either intrinsic or extrinsic.
An intrinsic semiconductor has its own charge carriers and is not an efficient semiconductor, e.g.
silicon. In intrinsic devices the only available electrons are in the valence band, and hence the photon
must have enough energy to excite the electron across the entire bandgap. Extrinsic devices have
impurities, also called dopants, added whose ground state energy is closer to the conduction band;
since the electrons do not have as far to jump, lower energy photons (i.e., longer wavelengths and
lower frequencies) are sufficient to trigger the device. If a sample of silicon has some of its atoms
replaced by phosphorus atoms (impurities), there will be extra electrons available for conduction. This
is an example of an extrinsic semiconductor. Photoresistors are basically photocells.
Seven segment display
A seven-segment display (SSD), or seven-segment indicator, is a form of electronic display device for
displaying decimal numerals that is an alternative to the more complex dot-matrix displays. Seven-

54
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

segment displays are widely used in digital clocks, electronic meters, and other electronic devices for
displaying numerical information.
Seven Segment are available in two configuration - (1) Common Anode (2) Common Cathode.
The circuit diagram shows the connections of seven segment to the controller. The pins a to g of the
Seven Segment are connected to the Port P2 of the microcontroller. The common pin of the seven
segment is connected to Vcc. The ‘h’ has not been used, which is the dot pin of the controller.
Since the seven segment display works on negative logic, we will have to provide logic 0 to the
corresponding pin to make an LED glow. Table below shows the hex values used to display the
different digits.

CIRCUIT DIAGRAM:

55
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

PROCEDURE:
1) Connect the set up as shown in the circuit above.
2) Switch ON the supply.
3) Now, when any object passes in front the photocell in the range of 1 ft., the photocell gives
the 12V in the output.
4) Now this 12V is given to 7404 IC which in turn gives 0V as output to the µC.
5) Here, sn7447a IC is used for conversion of BCD to 7 – Segment.
6) Now, as 0V is obtained at µC, 7 – Segment LED is incremented by 1.
7) Thus the passed object is counted.

PROGRAM:
Seven segment Interfacing
ORG 000H //initial starting address
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

PROGRAM :- To Display 0 to 9 number with 1 second delay between each number on 7 Segment using
Arduino.

void setup()
{
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);

56
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop ( ) {
PORTD=0x03;
delay(1000);

PORTD=0x9F;
delay(1000);

PORTD=0x25;
delay(1000);

PORTD=0x0D;
delay(1000);

PORTD=0x99;
delay(1000);

PORTD=0x49;
delay(1000);

PORTD=0x41;
delay(1000);

PORTD=0x1F;
delay(1000);

PORTD=0x01;
delay(1000);

PORTD=0x09;
delay(1000);
}

57
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

CIRCUIT DIAGARAM :-

Interfacing of Common Anode & Segment with Arduino Uno R3 Board.

CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________

LAB IN CHARGE: DATE:

58
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

EXPERIMENT-15
AIM: To study the interfacing of ADC-0804 with µC 89C51

APPARTUS:-
 Micro controller 89C51
 ADC-0804
 12 MHz crystal
 7805 voltage regulator IC
 Bridge rectifier IC
 33 PF capacitor
 10 microf/63V capacitor
 8K2 resistor
 1K resistors

THEORY:

ADC0804 is one of the most commonly used analog to digital converter IC. In many applications it is
required to convert the output of the sensor, which is analogue in nature to a digital form. The data in
digital format can then be utilized for further processing by the digital processors. Typical applications
include sound processing, temperature processing etc. This circuit demonstrates the principle and
operation of interfacing a simple ADC 0804 using 8051 microcontroller (AT89C51).

ADC0804 is connected as shown in the circuit diagram. Here the input is taken from a preset, which
gives different analog signals to the ADC. The output pins of the ADC are connected to LEDs. The
control pins of the ADC are connected to the microcontroller AT89C51.

ADC0804 is a single channel analog to digital convertor i.e., it can take only one analog signal. An
ADC has n bit resolution (binary form) where n can be 8,10,12,16 or even 24 bits. ADC 0804 has 8 bit
resolution. The higher resolution ADC gives smaller step size. Step size is smallest change that can be
measured by an ADC. For an ADC with resolution of 8 bits, the step size is 19.53mV (5V/255).

The time taken by the ADC to convert analog data into digital form is dependent on the frequency of
clock source. ADC0804 can be given clock from external source. It also has an internal clock.
However the conversion time cannot be more than110us. To use the internal clock a capacitor and

59
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

resistor is connected to pin 19 and 4 as shown in the circuit diagram. The frequency is given by the
relation f= 1/ (1.1RC). The circuit uses a resistance of 10k and a capacitor of 150pF to generate clock
for ADC0804. Vin, which is the input pin, is connected to a preset to provide analog input.

In this circuit microcontroller is used to provide the control signals to the ADC. CS pin of ADC is
directly connected to ground. The pin P1.1, P1.0 and P1.2 are connected to the pin WR, RD and INTR
of the ADC respectively. When the input voltage from the preset is varied the output of ADC varies
which can be seen on the LCD. When the preset is at minimum level, no LED glows and when the
preset is at maximum position all the LEDs starts glowing.

PIN DIAGRAM:

PIN DISCRIPTION:
Pin No Function Name

1 Activates ADC; Active low Chip select

2 Input pin; High to low pulse brings the data from internal registers Read
to the output pins after conversion

3 Input pin; Low to high pulse is given to start the conversion Write

4 Clock Input pin; to give external clock. Clock IN

60
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

5 Output pin; Goes low when conversion is complete Interrupt

6 Analog non-inverting input Vin(+)

7 Analog inverting Input; normally ground Vin(-)

8 Ground(0V) Analog Ground

9 Input pin; sets the reference voltage for analog input Vref/2

10 Ground(0V) Digital Ground

11-18 8 bit digital output pins D7-D7

19 Used with Clock IN pin when internal clock source is used Clock R

20 Supply voltage; 5V Vcc

PROCEDURE:
The following steps are used to interface the ADC0804.
1) Send a low to high pulse to pin WR to start the conversion.
2) Keep monitoring the INTR pin. If INTR is low, go to next step else keep checking the status.
3) A high to low pulse is sent to the RD pin to bring the converted data on the output pins.

CIRCUIT DIAGRAM:

61
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

PROGRAM:
rd equ P1.0 ;Read signal P1.0
wr equ P1.1 ;Write signal P1.1
cs equ P1.2 ;Chip Select P1.2
intr equ P1.3 ;INTR signal P1.3

adc_port equ P2 ;ADC data pins P2


adc_val equ 30H ;ADC read value stored here

org 0H
start: ;Start of Program
acall conv ;Start ADC conversion
acall read ;Read converted value
mov P3,adc_val ;Move the value to Port 3
sjmp start ;Do it again

conv: ;Start of Conversion


clr cs ;Make CS low
clr wr ;Make WR Low
nop
setb wr ;Make WR High
setb cs ;Make CS high
wait:
jb intr,wait ;Wait for INTR signal
ret ;Conversion done

read: ;Read ADC value


clr cs ;Make CS Low
clr rd ;Make RD Low
mov a,adc_port ;Read the converted value
mov adc_val,a ;Store it in local variable
setb rd ;Make RD High
setb cs ;Make CS High
ret ;Reading done

62
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)

CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
______

LAB IN CHARGE: DATE:

63

Das könnte Ihnen auch gefallen