You are on page 1of 27

MICRO CONTROLLER

Since the invention of microprocessor, different companies has started manufacturing more and more sophisticated processors with improved features such as large data bus, large address bus, sophisticated memory management techniques and instruction set, capability of handling wide range of integer and floating point data, parallel processing of instructions, etc.,. But these sophisticated processors are not necessary for small applications such as controlling a motor, monitoring/controlling temperature, switching ON/OFF traffic lights, etc.,. in the 1980s the manufacturers of microprocessors realized that there is need for low cost, compact, single chip programmable systems for small dedicated applications and so started manufacturing another class of programmable ICs called microcontrollers. The microcontrollers are similar to microprocessors, but they are designed to work as a true single chip system by integrating all the devices needed for a system on a single chip. The basic functional units of a microprocessor will be ALU, a set of registers, timing and control unit. The microcontroller will have these functional blocks and in addition may have I/O ports, programmable timers, RAM memory and EPROM/EEPROM memory. Some of the microcontroller may have even internal ADC and/or DAC.

What is a Microcontroller?
A Microcontroller is a programmable digital processor with necessary peripherals. Both microcontrollers and microprocessors are complex sequential digital circuits meant to carry out job according to the program / instructions. Microcontroller (MC) may be called computer on chip since it has basicfeatures of microprocessor with internal ROM, RAM, Parallel and serialports within single chip. Or we can say microprocessor with memory andports is called as microcontroller. This is widely used in washing machines,vcd player, microwave oven, robotics or in industries. Microcontroller can be classified on the basis of their bits processed like 8bit MC, 16bit MC. 8 bit microcontroller, means it can read, write and process 8 bit data. Ex. 8051 microcontroller. Basically 8 bit specifies the size of data bus. 8 bit microcontroller means 8 bit data can travel on the data bus or we can read, write process 8 bit data.

Microcontrollers Vs Microprocessors:
1. A microprocessor requires an external memory for program/data storage. Instruction execution requires movement of data from the external memory to the microprocessor or vice versa. Usually, microprocessors have good computing power and they have higher clock speed to facilitate faster computation. 2. A microcontroller has required on-chip memory with associated peripherals. A microcontroller can be thought of a microprocessor with inbuilt peripherals. 3. A microcontroller does not require much additional interfacing ICs for operation and it functions as a stand alone system. The operation of a microcontroller is multipurpose, just like a Swiss knife. 4. Microcontrollers are also called embedded controllers. A microcontroller clock speed is limited only to a few tens of MHz Microcontrollers are numerous and many of them are application specific.

Development/Classification of microcontrollers (Invisible):


Microcontrollers have gone through a silent evolution (invisible). The evolution can be rightly termed as silent as the impact or application of a microcontroller is not well known to a common user, although microcontroller technology has undergone significant change since early 1970's. Development of some popular microcontrollers is given as follows. Intel 4004 Intel 8048 Intel 8031 Intel 8051 Microchip PIC16C64 Motorola 68HC11 Intel 80C196 Atmel AT89C51 Microchip PIC 16F877 4 bit (2300 PMOS trans, 108 kHz) 8 bit 8 bit (ROM-less) 8 bit (Mask ROM) 8 bit 8 bit (on chip ADC) 16 bit 8 bit (Flash memory) 8 bit (Flash memory + ADC) 1971 1976 . 1980 1985 . 1982 . .

Microprocessors:
General-purpose microprocessor CPU for Computers No RAM, ROM, I/O on CPU chip itself ExampleIntels x86, Motorolas 680x0

Microcontroller:
A smaller computer On-chip RAM, ROM, I/O ports... ExampleMotorolas 6811, Intels 8051, Zilogs Z8 and PIC 16X

8051 MICRO CONTROLLER


The 8051 Microcontroller was designed in 1980s by Intel. Its foundation was on Harvard Architecture and was developed principally for bringing into play in Embedded Systems. At first it was created by means of NMOS technology but as NMOS technology needs more power to function therefore Intel re-intended Microcontroller 8051 employing CMOS technology and a new edition came into existence with a letter C in the title name, for illustration: 80C51. These most modern Microcontrollers need fewer amount of power to function in comparison to their forerunners. There are two buses in 8051 Microcontroller one for program and other for data. As a result, it has two storage rooms for both program and data of 64K by 8 size. The microcontroller comprise of 8 bit accumulator & 8 bit processing unit. It also consists of

8 bit B register as majorly functioning blocks. It also has a number of other 8 bit and 16 bit registers. For internal functioning & processing Microcontroller 8051 comes with integrated built-in RAM. This is prime memory and is employed for storing temporary data. It is unpredictable memory i.e. its data can get be lost when the power supply to the Microcontroller switched OFF.

Pin Diagram of 8051:

8051 Microcontroller us a 40 PIN Integrated Circuit. Following is the figure of PINs of IC of 8051 microcontroller. Explanation for each PIN is given below: Pins 1 to 8(Port 1): The Pins 1.0 to 1.7 are 8 Pins of port 1. Each of them can be configured as input or output pin. Pin 9: It is used to Reset Microcontroller 8051. A positive pulse is given on this Pin to reset Microcontroller. Pin 10 to 17(Port 3): These Pins are similar to Pins of Port 1. These Pins can be used as universal Input or output. These are dual function Pins. Function of each Pin is given as:

Pin 10:It is Serial Asynchronous Communication Input or Serial Asynchronous Communication Output. Pin 11: Serial Asynchronous Communication Output or Serial Synchronous Communication Output. Pin 12: Interrupt 0 input. Pin 13: Interrupt 1 input.

Pin 14: Counter 0 clock input. Pin 15: Counter 1 clock input. Pin 16: Writing Signal for writing content on external RAM. Pin 17: Reading Signal to read contents of external RAM.

Pin 18 and 19: These are input output PINS for oscillator. An internal oscillator is connected to Micro controller through these PINS. Pin 20: Pin 20 is grounded. Pin 21 to 28 (Port 2):These Pins can be configured as Input Output Pins. But this is only possible in case when we don't use any external memory. If we use external memory then these pins will work as high order address bus(A8 to A15). Pin 29: If we uses an external ROM then it should has a logic 0 which indicates Micro controller to read data from memory. Pin 30: This Pin is used for ALE that is Address Latch Enable. If we uses multiple memory chips then this pin is used to distinguish between them. This Pin also gives program pulse input during programming of EPROM. Pin 31: If we have to use multiple memories then by applying logic 1 to this pin instructs Micro controller to read data from both memories first internal and afterwards external. Pin 32 to 39(Port 0): Similar to port 2 and 3, these pins can be used as input output pins when we don't use any external memory. When ALE or Pin 30 is at 1 then this port is used as data bus, when ALE pin at 0, then this port is used as lower order address bus(A0 to A7). Pin 40: Power Supply, (Vcc).

Features of Microcontroller 8051:


Following are the main features of Micro controller 8051 which makes it most efficient Microcontroller chip:

It Includes Boolean Processing Engine. Thus internal registers and RAM can carry Boolean logic operations directly and efficiently. It gives us many functions in a single chip. (For example: CPU,RAM,ROM,I/O, Interrupt, Timer etc) It have 8 bit Data bus. It have 8 bit Stack Pointer. It have 16 bit Program Counter. It have 16 bit address bus which can access almost 65,536 memory locations. Data memory or RAM of 128 bytes.(On-Chip). Program Memory or ROM of 4 KB.(On Chip). Bi-Directional I/O port of 4 bytes. It has 4 separate Register Sets.

Serial Port or UART. It features Power Saving Mode which saves power. Two Timers/Counters each of 16 bit. Internal and External Interrupt Sources. 2 level interrupt priority.

Architecture of 8051:

Explanation:
ALU Arithmetic Logical Unit: This unit is used for the arithmetic calculations. A-Accumulator: This register is used for arithmetic operations. This is also bit addressable and 8 bitregister. B-Register: This register is used in only two instructions MUL AB and DIV AB. This is alsobit addressable and 8 bit register.

PC-Program Counter Points to the address of next instruction to be executed from ROM It is 16 bit register means the 8051 can access program address from0000H to FFFFH. A total of 64KB of code. 16 bit register means. Initial value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (0000H) Final value 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 (FFFFH) Initially PC has 0000H ORG instruction is used to initialize the PC ORG 0000H means PCinitialize by 0000H PC is incremented after each instruction. Central Processor Unit(CPU): As you may know that CPU is the brain of any processing device. It monitors and controls all operations that are performed in the Microcontroller. User have no control over the work of CPU. It reads program written in ROM memory and executes them and do the expected task. Interrupts: As its name suggests, Interrupt is a subroutine call that interrupts Microcontroller's main operation or work and causes it to execute some another program which is more important at that time. The feature of Interrupt is very useful as it helps in cases of emergency. Interrupts gives us a mechanism to put on hold the ongoingoperation , execute a subroutine and then again resumes normal program execution. The Microcontroller 8051 can be configured in such a way that it temporarily terminates or pause the main program at the occurrence of interrupt. When subroutine is completed then the execution of main program starts as usual. There are five interrupt sources in 8051 Microcontroller. 2 of them are external interrupts, 2 timer interruptsand one serial port interrupt. Memory: Microcontroller requires a program which is a collection of instructions. This program tells Microcontroller to do specific tasks. These programs requires a memory on which these can be saved and read by Microcontroller to perform specific operation. The memory which is used to store the program of Microcontroller, is known as code memory or Program memory. It is known as 'ROM'(Read Only Memory). Microcontroller also requires a memory to store data or operands temporarily. The memory which is used to temporarily store data for operation is known as Data Memory and we uses 'RAM'(Random Access Memory) for this purpose. Microcontroller 8051 has 4K of Code Memory or Program memory that is it has 4KB Rom and it also have 128 bytes of data memory i.e. RAM. Bus: Basically Bus is a collection of wires which work as a communication channel or medium for transfer of Data. These buses consist of8, 16 or more wires. Thus these can carry 8 bits, 16 bits simultaneously. Buses are of two types:

Address Bus Data Bus

Address Bus: Microcontroller 8051 has a 16 bit address bus. It used to address memory locations. It is used to transfer the address from CPU to Memory. Data Bus: Microcontroller 8051 has 8 bits data bus. It is used to carry data. Oscillator: As we know Microcontroller is a digital circuit device, therefore it requires clock for its operation. For this purpose, Microcontroller 8051 has an on-chip oscillator which works as a clock source for Central Processing Unit. As the output pulses of oscillator are stable therefore it enables synchronized work of all parts of 8051 Microcontroller. Input/Output Port: As we know that Microcontroller is used in Embedded systems to control the operation of machines. Therefore to connect it to other machines, devices or peripherals we requires I/O interfacing ports in Microcontroller. For this purpose Microcontroller 8051 has 4 input output ports to connect it to other peripherals. Timers/Counters: Microcontroller 8051 has 2 16 bit timers and counters. The counters are divided into 8 bit registers. The timers are used for measurement of intervals , to determine pulse width etc.

Memory Organization of 8051:


The 8051 has on-chip memories (internal memories RAM & EPROM), in case of internal memory is not sufficient, the controller will access two external memories of size 64Kb each. One for program code and another one for data. Memory Internal Memory: RAM Size: 128 bytes Address: 00-7FH ROM 4Kb 0000-0FFFH RAM 64Kb 0000-FFFFH External ROM 64Kb

The 8051 has a separate memory space for code (programs) and data. We will referhere to on-chip memory and external memory as shown in following figure. In an actual implementation the external memory may, in fact, be contained within themicrocomputer chip. However, we will use the definitions of internal and externalmemory to be consistent with 8051 instructions which operate on memory. Note, theseparation of the code and data memory in the 8051 architecture is a little unusual.The separated memory architecture is referred to as Harvard architecture whereasVon Neumann architecture defines a system where code and data can share common memory.

Internal Memory:
1. A functioning computer must have memory for program code bytes, commonly in ROM, and RAM memory for variable data that can be altered as the program runs 2. 8051 has internal RAM (128 bytes) and ROM (4Kbytes) 3. 8051 uses the same address but in different memories for code and data 4. Internal circuitry access the correct memory based on the nature of the operation in progress 5. Can add memory externally if needed The 8051s on-chip memory consists of 256 memory bytes organised as follows: First 128 bytes: 00h to 1Fh Register Banks 20h to 2Fh Bit Addressable RAM 30 to 7Fh General Purpose RAM

Next 128 bytes: 80h to FFh Special Function Registers. The 8- bit data pointers (DPH or DPL) is used to access these internal data memories.

128 bytes of Internal RAM Structure (lower address space):

The lower 32 bytes are divided into 4 separate banks. Each register bank has 8 registers of one byte each. A register bank is selected depending upon two bank select bits in the PSW register. Next 16bytes are bit addressable. In total, 128bits (16X8) are available in bit addressable area. Each bit can be accessed and modified by suitable instructions. The bit addresses are from 00H (LSB of the first byte in 20H) to 7FH (MSB of the last byte in 2FH). Remaining 80bytes of RAM are available for general purpose.

Register Banks: 00h to 1Fh


The 8051 uses 8 general-purpose registers R0 through R7 (R0, R1, R2, R3, R4, R5, R6, and R7). These registers are used in instructions such as: ADD A, R2 ; adds the value contained in R2 to the accumulator Note since R2 happens to be memory location 02h in the Internal RAM the following instruction has the same effect as the above instruction. ADD A, 02h

Bit Addressable RAM: 20h to 2Fh


The 8051 supports a special feature which allows access to bit variables. This iswhere individual memory bits in Internal RAM can be set or cleared. In all there

are128 bits numbered 00h to 7Fh. Being bit variables any one variable can have a value 0or 1. A bit variable can be set with a command such as SETB and cleared with acommand such as CLR. Example instructions are: SETB 25h ; sets the bit 25h (becomes 1) CLR 25h ; clears bit 25h (becomes 0) Note, bit 25h is actually bit b5 of Internal RAM location 24h. The Bit Addressable area of the RAM is just 16 bytes of Internal RAM locatedbetween 20h and 2Fh. So if a program writes a byte to location 20h, for example, itwrites 8 bit variables, bits 00h to 07h at once.

General Purpose RAM(Scratch pad): 30h to 7Fh


These 80 bytes of Internal RAM memory are available for general-purpose datastorage. Access to this area of memory is fast compared to access to the main memoryand special instructions with single byte operands are used. However, these 80 bytesare used by the system stack and in practice little space is left for general storage. Thegeneral purpose RAM can be accessed using direct or indirect addressing modes.

4KB ROM internal ROM:


In 8051, 4KB read only memory (ROM) is available for program storage.This is used for permanent data storage. Or the data which is not changedduring the processing like the program or algorithm for specific applications. This is volatile memory; the data saved in this memory does not disappearafter power failure. We can interface up to 64KB ROM memory externally if the application islarge. These sizes are specified different by their companies. Address Range of PC: Address range of PC means program counter (whichpoints the next instruction to be executing) can be moved between theselocations or we can save the program from this location to this location.The address range can be calculated in the same way just like the RAMwhich is discussed in previous section. 4KB=22.210B(since 1KB=210B) =212Byte Address range of PC is 0000H to 0FFFH means total 4KB locations are availablefrom 0000H to 0FFFH. At which we can save the program. Difference between RAM and ROM: RAM is used for data storage while ROM is used for program storage. Data of RAM can be changed during processing while data of ROM cantbe changed during processing. We can take an example of calculator. If we want to perform addition oftwo numbers then we type the two numbers in calculator, this is saved inthe RAM, but the Algorithms

by which the calculation is performed is savedin the ROM. Data which is given by us to calculator can be changed but thealgorithm or program by which calculation is erformed cant be changed.

EXTERNAL MEMORIES:
8051 can access external memories only when internal memory is not sufficient. 8051 can access two separate memories(program and data).

External Code Memory(program memory):


The executable program code is stored in this code memory. The code memory size islimited to 64KBytes (in a standard 8051). The code memory is read-only in normaloperation and is programmed under special conditions e.g. it is a PROM or a FlashRAM type of memory. The 16-bit Program counter is used to access this program memory.

External RAM Data Memory:


This is read-write memory and is available for storage of data. Up to 64KBytes ofexternal RAM data memory is supported (in a standard 8051). The 16- bit DPTR(data pointer) is used to access this data memory. FFFFH 64Kb RAM 0000H

Register set of 8051- SFRs:


The registration area or special functions(SFR) is between 80H and FFH address the internal memory of the microcontroller. This memory area cannot be used as data memory, it is clear that if we write out of control in the memory allocated to a special register, modify the behavior of the microcontroller, resulting in unexpected results as the affected record. Although the memory locations that are not assigned to a special register can be used to hold data, it is not advisable to use, it has to bear in mind that these may be reserved and assigned to special registers in other models or manufacturers of microcontrollers and our program will conflict when changing model. There are 21 Special function registers (SFR) in 8051 micro controller and this includes Register A, Register B, Processor Status Word (PSW), PCON etc etc. There are 21 unique locations for these 21 special function registers and each of these register is of 1 byte size. Some of these special function registers are bit addressable (which means you can access 8 individual bits inside a single byte), while some others are only byte addressable.

F0 F7

F6

F5

F4

F3

F2

F1

F0

E0 E7

E6

E5

E4

E3

E2

E1

E0

ACC

D0 D7

D6

D5

D4

D3

D2

D1

D0

PSW

B8 BF

BE

BD

BC

BB

BA

B9

B8

IP

B0 B7

B6

B5

B4

B3

B2

B1

B0

P3

A8 AF

AE

AD

AC

AB

AA

A9

A8

IE

A0 A7

A6

A5

A4

A3

A2

A1

A0

P2

99 98 9F 9E 9D 9C 9B 9A 99 98

SBUF SCON

90 97

96

95

94

93

92

91

90

P1

8D 8C

TH1 TH0

8B 8A 89 88 8F 87 8E 8D 8C 8B 8A 89 88

TL1 TL0 TMOD TCON PCON

83 82 81 80 87 86 85 84 83 82 81 80

DPH DPL SP P0

Fig: SFRs Lsit Note: bit addresses are specified in the bit addressable registers. These registers are byte addressable also. Register A/Accumulator The most important of all special function registers, thats the first comment about Accumulator which is also known as ACC or A. The Accumulator (sometimes referred to as Register A also) holds the result of most of arithmetic and logic operations. ACC is usually accessed by direct addressing and its physical address is E0H. Accumulator is both byte and bit addressable.

Register B The major purpose of this register is in executing multiplication and division. The 8051 micro controller has a single instruction for multiplication (MUL) and division (DIV). If you are familiar with 8085, you may now know that multiplication is repeated addition, whereas division is repeated subtraction. While programming 8085, you may have written a loop to execute repeated addition/subtraction to perform multiplication and division. Now here in 8051 you can do this with a single instruction. Ex: MUL A,B When this instruction is executed, data inside A and data inside B is multiplied and answer is stored in A.

Register B is also byte addressable and bit addressable. To access bit o or to access all 8 bits (as a single byte), physical address F0 is used. To access bit 1 you may use F1 and so on. Please take a look at the picture below.

Note: Register B can also be used for other general purpose operations. Port Registers There are 4 Input/Output ports in 8051, named P0, P1, P2 and P3 has got four corresponding port registers with same name P0, P1, P2 and P3. Data must be written into port registers first to send it out to any other external device through ports. Similarly any data received through ports must be read from port registers for performing any operation. All 4 port registers are bit as well as byte addressable.

The physical address of port 0 is 80 The physical address of port 1 is 90 And that of port 2 is A0 And that of port 3 is B0

Stack Pointer Known popularly with an acronym SP, stack pointer represents a pointer to the the system stack. Stack pointer is an 8 bit register, the direct address of SP is 81H and it is only byte addressable, which means you cant access individual bits of stack pointer. The content of the stack pointer points to the last stored location of system stack. To store something new in system stack, the SP must be incremented by 1 first and then execute the store command. Usually after a system reset SP is initialized as 07H and data can be stored to stack from 08H onwards. This is usually a default case and programmer can alter values of SP to suit his needs. Power Management Register (PCON) As the name indicates, this register is used for efficient power management of 8051 micro controller. Commonly referred to as PCON register, this is a dedicated SFR for power management alone. From the figure below you can observe that there are 2 modes for this register :-Idle mode and Power down mode.Setting bit 0 will move the micro controller to Idle mode and Setting bit 1 will move the micro controller to Power down mode.

Processor Status Word (PSW)


Commonly known as the PSW register, this is a vital SFR in the functioning of micro controller. This register reflects the status of the operation that is being carried out in the processor. The picture below shows PSW register and the way register banks are selected using PSW register bits RS1 and RS0. PSW register is both bit and byte addressable. The physical address of PSW starts from D0H. The individual bits are then accessed using D1, D2 D7.

So far we have discussed about all major SFRs in 8051. There many other still waiting! Please remember there are 21 SFRs and we have discussed only 9 specifically. The table below lists all other 12 SFRs. SFR DPH DPL IP IE SBUF SCON TCON TH0 TL0 TH1 TL1 TMOD Address 83 82 B8 A8 99 98 88 8C 8A 8D 8B 89 Function Data pointer registers (High). Only byte addressing possible. Data pointer register (Low). Only byte addressing possible. Interrupt priority. Both bit addressing and byte addressing possible. Interrupt enable. Both bit addressing and byte addressing possible. Serial Input/Output buffer. Only byte addressing is possible. Serial communication control. Both bit addressing and byte addressing possible. Timer control. Both bit addressing and byte addressing possible. Timer 0 counter (High). Only byte addressing is possible. Timer 0 counter (Low). Only byte addressing is possible. Timer 1 counter (High). Only byte addressing is possible. Timer 1 counter (Low). Only byte addressing is possible. Timer mode select. Only byte addressing is possible.

Interrupts:
Interrupt is an input to a processor that indicates the occurrence of an event. EXTERNAL EVENTS: change the status of microprocessor pin. INTERNAL EVENTS: timer overflow, transmission / reception of a byte through the serial ports. RESPONSE OF PROCESSOR: 1. saves the current machine status, 2. Branches to execute a subprogram called ISR. ACTION ON OCCURANCE OF AN INTERRUPT: CPU jumps to the location associated with that interrupt in the program memory and starts executing from there. This location is called VECTOR. This type of interrupt is called VECTORED INTERRUPT. After serving the interrupt, the processor restores the original machine status and continues with the original program. MCS-51 supports 5 Vectored interrupt Sources External Interrupt 0 External Interrupt 1 Timer / Counter 0 Interrupt Timer / Counter 1 Interrupt Serial Port Interrupts When an interrupt is generated PC is pushed onto a stack => Vector address is loaded in the PC =>While the program executes the ISR, the corresponding interrupt flag is made clear. MCS-51 contains 5 Flag bits -IE0, IE1, TF0, TF1, TI(or RI). Now, the program starts executing the ISR from the vectored location. ISR ends with RETI Instruction. Interrupts in 8051 and Vector Addresses are listed in the following table Interrupt External Interrupt 0 INT0 Pin (12) Timer / Counter 0 Interrupt External Interrupt 1 INT1 Pin Timer / Counter 1 Interrupt Serial Port Flag Affected IE0 Vector 003H Cause of Interrupt (if enabled) A HIGH to LOW Transition on pin INT0 Overflow of Timer / Counter 0 A HIGH to LOW Transition on pin INT1 Overflow of Timer / Counter 1
When either TI of RI flag is set

Highest

TF0 IE1

000BH 0013H

TF1 RI + TI

001BH 0023H

P R I O R I T Y Lowest

Example: Consider the external interrupt 0 1. CPU is busy in the main program 2. 1 0 Transition occurs at INT0 Pin Current contents of PC are loaded onto the stack PC is loaded with the vector address 0003H Next instruction at 0003H would be fetched and executed A JMP instruction is written at vectored location 0003H since only 8bytes are available to write the ISR 7. The ISR lying somewhere in the program memory ends with RETI instruction. RETI gets the PC contents from the stack and CPU again starts executing from where the main program was interrupted. External interrupts can be configured as either Level Triggered (Signal must stay low until the interrupt is generated) or Edge Triggered (A transition from High to Low at the interrupt is sufficient). It is also necessary to initialize MCS-51 Interrupts by setting Interrupt Enable (IE) register in the SFR. 3. 4. 5. 6.

Initializing 8051 Interrupts:


IE register allows the programmer to enable the interrupts as needed IE is bit addressable IE.7 EA IE.6 -IE.5 -IE.4 ES IE.3 ET1 IE.2 EX1 IE.1 ET0 IE.0 EX0

EA (Enable All) ES ET1 EX1 ET0 EX0

0= Disable all interrupts 1= Allows each of the individual interrupts to be enabled Enable/Disable serial port interrupt (Provided EA=1) Enable/Disable timer interrupt 1 (Provided EA=1) Enable/Disable external interrupt 1 (Provided EA=1) Enable/Disable timer interrupt 0 (Provided EA=1) Enable/Disable external interrupt 0 (Provided EA=1)

EA acts as Master Control Bit for any of the interrupt (If EA=0 => Disables the whole interrupt operation ) For any particular interrupt to occur, EA and the corresponding bit must be set Example: Enable External Interrupt 1 MOV IE # 1000 0100 B (Bit EA=1 and Bit EX1=1) To initialize the serial interrupt, IE register must be loaded with 10010000 B

Interrupt Priorities:
User can program the interrupt priority levels by setting or clearing the bits in SFR called Interrupt Priority Register (IP) IP Register is also bit addressable If the bit is set, it will have high priority A high priority interrupt can interrupt the low priority interrupt but not the other way round If request of interrupts of two different priority level occur simultaneously then the interrupt having the high priority will be served If request of interrupts having same priority occur simultaneously, then within each priority level there is a polling structure IP.7 X IP.5 IP.4 IP.3 IP.2 IP.1 IP.0 IP.6 X IP.5 PT2 PT2 PS PT1 PX1 PT0 PX0 IP.4 PS IP.3 PT1 IP.2 PX1 IP.1 PT0 IP.0 PX0

Timer 2 priority in case of 8052 only Serial Interrupt priority Timer 1 interrupt External interrupt Timer 0 interrupt External interrupt 0

Example: Assignment of interrupt priority to timer 1 interrupt MOV IE # 1000 1100 H (Enables external interrupt 1 and timer interrupt 1) SETB PT1 (assigns a high priority to the timer interrupt) So, if both of them occur simultaneously, then the timer interrupt will be served EXAMPLE: MOV IE # 1000 1100 B (Enable EX1 and ET1) SETB PT1 SETB PX1 (Both the interrupts have high priority) In this case, the external interrupt will be served as per above table

Timer control (TCON) Special function register:


TCON is bit addressable. The address of TCON is 88H. It is partly related to Timer and partly to interrupt.

Fig TCON Register

The various bits of TCON are as follows. TF1 :Timer1 overflow flag. It is set when timer rolls from all 1s to 0s. It is cleared when processor vectors to execute ISR located at address 001BH. TR1 :Timer1 run control bit. Set to 1 to start the timer / counter. TF0 :Timer0 overflow flag. (Similar to TF1) TR0 :Timer0 run control bit. IE1 :Interrupt1 edge flag. Set by hardware when an external interrupt edge is detected. It is cleared when interrupt is processed. IE0 :Interrupt0 edge flag. (Similar to IE1) IT1 :Interrupt1 type control bit. Set/ cleared by software to specify falling edge / low level triggered external interrupt. IT0 :Interrupt0 type control bit. (Similar to IT1)

Timer/Counters:
1. On chip timing/counting feature is used in RT Applications like pulse counting, frequency measurement, pulse width measurement, baud rate generation, keeping amount of time between events etc. 2. 8051 has two 16-bit timer/counter DIFFERENCE BETWEEN a TIMER and a COUNTER TIMER: It counts machine cycles and provides a reference time delay or a clock. A machine cycle of 8051 consists of 12 OSC periods or the counting rate or the counting rate 1/12 of the OSC frequencyAt 12 MHz, clocking period will be equal to 1s COUNTER: 1. Counter of 8051 is incremented in response to a transition from 1 0 at its corresponding external pins (T0 or T1). Thus, the counter output will be a count or a number representing the occurrences of such 1 0s at external pins. 2. For counting function 8051 takes 2 machine cycles or 24 OSC periods to detect a 10 transitions at pin T0 or T1. When a counter or a timer overflows from FFFF H to 0000 H it sets a flag and generates an interrupt. 3. The 16 bits of timer are referred as higher byte THx and the lower byte TLx TH1 = Higher byte of timer 1TL1 = Lower byte of timer 1 TH0 = Higher byte of timer 0 TL0= Lower byte of timer 0

Timer / Counter Modes: There are four timer modes Timer or counter function and modes are selected by writing appropriate bits in SFR called TMOD (Timer Mode Register) Control of Timer / Counter operation is done through SFR called TCON (Timer Control Register). Timer Control Register (TCON) Bit Addressable TCON.7 TF1
Timer 1 Overflow Flag Set when timer/counter overflows

TCON.6 TR1
Timer 1 run control bit

TCON.5 TF0
Timer 0 overflow flag, set when timer / counter 0 overflows

TCON.4 TR0
Timer 0 run control bit

TCON.3 IE1
Interrupt 1

TCON.2 IT1
Timer Interrupt 1

TCON.1 IE0
Interrupt 0 flag

TCON.0 IT0
Timer 0 interrupt, IT0=0, low level trigger IT0 = 1, edge trigger on falling edge

Timer Mode Control Register TMOD.7 TMOD.6 TMOD.5 TMOD.4 TMOD.3 TMOD.2 TMOD.1 TMOD.0 Gate M1 M0 Gate M1 M0 C/ T C/ T Timer 1 Timer 0

GATE bit is used for choice of internal or external control: GATE=0 is for internal control, start and stop are controlled by software GATE=1 is for external control, start and stop are controlled by software and and external source. C/ T :0: Timer, 1: Counter Timer Mode is determined by M1 M0 Mode 0 Mode 1 Mode 2 Mode 3 M1 0 0 1 1 M0 0 1 0 1

Timer/ Counter control logic:

Timer Mode-0: In this mode, the timer is used as a 13-bit UP counter as follows.

Operation of Timer on Mode-0 The lower 5 bits of TLX and 8 bits of THX are used for the 13 bit count.Upper 3 bits of TLX are ignored. When the counter rolls over from all 0's to all 1's, TFX flag is set and an interrupt is generated. The input pulse is obtained from the previous stage. If TR1/0 bit is 1 and Gate bit is 0, the counter continues counting up. If TR1/0 bit is 1 and Gate bit is 1, then the operation of the counter is controlled by input. This mode is useful to measure the width of a given pulse fed to input. Timer Mode-1: This mode is similar to mode-0 except for the fact that the Timer operates in 16-bit mode.

Operation of Timer in Mode 1 Timer Mode-2: (Auto-Reload Mode) This is a 8 bit counter/timer operation. Counting is performed in TLX while THX stores a constant value. In this mode when the timer overflows i.e. TLX becomes FFH, it is fed with the value stored in THX. For example if we load THX with 50H then the timer in

mode 2 will count from 50H to FFH. After that 50H is again reloaded. This mode is useful in applications like fixed time sampling.

Operation of Timer in Mode 2 Timer Mode-3: Timer 1 in mode-3 simply holds its count. The effect is same as setting TR1=0. Timer0 in mode-3 establishes TL0 and TH0 as two separate counters.

Operation of Timer in Mode 3 Control bits TR1 and TF1 are used by Timer-0 (higher 8 bits) (TH0) in Mode-3 while TR0 and TF0 are available to Timer-0 lower 8 bits(TL0).

Serial Communication:
1. Used for digital communication 2. ADVANTAGE: Less number of wires required as compared to parallel communication 3. 8051 supports full duplex serial port (it can simultaneously transmit (TXD) and receive (RXD) a byte ) 4. 8051 serial communication is supported by RS232 Standard (RS= Recommended Standard) 5. Baud Rate need not always equal to number of bits per second because each byte is preceded by a start bit and followed by one stop bit to synchronize the serial receivers 6. Data byte is transmitted with LSB first 7. For error checking purpose Parity Bit can also be added just prior to the stop bit 8. Mechanism: I. Data byte in parallel is converted into serial data stream II. Serial data frame is sent over the line along with start, stop and parity bits 9. There are 4 modes of serial data transmission 10. SFR called SBUF is used for transmission and reception 11. The data to be transmitted must be transferred to SBUF

12. Another SFR called SCON controls the serial communication 13. Bits SM0 and SM1 in SCON define serial port mode 14. SM2 enables the multi processor communication in modes 2 and 3 15. Transmission is initiated by the execution of any instruction that uses SBUF as destination
SCON.7 SM0 SCON.6 SM1 SCON.5 SM2 SCON.4 REN SCON.3 TB8 SCON.2 RB8 SCON.1 TI SCON.0 RI

Bit Address
9F H 9E H 9D H 9C H 9B H 9A H 99 H 98 H

SCON Bit
SM0 SM1 SM2 REN TB8 RB8 TI RI

Description
Serial Communication Mode In modes 2 and 3, if set, this will enable microprocessor communication Enables serial reception 9th data bit that is transmitted in modes 2 and 3 9th data bit that is received in modes 2 and 3. It is not used in mode 0. In mode 1, if SM2=0, RB8 is the stop bit that is received Transmit interrupt flag, set by hardware, must be cleared by software Receive interrupt flag, set by hardware, must be cleared by software

SM0
0 0 1 1

SM1
0 1 0 1

Mode
0 1 2 3

Description
8-bit shift register mode 8-bit UART 9-bit UART 9-bit UART

Baud Rate
fosc/12 Variable (set by timer 1) fosc / 16 or fosc/32 Variable (set by timer 1)

Mode 0
Called Shift Register Mode Only RXD pin can be used to transmit or receive data. Serial port can be worked as half duplex. TXD pin outputs the shift clock only Eight data bits are transmitted or received Baud Rate is fixed and is determined by fosc(fosc/12)

Mode 1 10 Bits are transmitted through TXD pin or RXD pin.

Serial port can be worked as full duplex. Start Bit (0) + 8 Data Bits + Stop Bit (1)

On receiving, the stop bit goes into RB8 in SCON Baud Rate is variable and is determined by timer 1 overflow rate, so Timer 1 must be initialized. =

( ).

Mode 2 11 Bits are transmitted with a low start bit , then 8 data bits then 9th bit and a stop bit (1)

Serial port can be worked as Full duplex. 9th bit is programmable as TB8 in SCON It may be used to give parity of data byte On reception, this 9th data bit goes into RB8 in SCON

( ).

Mode 3 11 Bits are transmitted Same as mode 2 except that baud rate is defined by timer 1 overflow rate Baud rate calculations same as mode 1

Multiprocessor Communication Setting SM2 bit in SCON register enables to communicate one 8051 with other 8051 controllers This communication is supported in mode 2 and 3 only Serial port can be worked as Full duplex. 9 Bits are transmitted or received 9th bit goes to RB8 Transmitting Processor = Master and all other = slaves 9th bit = 1 when address byte is sent and 9th bit = 0 when data is sent

You might also like