Professional Documents
Culture Documents
4363741911
4363741911
8051 microcontroller is a 40 pin Dual Inline Package (DIP). These 40 pins serve different
functions like read, write, I/O operations, interrupts etc. 8051 has four I/O ports wherein each
port has 8 pins which can be configured as input or output depending upon the logic state of
the pins. Therefore, 32 out of these 40 pins are dedicated to I/O ports. The rest of the pins are
dedicated to VCC, GND, XTAL1, XTAL2, RST, ALE, EA’ and PSEN’.
Pin 1 to Pin 8 are assigned to Port 1 for simple I/O operations. They can be configured as
input or output pins depending on the logic control i.e. if logic zero (0) is applied to the I/O
port it will act as an output pin and if logic one (1) is applied the pin will act as an input pin.
These pins are also referred to as P1.0 to P1.7 (where P1 indicates that it is a pin in port 1 and
the number after ‘.’ tells the pin number i.e. 0 indicates first pin of the port. So, P1.0 means
first pin of port 1, P1.1 means second pin of the port 1 and so on). These pins are
bidirectional pins.
Pin 9 (RST) –
Reset pin. It is an active-high, input pin. Therefore if the RST pin is high for a minimum of 2
machine cycles, the microcontroller will reset i.e. it will close and terminate all activities. It is
often referred as “power-on-reset” pin because it is used to reset the microcontroller to it’s
initial values when power is on (high).
Pin 10 to pin 17 are port 3 pins which are also referred to as P3.0 to P3.7. These pins are
similar to port 1 and can be used as universal input or output pins. These pins are
bidirectional pins.
These pins also have some additional functions which are as follows:
P3.0 (RXD) :
10th pin is RXD (serial data receive pin) which is for serial input. Through this input signal
microcontroller receives data for serial communication.
P3.1 (TXD) :
11th pin is TXD (serial data transmit pin) which is serial output pin. Through this output
signal microcontroller transmits data for serial communication.
12th and 13th pins are for External Hardware Interrupt 0 and Interrupt 1 respectively. When
this interrupt is activated(i.e. when it is low), 8051 gets interrupted in whatever it is doing and
jumps to the vector value of the interrupt (0003H for INT0 and 0013H for INT1) and starts
performing Interrupt Service Routine (ISR) from that vector location.
14th and 15th pin are for Timer 0 and Timer 1 external input. They can be connected with 16
bit timer/counter.
P3.6 (WR’) :
16th pin is for external memory write i.e. writing data to the external memory.
P3.7 (RD’) :
17th pin is for external memory read i.e. reading data from external memory.
Pin 18 and Pin 19 (XTAL2 And XTAL1) –
These pins are connected to an external oscillator which is generally a quartz crystal
oscillator. They are used to provide an external clock frequency of 4MHz to 30MHz. Pin 20
(GND) –
This pin is connected to the ground. It has to be provided with 0V power supply. Hence it is
connected to the negative terminal of the power supply.
Pin 21 to pin 28 are port 2 pins also referred to as P2.0 to P2.7. When additional external
memory is interfaced with the 8051 microcontroller, pins of port 2 act as higher-order address
bytes. These pins are bidirectional.
Pin 29 (PSEN) –
PSEN stands for Program Store Enable. It is output, active-low pin. This is used to read
external memory. In 8031 based system where external ROM holds the program code, this
pin is connected to the OE pin of the ROM.
ALE stands for Address Latch Enable. It is input, active-high pin. This pin is used to
distinguish between memory chips when multiple memory chips are used. It is also used to
de-multiplex the multiplexed address and data signals available at port 0.
During flash programming i.e. Programming of EPROM, this pin acts as program pulse input
(PROG).
Pin 32 to pin 39 are port 0 pins also referred to as P0.0 to P0.7. They are bidirectional
input/output pins. They don’t have any internal pull-ups. Hence, 10 K? pull-up registers are
used as external pull-ups. Port 0 is also designated as AD0-AD7 because 8051 multiplexes
address and data through port 0 to save pins.
Pin 40 (VCC) –
This pin provides power supply voltage i.e. +5 Volts to the circuit.
Q2. Describe the architecture of 8051 microcontroller with the help of neat diagram.
The 8051 Microcontroller is one of the basic type of microcontroller, designed by Intel in
1980’s. This microcontroller was based on Harvard Architecture and developed primarily for
use in embedded systems technology. Normally, this microcontroller was developed using
NMOS technology, which requires more power to operate. Therefore, Intel redesigned
Microcontroller 8051 using CMOS technology and their updated versions came with a letter
C in their name, for instance an 80C51 it is an 8 bit microcontroller. These latest
Microcontrollers requires less power to operate as compared to their previous versions. The
8051 Microcontroller has two buses and two memory spaces of 64K X 8 size for program
and data units. It has an 8 bit processing unit and 8 bit accumulator units.
Central Processor Unit (CPU)
the CPU is the brain of any processing device of the microcontroller. It monitors and controls
all operations that are performed on the Microcontroller units. The User has no control over
the work of the CPU directly . It reads program written in ROM memory and executes them
and do the expected task of that application.
Interrupts
As its name suggests, Interrupt is a subroutine call that interrupts of the microcontrollers
main operations or work and causes it to execute any other program, which is more
important at the time of operation. The feature of Interrupt is very useful as it helps in case of
emergency operations. An Interrupts gives us a mechanism to put on hold the ongoing
operations, execute a subroutine and then again resumes to another type of operations.
The Microcontroller 8051 can be configured in such a way that it temporarily terminates or
pause the main program at the occurrence of interrupts. When a subroutine is completed,
Then the execution of main program starts. Generally five interrupt sources are there in 8051
Microcontroller. There are 5 vectored interrupts are shown in below INTO
TFO
INT1
TF1
R1/T1
Out of these, (INT0) ̅ and (INT1) ̅ are external interrupts that could be negative edge
triggered or low level triggered. When All these interrupts are activated, set the
corresponding flogs except for serial interrupt,.The interrupt flags are cleared when the
processor branches to the interrupt service routine (ISR). The external interrupt flags are
cleared when the processor branches to the interrupt service routine, provides the interrupt is
a negative edge triggered whereas the timers and serial port interrupts two of them are
external interrupts, two of them are timer interrupts and one serial port interrupt terminal in
general.
Memory
BUS
Basically Bus is a collection of wires which work as a communication channel or medium for
transfer of Data. These buses consists of 8, 16 or more wires of the microcontroller. Thus,
these can carry 8 bits,16 bits simultaneously. Hire two types of buses that are shown in below
Address Bus
Data Bus
Address Bus: Microcontroller 8051 has a 16 bit address bus for transferring the data. It is
used to address memory locations and to transfer the address from CPU to Memory of the
microcontroller. It has four addressing modes that are
Data Bus: Microcontroller 8051 has 8 bits of the data bus, which is used to carry data of
particular applications.
Oscillator
Generally, we know that the microcontroller is a device, therefore it requires clock pulses for
its operation of microcontroller applications. For this purpose, microcontroller 8051 has an
on-chip oscillator which works as a clock source for Central Processing Unit of the
microcontroller. The output pulses of oscillator are stable. Therefore, it enables synchronized
work of all parts of the 8051 Microcontroller.
Input/Output Port
Timers/Counters
8051 microcontroller has two 16 bit timers and counters. These counters are again divided
into a 8 bit register. The timers are used for measurement of intervals to determine the pulse
width of pulses.
An Addressing Mode is a way to locate a target Data, which is also called as Operand. The
8051 Family of Microcontrollers allows five types of Addressing Modes for addressing the
Operands. They are:
Immediate Addressing
Register Addressing
Direct Addressing
Register – Indirect Addressing
Indexed Addressing
Immediate Addressing
In Immediate Addressing mode, the operand, which follows the Opcode, is a constant data of
either 8 or 16 bits. The name Immediate Addressing came from the fact that the constant data
to be stored in the memory immediately follows the Opcode.
The constant value to be stored is specified in the instruction itself rather than taking from a
register. The destination register to which the constant data must be copied should be the
same size as the operand mentioned in the instruction.
Example: MOV A, #030H
Here, the Accumulator is loaded with 30 (hexadecimal). The # in the operand indicates that it
is a data and not the address of a Register.
Immediate Addressing is very fast as the data to be loaded is given in the instruction itself.
Register Addressing
In the 8051 Microcontroller Memory Organization Tutorial, we have seen the organization of
RAM and four banks of Working Registers with eight Registers in each bank.
In Register Addressing mode, one of the eight registers (R0 – R7) is specified as Operand in
the Instruction.
It is important to select the appropriate Bank with the help of PSW Register. Let us see a
example of Register Addressing assuming that Bank0 is selected.
Example: MOV A, R5
Here, the 8-bit content of the Register R5 of Bank0 is moved to the Accumulator.
Direct Addressing
In Direct Addressing Mode, the address of the data is specified as the Operand in the
instruction. Using Direct Addressing Mode, we can access any register or on-chip variable.
This includes general purpose RAM, SFRs, I/O Ports, Control registers.
Example: MOV A, 47H
Here, the data in the RAM location 47H is moved to the Accumulator.
In the Indirect Addressing Mode or Register Indirect Addressing Mode, the address of the
Operand is specified as the content of a Register. This will be clearer with an example.
Example: MOV A, @R1
The @ symbol indicates that the addressing mode is indirect. If the contents of R1 is 56H, for
example, then the operand is in the internal RAM location 56H. If the contents of the RAM
location 56H is 24H, then 24H is moved into accumulator.
Only R0 and R1 are allowed in Indirect Addressing Mode. These register in the indirect
addressing mode are called as Pointer registers.
With Indexed Addressing Mode, the effective address of the Operand is the sum of a base
register and an offset register. The Base Register can be either Data Pointer (DPTR) or
Program Counter (PC) while the Offset register is the Accumulator (A).
In Indexed Addressing Mode, only MOVC and JMP instructions can be used. Indexed
Addressing Mode is useful when retrieving data from look-up tables.
Example: MOVC A, @A+DPTR
Here, the address for the operand is the sum of contents of DPTR and Accumulator.
Q4. Explain the following instructions giving proper format and example of each
i) DAA
ii) SWAP
iii) CALL & RET
iv) PUSH & POP
There are two 16-bit timers and counters in 8051 microcontroller: timer 0 and timer 1. Both
timers consist of 16-bit register in which the lower byte is stored in TL and the higher byte is
stored in TH. Timer can be used as a counter as well as for timing operation that depends on
the source of clock pulses to counters.
Timer Control Register (TCON): TCON is another register used to control operations of
counter and timers in microcontrollers. It is an 8-bit register wherein four upper bits are
responsible for timers and counters and lower bits are responsible for interrupts.
TF1: The TF1 stands for ‘timer1’ flag bit. Whenever calculating the time-delay in timer1, the
TH1 and TL1 reaches to the maximum value that is “FFFF” automatically.
EX: while (TF1==1)
Whenever the TF1=1, then clear the flag bit and stop the timer.
TR1: The TR1 stands for timer1 start or stop bit. This timer starting can be through software
instruction or through hardware method.
EX: gate=0 (start timer 1 through software instruction)
TR1=1; (Start timer)
TF0: The TF0 stands for ‘timer0’ flag-bit. Whenever calculating the time delay in timer1, the
TH0 and TL0 reaches to a maximum value that is ‘FFFF’, automatically.
EX: while (TF0==1)
Whenever the TF0=1, then clear the flag bit and stop the timer.
TR0: The TR0 stands for ‘timer0’ start or stop bit; this timer starting can be through software
instruction or through hardware method.
EX: gate=0 (start timer 1 through software instruction)
TR0=1; (Start timer)
F =0.921 MHz
Time delay=1/F
T=1/0.92MHz
1000us=1MS
1000ms=1sec
1. First we have to load the TMOD register value for ‘Timer0’ and ‘Timer1’in different
modes. For example, if we want to operate timer1 in mode1 it must be configured as
“TMOD=0x10”.
2. Whenever we operate the timer in mode 1, timer takes the maximum pulses of 65535.
Then the calculated time-delay pulses must be subtracted from the maximum pulses, and
afterwards converted to hexadecimal value. This value has to be loaded in timer1 higher bit
and lower bits. This timer operation is programmed using embedded C in a microcontroller.
Example: 500us time delay
500us/1.080806us
461pulses
P=65535-461
P=65074
65074 conveted by hexa decimal =FE32
TH1=0xFE;
TL1=0x32;
Q6. Write an assembly language program of 8051 microcontroller to count the number of
zeros in byte.
Algorithm:
4) If the carry is one then incrementing the counter for one and if the carry is zero then
incrementing the counter for one.
5) Checking the number of bits and repeating the above steps till the goal is reached.
This program finds the number of zeros and ones in binary equivalent number.
Code
ORG 0000h
LJMP MAIN
ORG 40h
MOV A,R0
LOOP: RLC A ; left rotating it to check each bit of number
SJMP LABEL2
END
Q7. Write an assembly language program of 8051 microcontroller to find the sum of 10
numbers stored in an array. Draw the flow chart to specify the steps of the design flow.
Algorithm:
1. Start
2. Store the value(N) up to which sum has to be found in a register(Ra)
3. Initialize a register(Rb) to 1
4. Enter the loop
Add Rb and Accumulator register(A) and store the value in A
Increment Rb
Decrement and check whether Ra is zero
o If Ra is zero exit the loop and move value stored in A to destination register(Rd)
o Else, jump back and repeat the steps in loop
5. Stop
LJMP main
ORG 0x40h
main:
MOV R0,#0Ah ; N=10 value
loop:
INC R1
end
Q8. In a semester, a student has to take six courses. The marks of the student (out of 25) are
stored in RAM location 47H onward. Find the average marks and output it on port 1.
Q9. WAP to generate a pulse of 2 kHz on port P1.1 (Assume that XTAL = 11.0592 MHz).
(b) 1 / 2 of it for the high and low portion of the pulse is 250 us.
(c) 250 us / 1.085 us = 230 and 65536 – 230 = 65306 which in hex is FF1AH.
Q10. Write an 8051 assembly language program that creates a square wave of 10 kHz on
P1.0 using Timer0. (Assume crystal oscillator frequency = 12 MHz)
Q11. What value do we need to load the timer’s register if we want to have a time delay of 5
milliseconds? Show the program for timer 0 to create pulse width of 5 milliseconds on P2.3.
(Assume that XTAL = 11.0592 MHz)
RET
Q12. Write an assembly language program for the 8051 to transfer “MANAV” serially at
9600 baud, 8-bit data, 1 stop bit, do this continuously.
ORG 0
H_1: CLR A
; ------------The message---------------
MYDATA: DB “MANAV”, 0
END
Q13. Write an assembly language program for the 8051 to receive bytes of data serially, and
put them in P1, set the baud rate at 4800, 8-bit data and 1 stop bit.
Q14. Interface 16*2 LCD with 8051 microcontroller and write the assembly language
program to display “12” on it.
The LCD ( liquid crystal display) is device to good communicate between user and machine.
It display the Software status, Errors, Indicators, Data as per user program by which a good
Control method and a good communication occurs between user and electronics system and
make a more user Friendly and attractive.
Give the Supply and Wait for a Second to Stabilize the Display.
Give the Instruction 38h for Initialize 2 Line display of 5*7 Matrix LCD.
Wait for a few millisecond (approx 5ms) to Complete Operation and LCD take Action as per
given Command.
Give command 06h for making Increment Cursor mode of LCD. By this, cursor should
increase after every Character is Written to display automatically.
Give the command 80h for Cursor take position at 1st Line 1st Character.
This above 10 step is Require and Basic Common Instruction for 16*2 LCD Initialization.
Thus it write to start up of every Program Related to the LCD Projects. Then the User can
Program to display the Data or Information on LCD as per requirement of User. This is
Explain with a Short Program example of to Display the "Welcome to Here" on the LCD
Display.
Circuit Diagram:
org 0000h
port_ini:
mov p1,#00h
setb p3.4
setb p3.5
setb p3.6
acall delay_1s
Lcd_ini:
mov a,#38h
acall command
mov a,#06h
acall command
mov a,#0ch
acall command
mov a,#01h
acall command
mov a,#80h
acall command
acall delay_1s
Display:
mov a,#'W'
acall write
mov a,#'e'
acall write
mov a,#'l'
acall write
mov a,#'c'
acall write
mov a,#'o'
acall write
mov a,#'m'
acall write
mov a,#'e'
acall write
mov a,#14h
acall command
mov a,#'t'
acall write
mov a,#'o'
acall write
mov a,#14h
acall command
mov a,#'H'
acall write
mov a,#'e'
acall write
mov a,#'r'
acall write
mov a,#'e'
acall write
here:
sjmp here
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
command:
mov p1,a
clr p3.4
clr p3.5
setb p3.6
acall delay
clr p3.6
acall delay
acall delay
ret
write:
lcall ready
mov p1,a
setb p3.4
clr p3.5
setb p3.6
acall delay
clr p3.6
acall delay
acall delay
ret
ready:
setb p1.7
clr p3.4
setb p3.5
wait:
clr p3.6
acall delay
setb p3.6
jb p1.7,wait
ret
delay:
mov r0,#1ch
rep:
djnz r0,rep
ret
delay_1s:
mov r3,#08h
df1s:
mov r2,#0ffh
d1s:
mov r1,#0ffh
de1s:
djnz r1,de1s
djnz r2,d1s
djnz r3,df1s
ret
end
ADC (Analog to digital converter) forms a very essential part in many embedded projects and
this article is about interfacing an ADC to 8051 embedded controller. ADC 0804 is the ADC
used here and before going through the interfacing procedure, we must neatly understand
how the ADC 0804 works. ADC 0804.
PROGRAM
ORG 00H
CLR P3.6 // high to low pulse to RD for reading the data from ADC
MOV A,P1 // moves the digital data to accumulator
END