Microprocessors & Microcontrollers Guide
Microprocessors & Microcontrollers Guide
INDEX
Sr. Page CO
Description Remarks / Sign
No. No. conformity
Comparison: Microprocessor
1 CO1
and Microcontroller
Experiment No. 1
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
NILESH KHODIAR DIC-B 200280109524
Experiment No. 2
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.
NILESH KHODIAR DIC-B 200280109524
As the name suggests, it performs arithmetic and logical operations like Addition, Subtraction, AND,
OR, etc. on 8-bit data.
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
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.
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 −
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.
It controls the serial data communication by using these two instructions: SID (Serial input data) and
SOD (Serial output data).
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.
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.
NILESH KHODIAR DIC-B 200280109524
Address bus
Data bus
AD7-AD0, it carries the least significant 8-bit address and data bus.
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.
NILESH KHODIAR DIC-B 200280109524
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
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.
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.
NILESH KHODIAR DIC-B 200280109524
Experiment No. 3
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.
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
NILESH KHODIAR DIC-B 200280109524
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
NILESH KHODIAR DIC-B 200280109524
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.
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
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.
NILESH KHODIAR DIC-B 200280109524
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.
NILESH KHODIAR DIC-B 200280109524
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.
NILESH KHODIAR DIC-B 200280109524
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.
⮚ 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 memoryspace.
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.
NILESH KHODIAR DIC-B 200280109524
(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.
NILESH KHODIAR DIC-B 200280109524
Experiment No. 4
2. Write and ALP to copy the contents of external data memory location pointed by DPTR to
internal data memory location pointed by R0
4. Write an ALP to add contents of A with the contents of R2 and store the result in A
5. Write an ALP to subtract contents of R2 from the contents of A and store the result in 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)
7. Write an ALP to divide contents of register A by the contents of B and store the result in
locations 60H and 61H
8. Write an ALP to add two BCD numbers and store the result in BCD in register R1
11. Write an ALP to swap the upper and lower nibble of 20H
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
CONCLUSION: We have learned about programming of microcontroller 8051 with basic data transfer and
arithmetic operations.
NILESH KHODIAR DIC-B 200280109524
Experiment No. 5
1. Write an ALP to add two 8 bit numbers and store the 16 bit results in 20H and 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
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
4. Write an ALP to add two BCD numbers stored in 51H-52H and store the results in 61H-62H
6. Write an ALP to add 1+2+3+……+N (say 10) and save the sum at 20H
7. Write an ALP to count the number of 1’s and 0’s of 8 bit data stored in A
8. N 8 bit numbers are stored in internal data memory. Write an ALP to arrange the numbers in
descending order
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
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:
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
CONCLUSION: We have learned about data transfer, arithmetic operations, loops and adding delay.
NILESH KHODIAR DIC-B 200280109524
Experiment No. 6
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
3s
0.5 =
3s+Toff
3s + Toff = 6s
= 3
#include<reg51.h>
void delay(unsigned int);
void main()
NILESH KHODIAR DIC-B 200280109524
{
while(1)
{
P1^2=0;
delay(3000);
P1^2=1;
delay=(3000);
}
}
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);
}
}
5. Write a ‘C’ program to convert packed BCD 0x29 to unpacked BCD 0x02 and 0x09
#include<Intel\8051.h>
void main()
{
NILESH KHODIAR DIC-B 200280109524
unsigned char x, y;
unsigned char no;
x = no&0x0F;
P1 = x;
NILESH KHODIAR DIC-B 200280109524
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;
}
}
Experiment No. 7
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
than Embedded‘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.
CONCLUSION: We have learned that programs written in assembly can execute faster, while
programs written in C are easier to develop and maintain
NILESH KHODIAR DIC-B 200280109524
Experiment No. 8
⮚ 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.
⮚ The external input is sampled once every machine cycle. A “high” sample followed by a low
sample is counted once.
D5 / D1 M1
Mode select bits
D4 / D0 M0
⮚ TF0/TF1: Timer0/1 overflow flag is set when the timer/ counter overflows, reset byprogram
⮚ 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.
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
= 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
NILESH KHODIAR DIC-B 200280109524
Solution:- 1
= 921.6 𝐻; =
11.0592
= 1.085μs
12 921.6
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
22
= 1.833 𝐻; =
Solution:- = 0.546μs
12
1.833
= 65535 −
= 65535 - 9
= 65527 (in decimal)
= FFF7 (in Hex)
#include<reg51.h>
void main()
{
TMOD = 0x01;
while(1)
{
TL0 = 0xF7;
TH0 = 0XFF;
TR0 = 1;
while (TF0 = = 0);
TR0 = 0;
TF0 = 0;
}
}
NILESH KHODIAR DIC-B 200280109524
CONCLUSION: We have learned about programming of Timer and Counter Mode Operation of
Microcontroller 8051.
NILESH KHODIAR DIC-B 200280109524
Experiment No. 9
Aim:- Serial Port Mode Operation and PCON register of Microcontroller 8051
Cheaper Costlier
⮚ 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 byprogram
NILESH KHODIAR DIC-B 200280109524
⮚ IDL:1=idlemode.
// 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
NILESH KHODIAR DIC-B 200280109524
SOLUTION:
6 = 253 = FDH
= 256 − 1× 11.0592 ×10
384 × 9600
TH1 = 256 − k ×
Oscillator Frequency
384 × Baud Rate
NILESH KHODIAR DIC-B 200280109524
2. WRITE AN 8051 ALP TO SERIALLY TRANSMIT LETTER “Y” AT 9600 BAUD RATE
CONTINUOUSLY
SOLUTION:
#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:
#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’);
}
}
CONCLUSION: We have learned about programming of Serial Port Mode Operation and PCON register
of Microcontroller 8051 including serial transmission.
NILESH KHODIAR DIC-B 200280109524
Experiment No. 10
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.
4 RS Register selection
7 DB0 Data
8 DB1 Data
9 DB2 Data
10 DB3 Data
11 DB4 Data
12 DB5 Data
13 DB6 Data
14 DB7 Data
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.
Command Function
0F LCD ON, Cursor ON, Cursor blinking ON
01 Clear screen
02 Return home
04 Decrement cursor
06 Increment cursor
80 st
Force cursor to the beginning of 1 line
C0 nd
Force cursor to the beginning of 2 line
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.
The steps for sending data to the LCD module is given below. I have alreadysaidthatthe
LCDmodulehas 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.
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.
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
CLR P1.3
CLR P1.7
CLR P1.6
SETB P1.5
NILESH KHODIAR DIC-B 200280109524
CLR P1.4
NILESH KHODIAR DIC-B 200280109524
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
NILESH KHODIAR DIC-B 200280109524
SETB
P1.6
SETB
P1.5
SETB
P1.4
SETB P1.2
CLR P1.2
CALL delay
NILESH KHODIAR DIC-B 200280109524
S
E
T
loop: B
P
1
.
3
M
O
V
R
1
,
#
3
0
H
MOV A, @R1
JZ finish
C
A
L
L
s
e
n
d
C
h
a
r
a
c
t
e
r
I
N
C
R
1
JMP loop
finish:
ret
NILESH KHODIAR DIC-B 200280109524
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
CONCLUSION: We have learned about programming of LCD Display and 7 Segment Display Interfacing
with 8051 for displaying numbers and characters
NILESH KHODIAR DIC-B 200280109524
Experiment No. 11
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.
C
A
L
L
NILESH KHODIAR DIC-B 200280109524
s O
e V
t C
D A
i ,
r @
e A
c +
t D
i P
o T
n R
M M
O O
V V
A C
, ,
T F
L 0
1 M
C O
J V
N A
E C
A C
, .
# 7
1 ,
0 C
, M
s O
k V
i P
p 1
CALL clearTimer ,
A
M JMP again
setDirection:
PUSH
ACC
PUSH 20H
CLR A
MOV 20H, #0
NILESH KHODIAR DIC-B 200280109524
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: P
O
P
2
0
H
P
O
P
A
C
C
R
E
T
clearTimer:
CLR A
CLR TR1
MOV TL1, #0
SETB TR1
RET
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.
NILESH KHODIAR DIC-B 200280109524
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.
NILESH KHODIAR DIC-B 200280109524
CONCLUSION: We have learned about programming of Motor and Keyboard Interfacing with 8051.