M&M 3160904 LabManual Latest

Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

L. D.

COLLEGE OF ENGINEERING, AHMEDABAD


DEPARTMENT OF ELECTRICAL ENGINEERING
SUBJECT: MICROPROCESSORS AND MICROCONTROLLERS (3160904)

INDEX

Sr. Page CO
Description Remarks / Sign
No. No. conformity

Comparison: Microprocessor and


1 CO1
Microcontroller

Architecture and Pin Diagram of


2 CO1
Microprocessor 8085

Architecture and Pin Diagram of


3 CO1
Microcontroller 8051

4 Programs of Microcontroller 8051 - I CO2

5 Programs of Microcontroller 8051 - II CO2

6 Programs of Microcontroller 8051 - III CO2

Comparison between Assembly Language


7 CO2
and Embedded 'C'

Timer and Counter Mode Operation of


8 CO3
Microcontroller 8051

Serial Port Mode Operation and PCON


9 CO3
register of Microcontroller 8051

LCD & 7 Segment Display Interfacing with


10 CO4
8051

11 Motor and Keyboard Interfacing with 8051 CO4

Electrical Department, LDCE


Experiment No. 1

Aim:- Comparison: Microprocessor and Microcontroller

Microprocessor Microcontroller
General purpose processor Specific application controller
Contains RAM, ROM, I/O ports on the chip
Does not contain RAM, ROM, I/O ports on the chip
itself
Cannot be used as stand alone Can be used as stand alone
Overall system is bulkier and costly Overall system is compact and cheap
Less bit handling instructions More bit handling instructions
Less number of pins are multiplexed More number of pins are multiplexed
More flexible for designing Less flexibility
More GPRs and addressing modes Less GPRs and addressing modes
Execution is faster Comparatively slower
Von Neumann model - data and instructions share Havard model - data and instructions have
memory separate memory
Inefficient for compact systems Suitable for compact systems
May or may not be Real time application oriented Real time application oriented

Electrical Department, LDCE 1


Experiment No. 2

Aim:- Architecture and Pin Diagram of Microprocessor 8085

8085 is an 8-bit microprocessor designed by Intel in 1977 using NMOS technology.


It has the following configuration −

 8-bit data bus


 16-bit address bus, which can address upto 64KB
 A 16-bit program counter
 A 16-bit stack pointer
 Six 8-bit registers arranged in pairs: BC, DE, HL
 Requires +5V supply to operate at 3.2 MHZ single phase clock

8085 consists of the following functional units −

Accumulator

It is an 8-bit register used to perform arithmetic, logical, I/O & LOAD/STORE operations. It is
connected to internal data bus & ALU.

Electrical Department, LDCE 2


Arithmetic and logic unit

As the name suggests, it performs arithmetic and logical operations like Addition, Subtraction, AND,
OR, etc. on 8-bit data.

General purpose register

There are 6 general purpose registers in 8085 processor, i.e. B, C, D, E, H & L. Each register can
hold 8-bit data.
These registers can work in pair to hold 16-bit data and their pairing combination is like B-C, D-E &
H-L.

Program counter

It is a 16-bit register used to store the memory address location of the next instruction to be
executed. Microprocessor increments the program whenever an instruction is being executed, so
that the program counter points to the memory address of the next instruction that is going to be
executed.

Stack pointer

It is also a 16-bit register works like stack, which is always incremented/decremented by 2 during
push & pop operations.

Temporary register

It is an 8-bit register, which holds the temporary data of arithmetic and logical operations.

Flag register

It is an 8-bit register having five 1-bit flip-flops, which holds either 0 or 1 depending upon the result
stored in the accumulator.
These are the set of 5 flip-flops −

 Sign (S)
 Zero (Z)
 Auxiliary Carry (AC)
 Parity (P)
 Carry (C)
Its bit position is shown in the following table −

D7 D6 D5 D4 D3 D2 D1 D0

S Z ---- AC ---- P ---- CY

Electrical Department, LDCE 3


Instruction register and decoder

It is an 8-bit register. When an instruction is fetched from memory then it is stored in the Instruction
register. Instruction decoder decodes the information present in the Instruction register.

Timing and control unit

It provides timing and control signal to the microprocessor to perform operations. Following are the
timing and control signals, which control external and internal circuits −

 Control Signals: READY, RD’, WR’, ALE


 Status Signals: S0, S1, IO/M’
 DMA Signals: HOLD, HLDA
 RESET Signals: RESET IN, RESET OUT

Interrupt control

As the name suggests it controls the interrupts during a process. When a microprocessor is
executing a main program and whenever an interrupt occurs, the microprocessor shifts the control
from the main program to process the incoming request. After the request is completed, the control
goes back to the main program.
There are 5 interrupt signals in 8085 microprocessor: INTR, RST 7.5, RST 6.5, RST 5.5, TRAP.

Serial Input/output control

It controls the serial data communication by using these two instructions: SID (Serial input data) and
SOD (Serial output data).

Address buffer and address-data buffer

The content stored in the stack pointer and program counter is loaded into the address buffer and
address-data buffer to communicate with the CPU. The memory and I/O chips are connected to
these buses; the CPU can exchange the desired data with the memory and I/O chips.

Address bus and data bus

Data bus carries the data to be stored. It is bidirectional, whereas address bus carries the location to
where it should be stored and it is unidirectional. It is used to transfer the data & Address I/O
devices.

Electrical Department, LDCE 4


The pin diagram of 8085 Microprocessor is shown below:

The pins of a 8085 microprocessor can be classified into seven groups −

Address bus

A15-A8, it carries the most significant 8-bits of memory/IO address.

Data bus

AD7-AD0, it carries the least significant 8-bit address and data bus.

Control and status signals

These signals are used to identify the nature of operation. There are 3 control signal and 3 status
signals.
Three control signals are RD, WR & ALE.
RD − This signal indicates that the selected IO or memory device is to be read and is ready
for accepting data available on the data bus.
WR − This signal indicates that the data on the data bus is to be written into a selected
memory or IO location.

Electrical Department, LDCE 5


ALE − It is a positive going pulse generated when a new operation is started by the
microprocessor. When the pulse goes high, it indicates address. When the pulse goes down
it indicates data.
Three status signals are IO/M, S0 & S1.

IO/M - This signal is used to differentiate between IO and Memory operations, i.e. when
it is high indicates IO operation and when it is low then it indicates memory operation.

S1 & S0 - These signals are used to identify the type of current operation.

Power supply

There are 2 power supply signals − VCC & VSS. VCC indicates +5v power supply and VSS
indicates ground signal.

Clock signals

There are 3 clock signals, i.e. X1, X2, CLK OUT.


X1, X2 − A crystal (RC, LC N/W) is connected at these two pins and is used to set frequency
of the internal clock generator. This frequency is internally divided by 2.
CLK OUT − This signal is used as the system clock for devices connected with the
microprocessor.

Interrupts & externally initiated signals

Interrupts are the signals generated by external devices to request the microprocessor to perform a
task. There are 5 interrupt signals, i.e. TRAP, RST 7.5, RST 6.5, RST 5.5, and INTR. We will
discuss interrupts in detail in interrupts section.
INTA − It is an interrupt acknowledgment signal.
RESET IN − This signal is used to reset the microprocessor by setting the program counter
to zero.
RESET OUT − This signal is used to reset all the connected devices when the
microprocessor is reset.
READY − This signal indicates that the device is ready to send or receive data. If READY is
low, then the CPU has to wait for READY to go high.
HOLD − This signal indicates that another master is requesting the use of the address and
data buses.
HLDA (HOLD Acknowledge) − It indicates that the CPU has received the HOLD request
and it will relinquish the bus in the next clock cycle. HLDA is set to low after the HOLD signal
is removed.

Serial I/O signals

There are 2 serial signals, i.e. SID and SOD and these signals are used for serial communication.
SOD (Serial output data line) − The output SOD is set/reset as specified by the SIM
instruction.
SID (Serial input data line) − The data on this line is loaded into accumulator whenever a
RIM instruction is executed.

Electrical Department, LDCE 6


Experiment No. 3

Aim:- Architecture and Pin Diagram of Microprocessor 8051

8051 microcontroller is designed by Intel in 1981. It is an 8-bit microcontroller. It is built with 40 pins
DIP (dual inline package), 4kb of ROM storage and 128 bytes of RAM storage, 2 16-bit timers. It
consists of are four parallel 8-bit ports, which are programmable as well as addressable as per the
requirement. An on-chip crystal oscillator is integrated in the microcontroller having crystal
frequency of 12 MHz.
The system bus connects all the support devices to the CPU. The system bus consists of an 8-bit
data bus, a 16-bit address bus and bus control signals. All other devices like program memory, ports,
data memory, serial interface, interrupt control, timers, and the CPU are all interfaced together
through the system bus.

The architecture of 8051 Microcontroller is depicted below.


It has Harvard architecture with RISC (Reduced Instruction Set Computer) concept. The block
diagram of 8051 microcontroller is shown in Fig. It consists of an 8-bit ALU, one 8- bit
PSW(Program Status Register), A and B registers, one 16-bit Program counter, one 16-bit
Data pointer register(DPTR), 128 bytes of RAM and 4kB of ROM and four parallel I/O ports
each of 8-bit width.

8051 has 8-bit ALU which can perform all the 8-bit arithmetic and logical operations in one
machine cycle. The ALU is associated with two registers A & B

A and B Registers : The A and B registers are special function registers which hold the results
of many arithmetic and logical operations of 8051. The A register is also called the

Electrical Department, LDCE 7


Accumulator and as it’s name suggests, is used as a general register to accumulate the results
of a large number of instructions. By default it is used for all mathematical operations and
also data transfer operations between CPU and any external memory.
The B register is mainly used for multiplication and division operations along with A register.
MUL AB : DIV AB.
It has no other function other than as a location where data may be stored.

The R registers: The "R" registers are a set of eight registers that are named R0, R1, etc. up
to and including R7. These registers are used as auxiliary registers in many operations. The
"R" registers are also used to temporarily store values.

Program Counter(PC) : 8051 has a 16-bit program counter .The program counter always
points to the address of the next instruction to be executed. After execution of one instruction
the program counter is incremented to point to the address of the next instruction to be
executed.It is the contents of the PC that are placed on the address bus to find and fetch the
desired instruction.Since the PC is 16-bit width ,8051 can access program addresses from
0000H to FFFFH, a total of 6kB of code.

Stack Pointer Register (SP) : It is an 8-bit register which stores the address of the stack top.
i.e the Stack Pointer is used to indicate where the next value to be removed from the stack
should be taken from. When a value is pushed onto the stack, the 8051 first increments the

Electrical Department, LDCE 8


value of SP and then stores the value at the resulting memory location. Similarly when a
value is popped off the stack, the 8051 returns the value from the memory location indicated by
SP, and then decrements the value of SP. Since the SP is only 8-bit wide it is incremented or
decremented by two. SP is modified directly by the 8051 by six instructions: PUSH, POP,
ACALL, LCALL, RET, and RETI. It is also used intrinsically whenever an interrupt is triggered.

Data Pointer Register(DPTR) : It is a 16-bit register which is the only user-accessible. DPTR,
as the name suggests, is used to point to data. It is used by a number of commands which
allow the 8051 to access external memory. When the 8051 accesses external memory it will
access external memory at the address indicated by DPTR. This DPTR can also be used as
two 8-registers DPH and DPL.

Program Status Register (PSW) : The 8051 has a 8-bit PSW register which is also known as
Flag register. In the 8-bit register only 6-bits are used by 8051.The two unused bits are user
definable bits.In the 6-bits four of them are conditional flags. They are Carry –CY, Auxiliary
Carry-AC, Parity-P and Overflow-OV. These flag bits indicate some conditions that resulted
after an instruction was executed.

D7 D6 D5 D4 D3 D2 D1 D0

CY AC F0 RS1 RS0 OV -- P

The bits PSW3 and PSW4 are denoted as RS0 and RS1 and these bits are used th select the
bank registers of the RAM location. The meaning of various bits of PSW register is shown
below.

CY PSW.7 Carry Flag


AC PSW.6 Auxiliary Carry Flag
F0 PSW.5 Flag 0 available for general purpose
RS1 PSW.4 Register Bank select bit 1
RS0 PSW.3 Register bank select bit 0
OV PSW.2 Overflow flag
--- PSW.1 User definable flag
P PSW.0 Parity flag .set/cleared by hardware

The selection of the register Banks and their addresses are given below.
RS1 RS0 Register Bank Address
0 0 0 00H-07H
0 1 1 08H-0FH
1 0 2 10H-17H
1 1 3 18H-1FH

The pin diagram of 8051 microcontroller is shown below.


Pins 1 to 8 − These pins are known as Port 1. This port doesn’t serve any other functions. It
is internally pulled up, bi-directional I/O port.
Pin 9 − It is a RESET pin, which is used to reset the microcontroller to its initial values.
Pins 10 to 17 − These pins are known as Port 3. This port serves some functions like
interrupts, timer input, control signals, serial communication signals RxD and TxD, etc.

 RxD (P3.0): Serial input port

Electrical Department, LDCE 9


 TxD (P3.1): Serial output port
 INT0 (P3.2): External interrupt
 INT1 (P3.3): External interrupt
 T0 (P3.4): Timer 0 external input
 T1 (P3.5): Timer 1 external input
 WR (P3.6): External data memory write strobe
 RD (P3.7): External data memory read strobe

Pins 18 & 19 − These pins are used for interfacing an external crystal to get the system
clock.
Pin 20 − This pin provides the power supply to the circuit.
Pins 21 to 28 − These pins are known as Port 2. It serves as I/O port. Higher order address
bus signals are also multiplexed using this port.

Pin 29 − This is PSEN pin which stands for Program Store Enable. It is used to read a signal
from the external program memory.
Pin 30 − This is EA pin which stands for External Access input. It is used to enable/disable
the external memory interfacing.

Electrical Department, LDCE 10


Pin 31 − This is ALE pin which stands for Address Latch Enable. It is used to demultiplex the
address-data signal of port.
Pins 32 to 39 − These pins are known as Port 0. It serves as I/O port. Lower order address
and data bus signals are multiplexed using this port.
Pin 40 − This pin is used to provide power supply to the circuit.
8051 microcontrollers have 4 I/O ports each of 8-bit, which can be configured as input or output.
Hence, total 32 input/output pins allow the microcontroller to be connected with the peripheral
devices.
Pin configuration, i.e. the pin can be configured as 1 for input and 0 for output as per the
logic state.
Input/Output (I/O) pin − All the circuits within the microcontroller must be connected
to one of its pins except P0 port because it does not have pull-up resistors built-in.
Input pin − Logic 1 is applied to a bit of the P register. The output FE transistor is
turned off and the other pin remains connected to the power supply voltage over a
pull-up resistor of high resistance.
Port 0 − The P0 (zero) port is characterized by two functions −
When the external memory is used then the lower address byte (addresses A0A7) is
applied on it, else all bits of this port are configured as input/output.
When P0 port is configured as an output then other ports consisting of pins with
built-in pull-up resistor connected by its end to 5V power supply, the pins of this port
have this resistor left out.

Input Configuration

If any pin of this port is configured as an input, then it acts as if it “floats”, i.e. the input has unlimited
input resistance and in-determined potential.

Output Configuration

When the pin is configured as an output, then it acts as an “open drain”. By applying logic 0 to a port
bit, the appropriate pin will be connected to ground (0V), and applying logic 1, the external output
will keep on “floating”.
In order to apply logic 1 (5V) on this output pin, it is necessary to build an external pullup resistor.

Port 1

P1 is a true I/O port as it doesn’t have any alternative functions as in P0, but this port can be
configured as general I/O only. It has a built-in pull-up resistor and is completely compatible with
TTL circuits.

Port 2

P2 is similar to P0 when the external memory is used. Pins of this port occupy addresses intended
for the external memory chip. This port can be used for higher address byte with addresses A8-A15.
When no memory is added then this port can be used as a general input/output port similar to Port
1.

Electrical Department, LDCE 11


Port 3

In this port, functions are similar to other ports except that the logic 1 must be applied to appropriate
bit of the P3 register.

INTERNAL RAM STRUCTURE OF MICROCONTROLLER 8051

 The 8051 code(program) memory is read-only, while the data memory is read/write
accessible. The program memory( in EPROM) can be rewritten by the special programmer
circuit.

 The 8051 memory is organized in a Harvard Architecture. Both the code memory space and
data memory space begin at location 0x00 for internal or external memory which is different
from the Princeton Architecture where code and data share same memory space.

The advantage of the Harvard Architecture is not only doubling the memory capacity of the
microcontroller with same number of address lines but also increases the reliability of the
microcontroller, since there are no instructions to write to the code memory which is read only.
This Internal RAM is found on-chip on the 8051 .So it is the fastest RAM available, and it is also the
most flexible in terms of reading, writing, and modifying it’s contents. Internal RAM is volatile, so
when the 8051 is reset this memory is cleared. The 128 bytes of internal RAM is organized as
below.

Electrical Department, LDCE 12


(i) Four register banks (Bank0, Bank1, Bank2 and Bank3) each of 8-bits (total 32 bytes). The
default bank register is Bank0. The remaining Banks are selected with the help of RS0 and RS1
bits of PSW Register.
(ii) 16 bytes of bit addressable area and
(iii) 80 bytes of general purpose area (Scratch pad memory) as shown in the diagram below.
This area is also utilized by the microcontroller as a storage area for the operating stack.
The 32 bytes of RAM from address 00 H to 1FH are used as working registers organized as four
banks of eight registers each.The registers are named as R0-R7. Each register can be addressed
by its name or by its RAM address.
For EX : MOV A, R7 or MOV R7,#05H

Internal ROM (On –chip ROM): The 8051 microcontroller has 4kB of on chip ROM but it can be
extended up to 64kB.This ROM is also called program memory or code memory. The CODE
segment is accessed using the program counter (PC) for opcode fetches and by DPTR for data.
The external ROM is accessed when the EA (active low) pin is connected to ground or the contents
of program counter exceeds 0FFFH. When the Internal ROM address is exceeded the 8051
automatically fetches the code bytes from the external program memory.

SPECIAL FUNCTION REGISTERS (SFRs) : In 8051 microcontroller there certain registers which
uses the RAM addresses from 80h to FFh and they are meant for certain specific operations .These
registers are called Special function registers (SFRs). Some of these registers are bit addressable
also. The list of SFRs and their functional names are given below. In these SFRs some of them are
related to I/O ports (P0, P1, P2 and P3) and some of them are meant for control operations (TCON,
SCON, PCON..) and remaining are the auxilary SFRs, in the sense that they don't directly configure
the 8051.

Electrical Department, LDCE 13


Experiment No. 4

Aim:- Programs of Microcontroller 8051 - I

1. Write an ALP to load data 25H to registers A and R2

MOV A, #25H ; Load data 25H to A


MOV R2, #25H ; Load data 25H to R2

2. Write and ALP to copy the contents of external data memory location pointed by DPTR to
internal data memory location pointed by R0

MOV DPTR, #3000H ; Load 16 bit external address to DPTR


MOV R0, #25H ; Load 8 bit internal address to R0
MOVX A, @DPTR ; Copy contents of external memory to A
MOV @R0, A ; Copy contents of A to internal memory

3. Write an ALP to exchange contents of register R2 with A

MOV A, #35H ; Load data 35H to A


MOV R2, #57H ; Load data 57H to R2
XCH A, R2 ; Exchange contents of A with R2

4. Write an ALP to add contents of A with the contents of R2 and store the result in A

MOV A, #35H ; Load data 35H to A


MOV R2, #57H ; Load data 57H to R2
ADD A, R2 ; Add contents of A with R2

5. Write an ALP to subtract contents of R2 from the contents of A and store the result in A

MOV A, #45H ; Load data 45H to A


MOV R2, #27H ; Load data 27H to R2
CLR C ; Clear carry flag
SUBB A, R2 ; SUB contents of R2 and CY flag from A

6. Write an ALP to multiply contents of register B with the contents of A and store the result in
locations 50H and 51H (LSB is stored in 50H)

MOV A, #45H ; Load data 45H to A


MOV B, #27H ; Load data 27H to B
MUL AB ; Multiply contents of A and B
MOV 50, A ; Store LSB of result in 50H
MOV 51, B ; Store MSB of result in 51H

7. Write an ALP to divide contents of register A by the contents of B and store the result in locations
60H and 61H

MOV A, #45H ; Load data 45H to A


MOV B, #27H ; Load data 27H to B
DIV AB ; Divide contents of A with B
MOV 60, A ; Store LSB of result in 60H
MOV 61, B ; Store MSB of result in 61H

Electrical Department, LDCE 14


8. Write an ALP to add two BCD numbers and store the result in BCD in register R1

MOV A, #09H ; Load data 09 to A


ADD A, #07H ; Add 07 to content of A
DAA ; Convert result to BCD
MOV R1, A ; Move contents of R1 to A

9. Write an ALP to mask the MSB and LSB of A

MOV A, #85H ; Load 85H to A


ANL A, #7EH ; Mask MSB and LSB of A

10. Write an ALP to set LSB and LSB+1 of A

MOV A, #85H ; Load 85H to A


ORL A, #03H ; Set LSB+1 and LSB of A

11. Write an ALP to swap the upper and lower nibble of 20H

MOV A, #20H ; Load 20H to A


SWAP A ; Swap lower and upper nibbles of A
MOV 20H, A ; Copy contents of A to 20H

12. Write an ALP to decrement the contents of R2 until it becomes zero


MOV R2, #0FFH ; Load FFh to R2
LOOP: DJNZ R2, LOOP ; Decrement R2 until it becomes 0

13. Write an ALP to rotate the contents of A left by two positions

MOV A, #25H ; Load 25H to A


MOV R2, #02H ; Load 02H to R2
LOOP: RLA ; Rotate contents of A left by one place
DJNZ R2, LOOP ; Decrement R2 and go to LOOP until R2 becomes 0

14. Write an ALP to compare contents of A with contents of 20H. If contents are equal, store 00H
in 21H, else store FFH in 21H

MOV A, #25H ; Load 25H to A


MOV 20H, #15H ; Load 15H to location 20H
MOV R3, #00H ; Load 00H to R3
CLR C ; Clear Carry flag
SUBB A, 20H ; Subtract contents of loc. 20H and CY flag from
contents of A
JZ LOOP1 ; If result is 0 branch to LOOP1
DEC R3 ; Decrement R3
LOOP1: MOV 21H, R3 ; If result is 0, store 00 in loc. 21H else store FF

Electrical Department, LDCE 15


Experiment No. 5

Aim:- Programs of Microcontroller 8051 - II

1. Write an ALP to add two 8 bit numbers and store the 16 bit results in 20H and 21H

MOV A, #77H ; Load 77H to A


MOV R2, #99H ; Load 99H to R2
MOV R3, #00H ; Load 00H to R3
ADD A, R2 ; Add contents of A with R2
MOV 20H, A ; Store contents of A in loc. 20H
JNC LOOP1 ; If CY flag is reset, branch to LOOP1
INC R3 ; Increment R3
LOOP1: MOV 21H, R3 ; Store contents of R3 in location 21H

2. Write an ALP to add two 16 bit numbers stored in 51H-52H and 61H-62H and store the results in
20H, 21H and 22H

MOV 51H, #12H ; Load 12H to location 51H


MOV 52H, #34H ; Load 34H to location 52H
MOV 61H, #43H ; Load 43H to location 61H
MOV 62H, #21H ; Load 12H to location 62H
MOV A, 51H ; Copy content from loc. 51H to A
ADD A, 61H ; Add contents of A and 61H
MOV 20H, A ; Copy result from A to 20H
MOV A, 52H ; Copy content from loc. 52H to A
ADDC A, 62H ; Add contents of A, CY flag and 62H
MOV 21H, A ; Copy result from A to 21H
MOV A, #00H ; Load 00H to A
ADDC A, #00H ; Add 00H, contents of A and CY flag
MOV 22H, A ; Copy result from A to 22H

3. Write an ALP to subtract two 16 bit numbers stored in 51H-52H from 61H-62H and store the
results in 20H and 21H. If result is positive store 00H else store 01H in 25H

MOV 51H, #12H ; Load 12H to location 51H


MOV 52H, #34H ; Load 34H to location 52H
MOV 61H, #43H ; Load 43H to location 61H
MOV 62H, #21H ; Load 12H to location 62H
MOV A, 61H ; Copy content from loc. 61H to A
CLR C ; Clear Borrow flag
SUBB A, 51H ; Sub contents of loc. 51H from contents of A
MOV 20H, A ; Copy result from A to 20H
MOV A, 62H ; Copy content from loc. 62H to A
SUBB A, 52H ; Sub contents of loc. 52H from contents of A
MOV 21H, A ; Copy result from A to 21H
MOV A, #00H ; Load 00H to A
ADDC A, #00H ; Add 00H, contents of A and CY flag
MOV 22H, A ; If result is +, store 00H else store 01H in loc. 22H

Electrical Department, LDCE 16


4. Write an ALP to add two BCD numbers stored in 51H-52H and store the results in 61H-62H

MOV 51H, #12H ; Load 12H to location 51H


MOV 52H, #34H ; Load 34H to location 52H
MOV A, 51H ; Copy content from loc. 51H to A
ADD A, 52H ; Add contents of A and 52H
DA A ; Decimal adjustment of result in A
MOV 61H, A ; Copy content from A to loc. 61H
MOV A, #00H ; Load 00H to A
ADDC A, #00H ; Add 00H, contents of A and CY flag
MOV 62H, A ; Copy result from A to 62H

5. Write an ALP to set carry flag and complement MSB of 20H

CLR C ; Clear Carry flag


CPL C ; Complement carry flag, CY is set
CPL 07H ; Complement MSB of reg 20H

6. Write an ALP to add 1+2+3+……+N (say 10) and save the sum at 20H

N EQU 15 ; Ass. Dir. EQU to set counter


MOV R0, #00H ; Clear R0
CLR A ; Clear A
again: INC R0 ; Increment R0
ADD A, R0 ; Add contents of R0 and A
CJNE R0, #N, again ; Loop until counter R0 = N
MOV 20H, A ; Copy result in loc. 20H

7. Write an ALP to count the number of 1’s and 0’s of 8 bit data stored in A

MOV A, #0ABH ; Load number to A


MOV R0, #08H ; Copy 08 to R0
MOV R2, #00H ; Copy 00 to R2
MOV R3, #00H ; Copy 00 to R3
CLR C ; Clear carry flag
BACK: RLC A ; Rotate contents of A left through carry
JC NEXT ; If CY = 1 goto NEXT
INC R2 ; If CY = 0 increment R2
AJMP NEXT2
NEXT: INC R3 ; If CY = 1 increment R3
NEXT2: DJNZ R0, BACK ; Repeat until R0 = 0

8. N 8 bit numbers are stored in internal data memory. Write an ALP to arrange the numbers in
descending order

TEMP EQU 40H


N EQU 04H ; Array count
MOV R4, #N-1 ; Load N-1 to R4
LOOPS: MOV R0, #41 ; Store starting address of array in R0
MOV R5,R4
LOOP1: MOV A, @R0 ; Copy contents of memory pointed by R0 to A
MOV R1, A ; Copy contents of A to R1

Electrical Department, LDCE 17


INC R0 ; Increment R0
MOV A, @R0 : Get the number next to A
MOV TEMP, A ; Copy the next number to TEMP
MOV A, R1
CJNE A, TEMP, LOOP2 ; A ≠TEMP branch to LOOP2
SJMP LOOP3 ; A =TEMP branch to LOOP2
LOOP2: JNC LOOP3 ; A >TEMP branch to LOOP2
MOV @R0, A ; A <TEMP exchange contents of two locations
MOV A, TEMP
DEC R0
MOV @R0, A
INC R0
LOOP3: DJNZ R5, LOOP1 ; Repeat until R5 is 0
DJNZ R4, LOOPS ; Repeat until R4 is 0

9. Write an ALP to create a delay of 1ms. Assume the oscillator frequency to be 12Mhz

Oscillator period = 1/12MHz


1 machine cycle = (1/12MHz) * 12 = 1μs
Execution time for the following program sequence is 8μs
Start: PUSH ACC 2 m/c
POP 2 m/c
NOP 1 m/c
NOP 1 m/c
DJNZ R1, Start 2 m/c

To create a delay of 1ms, the program must be executed 125 times = 1ms/8μs = 1000μs/8μs =
125
Program to create a delay of 1ms is as follows:

MOV R1, #125H 2 m/c


Start: PUSH ACC 2 m/c
POP 2 m/c
NOP 1 m/c
NOP 1 m/c
DJNZ R1, Start 2 m/c

Actual time is 2μs more than 1ms. This is the overhead fro setting up the delay loop.

10. Find the time delay for the following subroutine, assuming a crystal frequency of 11.0592MHz

MOV R1, #25H 2 m/c


Start: NOP 1 m/c
NOP 1 m/c
NOP 1 m/c
NOP 1 m/c
DJNZ R1, Start 2 m/c
RET 1 m/c
Oscillator period = 1/11.0592MHz
1 machine cycle = (1/11.0592MHz) * 12 = 1.085μs
Time delay inside the Start loop = [25(1+1+1+1+2)]*1.085 = 150*1.085 = 162.75μs
Adding 3 m/c of MOV R1, #25H and RET instructions = 3*1.085 = 3.225μs
Total delay = 162.75 +3.225 = 166.005μs

Electrical Department, LDCE 18


Experiment No. 6

Aim:- Programs of Microcontroller 8051 - III

1. Write an ALP and C program to send 8 bit hex number to Port 0

ASSEMBLY LANGUAGE PROGRAM:

MOV A, #0CH ; Load hex no. 0c to A


MOV P0, A ; Send data to port
END

C PROGRAM:

#include<reg51.h>
void main()
{
unsigned char a;
a = 0x0C; //initialize a variable hex value
P0 = a; //send hex number to port 0
}

2. Write a ‘C’ program to toggle bits of P1 continuously for 100 times with time delay of 1sec

#include<reg51.h>
void main()
{
int i=0, j=0;
while (j < 100)
{
P1=0xFF;
for(i=0; i<10000; ++i);
P1=0x00;
for(i=0; i<10000; ++i);
j++;
}
}

3. Write a ‘C’ program to generate a square wave of 3s on time, 50% duty cycle on P1.2
Ton
Duty Cycle D = Ton + Toff
= 50% = 0.5
3s
0.5 = 3s+Toff
3s + Toff = 6s
���� = 3�

#include<reg51.h>
void delay(unsigned int);
void main()

Electrical Department, LDCE 19


{
while(1)
{
P1^2=0;
delay(3000);
P1^2=1;
delay=(3000);
}
}

void delay(unsigned int count)


{
unsigned int i, k;
for(i=0; i<count; ++i)
{
for(k=0; k<1275; ++k);
}
}

4. Write a ‘C’ program to toggle all pins of P0 for every 1 sec using NOT operation

#include<reg51.h>
void delay(unsigned int);
void main()
{
P0 = 0x55;
while(1)
{
P0=~P0;
delay(1000);
}
}

void delay(unsigned int count)


{
unsigned int i, k;
for(i=0; i<count; ++i)
{
for(k=0; k<1275; ++k);
}
}

5. Write a ‘C’ program to convert packed BCD 0x29 to unpacked BCD 0x02 and 0x09

#include<Intel\8051.h>
void main()
{
unsigned char x, y;
unsigned char no;
x = no&0x0F;
P1 = x;

Electrical Department, LDCE 20


y = no&0xF0;
y = y>>4;
P2 = y;
}

6. Write a ‘C’ program to get data from P1.2, invert it and send it to P2.0, 8 times

#include<reg51.h>
sbit inbit = P1^2;
sbit outbit = P2^7;
bit tempbit;
void main()
{
int i;
for(i=0; i<8; ++i)
{
tempbit = inbit;
outbit = ~tempbit;
}
}

7. Write a ‘C’ program to check P1.5. If P1.5 =1, send FFH to P0, else send 00H to P0

#include<reg51.h>
void main()
{
while(1)
{
if(P1^5==1)
P0=0xFF;
else
P0=0x00;
}
}

Electrical Department, LDCE 21


Experiment No. 7

Aim:- Comparison between Assembly Language and Embedded 'C

Assembly language Embedded ‘C’ language

This language is a high level language. This language is a low level language.
Embedded ‘C’ is fixed.
If the controller is changed, it also changes.
If controller changes, it is not changed.
How many data will be used in How much memory will be used in
program will be known . program isnot known.
Assembly language produces smaller file Embedded ‘C’ produces larger file than
thanEmbedded ‘C’ . assembly.
Programming in assembly is more tedious & Programming in ‘C’ is less tedious & less time
more time consuming. consuming.
It is not easy to modify and update. It is easier to modify and update.
It is not portable to other micro controllers It is portable to other micro controllers
even with modification. with little modification.
Programming in assembly does not require Programming in ‘C’ requires a compiler
a compiler to convert it in to machine language. to convert it in to machine language.

Electrical Department, LDCE 22


Experiment No. 8

Aim:- Timer and Counter Mode Operation of Microcontroller 8051

TMOD (Timer Mode) Register

 Working as a “Timer”, the timer is incremented by one every machine cycle. A machine cycle
consists of 12 oscillator periods, so the count rate is 1/12 of the oscillator frequency.
 Working as a “Counter”, the counter is incremented in response to a falling edge transition in the
external input pins.
 The external input is sampled once every machine cycle. A “high” sample followed by a low
sample is counted once.
 Timer 0 and Timer 1 have four operating modes.

bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0


GATE C/T M1 M0 GATE C/T M1 M0

TIMER-1 OPERATION TIMER-2 OPERATION

Gate = 0, T0 or T1 is enabled, if TR0 or TR1 is set


D7 / D3 Gate
Gate = 1, T0 or T1 is enabled, if INT0 = 1 and TR0 = 1 or INT1
= 1 and TR1 = 1
Set to 1 by program to make timer 1/0 act as a counter by counting pulses from
D6 / D2 C/T external input pins 3.5(T1) or 3.4(T0). Cleared to 0 by program to make timer a
ct as timer by counting internal frequency

D5 / D1 M1
Mode select bits
D4 / D0 M0

 Timer mode can be define by selection of M0 & M1.

M1 M0 MODE OPERATION MODE


0 0 MODE- 0 13 bit timer mode

0 1 MODE- 1 16 bit timer / counter mode

1 0 MODE- 2 8 bit auto-reload timer / counter

1 1 MODE- 3 Split mode

Electrical Department, LDCE 23


TCON (Timer Control) Register

bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0


TF1 TR1 TF0 TR0 IE1 IT0 IE0 IT0

 TF0/TF1: Timer0/1 overflow flag is set when the timer/ counter overflows, reset by program
 TR0/TR1: Timer0/1 run control bit is set to start, reset to stop the timer0/1
 IE0/IE1: External Interrupt 0/1 edge detected flag: 1 is set when a falling edge interrupt on the
external port 0/1, reset(cleared) by hardware itself for falling edge transition activated INT; Reset
by code for low level INT.
 IT0/IT1External InterruptType (1:falling edge triggered,0 low level triggered)
-------------------------------------------------------------------------------------------------------------------------------
1. PROGRAM TO VERIFY TIMER-1 COUNTER MODE:

Solution:-
CLR A
MOV A, TMOD (TMOD=89)
ORL A,#50H
MOV TMOD,A
SETB TR1 (TR1=8E)
LOOP LCALL 68EAH
MOV DPTR,#0194H
MOV A,TL1 (TL1=8B)
MOVX @DPTR,A
INC DPTR
MOV A,TH1 (TH1=8D)
MOVX @DPTR,A
LCALL 6748H
SJMP LOOP

2. CALCULATE AMOUNT OF TIME DELAY IN THE FOLLOWING PROGRAM. ASSUME XTAL


= 11.0592MHz
MOV TMOD, #01H
BACK: MOV TL0, #0F2H
MOV TH0, #0FFH
CPL P1.5
SETB TR0
AGAIN: JNB TF0, AGAIN
CLR TR0
CLR TF0
SJMP BACK

Solution:-

11.0592 1
= 921.6 ���; � = = 1.085μs
12 921.6

Delay count = (FFFF - FFF2) + 1


= 0DH + 1
= 0EH (14 in decimal)

Electrical Department, LDCE 24


Delay = 14 * 1.085μs
= 15.19μs for half the pulse
For the entire period T = 2 * 15.19μs
= 30.38μs

3. GENERATE A TIME DELAY OF 5μsec USING TIMER0 MODE 1 WITH XTAL = 22MHz

Solution:-
22 1
= 1.833 ���; � = = 0.546μs
12 1.833
Pulse period
Initial count value = Maximum value −
Clock period

5μs
= 65535 − 0.546μs
= 65535 - 9
= 65527 (in decimal)
= FFF7 (in Hex)

MOV TMOD, #01H


BACK: MOV TL0, #0F7H
MOV TH0, #0FFH
SETB TR0
AGAIN: JNB TF0, AGAIN
CLR TR0
CLR TF0
SJMP BACK

#include<reg51.h>
void main()
{
TMOD = 0x01;
while(1)
{
TL0 = 0xF7;
TH0 = 0XFF;
TR0 = 1;
while (TF0 = = 0);
TR0 = 0;
TF0 = 0;
}
}

Electrical Department, LDCE 25


Experiment No. 9

Aim:- Serial Port Mode Operation and PCON register of Microcontroller 8051

Serial Transmission Parallel Transmission

8 or more lines - multiple bits at a


Single wire - one bit at a time
time

Used over Long distances Used over Short distances

Cheaper Costlier

Slow in speed High in speed

SCON ( Serial Port Control Register) SFR

bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0


SM0 SM1 SM2 REN TB8 RB8 T1 R1

 REN: Receiver enable is set/reset by program


 TB8: stores transmitted bit 8(9th bit, the stop bit)
 RB8: Stores received bit 8(9th bit, the stop bit)
 TI: Transmit Interrupt is set at the end of 8th bit (mode 0) or at the stop bit (other modes) indicating
the completion of one byte transmission, reset byprogram
 RI: Receive Interrupt is set at the end of 8th bit (mode 0) or at the stop bit (other modes) indicating
the completion of one byte receiving, reset by program

Electrical Department, LDCE 26


SMOD (Serial Port Mode Register)

SM0 & SM1 specify the serial port mode, as follow

SM0 AM1 Mode Description Baud rate


0 0 0 Shift register Fosc/12
0 1 1 8-bit UART Variable
1 0 2 9-bit UART Fosc/64 or fosc/32
1 1 3 9-bit UART variable

PCON ( Power Control Register) SFR (Not bit addressable)

bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0


SMOD - - - GF1 GF2 PD IDL

 SMOD(serial mode) 1= high baud rate, 0 = low baud rate


 GF1, GF2 flags for freeuse
 PD: 1= power down mode for CMOS
 IDL:1=idlemode.
 Ex. PCON |= 0x01;
// to set the IDL bit 1 to force the CPU in a power save mode
// the |= operator is a shorthand bit wise logical OR operator
 TheACC, B,DPH, DPL, SP, SFRs are only accessible by Assembly Languages

1. PROGRAM 8051 USING 11.0592MHz CRYSTAL FOR SERIAL TRANSMISSION TO GET


9600Hz BAUD RATE

SOLUTION:
k × Oscillator Frequency 1× 11.0592 ×106
TH1 = 256 − 384 × Baud Rate
= 256 − 384 × 9600
= 253 = FDH

Electrical Department, LDCE 27


MOV TMOD, #20H ; Initialize Timer1 in mode 2
MOV SCON, #4CH ; Initialize serial mode 1
ORL PCON, #80H ; Make SMOD =1
MOV TH1, #0FDH ; Load count

2. WRITE AN 8051 ALP TO SERIALLY TRANSMIT LETTER “Y” AT 9600 BAUD RATE
CONTINUOUSLY

SOLUTION:

MOV TMOD, #20H ; Initialize Timer1 in mode 2


MOV TH1, #0FDH ; Baud Rate 9600
MOV SCON, #50H ; 8 bit, 1 stop REN enabled
SETB TR1 ; Start Timer1
START: MOV SBUF, #”Y” ; Letter “Y” to be transmitted
HERE: JNB TI, HERE ; Wait for last bit to transfer (TI - Transmit Interrupt flag)
CLR TI ; Clear TI for next character
SJMP START ; Goto send character again

#include<reg51.h>
void main (void)
{
TMOD = 0x20;
TH1 = 0xFD;
SCON = 0x50;
TR1 = 1;
while(1)
{
SBUF = ‘Y’;
while(TI == 0);
TI = 0;
}
}

3. WRITE AN 8051 ALP TO SERIALLY TRANSMIT LETTER “HELLO” AT 9600 BAUD RATE
CONTINUOUSLY (8 bit data, 1 stop bit)

SOLUTION:

MOV TMOD, #20H ; Initialize Timer1 in mode 2


MOV TH1, #0FDH ; Baud Rate 9600
MOV SCON, #50H ; 8 bit, 1 stop REN enabled
SETB TR1 ; Start Timer1
START: MOV A, #”H” ; Letter “H” to be transmitted
ACALL TRANS
MOV A, #”E” ; Letter “E” to be transmitted
ACALL TRANS
MOV A, #”L” ; Letter “L” to be transmitted
ACALL TRANS
MOV A, #”L” ; Letter “L” to be transmitted
ACALL TRANS

Electrical Department, LDCE 28


MOV A, #”O” ; Letter “O” to be transmitted
ACALL TRANS

TRANS: MOV SBUF, A ; Load SBUF


HERE: JNB TI, HERE ; Wait for last bit to transfer (TI - Transmit Interrupt flag)
CLR TI ; Clear TI for next character
RET

#include<reg51.h>
void stx (unsigned char);
void main ()
{
TMOD = 0x20;
TH1 = 0xFD;
SCON = 0x50;
TR1 = 1;
while(1)
{
stx(‘H’);
stx(‘E’);
stx(‘L’);
stx(‘L’);
stx(‘O’);

}
}

void stx (unsigned char a)


{
SBUF = a;
while(TI == 0);
TI = 0;
}

Electrical Department, LDCE 29


Experiment No. 10

Aim:- LCD Display and 7 Segment Display Interfacing with 8051

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.

16×2 LCD module.


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

Pin No: Name Function

1 VSS This pin must be connected to the ground


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

Electrical Department, LDCE 30


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 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.

16×2 LCD module commands.


16×2 LCD module has a set of preset command instructions. Each command will make the module
to do a particular task. The commonly used commands and their function are given in the table
below.

Command Function
0F LCD ON, Cursor ON, Cursor blinking ON
01 Clear screen
02 Return home
04 Decrement cursor
06 Increment cursor
0E Display ON ,Cursor blinking OFF

80 Force cursor to the beginning of 1st line

C0 Force cursor to the beginning of 2nd line


38 Use 2 lines and 5×7 matrix
83 Cursor line 1 position3
3C Activate second line
08 Display OFF, Cursor OFF

Electrical Department, LDCE 31


C1 Jump to second line, position1
OC Display ON, Cursor OFF
C1 Jump to second line, position1
C2 Jump to second line, position2

LCD initialization

The steps that has to be done for initializing the LCD display is given below and these steps are common
for almost all applications.

 Send 38H to the 8 bit data line for initialization


 Send 0FH for making LCD ON, cursor ON and cursor blinking ON.
 Send 06H for incrementing cursorposition.
 Send 01H for clearing the display and return the cursor.

Sending data to the LCD

The steps for sending data to the LCD module is given below. I have alreadysaid thattheLCD modulehas
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 bedisplayed.
 Place data byte on the dataregister.
 Pulse E from high tolow.
 Repeat above steps for sending another data.

Electrical Department, LDCE 32


The circuit diagram given shows how to interface a 16×2 LCD module with AT89S1 microcontroller.
Capacitor C3, resistor R3 and push button switch S1 forms the reset circuitry. Ceramic capacitors
C1,C2 and crystal X1 is related to the clock circuitry which produces the system clock frequency.
P1.0 to P1.7 pins of the microcontroller is connected to the DB0 to DB7 pins of the module
respectively and through this route the data goes to the LCD module. P3.3, P3.4 and P3.5 are
connected to the E, R/W, RS pins of the microcontroller and through this route the control signals are
transferred to the LCD module. Resistor R1 limits the current through the back light LED and so do
the back light intensity. POT R2 is used for adjusting the contrast of the display.

1. WRITE AN 8051 ALP TO MULTIPLEX THE NUMBER 1234 ON THE FOUR 7-SEGMENT
DISPLAYS

SOLUTION:
start:
SETB P3.3
SETB P3.4
MOV P1, #11111001B
CALL delay
CLR P3.3
MOV P1, #10100100B
CALL delay
CLR P3.4
SETB P3.3
MOV P1, #10110000B
CALL delay
CLR P3.3
MOV P1, #10011001B
CALL delay
JMP start

delay:
MOV R0, #20
here:DJNZ R0, here
RET

2. WRITE AN 8051 ALP TO DISPLAY “ABC0” ON THE LCD MODULE ASSEMBLY

MOV 30H, #'A'


MOV 31H, #'B'
MOV 32H, #'C'
MOV 33H, #0

CLR P1.3

CLR P1.7
CLR P1.6
SETB P1.5
CLR P1.4

Electrical Department, LDCE 33


SETB P1.2
CLR P1.2

CALL delay

SETB P1.2
CLR P1.2

SETB P1.7

SETB P1.2
CLR P1.2

CALL delay

CLR P1.7
CLR P1.6
CLR P1.5
CLR P1.4

SETB P1.2
CLR P1.2

SETB P1.6
SETB P1.5

SETB P1.2
CLR P1.2

CALL delay

CLR P1.7
CLR P1.6
CLR P1.5
CLR P1.4

SETB P1.2
CLR P1.2

SETB P1.7
SETB P1.6
SETB P1.5
SETB P1.4

SETB P1.2
CLR P1.2

CALL delay

Electrical Department, LDCE 34


SETB P1.3
MOV R1, #30H
loop:
MOV A, @R1
JZ finish

CALL sendCharacter
INC R1
JMP loop

finish:
ret

sendCharacter:
MOV C, ACC.7
MOV P1.7, C
MOV C, ACC.6
MOV P1.6, C
MOV C, ACC.5
MOV P1.5, C
MOV C, ACC.4
MOV P1.4, C

SETB P1.2
CLR P1.2

MOV C, ACC.3
MOV P1.7, C
MOV C, ACC.2
MOV P1.6, C
MOV C, ACC.1
MOV P1.5, C
MOV C, ACC.0
MOV P1.4, C

SETB P1.2
CLR P1.2

CALL delay

delay:
MOV R0, #20
here: DJNZ R0, here
RET

Electrical Department, LDCE 35


Experiment No. 11

Aim:- Motor and Keyboard Interfacing with 8051

This program exercises the motor. The motor is rotated in a clockwise direction and the number
of revolutions is displayed on Display 0 (the 7-segment ; display). The display only shows up to
nine revolutions and then resets.

The motor sensor is connected to P3.5, which is the external clock source for timer 1. Therefore,
timer 1 is put into event counting mode. In this way, the timer increments once every motor
revolution is completed.

The value in timer 1 low byte is moved to A and this value together with the data pointer (DPH
and DPL) are used to get the 7-segment code from program memory. The code is then sent to
P1 to put the appropriate number on the Display 0.

The motor can be changed from clockwise to anti-clockwise by pressing SW0 (on P2.0). The
motor direction is stored in F0 (1 for clockwise, 0 for anti-clockwise). The value in F0 is sent to
Display 0's decimal point (P1.7). This indicates the motor's direction - if the decimal point is lit,
the motor is rotating anti-clockwise, while if it is not lit the motor is rotating clockwise.

The value in F0 is compared with the value of SW0. If they are the same the motor direction does
not need to be changed. If they are not the same it means the user has pressed SW0 and the
motor direction must be reversed. When this happens the new motor direction is then stored in
F0.

MOV TMOD, #50H


SETB TR1

MOV DPL, #LOW(LEDcodes)


MOV DPH, #HIGH(LEDcodes)

CLR P3.4
CLR P3.3
again:
CALL setDirection
MOV A, TL1
CJNE A, #10, skip
CALL clearTimer
skip:
MOVC A, @A+DPTR
MOV C, F0
MOV ACC.7, C
MOV P1, A
JMP again

setDirection:
PUSH ACC
PUSH 20H
CLR A
MOV 20H, #0

Electrical Department, LDCE 36


MOV C, P2.0
MOV ACC.0, C
MOV C, F0
MOV 0, C
CJNE A, 20H, changeDir
JMP finish

changeDir:
CLR P3.0
CLR P3.1
CALL clearTimer
MOV C, P2.0
MOV F0, C
MOV P3.0, C
CPL C
MOV P3.1, C

finish:
POP 20H
POP ACC
RET

clearTimer:
CLR A
CLR TR1
MOV TL1, #0
SETB TR1
RET

LEDcodes DB 11000000B, 11111001B, 10100100B, 10110000B, 10011001B,


10010010B, 10000010B, 11111000B, 10000000B, 10010000B

Note: The above program is a good illustration of what can go wrong if the sampling frequency is
too low. Try running the program with the motor at full speed (use the slider to the right of the motor
to increase the motor speed). You should notice the motor completes a number of revolutions
before the display updates. In other words, the motor's highest speed is too fast for the 8051
running at system clock of 12 MHz.

Since the program only shows up to nine revolutions (displayed on the 7-segment display) and
then starts counting again, it is best to run this program with an Update Freq. of 1. This allows the
user observe the display count up from 0 to 9 and back to 0 again. It also illustrates the delay
between the motor arm passing the sensor and the display update. Similarly, the delay between
pressing the switch for a direction change and the actual direction change occurring is
evident. The student will learn that, while in real time these delays are not noticeable, to the
microcontroller these operations take quite some time.

Electrical Department, LDCE 37


4X4 Matrix Keypad
Before we interface the keypad with microcontroller, first we need to understand how it works. Matrix
keypad consists of set of Push buttons, which are interconnected. Like in our case we are using
4X4 matrix keypad, in which there are 4 push buttons in each of four rows. And the terminals of the
push buttons are connected according to diagram. In first row, one terminal of all the 4 push buttons
are connected together and another terminal of 4 push buttons are representing each of 4 columns,
same goes for each row. So we are getting 8 terminals to connect with a microcontroller.

Electrical Department, LDCE 38


Link to virtual Lab: http://vlabs.iitb.ac.in/vlabs-dev/labs/8051-Microcontroller-Lab/labs/explist.php

Simulator: Edsim51 dynamic interface

Electrical Department, LDCE 39

You might also like