Professional Documents
Culture Documents
MICROCONTROLL
ER
By
K. Vijay Kumar
Assistant
Professor
Dept. of ECE
2/11/17 8051 by Vijay Kumar K
1
Why do we need to learn
Microprocessors/Microcontrolle
rs?
The microprocessor is the core of
computer systems.
5. Timer
6. Interrupt
7. Serial Port
8. Parallel Port
2/11/17 8051 by Vijay Kumar K 3
Microprocessors
CPU for Computers
No RAM, ROM, I/O on CPU chip itself
Example: Intel's x86, Motorolas 680x0
Micro Controller
General-purpose Single-purpose
2/11/17 8051 by Vijay Kumar K 10
Systems
Home
Appliances, intercom, telephones, security systems,
garage door openers, answering machines, fax
machines, home computers, TVs, cable TV tuner, VCR,
camcorder, remote controls, video games, cellular
phones, musical instruments, sewing machines, lighting
control, paging, camera, pinball machines, toys,
exercise equipment etc.
Office
Telephones, computers, security systems, fax machines,
microwave, copier, laser printer, color printer, paging etc.
Auto
Trip computer, engine control, air bag, ABS,
instrumentation, security system, transmission control,
entertainment, climate control, cellular phone, keyless
Choosing a Microcontroller
8-bit microcontrollers
Motorolas 6811
Intels 8051
Zilogs Z8
Microchips PIC
Packaging
Power consumption
The amount of RAM and ROM on chip
The number of I/O pins and the timer on chip
How easy to upgrade to higher performance
or lower power-consumption versions
Cost per unit
Criteria for Choosing a
Microcontroller
2. Availability of software development tools, such as compilers,
assemblers, and debuggers
source) suppliers
Intel (original)
Atmel
Philips/Signetics
AMD
Infineon (formerly Siemens)
Dallas Semiconductor/Maxim
Salient Features
1. 8 bit microcontroller originally developed by Intel in
1980.
3. 40 pin DIP IC
16. RAM, ROM, I/O ports, one serial port and timers are all on-
chip.
2/11/17
8051 by Vijay Kumar K 18
2/11/17 19
8051 by Vijay Kumar K
XTAL2 and XTAL1 (18 and 19 pins)
The 8051 has an on-chip oscillator but requires
an external clock to run it
A quartz crystal oscillator is connected to
inputs XTAL1 (pin19) and XTAL2 (pin18)
The quartz crystal oscillator also needs two
capacitors of 30 pF value
ALE pin 30
Address Latch Enable
It is an output pin and is active high.
8051 port 0 provides both address and data.
The ALE pin is used for de-multiplexing the address and data by
connecting to the G pin of the 74LS373 latch.
I/O Port Pins
The four 8-bit I/O ports P0, P1, P2 and
P3 each uses 8 pins.
External
60K
64K 64K
SFR
EXT INT 4K
128
EA = EA =
0 1
Program Memory Data Memory
Internal RAM Structure
Direct
Addressing
Only
SFR [ Special
Direct & Function Registers]
Indirect
Addressing
128 Byte Internal
RAM
Special Function Registers
[SFR]
Register (Accumulator)
B Register
Multiplication and division can be performed only upon numbers
stored in the A and B registers. All other instructions in the
program can use this register as a spare accumulator (A).
44
2/11/17 8051 by Vijay Kumar K
Stack Pointer (SP) Register
A value stored in the Stack Pointer points to
the first free stack address and permits stack
availability.
Stack pushes increment the value in the Stack
Pointer by 1. Likewise, stack pops decrement
its value by 1. Upon any reset and power-on,
the value 7 is stored in the Stack Pointer,
which means that the space of RAM reserved
for the stack starts at this location. If another
value is written to this register, the entire
Stack is moved to the new memory location.
46
2/11/17 8051 by Vijay Kumar K
As mentioned, port bit state affects
performance of port pins, i.e. whether they will
be configured as inputs or outputs.
If a bit is cleared (0), the appropriate pin will be
configured as an output, while if it is set (1), the
appropriate pin will be configured as an input.
Upon reset and power-on, all port bits are set
(1), which means that all appropriate pins will be
configured as inputs.
2/11/17 8051 by Vijay Kumar K 47
Program Status Word [PSW]
C AC F0 RS1 RS0 OV F1 P
Carry Parity
Auxiliary Carry User Flag 1
Bank R0 R1 R2 R3 R4 R5 R6 R7
3
Bank R0 R1 R2 R3 R4 R5 R6 R7
2
Bank R0 R1 R2 R3 R4 R5 R6 R7
1
Bank R0 R1 R2 R3 R4 R5 R6 R7
0
8051 Register Banks with
address
8051 Programming Model
8051 Stack
The stack is a section of RAM used by the CPU to
store information temporarily.
This information could be data or an address
T PIN
INTERRUPT
TR
Gate
INT PIN
GATE:
When set, timer/counter x is enabled, if INTx pin is
high and TRx is set.
When cleared, timer/counter x is enabled, if TRx bit
set.
C/T:
When set, counter operation (input from Tx input pin).
When cleared, timer operation (input from internal
clock).
2/11/17 8051 by Vijay Kumar K 65
TMOD Register
8051 TIMERS
Timer 0 Timer 1
Mode Mode
0 0
Mode Mode
1 1
Mode Mode
2 2
Mode
3 69
2/11/17 8051 by Vijay Kumar K
TIMER 0
OSC 12
C /T 0
TL0 TH0 TF0
C /T 1
T 0 PIN
TR 0 INTERRU
PT
Gate
INT 0 PIN
T 0 PIN
TR 0
Gate
INT 0 PIN
T 0 PIN
TR 0
Gate
INT 0 PIN
T 0 PIN
TR 0
Gate Reloa
d
INT 0 PIN
TH0
(8 Bit)
T 0 PIN
TR 0
Gate
INT 0 PIN
TR1
2/11/17 8051 by Vijay Kumar K 74
TIMER 1
OSC 12
C /T 0
TL1 TH1 TF1
C /T 1
T 1PIN
INTERR
TR1 UPT
Gate
INT 1 PIN
T 1PIN
TR1
Gate
INT 1 PIN
T 1PIN
TR1
Gate
INT 1 PIN
T 1PIN
TR1
Gate Reloa
d
INT 1 PIN
TH1
(8 Bit)
1.SBUF Register
2.SCON Register
3.PCON Register
SBUF Register
SBUF is an 8-bit register used solely for serial
communication.
For a byte data to be transferred via the TxD line,
it must be placed in the SBUF register.
The moment a byte is written into SBUF, it is
framed with the start and stop bits and transferred
serially via the TxD line.
SBUF holds the byte of data when it is received by
8051 RxD line.
When the bits are received serially via RxD, the
8051 deframes it by eliminating the stop and start
bits, making a byte out of the data received, and
SBUF Register
Sample Program:
SCON Register
Set to
Enable
Serial Data
Enable reception
Multiprocessor Set when Stop
Communication bit Txed
Mode 9th
th Data Bit
Sent in Mode
2,3
Descriptio Set when a
SM0 SM1 Mode Character
n received
8-bit Shift
0 0 0
Register 9th
th Data Bit
We
canBaud
SMOD set it to is
rate high byas
twice software and thereby
much higher double
by setting the
this bit.
baud rate.
GF1 General-purpose bit (available for use).
GF0 General-purpose bit (available for use).
PD By setting this bit the microcontroller enters the Power
Down mode.
IDL By setting this bit the microcontroller enters the Idle
mode.
2/11/17 8051 by Vijay Kumar K 99
2/11/17 8051 by Vijay Kumar K 100
Doubling Baud Rate (cont)
8051
Interrupts
INTERRUPTS
An interrupt is an external or internal event that
by two ways:
1. Interrupt
2. Polling
Interrupt Vs Polling
1. Interrupts
Whenever any device needs its service, the device
notifies the microcontroller by sending it an interrupt
signal.
Upon receiving an interrupt signal, the microcontroller
interrupts whatever it is doing and serves the device.
The program which is associated with the interrupt is
called the interrupt service routine (ISR) or interrupt
handler.
2. Polling
The microcontroller continuously monitors the status
of a given device.
When the conditions met, it performs the service.
After that, it moves on to monitor the next device until
every one is serviced.
Interrupt Vs Polling
The polling method is not efficient, since it wastes
much of the microcontrollers time by polling devices
that do not need service.
The advantage of interrupts is that the
microcontroller can serve many devices (not all at the
same time).
Each devices can get the attention of the
microcontroller based on the assigned priority.
For the polling method, it is not possible to assign
priority since it checks all devices in a round-robin
fashion.
The microcontroller can also ignore (mask) a device
request for service in Interrupt.
Steps in Executing an Interrupt
1. It finishes the instruction it is executing and saves the
address of the next instruction (PC) on the stack.
2. It also saves the current status of all the interrupts
internally (i.e: not on the stack).
3. It jumps to a fixed location in memory, called the
interrupt vector table, that holds the address of the
ISR.
4. The microcontroller gets the address of the ISR from
the interrupt vector table and jumps to it.
5. It starts to execute the interrupt service subroutine
until it reaches the last instruction of the subroutine
which is RETI (return from interrupt).
6. Upon executing the RETI instruction, the microcontroller
returns to the place where it was interrupted.
Six Interrupts in 8051
Six interrupts are allocated as follows:
1. Reset power-up reset.
2. Two interrupts are set aside for the timers.
one for Timer 0 (T0) and one for Timer 1 (T1)
Timer 1 Overflow.
External Event 0.
External Event 1.
of interrupts are:
Interrupts 0/1
SCON - RI and TI interrupt flags for
RS232
IE - Enable interrupt sources
Serial Port
INT 0 Pin
Timer 1 Pin
PUSH A is invalid.
ADDC A,Rn Adds the register to the accumulator with a carry flag
Adds the direct byte to the accumulator with a carry
ADDC A,direct
flag
Adds the indirect RAM to the accumulator with a carry
ADDC A,@Ri
flag
Adds the immediate data to the accumulator with a
ADDC A,#data
carry flag
Subtracts the register from the accumulator with a
SUBB A,Rn
borrow
Subtracts the direct byte from the accumulator with a
SUBB A,direct
borrow