General-purpose microprocessor Chip contains :
No RAM
No ROM
No I/O ports
General-purpose microprocessors
� Must add RAM, ROM, I/O ports, and timers externally to make them functional
� Make the system bulkier and much more expensive
� Have the advantage of versatility on the amount of RAM, ROM, and I/O ports
Microcontroller Chip has:
CPU (microprocessor)
RAM
ROM
I/O ports
Timer
May be ADC and other peripherals
� Microcontroller
� The fixed amount of on-chip ROM, RAM, and number of I/O ports makes them ideal for
many applications in which cost and space are critical.
� In many applications, the space it takes, the power it consumes, and the price per unit are
much more critical considerations than the computing power.
Embedded System
An embedded system uses a microprocessor (or microcontroller) to do one task only.
� At a time there is only one application software that is typically burned into ROM.
Registers
� The CPU uses registers to store information temporarily
Values (data) to be processed
Address of value to be fetched from memory
� In general, the more and bigger the registers, the better the CPU
� Registers can be 8-, 16-, 32-, or 64-bit
� The disadvantage of more and bigger registers is the increased cost of such a CPU
Brief History of 8051
Intel introduced an 8-bit microcontroller called the 8051. It was referred as system on a
chip because it had 128 bytes of RAM, 4K byte of on-chip ROM, two timers, one serial
port, and 4 ports (8-bit wide), all on a single chip. When it became widely popular, Intel
allowed other manufacturers to make and market different flavours of 8051 with its code
compatible with 8051. It means that if you write your program for one flavour of 8051, it
will run on other flavours too, regardless of the manufacturer. This has led to several
versions with different speeds and amounts of on-chip RAM.
Features of 8051 Microcontroller
An 8051 microcontroller comes bundled with the following features:
64K bytes on-chip program memory (ROM)
128 bytes on-chip data memory (RAM)
Four register banks
128 user defined software flags
8-bit bidirectional data bus
16-bit unidirectional address bus
32 general purpose registers each of 8-bit
16 bit Timers (usually 2, but may have more or less)
Three internal and two external Interrupts
Four 8-bit ports,(short model have two 8-bit ports)
16-bit program counter and data pointer
8051 may also have a number of special features such as UARTs, ADC, Op-amp, etc.
The 8051 became widely popular after allowing other manufactures to make and
market any flavour of the 8051, but remaining code-compatible.(8051, 8052, 8030).
Feature 8031 8051 8052
ROM space in Chip No ROM 4K Bytes 8K Bytes
RAM space in Chip 128 Bytes 128 Bytes 256 Bytes
Timers 2 2 3
I/O pins 32 32 32
Serial port 1 1 1
Interrupt sources 6 6 8
The 8031 has no ROM in chip, to add an external ROM to it you lose two ports as two ports
are used to interface the external ROM to the 8031 chip, and leave only two ports for I/O
operations.
Block Diagram of 8051 Microcontroller
The following illustration shows the block diagram of an 8051 microcontroller.
40 Pin PDIP (Plastic Dual Inline Package)
8051 Input/ Output Ports
One of the most useful features of the 8051 is that it consists of 4 I/O ports P0, P1, P2 and
P3, each port is 8 pins. All ports are bidirectional ( they can take input and to provide
output). All the ports upon RESET are configured as output, ready to be used as output ports.
� When the first 0 is written to a port, it becomes an output.
� To reconfigure it as an input, a 1 must be sent to the port.
� To use any of these ports as an input port, it must be programmed.
Example 1
The following code will continuously send out to port P0 the alternating value 55H and AAH
back: MOV A,#55H
MOV P0,A
ACALL DELAY
MOV A,#0AAH
MOV P0,A
ACALL DELAY
SJMP back
Example 2
Port 0 is configured first as an input port by writing 1s to it, and then data is received from
that port and sent to P1
MOV A,#0FFH ;A=FF hex
MOV P0,A ;make P0 as input port
BACK: MOV A,P0 ;get data from P0
MOV P1,A ;send it to port 1
SJMP BACK ;keep doing it
Example 3
Port 1 is configured first as an input port by writing 1s to it, then data is received from that
port and saved in R7 and R5
MOV A,#0FFH ;A=FF hex
MOV P1,A ;make P1 as an input port
MOV A,P1 ;get data from P1
MOV R7,A ;save it to in reg R7
ACALL DELAY ;wait
MOV A,P1 ;another data from P1
MOV R5,A ;save it to in reg R5
Example 4
Write a program to read 35h bytes from an external device connected to port P1 and send it
to an external device connected to port P3.
MOV A,#0FFH ;A=FF hex
MOV P1,A ;make P1 as an input port.
MOV R4,#35H ;use R4 as a counter.
Again: MOV A,P1 ;get data from P1
MOV P3,A
DJNZ R4, again
Example 5
Write a program to read a byte from a device connected to port P0 add it to a byte from P1,
save the result into the RAM started at location 60h. Repeat the sequence 40h times.
Solution
MOV A,#0FFH ;A=FF hex
MOV P0,A ;make P0 an input port
MOV P1,A ;make P1 an input port
MOV R3,#40H ;use R3 as a counter
MOV R1,#60H ; move the 1st address to be used in R1
Again: MOV A, P0
ADD A, P1
MOV @R1,A
DJNZ R3, again
Example 6
Write a program to perform the following:
(a) Keep monitoring the P1.2 bit until it becomes high
(b) When P1.2 becomes high, write value 45H to port 0
(c) Send a high-to-low (H-to-L) pulse to P2.3
Solution:
SETB P1.2 ;make P1.2 an input
MOV A,#45H ;A=45H
AGAIN: JNB P1.2,AGAIN ; get out when P1.2=1
MOV P0,A ;issue A to P0
SETB P2.3 ;make P2.3 high
CLR P2.3 ;make P2.3 low for H-to-L
Example 7
Assume that bit P2.3 is an input and represents the condition of an oven. If it goes high, it
means that the oven is hot. Monitor the bit continuously. Whenever it goes high, send a high-
to-low pulse to port P1.5 to turn on a buzzer.
Solution:
HERE: JNB P2.3,HERE ;keep monitoring for high
SETB P1.5 ;set bit P1.5=1
CLR P1.5 ;make high-to-low
SJMP HERE ;keep repeating
Example 8
A switch is connected to pin P1.7. Write a program to check the status of SW and perform the
following:
(a) If SW=0, send EC to P2
(b) If SW=1, send letter 9A to P2
Solution:
SETB P1.7 ;make P1.7 an input
AGAIN: JB P1.2,OVER ;jump if P1.7=1
MOV P2,#0EC ;SW=0, send EC to P2
SJMP AGAIN ;keep monitoring
OVER: MOV P2,#9A ;SW=1, send 9A to P2
SJMP AGAIN ;keep monitoring
Example 9
A switch is connected to pin P1.0 and an LED to pin P2.7. Write a program to get the status
of the switch and send it to the LED.
Solution:
SETB P1.7 ;make P1.7 an input
AGAIN: MOV C,P1.0 ;read SW status into CF
MOV P2.7,C ;send SW status to LED
SJMP AGAIN ;keep repeating
Example 10
Write a program to generate a square wave on bit P1.0 of port 1.
Solution:
HERE: SETB P1.0 ;set to high bit 0 of port 1
LCALL DELAY ;call the delay subroutine
CLR P1.0 ;P1.0=0
LCALL DELAY
SJMP HERE ;keep doing it
Another way to write the above program is:
HERE: CPL P1.0 ;set to high bit 0 of port 1
LCALL DELAY ;call the delay subroutine
SJMP HERE ;keep doing it