8051 microcontroller · 8051 microcontroller. contents •introduction to mc •history of 8051 mc...

69
8051 Microcontroller

Upload: others

Post on 14-Aug-2020

215 views

Category:

Documents


11 download

TRANSCRIPT

Page 1: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

8051 Microcontroller

Page 2: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Contents

• Introduction to MC

• History of 8051 MC

• 8051 Architecture

Mechatronics & Microcontrollers:1105_001 2

Page 3: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Introduction • A MC is a single integrated circuit that executes a user program, normally for the

purpose of controlling some device.

• It is a particular type of processor, optimized to perform control functions for thelowest cost and at the smallest size possible.

• The microcontroller typically includes:

- CPU (Central Processing Unit),

- RAM (Random Access Memory),

- EPROM/PROM/ROM (Erasable Programmable Read Only Memory),

- I/O (input/output) - serial and parallel,

- timers,

- interrupt controller.

Mechatronics & Microcontrollers:1105_001 3

Page 4: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Introduction

Mechatronics & Microcontrollers:1105_001 4

A Microcontroller

Page 5: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Introduction

What is the difference between a microprocessor and a microcontroller?

Mechatronics & Microcontrollers:1105_001 5

Page 6: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Introduction

Mechatronics & Microcontrollers:1105_001 6

Page 7: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Introduction MCs are used in almost all fields. Few applications are listed below:

• Home automation products like thermostats, air conditioners, sprinklers,security monitoring systems.

• Household appliances including microwave ovens, washing machines,television sets, DVD players and recorders.

• Computer peripherals like routers and printers.

• Handheld calculators, personal digital assistants, Cellular telephones.

• Avionics like flight HW/SW control, missiles control system etc.

• Medical equipment.

• Industrial automation etc.

• Digital musical instruments.

Mechatronics & Microcontrollers:1105_001 7

Page 8: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

History of 8051• The 8051MC is an 8 bit MC originally designed in the year 1980

by Intel that has gained great popularity since its introduction.

• The CPU of the 8bit MC can work only with 8bits of data at atime. If data is larger than 8bits, it has to be spilt into 8 bits sizeto be processed.

• MC8051 became popular after Intel permitted othermanufacturers to make and promote other variants of the8051MC .

• The 8051 is the original member of the 8051 family. Intel refersto it as MCS-51.

• There are 2 other members of 8051 family. They are the 8052and 8031 MCs.

Mechatronics & Microcontrollers:1105_001 8

Page 9: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

History of 8051Contrast of 8051 family members

Other manufacturers of 8051MC are: Atmel, Dallas,Philips and Siemens. MCs produced by them are withadvanced features.

Mechatronics & Microcontrollers:1105_001 9

Features 8051 8031 8052

ROM 4KB 0KB 8KB

RAM 128B 128B 256B

I/O pins 32 32 32

Serial port 1 1 1

Interrupt sources

6 6 8

Timers 2 2 3

Page 10: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

8051 Architecture

1. Internal ROM (4kB)

2. Internal RAM (128) - 4 register banks, each containing 8 registers; 16 bytes which may be addressed at the bit level ; 8 bytes of general purpose data memory.

3. I/O ports with programmable pins - 32 I/O pins arranged as 4, 8 bit ports (P0 to P3).

4. Timers & counters - 2, 16 bit timer/ counter (T0 & T1).

5. Serial data communication - Full duplex serial data receiver/ transmitter – SBUF

Mechatronics & Microcontrollers:1105_001 10

Page 11: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

8051 Architecture

6. 8 bit CPU with registers A & B

7. 16 bit PC & data pointer (DPTR).

8. 8 bit PSW.

9. Control registers – TCON, TMOD, SCON, PCON, IP & IE.

10. 2 external and 3 internal interrupt sources.

11. Oscillator and clock circuits.

Mechatronics & Microcontrollers:1105_001 11

Page 12: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

8051 Architecture

Mechatronics & Microcontrollers:1105_001 12

Page 13: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

8051 Architecture

Mechatronics & Microcontrollers:1105_001 13

Page 14: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Pin Description• Vcc(pin 40): Vcc provides supply voltage to the chip. The voltage source

is +5V.

• GND(pin 20):ground.

• XTAL1 and XTAL2(pins 19,18):These 2 pins provide external clock.

• RST(pin 9):reset. It is an input pin and is active high.Upon applying ahigh pulse to RST, the microcontroller will reset and all values in registerswill be lost.

• EA’(pin 31):external access. The EA’ pin is connected to GND toindicate the code is stored externally. For 8051, EA’ pin is connected to Vcc.

• PSEN’(pin 29):program store enable. This is an output pin and isconnected to the OE pin of the ROM

Mechatronics & Microcontrollers:1105_001 14

Page 15: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Pin Description• ALE(pin 30):address latch enable

oIt is an output pin and is active high.

o8051 port 0 provides both address and data.

The ALE pin is used for de-multiplexing the address and data

• I/O port pins

oThe four ports P0, P1, P2, and P3.

oEach port uses 8 pins.

oAll I/O pins are bi-directional.

Mechatronics & Microcontrollers:1105_001 15

Page 16: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

8051 Programming model

Mechatronics & Microcontrollers:1105_001 16

Page 17: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming Model

• The programming model of 8051 is shown in fig.It has 8 and 16 bit registers and 8 bit memorylocations which can be made to operate usingS/W instructions.

• Most of the registers have a specific function.

• Each register except PC has an internal 1 byteaddress assigned to it.

• SW instructions are able to specify a register byits address, its symbolic name .

Mechatronics & Microcontrollers:1105_001 17

Page 18: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming Model8051 Oscillator & Clock

• The heart of the 8051 MC is thecircuitry that generates clock pulsesby which all internal operations aresynchronised.

• Pins XTAL1 and XTAL2 are providedfor connecting a resonant N/W toform an oscillator.

• The crystal frequency is the basicinternal clock frequency of the MC.Typically a quartz crystal andcapacitors are employed.

• Ceramic resonators may be used asa low cost alternative to crystalresonators. However decrease infrequency stability and accuracymake it a poor choice.

Mechatronics & Microcontrollers:1105_001 18

Page 19: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming Model8051 Oscillator & Clock• Serial data communication needs the frequency of the oscillator because of

the requirement that internal counters must divide the basic clock rate toyield standard communication bit per second rate.

• The oscillator formed by the crystal, capacitors generates a pulse train atthe frequency of the crystal.

• The clock frequency f, establishes the smallest interval of time within theMC called pulse, P time.

• The smallest interval of time to execute any simple instructions or part of acomplex instruction is machine cycle.

• One machine cycle = 6 T states. A state is the basic time interval fordiscrete operations of the MC like fetching an opcode byte, decoding anopcode, executing an opcode or writing a data byte.

Mechatronics & Microcontrollers:1105_001 19

Page 20: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming Model8051 Oscillator & Clock• To calculate the time any particular instruction will take to be executed, number of cycles C is to

be found. The time to execute that instruction is then found by multiplying C by 12 and dividingthe product by crystal frequency.

Tinst = [C x 12d] / crystal frequency.

• One machine cycle = 2 ALE pulses. The ALE pulse is used as a timing pulse for external memoryaccess indicates when every instruction byte is fetched.

Mechatronics & Microcontrollers:1105_001 20

Page 21: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelProgram counter & Data pointer• They are both 16 bit registers.

• Each is to hold the address of a byte in memory.

• PC contains the address of the next instruction to be executed. Itdoesn’t have an internal address.

• DPTR is made up of two 8 bit register DPH and DPL.

• DPTR contains the address of internal & external code and data thathas to be accessed.

• DPTR does not have a single internal address. DPH and DPL areassigned as address.

Mechatronics & Microcontrollers:1105_001 21

Page 22: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelA & B CPU Registers• Totally there are 34 general purpose registers or working registers.

• Two of them, A and B hold results of many instructions, particularlymath and logical operations of 8051 cpu.

• The other 32 are in four banks,B0 – B3 of eight registers each.

• A(accumulator) is used for addition, subtraction, mul, div, boolean bitmanipulation and for data transfers.

• But B register can only be used for mul and div operations.

Mechatronics & Microcontrollers:1105_001 22

Page 23: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelProgram Status Word Register• Flags are 1 bit registers provided to store the results of certain program

instructions . Other instructions can test the condition of the flags andmake decisions based on the flag status.

• Flags are grouped inside the PSW and the PCON registers.

• 8051 has 4 math flags that respond to math operations and 3 generalpurpose flags that can be set to 1 or cleared to 0 by the programmer asdesired.

• The math flags are C, AC, OV, P. the user flags are F0, GF0, GF1.

• PSW contains math flags , F0 and register select bits that identify which ofthe 4 general purpose register banks is currently in use by the program.

• The remaining 2 user flags GF0 and GF1 are in PCON register.

Mechatronics & Microcontrollers:1105_001 23

Page 24: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelProgram Status Word Register

Mechatronics & Microcontrollers:1105_001 24

Page 25: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSpecial Function Registers

Mechatronics & Microcontrollers:1105_001 25

• 8051 operations that do not use the internal 128 byte RAM address from00h to 7Fh are done by a group of specific internal registers each called aspecial function register which may be addressed much like internal RAMusing addresses from 80h to FFh.

• SFRs allows the programmer to change only what needs to be altered,leaving the remaining bits in that SFR unchanged.

• SFRs are named in certain opcodes by their functional names such as A orTH0 and are referenced by other opcodes by their addresses such as 0Ehor 8Ch.

• Any address used in the program must start with a number. Thus addressE0h for the A SFR begins with 0. if 0 is not used then assembler will result aerror.

Page 26: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSpecial Function Registers

Mechatronics & Microcontrollers:1105_001 26

Page 27: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSpecial Function Registers

Mechatronics & Microcontrollers:1105_001 27

Register Name Function Internal RAM address

A Accumulator 0E0h

B Arithmetic 0F0h

DPH Addressing external memory 83h

DPL Addressing external memory 82h

IE Interrupt enable control 0A8h

IP Interrupt priority 0B8

P0 I/O port 80h

P1 I/O port 90h

P2 I/O port 0A0h

P3 I/O port 0B0h

Page 28: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSpecial Function Registers

Mechatronics & Microcontrollers:1105_001 28

Register Name Function Internal RAM address

PCON Power control 87h

PSW Program status word 0D0h

SCON Serial port control 98h

SBUF Serial port data buffer 99h

SP Stack pointer 81h

TMOD Timer/ counter mode control 89h

TCON Timer/ counter control 88h

TL0 Timer0 lower byte 8Ah

TH0 Timer0 higher byte 8Ch

TL1 Timer1 lower byte 8Bh

TH1 Timer1 higher byte 8Dh

Page 29: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelStack Pointer• Stack refers to an area of internal RAM which is used to store and return

data quickly.

• The 8 bit stack pointer (SP) register is used by 8051 to hold an internal RAMaddress called as top of stack.

• The address in the SP register is the location in the internal RAM where thelast byte of data was stored by a stack operation.

• When data is to be placed on the stack, the SP increments before storingdata on the stack so that the stack grows up as data is stored.

• As the data is retrieved from the stack, the byte is read from the stack andthen the SP decrements to point to the next available byte of stored data.

Mechatronics & Microcontrollers:1105_001 29

Page 30: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelInternal Memory• 8051 has internal RAM for variable

data storage that can be altered asthe program runs and ROM forprogram code bytes. Additionalmemory can be added externallyusing suitable circuits.

• 8051 has a harvard architecturewhich uses same address indifferent memories for code anddata.

• Internal circuitry accesses thecorrect memory based on thenature of the operation inprogress.

Mechatronics & Microcontrollers:1105_001 30

Page 31: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelInternal Memory• Internal RAM

o 32 bytes from address 00h to 1Fh make up 32 working registers organised as 4 banks of 8registers each. The 4 register banks are numbered 0 to 3 and are made up of 8 registersnamed R0 to R7.

o Each register can be addressed by name or by its RAM address. Ex. R0 of bank3 is R0 ifbank3 is selected in PSW or else 18h whether bank3 is selected or not.

o Register banks can be selected using bits RS0 and RS1 in PSW. Register banks notselected can be used as general purpose RAM. Bank 0 is selected on reset.

o A bit addressable area of 16 bytes occupies RAM byte addresses 20h to 2Fh forming atotal of 128 addressable bits.

o An addressable bit may be specified by its bit address 00h to 7Fh or 8 bits may form anybyte address from 20h to 2Fh.

o A general purpose RAM area above the bit area from 30h to 7Fh addressable as bytes.

Mechatronics & Microcontrollers:1105_001 31

Page 32: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelInternal Memory

Mechatronics & Microcontrollers:1105_001 32

Page 33: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelInternal Memory• Internal ROM

oThe internal program code present in ROM occupies address space0000h to 0FFFh. PC is used to address program code bytes fromaddress 0000h to FFFFh.

oProgram addresses higher than 0FFFh will cause 8051 toautomatically fetch code bytes from external memory.

oCode bytes can also be fetched from an external memory addresses0000h to FFFFh by connecting the external access in (EA’) to gnd.Code bytes can be in internal ROM or in external ROM .

Mechatronics & Microcontrollers:1105_001 33

Page 34: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelPorts• The function a pin at any given instant depends on what is physically

connected to it and then on what software commands are used toprogram the pin. Both of these factors are under the control of the8051 programmer and circuit designer.

• 8051 has 32 I/O pins that are divided into 4 ports - port0, port1,port2 and port3.These I/O pins help 8051 to monitor and controlother devices.

• Each port consists of a D latch, an O/P driver and an I/P buffer.

• All the ports can be defined as I/P or O/P port.

Mechatronics & Microcontrollers:1105_001 34

Page 35: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelPortsO/P Port:

• All the bits are connected to data bus in theport register.

• When data is written to port register, databit is latched to D ff in the port circuit.

• The processor generates WR’ signal which isconnected as active low clock of D ff.

• The data from data bus is latched to D ff.

• If I/P is 0 (D=0) then Q=0 and Q’=1. T2 turnsON and since T2 is connected to GND, O/P=0.

• If I/P is 1 (D=1) then Q=1 and Q’=0. T2 turnsOFF and T1 turns ON and O/P is 1.

Mechatronics & Microcontrollers:1105_001 35

Page 36: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelPorts

I/P Port:

• While reading ports there are 2possibilities.

1. Reading I/P pin: to define any port of8051 as I/P first write ‘1’ to that port bit. Asa result Q=1 and Q’=0.

- In port 0 and 1, Q’ is connected to T2, inport 2 Q is connected to T2 through a NOTgate and in port 3 Q is connected to T2through a NAND gate.(refer slide 32)

- In all cases T2 is OFF pin is simply pulledhigh by the pull up resistors and connectedto I/P buffer. B2 is activated.

2. Reading the latch: B1 is activated and datafrom Q is put to B1 buffer.

Mechatronics & Microcontrollers:1105_001 36

Page 37: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelPortsRead modify write

• Some of the instructions (like INC P2, DEC P1) will read a latch, modify it and then writes to the latch.

• These instructions read the latch instead of reading port pin.

• Those instructions have destination operand as port or a port bit. These instructions are called as read modify write instructions.

Mechatronics & Microcontrollers:1105_001 37

Page 38: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelPorts

Mechatronics & Microcontrollers:1105_001 38

(a) Port 0 (b) Port 1 (c) Port 2 (d) Port3

Page 39: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelPorts

Mechatronics & Microcontrollers:1105_001 39

• Port 0: It is it is used as I/P, O/P and as an address/ data bus (AD0 – AD7)to external memory.

• Port 1: port 1 pins have no dual functions. Only behaves as simple I/O.

• Port 2: it may be used as an I/P or O/P. the alternate use of port 2 is to send high order address byte (A8 –A15).

• Port 3: it is an I/P or O/P port similar to port1. The I/P and O/P functions can be programmed under thecontrol of P3 latches or under the control of other SFR’s. Each pin of port 3 may be individually programmedto be used either as I/O or as one of the alternate functions.

Page 40: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelTimers & Counters

• 8051 has two 16-bit Timer/Counter registers

oTimer/Counter 0 (TL0 & TH0)

oTimer/Counter 1 (TL1 & TH1)

• These registers can be used as timers or as event counters.

• When a register is in “Timer” state, it is incremented every clock cycle.

• In “Counter” state, the register is incremented when there is a 1-to-0transition at its external input pin, pin T0 or T1.

• Both registers have additional four operating modes.

• All counter action is controlled by bits in timer mode control register(TMOD), the timer/ counter control register (TCON) and certain programinstructions.

Mechatronics & Microcontrollers:1105_001 40

Page 41: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelTimers & Counters• TMOD register can be considered as 2, 4 bit registers each of which controls

the action of one of the timers.

Mechatronics & Microcontrollers:1105_001 41

Page 42: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelTimers & Counters• TCON has control bits and flags for the timers in upper nibble and control bits

and flags for external interrupts in lower nibble.

Mechatronics & Microcontrollers:1105_001 42

Page 43: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelTimers & Counters• The counters have been included on chip to relieve the processor of timing and

counting work.

• When the program wishes to count a certain number of internal pulses or external events a number ( maximum count – desired count +1) is placed in one of the counters.

• The counter increments from the initial number to the maximum and then rolls over to the final pulse and sets a timer flag.

• The flag condition may be tested by an instruction to tell the program that the count has been accomplished or the flag may be used to interrupt the program.

• If a counter is programmed to be a timer, it will count the internal clock frequency of 8051 oscillator divided by 12.

Mechatronics & Microcontrollers:1105_001 43

Page 44: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelTimers & Counters• The resultant timer clock is gated to the

timer by means of the circuit shown.

• In order for oscillator clock pulses to reachthe timer the C/T’ bit in the TMOD registermust be set to 0 (timer operation).

• Bit TRX in TCON register = 1 and gate bit inTMOD register = 0 or external pin INTX’ =1.

Mechatronics & Microcontrollers:1105_001 44

Page 45: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelTimers & Counters – Timer modes

Mode0:

• Timer high-byte (THx) is cascadedwith the 5 least-significant bits ofthe timer low-byte (TLx) to form a13-bit timer, where x = 0 or 1.

• Upper 3-bits of TLx are not used.

• Overflow occurs on the 1FFFH-to-0000H and sets the timeroverflow flag.

• MSB is THx bit 7, and LSB is TLx bit0.

Mechatronics & Microcontrollers:1105_001 45

Page 46: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelTimers & Counters – Timer modes

Mode1:

• Same as mode 0 except that it is16-bit. Timer high-byte (THx) iscascaded the timer low-byte (TLx)to form a 16-bit timer, where x = 0or 1.

• Overflow occurs on the FFFFH-to-0000H and sets the timeroverflow flag.

• MSB is THx bit 7, and LSB is TLx bit0.

Mechatronics & Microcontrollers:1105_001 46

Page 47: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelTimers & Counters – Timer modes

Mode2:

• Timer low -byte (TLx) operates asan 8-bit timer while the timerhigh -byte (THx) holds a reloadvalue.

• When the count overflows fromFFH-to-00H, not only the timerflag set, but also the value in THxis loaded into TLx, and countingcontinues from this value up tonext FFH-to-00H, so on.

Mechatronics & Microcontrollers:1105_001 47

Page 48: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelTimers & Counters – Timer modes

Mode3:

• Timer 0 Splits into two 8-bitcounter/timers. TL0 and TH0 actas two separate timers withoverflows setting the TF0 and TF1respectively.

• Timer 1 (when timer 0 is in mode3 ) can be used in mode 0, 1, or 2

Mechatronics & Microcontrollers:1105_001 48

Page 49: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelInterrupts

• A computer program has only 2 ways to determine the conditionsthat exist in internal and external circuits.

• One method uses S/W instructions that jump to sub routines on thestates of flags and port pins. Second method responds to H/W signalscalled interrupts that force the program to call a sub routine.

• S/W techniques use up the controllers time where as the interruptstake the controllers time only when action by program is required.

• Interrupts may be generated by internal chip operations or providedby external sources. Ant interrupt can cause 8051 to perform H/Wcall to an interrupt handling subroutine that is located at apredetermined absolute address in program memory.

• All interrupt functions are under the control of the program. Theprogrammer is able to alter control bits in IE, IP and TCON registers.

Mechatronics & Microcontrollers:1105_001 49

Page 50: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelInterrupts• 5 interrupts are provided in 8051 , generated automatically by

internal operations.

- Timer flag interrupt (TF0, TF1): when a timer overflows TF is setto 1. it is reset when the resulting interrupt generates aprogram call to appropriate timer subroutine in memory.

- Serial port interrupt (TI, RI): if data byte is received the RI=1 inSCON and if data byte is transmitted then TI=1 in SCON. Theseare ORed to get a single interrupt. When the processorexecutes corresponding ISR, TI and RI are not cleared. Programused for serial data communication must reset TI and RI.

- External interrupts (IE0, IE1): INTR0’ and INTR1’ are used to setinterrupt flags IE0 and IE1 in TCON register. IEx =1 when INTRx’=0 or H-to-L on INTRx’. INTRx’ = 0 when ITX=0. INTRx’ = H-to-Lwhen ITX=1. ITX is in TCON register. It is programmers duty toremove signals at INTRx’ pins when ISR starts executing andbefore RETI instruction.

Mechatronics & Microcontrollers:1105_001 50

Page 51: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelInterrupts• RESET – non maskable interrupt. It is a absoulte command

to jump to program address 0000h and start running fromthere.

• RST = 1. MC is reset.

• After RST is made low, following registers have contents –

PC, DPTR, A,B, PSW, TCON, TMOD, TH0,TL0, TH1, TL1,SCON =

00h.

SP = 07h, P0 to P3 = FFh, SBUF = XX.

IP = XXX00000b, IE = 0XX00000b, PCON = 0XXXXXXXb.

• Register bank 0 is selected and RAM contents maychange.

Mechatronics & Microcontrollers:1105_001 51

Page 52: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelInterrupt Control• The IE register holds the programmable bits that

can enable or disable all the interrupts as a groupor if the group is enabled, each individualinterrupt source can be enabled or disabled.

• Bits in IE are set to 1 if corresponding interruptsource is to be enabled and set to 0 to disableinterrupt source. Bit EA is a master or global bitthat can enable or disable all the interrupts.

Mechatronics & Microcontrollers:1105_001 52

Page 53: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelInterrupt Control

Mechatronics & Microcontrollers:1105_001 53

• Interrupt Enable register:EA ---- ET2 ES ET1 EX1 ET0 EX0

- EA : Global interrupt enable.

- ET2 : reserved

- ES : Serial interface.

- ET1 : Timer 1.

- EX1 : External interrupt 1.

- ET0 : Timer 0.

- EX0 : External interrupt 0.

- 0 = Disabled.

- 1 = Enabled.

Page 54: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelInterrupt Control• The IP register bits may be set by the program to assign

priorities among the various interrupt sources so that moreimportant interrupts can be serviced first when manyinterrupts occur at same time.

• Register IP determines if any interrupt is to have a high or lowpriority. If bit is set to 1 then that interrupt has high priorityand if set to 0 then that interrupt has low priority.

• If 2 interrupts with same priority occur at same time thenfollowing order is used – IE0>TF0>IE1>TF1>serial interrupt.

Mechatronics & Microcontrollers:1105_001 54

Page 55: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelInterrupt Control

Mechatronics & Microcontrollers:1105_001 55

• Interrupt Priority register:----- ----- PT2 PS PT1 PX1 PT0 PX0

- PT2 : reserved.

- PS : Serial interface.

- PT1 : Timer 1.

- PX1 : External interrupt 1.

- PT0 : Timer 0.

- PX0 : External interrupt 0.

- 0 = Low priority.

- 1 = High priority.

Page 56: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelInterrupt Control• Each interrupt source causes the program to do a

H/W call to one of the dedicated address inprogram memory.

Mechatronics & Microcontrollers:1105_001 56

Interrupt Address(hex)

IE0 0003h

IE1 0013h

TF0 000Bh

TF1 001Bh

Serial 0023h

Page 57: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSerial Communication• Computers must be able to communicate with other

computers in modern computer multiprocessorsystems. One cost effective way to communicate is tosend and receive data bits serially.

• 8051 has a serial data communication circuit thatuses register SBUF to hold data. Register SCONcontrols data communication and register PCONcontrols data rates and pins RXD (P3.0) and TXD(P3.1) connect to the serial data network.

• SBUF is considered as 2 registers. One is write onlyand is used to hold data to be transmitted out of8051 via TXD. The other is read only and holdsreceived data from external sources via RXD. Bothregisters use single address 99h.

Mechatronics & Microcontrollers:1105_001 57

Page 58: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSerial Communication• SCON register

• REN: Receiver enable is set/reset by program

• TB8: stores transmitted bit 8(9th bit, the stop bit)

• RB8: Stores received bit 8(9th bit, the stop bit)

• 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 by program

• 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 by program

• RI and TI flag in SCON SFR are used to detect the interrupt events.

• If RI = 1 then a byte is received at the RxD pin. If TI = 1 then a byte is transmitted from the TxD pin.

Mechatronics & Microcontrollers:1105_001 58

Page 59: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSerial Communication

• SCON register

Mechatronics & Microcontrollers:1105_001 59

Page 60: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSerial Communication

• PCON register

• SMOD(serial mode) 1= high baud rate, 0 = low baud rate

• GF1, GF2 flags for free use

• PD: 1= power down mode .

• IDL: 1= idle mode.

Mechatronics & Microcontrollers:1105_001 60

Page 61: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSerial CommunicationSerial data interrupts:

• Serial data communication is relatively slowprocess, occupying many milliseconds per databyte to accomplish. Serial data flags are includedin SCON to aid in efficient data transmission andreception.

• The flags in SCON TI and RI are set whenever databyte is transmitted or received. These flags areORed together to produce an interrupt to theprogram. The program must read these flags tofind what caused the interrupt and then clear theflag.

Mechatronics & Microcontrollers:1105_001 61

Page 62: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSerial Communication

Data Transmission:

• Transmission of serial data begins anytime data iswritten to SBUF. TI is set to 1 when data has beentransmitted and signifies that SBUF is empty andanother byte can be sent.

• If program overwrites data before TI = 1 then datawill be lost.

Mechatronics & Microcontrollers:1105_001 62

Page 63: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSerial CommunicationData Reception:• Reception of serial data will begin if REN =1 in SCON

for all modes. RI is set after data has been received inall modes.

• Setting REN is the only direct program control thatlimits the reception of unexpected data.

• Reception can begin in modes 1,2 and 3 if RI is setwhen serial stream of bits begin. RI must have beenreset by the program before the last bit is received orincoming data will be lost.

• Incoming data is not transferred to SBUF until the lastdata bit has been received so that the previoustransmission can be read from SBUF while new datais being received.

Mechatronics & Microcontrollers:1105_001 63

Page 64: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSerial CommunicationSerial Data Transmission modes:

• There are 4 programmable modes for serial data communication that are chosen by setting SMX bits in SCON.

• Baud rates are determined by the modes chosen.

• The 4 modes are –

- Serial data mode 0 – shift register mode.

- Serial data mode 1 – standard UART.

- Serial data mode 2 – multiprocessor mode.

- Serial data mode 3

Mechatronics & Microcontrollers:1105_001 64

Page 65: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSerial Communication1. Serial data mode 0:

• Setting bits SM0 and SM1 in SCON to 00b configuresSBUF to receive or transmit 8 data bits using pin RXDfor both functions. Pin TXD is connected to theinternal shift frequency pulse source to supply shiftpulses to external circuits.

• The shift frequency or baud rate is fixed at 1/12 ofthe oscillator frequency. When transmitting data isshifted out of RXD one clock pulse , after the risingedge of TXD shift clock.

• Received data comes in on pin RXD and should besynchronised with the shift clock produced at TXD.

Mechatronics & Microcontrollers:1105_001 65

Page 66: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSerial Communication2. Serial data mode 1:

• When SM0 and SM1 are set to 01b, SBUF becomes a 10 bit fullduplex receiver/ transmitter that may receive and transmit data atthe same time.

• Pin RXD receives all data and pin TXD transmits all data.

• Transmitted data : start bit, 8 data bits (lsb…….msb), stop bit.

• Interrupt flag TI is set when all 10bits have been sent.

• Each bit interval = 1/ baud rate frequency and each bit is high or lowfor that interval.

• Received data is obtained in same order.

• Data bits are shifted into the receiver at the programmed baud rateand the data will be loaded to SBUF if RI=0 and SM2 =0 or stop bit =1.

• If 10 bits, start bit is discarded, 8 data bits go to SBUF and stop bit isstored in RB8 of SCON. RI is set to 1 indicating new data byte isreceived.

Mechatronics & Microcontrollers:1105_001 66

Page 67: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSerial Communication2. Serial data mode 1:• If RI is set at the end of reception then previous data byte may not be

read and new data loaded will be lost.

• Timer 1 is used to generate the baud rate for mode1 using timeroverflow (TF) flag to determine baud frequency. Typically timer1 isused in mode 2 (autoreload 8 bit timer) that generates baudfrequency.

• fbaud = [2smod/ 32d] x [ oscillator frequency / {12d x (256d – TH1)}].

• If timer1 is not in mode2 then fbaud = [2smod/ 32d] x timer1 overflowfrequency.

• Example: if baud rate = 9600, oscillator frequency = 11.0592MHz thencount in TH1?

TH1 = 256d – [(2smod x oscillator frequency)/ (32d x 12d x 9600)]

Smod =0, TH1 = 253d = FDh.

Mechatronics & Microcontrollers:1105_001 67

Page 68: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSerial Communication3. Serial data mode 2:

• Mode 2 is similar to mode 1 except 11 bits aretransmitted – a start bit, 9 data bits and a stopbit.

• 9th bit is copied to TB8 in SCON duringtransmission and RB8 in SCON during reception.Both start and stop bits are discarded.

• F baud = [2smod / 64d] x oscillator frequency.

• Data can be collected quickly from an extensivenetwork of communicating microcontrollers ifhigh baud rates are employed.

Mechatronics & Microcontrollers:1105_001 68

Page 69: 8051 Microcontroller · 8051 Microcontroller. Contents •Introduction to MC •History of 8051 MC •8051 Architecture Mechatronics & Microcontrollers:1105_001 2. Introduction •A

Programming ModelSerial Communication

4. Serial data mode 3:

• It is similar to mode 2 except that baud rate is determined exactly as in mode 1 using timer 1 to generate communication frequencies.

Mechatronics & Microcontrollers:1105_001 69