8051 Lecture
8051 Lecture
8051 Microcontroller
Richard Myrick T. Arellaga
Contents
1 Review of Computer Architecture 1
1.1 How it All Started . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Computer Architecture . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.1 Internal Organization of a Computer . . . . . . . . . . . . 3
1.2.2 Functions of the CPU . . . . . . . . . . . . . . . . . . . . 3
1.2.3 Instruction Execution . . . . . . . . . . . . . . . . . . . . 4
1.2.4 Types of Memory . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.5 Hardware Architecture . . . . . . . . . . . . . . . . . . . . 5
1.2.6 Software Architecture . . . . . . . . . . . . . . . . . . . . 6
1.3 Microprocessor Vs Microcontroller . . . . . . . . . . . . . . . . . 7
1.3.1 General Purpose Microprocessor System and Microcon-
troller System . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3.2 Application of Microcontrollers . . . . . . . . . . . . . . . 8
1.3.3 Characteristics of Embedded Systems . . . . . . . . . . . 8
1.3.4 Design Challenges . . . . . . . . . . . . . . . . . . . . . . 8
1.3.5 Microcontroller for Embedded System . . . . . . . . . . . 9
1.3.6 High End Embedded System . . . . . . . . . . . . . . . . 9
Rear Admiral Grace Hopper coined the term ”Bug” when a moth was
stuck in a relay in the Harvard Mark II Computer impeding the operation
of the relay.
A breakthrough in December 23, 1947 were the development of the
transistor at the Bell Labs by John Bardeen, William Shockley and
Walter Brattain.
1958 an engineer in Texas Instruments named Jack Kilby invented the
Integrated Circuit. This invention led to the development of digital inte-
grated circuits in the 1960’s.
With the development of integrated circuits Intel Engineers Federico
Faggin, Ted Hoff (Marcian Hoff ) and Stan Mazor developed the
4004 microprocessor (US Patent 3,821,715)originally designed for the Bu-
sicom calculator in 1969.
In 1971 Intel bought the rights of the 4004 from Busicom and later that
year the 4004 was released on the market. The First 4-bit microprocessor
with the operations of 6000 operations per second much slower than the
ENIAC but it was so small and requires lower power.
Intel released the first 8 bit microprocessor called the 8008. It was able
to address 16kb of memory had 45 instructions and the speed of 300,000
operations per second. The 8008 is the predecessor of today’s computer.
1974, the 8080 microprocessor was introduced by Intel with 64kb of mem-
ory and 74 instructions. Motorola also launched the 68000 microprocessor.
1976, Federico Faggin left Intel and started his own company Zilog. Dur-
ing this year the Z80 microprocessor was announced and was compatible
with the programs written for the 8080 plus added features making it
the most powerful microprocessor at that time. The Z80 had 64kb direct
addressable memory and 176 instructions.
While Intel was busy inventing the first microprocessor, It was during
1970 and 1971 Gary Boone of Texas Instruments invented the Micro-
controller the TMS1000.
Intel also created many significant Microcontrollers beside producing the
world’s first ever microprocessor. The important ones produced by Intel
are the 8048 and the 8051 microcontrollers. The 8048 was introduced in
1976 and was the first of Intel’s microcontrollers.
Zilog introduced the Zilog Z8 microcontroller in 1979 which includes Z8,
Z8 Encore, eZ8 Encore, eZ8 Encore XP and eZ8 Encore MC.
In 1981, the 8051 was introduced and one of the most popular microcon-
trollers. It is even used now and is considered to be the one of the most
long-lived microcontrollers.
Microprocessor Systems Page 3
Fetch Instruction – the task of reading the next instruction from memory
into the instruction register.
Decode Instruction – the task of determining what operation the instruc-
tion in the instruction register represents (ex., add, move, etc.)
Fetch Operands – the task of moving the instruction’s operand data into
appropriate registers.
Execute Operation – the task of feeding the appropriate registers through
the ALU and back into an appropriate register.
Store Results – the task of writing into memory. If each stage takes one
clock cycle, then we can see that a single instruction may take several
cycles to complete.
Harvard Architecture
is a computer architecture with physically separate storage and signal
pathways for instructions and data. The term originated from the Har-
vard Mark I relay-based computer, which stored instructions on punched
tape (24 bits wide) and data in electro-mechanical counters. These early
machines had data storage entirely contained within the central processing
unit, and provided no access to the instruction storage as data. Programs
needed to be loaded by an operator; the processor could not boot itself.
RISC - is a CPU design strategy based on the insight that simplified (as
opposed to complex) instructions can provide higher performance if this
simplicity enables much faster execution of each instruction.
Microprocessor Systems Page 7
Although it becomes bulkier and costlier still they have the advantage of
versatility
But for the microcontroller all are integrated in a single chip, so it is
sometimes called “Single Chip Computer”
In many applications, the space used, the power consumption and the
price per unit is much more critical consideration than the computing
power, in those applications microcontrollers are used.
Unit cost
Size
Performance
Power
Flexibility
Microprocessor Systems Page 9
Time-to-market
Time-to-prototype
Correctness
Safety
So, some manufacturers like Intel, AMD, and Freescale targeted their
microprocessor for the high end embedded market.
Another chip called ARM (Advance RISC Machine) is used for high end
embedded system. ARM is developed by Acorn Computers for the BBC
Microcomputer in the 80’s.
Microprocessor Systems Page 10
8051 has 4KBytes of internal ROM. The address space is from 0000 to
0FFFh. If the program size is more than 4KBytes , the 8051 will fetch
the code automatically from external memory.
Accumulator is an 8-bit register widely used for all arithmetic and logical
operations. It is also used to transfer data between external memory. B
register is used along with Accumulator for multiplication and division.
PSW (Program Status Word). This is an 8 bit register which contains the
arithmetic status of ALU and the bank select bits of register banks.
Stack Pointer (SP) – it contains the address of the data item on the top
of the stack. Stack may reside anywhere on the internal RAM. On reset,
SP is initialized to 07 so that the default stack will start from address 08
onwards.
Microprocessor Systems Page 11
Data Pointer (DPTR) – DPH (Data pointer higher byte), DPL (Data
pointer lower byte). This is a 16 bit register which is used to furnish
address information for internal and external program memory and for
external data memory.
Program Counter (PC) – 16 bit PC contains the address of next instruc-
tion to be executed. On reset PC will set to 0000. After fetching every
instruction PC will increment by one.
Pinout Description
Pins 1-8 PORT 1. Each of these pins can be configured as an input or an output.
Pin 9 RESET. A logic one on this pin disables the microcontroller and clears the
contents of most registers. In other words, the positive voltage on this pin
resets the microcontroller. By applying logic zero to this pin, the program
starts execution from the beginning.
Pins 10-17 PORT 3. Similar to port 1, each of these pins can serve as general input or
output. Besides, all of them have alternative functions.
Pin 10 RXD. Serial asynchronous communication input or Serial synchronous com-
munication output.
Pin 11 TXD. Serial asynchronous communication output or Serial synchronous com-
munication clock output.
Pin 12 INT0. External Interrupt 0 input
Pin 13 INT1. External Interrupt 1 input
Pin 14 T0. Counter 0 clock input
Pin 15 T1. Counter 1 clock input
Pin 16 WR. Write to external (additional) RAM
Pin 17 RD. Read from external RAM
Pin 18-19 XTAL2, XTAL1. Internal oscillator input and output. A quartz crystal
which specifies operating frequency is usually connected to these pins.
Pin 20 GND. Ground.
Pin 21-28 Port 2. If there is no intention to use external memory then these port pins
are configured as general inputs/outputs. In case external memory is used,
the higher address byte, i.e. addresses A8-A15 will appear on this port. Even
though memory with capacity of 64Kb is not used, which means that not all
eight port bits are used for its addressing, the rest of them are not available as
inputs/outputs.
Pin 29 PSEN. If external ROM is used for storing program then a logic zero (0)
appears on it every time the microcontroller reads a byte from memory.
Pin 30 ALE. Prior to reading from external memory, the microcontroller puts the
lower address byte (A0-A7) on P0 and activates the ALE output. After receiv-
ing signal from the ALE pin, the external latch latches the state of P0 and uses
it as a memory chip address. Immediately after that, the ALE pin is returned
its previous logic state and P0 is now used as a Data Bus.
Pin 31 EA. By applying logic zero to this pin, P2 and P3 are used for data and
address transmission with no regard to whether there is internal memory or
not. It means that even there is a program written to the microcontroller, it
will not be executed. Instead, the program written to external ROM will be
executed. By applying logic one to the EA pin, the microcontroller will use
both memories, first internal then external (if exists).
Pin 32-39 PORT 0. Similar to P2, if external memory is not used, these pins can be
used as general inputs/outputs. Otherwise, P0 is configured as address output
(A0-A7) when the ALE pin is driven high (1) or as data output (Data Bus)
when the ALE pin is driven low (0).
Pin 40 VCC. +5V power supply.
Microprocessor Systems Page 13
2.3.2 Port 1
Port-1 does not have any alternate function i.e. it is dedicated solely for I/O
interfacing. When used as output port, the pin is pulled up or down through
internal pull-up. To use port-1 as input port, ’1’ has to be written to the latch.
In this input mode when ’1’ is written to the pin by the external device then
it read fine. But when ’0’ is written to the pin by the external device then the
external source must sink current due to internal pull-up. If the external device
is not able to sink the current the pin voltage may rise, leading to a possible
wrong reading.
2.3.3 Port 2
Port-2 is used for higher external address byte or a normal input/output port.
The I/O operation is similar to Port-1. Port-2 latch remains stable when Port-2
Microprocessor Systems Page 15
pin are used for external memory access. Here again due to internal pull-up
there is limited current driving capability.
2.3.4 Port 3
Each pin of Port-3 can be individually programmed for I/O operation or for
alternate function. The alternate function can be activated only if the corre-
sponding latch has been written to ’1’. To use the port as input port, ’1’ should
be written to the latch. This port also has internal pull-up and limited current
driving capability.
Microprocessor Systems Page 16
With the LED Interface 1, the LED is connected in current source mode,
thus the microcontroller is providing current to the LED, with this connection
the microcontroller port must be HIGH to turn the LED on. LED Interface 2
is current sink mode, the Input Voltage is the source of current thus the flow of
current exits thru the microcontroller pin. In current sink, the microcontroller
must be LOW to make the LED glow.
Notice that a resistor is used with the LED. The resistor is an integral part
to limit the current flow to protect the LED as well as the microcontroller. The
AT89S52 can provide a maximum of 26mA at PORT0 and 71mA for
Ports 1,2 and 3. However it is recommended that current in each port pin
must be limited to 10mA under steady state condition. With these we
can compute the value of the resistor by looping from the microcontroller to the
LED resulting to the following equation:
V CC − VF
R=
IF
Example:
Interface a typical red LED with VF=1.2V and IF=8mA
5V − 1.2V
R= ; R = 475ohms
8mA
if the computed value is not within commercial values available, round off to the
nearest commercial values say 470 ohms. Re-calculate if the available resistor
is within the maximum electrical characteristics of the LED and the microcon-
troller. Compute for the power dissipated, for our application a typical 0.25W
Microprocessor Systems Page 18
To make the LEDs blink we need some sort of delay since the microcon-
troller runs fast that the human eye could not distinguish. Using a crystal
of 11.0592MHz and 1 machine cycle needs 12 clock cycles to complete we get
11.0592M/12 = 926.6KHz. Getting the period T=1/F, we get 1.078uS. To cre-
ate an approximate 1ms delay we need 1275 machine cycles (1.079x1275 = 1375
approx 1.3ms) .
1
2 /* ***
3 LED Blink
4 *** */
5 include < reg52 .h >
6
7 /* milliseconds delay for AT89S52 with 11.0592 MHz Clock using Keil Compiler */
8 void delay_ms ( unsigned int ms )
9 {
10 unsigned int i , j ;
11
12 for ( i =0; i < ms ; i ++) {
13 for ( j =0; j <1275; j ++) ;
14 }
15 }
16
17 void main ( void )
18 {
19
20 while (1)
21 {
22 P2 = 0 x00 ;
23 delay_ms (500) ;
24 P2 = 0 xFF ;
25 delay_ms (500) ;
26 }
27 }