Top Banner
MICROCONTROLLERS AND APPLICATIONS 1 Module 1 MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA Module I Introduction to Microcontrollers: Comparison with Microprocessors – Harvard and Von Neumann Architectures - 80C51 microcontroller – features - internal block schematic – pin descriptions, I/O ports. INTRODUCTION TO MICROCONTROLLERS The microcontroller incorporates all the features that are found in microprocessor. The microcontroller has built in ROM, RAM, Input Output ports, Serial Port, timers, interrupts and clock circuit. A microcontroller is an entire computer manufactured on a single chip. Microcontrollers are usually dedicated devices embedded within an application. For example, microcontrollers are used as engine controllers in automobiles and as exposure and focus controllers in cameras. In order to serve these applications, they have a high concentration of on-chip facilities such as serial ports, parallel input output ports, timers, counters, interrupt control, analog-to- digital converters, random access memory, read only memory, etc. The I/O, memory, and on- chip peripherals of a microcontroller are selected depending on the specifics of the target application. Since microcontrollers are powerful digital processors, the degree of control and programmability they provide significantly enhances the effectiveness of the application. The 8051 is the first microcontroller of the MCS-51 family introduced by Intel Corporation at the end of the 1970s. The 8051 family with its many enhanced members enjoys the largest market share, estimated to be about 40%, among the various microcontroller architectures. Microcontrollers • Microcontroller (MC) may be called computer on chip since it has basic features of microprocessor with internal ROM, RAM, Parallel and serial ports within single chip. Or we can say microprocessor with memory and ports 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. COMPARISON WITH MICROPROCESSORS It is very clear from figure that in microprocessor we have to interface additional circuitry for providing the function of memory and ports, for example we have to interface external RAM for data storage, ROM for program storage, programmable peripheral interface (PPI) 8255 for the Input Output ports, 8253 for timers, USART for serial port. While in the microcontroller
22

INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

Mar 15, 2018

Download

Documents

dobao
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 1 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

Module I

Introduction to Microcontrollers: Comparison with Microprocessors – Harvard and Von Neumann Architectures - 80C51 microcontroller – features - internal block schematic – pin descriptions, I/O ports.

INTRODUCTION TO MICROCONTROLLERS

The microcontroller incorporates all the features that are found in microprocessor. The microcontroller has built in ROM, RAM, Input Output ports, Serial Port, timers, interrupts and clock circuit. A microcontroller is an entire computer manufactured on a single chip. Microcontrollers are usually dedicated devices embedded within an application. For example, microcontrollers are used as engine controllers in automobiles and as exposure and focus controllers in cameras.

In order to serve these applications, they have a high concentration of on-chip facilities such as serial ports, parallel input output ports, timers, counters, interrupt control, analog-to-digital converters, random access memory, read only memory, etc. The I/O, memory, and on-chip peripherals of a microcontroller are selected depending on the specifics of the target application. Since microcontrollers are powerful digital processors, the degree of control and programmability they provide significantly enhances the effectiveness of the application. The 8051 is the first microcontroller of the MCS-51 family introduced by Intel Corporation at the end of the 1970s. The 8051 family with its many enhanced members enjoys the largest market share, estimated to be about 40%, among the various microcontroller architectures. Microcontrollers

• Microcontroller (MC) may be called computer on chip since it has basic features of microprocessor with internal ROM, RAM, Parallel and serial ports within single chip. Or we can say microprocessor with memory and ports 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.

COMPARISON WITH MICROPROCESSORS

It is very clear from figure that in microprocessor we have to interface additional circuitry for providing the function of memory and ports, for example we have to interface external RAM for data storage, ROM for program storage, programmable peripheral interface (PPI) 8255 for the Input Output ports, 8253 for timers, USART for serial port. While in the microcontroller

Page 2: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 2 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

RAM, ROM, I/O ports, timers and serial communication ports are in built. Because of this it is called as “system on chip”. So in micro-controller there is no necessity of additional circuitry which is interfaced in the microprocessor because memory and input output ports are inbuilt in the microcontroller.

Microcontroller gives the satisfactory performance for small applications. But for large applications the memory requirement is limited because only 64 KB memory is available for program storage. So for large applications we prefer microprocessor than microcontroller due to its high processing speed.

Page 3: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 3 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

Microprocessor

Microcontroller

CPU is stand-alone, RAM,ROM,

I/O, timer are separate CPU, RAM, ROM, I/O and timer

are all on a single chip.

Designer can decide on the amount of ROM, RAM and I/O ports.

fixed amount of on-chip ROM,RAM, I/O ports

versatility

for applications in which

cost,power and space are critical.

general-purpose

single-purpose

Harvard and Von Neumann Architectures

There are two computer architectures, which are different in the way of accessing memories: von Neumann Architecture and Harvard Architecture.

In von Neumann architecture, programs and data are stored in the same memory and managed by the same information-handling subsystem. In the Harvard architecture program and data are stored and handled by different subsystems. Harvard architecture uses physically separate memories for their instructions and data, requiring dedicated buses for each of them. Instructions and operands can therefore be fetched simultaneously.

Eg:8051 & PIC Microcontrollers

Today, the vast majority of computers are von Neumann architecture because of the efficiency of design, implementation, and operation of one memory system instead of two. Under certain conditions, a Harvard computer can be much faster than a von Neumann computer because data and program do not contend for the same information pathway, and storing the program in an immutable read-only memory can result in vast reliability improvements.

Page 4: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 4 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

Features of von Neumann Architecture :- 1. Instructions and Data are stored in the same memory. 2. Instructions and Data share one memory system.

Features of Harvard Architecture :-

1. Physically separates storage and signal pathway for instructions and data. 2. Generally, the bit of Instructions is wider than Data. 3. For some computers, the Instruction memory is read-only. 4. In cases without caches, the Harvard Architecture is more efficient than von-Neumann.

Bus structures of the two are also different: Harvard architecture has separate data and instruction busses, allowing transfers simultaneously on both busses. Von Neumann architecture has only one bus which is used for both data transfers and instruction fetches, and therefore data transfers and instruction fetches must be scheduled – they cannot be performed at the same time.

Page 5: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 5 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

80C51 microcontroller

The Intel 8051 is a Harvard architecture, single chip microcontroller (µC) which was developed by Intel in 1980 for use in embedded systems. This is mostly used microcontroller in the robotics, home appliances like mp3 player, washing machines, electronic iron and industries. Intel's official designation for the 8051 family of µCs is MCS 51.Many vendors such as Atmel, Philips, and Texas Instruments produce MCS-51 family microcontroller chips.

The 8051 is an 8-bit microcontroller with 8 bit data bus and 16-bit address bus. The 16 bit address bus can address a 64K ( 216) byte code memory space and a separate 64K byte of data memory space. The 8051 has 4K on-chip read only code memory and 128 bytes of internal Random Access Memory (RAM).

Intel's original 8051 family was developed using NMOS technology, but later versions, identified by a letter "C" in their name, e.g. 80C51, used CMOS technology and were less power-hungry than their NMOS predecessors - this made them eminently more suitable for battery-powered devices.

Features of 8051 microcontroller are given as follows.

8-bit CPU 16-bit Program Counter 8-bit Processor Status Word (PSW) 8-bit Stack Pointer Internal RAM of 128bytes Special Function Registers (SFRs) of 128 bytes 32 I/O pins arranged as four 8-bit ports (P0 - P3) Two 16-bit timer/counters : T0 and T1 Two external and three internal vectored interrupts One full duplex serial I/O

Page 6: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 6 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

Internal Schematic of 80C51 microcontroller

Mostly used blocks in the architecture of 8051 are as follows:

Fig: 8051 architecture

128 Byte RAM for Data Storage

MC 8051 has 128 byte Random Access memory for data storage. Random access memory is non volatile memory. During execution for storing the data the RAM is used. RAM consists of the register banks, stack for temporary data storage. It also consists of some special function register (SFR) which are used for some specific purpose like timer, input output ports etc. Normally microcontroller has 256 byte RAM in which 128 byte is used for user space which is normally Register banks and stack. But other 128 byte RAM which consists of SFRs.

Now what is the meaning of 128 byte RAM. What are address range which is provided for data storage. We will discuss here.

We know that 128 byte = 27 byte

Page 7: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 7 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

Since 27 bytes so last 7 bits can be changed so total locations are from 00H to 7F H. This procedure of calculating the memory address is called as “memory mapping”. We can save data on memory locations from 00H to 7FH. Means total 128 byte space from 00H to 7FH is provided for data storage.

4KB 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 changed during the processing like the program or algorithm for specific applications.

• This is volatile memory; the data saved in this memory does not disappear after power failure. • We can interface up to 64KB ROM memory externally if the application is large. These sizes are specified different by their companies. • Address Range of PC: Address range of PC means program counter (which points the next instruction to be executing) can be moved between these locations 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 RAM which is discussed in previous section.

4KB = 22. 210 B (since 1KB = 210 B = 212 Byte

Address range of PC is 0000H to 0FFFH means total 4KB locations are available from 0000H to 0FFFH. At which we can save the program. Timers and Counters

Timer means which can give the delay of particular time between some events. For example on or off the lights after every 2 sec. This delay can be provided through some assembly

Page 8: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 8 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

program but in microcontroller two hardware pins are available for delay generation. These hardware pins can be also used for counting some external events. How much times a number is repeated in the given table is calculated by the counter.

• In MC8051, two timer pins are available T0 and T1, by these timers we can give the delay of particular time if we use these in timer mode. • We can count external pulses at these pins if we use these pins in counter mode. • 16 bits timers are available. Means we can generate delay between 0000H to FFFFH. • Two special function registers are available.

If we want to load T0 with 16 bit data then we can load separate lower 8 bit in TL0 and higher 8 bit in TH0.

In the same way for T1. TMOD, TCON registers are used for controlling timer operation.

Serial Port

• There are two pins available for serial communication TXD and RXD. • Normally TXD is used for transmitting serial data which is in SBUF register, RXD is used for receiving the serial data. • SCON register is used for controlling the operation. Input Output Ports

• There are four input output ports available P0, P1, P2, P3.

• Each port is 8 bit wide and has special function register P0, P1, P2, P3 which are bit addressable means each bit can be set or reset by the Bit instructions (SETB for high, CLR for low) independently.

• The data at any port which is transmitting or receiving is in these registers.

• The port 0 can perform dual works. It is also used as Lower order address bus (A0 to A7) multiplexed with 8 bit data bus P0.0 to P0.7 is AD0 to AD7 respectively the address bus and data bus is demultiplexed by the ALE signal and latch which is further discussed in details.

Page 9: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 9 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

• Port 2 can be used as I/O port as well as higher order address bus A8 to A15.

• Port 3 also have dual functions it can be worked as I/O as well as each pin of P3 has specific function.

P3.0 – RXD – Serial I / P for Asynchronous communication/Serial O / P for synchronous communication. P3.1 – TXD – Serial data transmit. P3.2 – INT0 – External Interrupt 0. P3.3 – INT1 – External Interrupt 1. P3.4 – T0 – Clock input for counter 0. P3.5 – T1 – Clock input for counter 1. P3.6 – WR – Signal for writing to external memory. P3.7 – RD – Signal for reading from external memory. When external memory is interfaced with 8051 then P0 and P2 can’t be worked as I/O port they works as address bus and data bus, otherwise they can be accessed as I/O ports.

Oscillator

• It is used for providing the clock to MC8051 which decides the speed or baud rate of MC.

• We use crystal which frequency vary from 4MHz to 30 MHz, normally we use 11.0592 MHz frequency.

Interrupts

• Interrupts are defined as requests because they can be refused (masked) if they are not used, that is when an interrupt is acknowledged. A special set of events or routines are followed to handle the interrupts. These special routines are known as interrupt handler or interrupt service routines (ISR). These are located at a special location in memory.

• INT0 and INT1 are the pins for external interrupts.

Page 10: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 10 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

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 bit register.

B-Register

This register is used in only two instructions MUL AB and DIV AB. This is also bit addressable and 8 bit register.

Page 11: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 11 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

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 from 0000H to FFFFH. A total of 64KB of code. • Initially PC has 0000H. • ORG instruction is used to initialize the PC ORG 0000H means PC initialize by 0000H. • PC is incremented after each instruction.

ROM Memory Map in 8051

→ 4KB, 8KB, 16KB, 32KB, 64KB on chip ROM is available. → Max ROM space is 64 KB because 16 bit address line is available in 8051. → Starting address for ROM is 0000H (because PC which points the ROM is 16 bit wide).

8051 Flag Bits and PSW Register

→ Used to indicate the Arithmetic condition of ACC. → Flag register in 8051 is called as program status word (PSW). This special function register PSW is also bit addressable and 8 bit wide means each bit can be set or reset independently.

There are four flags in 8051 • P → Parity flag → PSW 0.0

1 – odd number of 1 in ACC 0 – even number of 1 in ACC

• OV(PSW 0.2) → overflow flag → this is used to detect error in signed arithmetic operation. This is similar to carry flag but difference is only that carry flag is used for unsigned operation.

Page 12: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 12 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

Eg: for selecting Bank 1, we use following commands SETB PSW0.3 (means RS0=1) CLR PSW0.4 (means RS1=0).

Initially by default always Bank 0 is selected.

• F0 → user definable bit

• AC → Auxiliary carry flag → when carry is generated from D3 to D4, it is set to 1, it is used in BCD arithmetic.

• CY → carry flag → Affected after 8 bit addition and subtraction. It is used to detect error in unsigned arithmetic opr. We can also use it as single bit storage. SETB C → for cy = 1 CLR C → for cy = 0

Structure of RAM or 8051 Register Bank and Stack

→ 128 byte RAM is available in 8051

→ 128 byte = 27B .Therefore address range of RAM is 00H to 7FH.

→ In MC8051, 128 byte visible or user accessible RAM is available which is shown in figure. Extra 128B RAM which is not user accessible. 80H to FFH used for storage of SFR (special function register).

Four Register Banks

→ There are four register banks, in each register bank there are eight 8 bit register available from R0 to R7 → By default Bank 0 is selected. → Locations 20H to 2FH is bit addressable RAM means each bit from 00H to FFH in this we can set or reset CF rather than changing whole byte. → Locations 30H to 7FH is used as scratch pad means we can use this space for data reading and writing or for data storage.

Page 13: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 13 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

Stack in 8051

→ RAM locations from 08H to 1FH can be used as stack. Stack is used to store the data temporarily. Stack is last in first out (LIFO)

→ Stack pointer (SP) → 8bit register to indicate current RAM address available for stack or it points the top of stack. • Initially by default at 07H because first location of stack is 08H. • After each PUSH instruction the SP is incremented by one while in MP after PUSH instruction SP is decremented. • After each POP instruction the SP is decremented.

DPTR → Data Pointer in 8051

→ 16 bit register, it is divided into two parts DPH and DPL. → DPH for Higher order 8 bits, DPL for lower order 8 bits. → DPTR, DPH, DPL these all are SFRs in 8051.

Special Function Register

→ RAM scratch pad, there is extra 128 byte RAM which is used to store the SFRs. → Following figure shows special function bit address, all access to the four I/O ports CPU register, interrupt control register, timer/counter, UART, power control are performed through registers between 80H and FFH.

Page 14: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 14 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

Pin Descriptions of 80C51

Description of each pin is discussed here:

• VCC → 5V supply

• VSS → GND

• XTAL2/XTALI are for oscillator input

• Port 0 – 32 to 39 – AD0/AD7 and P0.0 to P0.7

• Port 1 – 1 to 8 – P1.0 to P1.7

• Port 2 – 21 to 28 – P2.0 to P2.7 and A 8 to A15

• Port 3 – 10 to 17 – P3.0 to P3.7

• P 3.0 – RXD – Serial data input – SBUF

• P 3.1 – TXD – Serial data output – SBUF

• P 3.2 – INT0 – External interrupt 0 – TCON 0.1

• P 3.3 – INT1 – External interrupt 1 – TCON 0.3

• P 3.4 – T0 – External timer 0 input – TMOD

Page 15: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 15 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

• P 3.5 – T1 – External timer 1 input – TMOD

• P 3.6 –WR – External memory write cycle – Active LOW

• P 3.7 – RD – External memory read cycle – Active LOW

• RST – for Restarting 8051

• ALE – Address latch enable

1 – Address on AD 0 to AD 7

0 – Data on AD 0 to AD 7

• PSEN – Program store enable

Page 16: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 16 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

Pins 1-8: Port 1 Each of these pins can be configured as an input or an output.

Pin 9: RS 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.

Pins10-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 communication

output.

Pin 11: TXD Serial asynchronous communication output or Serial synchronous communication

clock output.

Pin 12: INT0 Interrupt 0 input.

Pin 13: INT1 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: X2, X1 Internal oscillator input and output. A quartz crystal which specifies

operating frequency is usually connected to these pins. Instead of it, miniature ceramics

resonators can also be used for frequency stability. Later versions of microcontrollers operate at

a frequency of 0 Hz up to over 50 Hz.

Pin 20: GND Ground.

Page 17: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 17 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

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 receiving signal from the ALE pin, the

external register (usually 74HCT373 or 74HCT375 add-on chip) memorizes the state of P0 and

uses it as a memory chip address. Immediately after that, the ALU pin is returned its previous

logic state and P0 is now used as a Data Bus. As seen, port data multiplexing is performed by

means of only one additional (and cheap) integrated circuit. In other words, this port is used for

both data and address transmission.

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.

Page 18: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 18 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

I/O Ports

There are four ports P0, P1, P2 and P3 each use 8 pins, making them 8-bit ports. All the ports upon RESET are configured as output, ready to be used as output ports. To use any of these ports as an input port, it must be programmed.

Port 0: Port 0 occupies a total of 8 pins (pins 32-39) .It can be used for input or output. To use the pins of port 0 as both input and output ports, each pin must be connected externally to a 10K ohm pull-up resistor. This is due to the fact that P0 is an open drain, unlike P1, P2, and P3.Open drain is a term used for MOS chips in the same way that open collector is used for TTL chips. With external pull-up resistors connected upon reset, port 0 is configured as an output port. For example, the following code will continuously send out to port 0 the alternating values 55H and AAH.

MOV A,#55H BACK: MOV P0,A ACALL DELAY CPL A SJMP BACK

Port 0 as Input : With resistors connected to port 0, in order to make it an input, the port must be programmed by writing 1 to all the bits. In the following code, port 0 is configured first as an input port by writing 1′s to it, and then data is received from the port and sent to P1.

MOV A,#0FFH ; A = FF hex MOV P0,A ; make P0 an input port BACK: MOV A,P0 ;get data from P0 MOV P1,A ;send it to port 1 SJMP BACK

Page 19: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 19 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

Dual role of port 0: Port 0 is also designated as AD0-AD7, allowing it to be used for both address and data. When connecting an 8051/31 to an external memory, port 0 provides both address and data. The 8051 multiplexes address and data through port 0 to save pins. ALE indicates if P0 has address or data. When ALE = 0, it provides data D0-D7, but when ALE =1 it has address and data with the help of a 74LS373 latch.

Port 1: Port 1 occupies a total of 8 pins (pins 1 through 8). It can be used as input or output. In contrast to port 0, this port does not need any pull-up resistors since it already has pull-up resistors internally. Upon reset, Port 1 is configured as an output port. For example, the following code will continuously send out to port1 the alternating values 55h & AAh.

MOV A,#55H ; A = 55 hex BACK: MOV P1,A ;send it to Port 1 ACALL DELAY ;call delay routine CPL A ;make A=0 SJMP BACK

Port 1 as input: To make port1 an input port, it must programmed as such by writing 1 to all its bits. In the following code port1 is configured first as an input port by writing 1’s to it, then data is received from the port and saved in R7 ,R6 & R5.

MOV A,#0FFH ;A=FF HEX MOV P1,A ;make P1 an input port by writing all 1’s to it MOV A,P1 ;get data from P1 MOV R7,A ;save it in register R7 ACALL DELAY ;wait MOV A,P1 ;get another data from P1 MOV R6,A ;save it in register R6 ACALL DELAY ;wait MOV A,P1 ;get another data from P1 MOV R5,A ;save it in register R5

Port 2 : Port 2 occupies a total of 8 pins (pins 21- 28). It can be used as input or output. Just like P1, P2 does not need any pull-up resistors since it already has pull-up resistors internally. Upon reset,Port 2 is configured as an output port. For example, the following code will send out continuously to port 2 the alternating values 55h and AAH. That is all the bits of port 2 toggle continuously.

MOV A,#55H ; A = 55 hex BACK: MOV P2,A ;send it to Port 2 ACALL DELAY ;call delay routine CPL A ;make A=0 SJMP BACK

Page 20: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 20 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

Port 2 as input : To make port 2 an input, it must programmed as such by writing 1 to all its bits. In the following code, port 2 is configured first as an input port by writing 1’s to it. Then data is received from that port and is sent to P1 continuously.

MOV A,#0FFH ;A=FF hex MOV P2,A ;make P2 an input port by writing all 1’s to it BACK: MOV A,P2 ;get data from P2 MOV P1,A ;send it to Port1 SJMP BACK ;keep doing that

Dual role of port 2 : In systems based on the 8751, 8951, and DS5000, P2 is used as simple I/O. However, in 8031-based systems, port 2 must be used along with P0 to provide the 16-bit address for the external memory. As shown in pin configuration 8051, port 2 is also designed as A8-A15, indicating the dual function. Since an 8031 is capable of accessing 64K bytes of external memory, it needs a path for the 16 bits of the address. While P0 provides the lower 8 bits via A0-A7, it is the job of P2 to provide bits A8-A15 of the address. In other words, when 8031 is connected to external memory, P2 is used for the upper 8 bits of the 16 bit address, and it cannot be used for I/O.

Port 3 : Port 3 occupies a total of 8 pins, pins 10 through 17. It can be used as input or output. P3 does not need any pull-up resistors, the same as P1 and P2 did not. Although port 3 is configured as an output port upon reset. Port 3 has the additional function of providing some extremely important signals such as interrupts. This information applies both 8051 and 8031 chips.

Page 21: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 21 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

Functions of port 3

P3.0 and P3.1 are used for the RxD and TxD serial communications signals. Bits P3.2 and P3.3 are set aside for external interrupts. Bits P3.4 and P3.5 are used for timers 0 and 1. Finally P3.6 and P3.7 are used to provide the WR and RD signals of external memories connected in 8031 based systems.

Read-modify-write feature : The ports in the 8051 can be accessed by the read-modify-write technique. This feature saves many lines of code by combining in a single instruction all three action of (1) reading the port, (2) modifying it, and (3) writing to the port. The following code first places 01010101 (binary) into port 1. Next, the instruction “XLR P1,#0FFH” performs an XOR logic operation on P1 with 1111 1111 (binary), and then writes the result back into P1.

MOV P1,#55H ;P1=01010101 AGAIN: XLR P1,#0FFH ;EX-OR P1 with 1111 1111 ACALL DELAY SJMP AGAIN

Single bit addressability of ports: There are times that we need to access only 1 or 2 bits of the port instead of the entire 8 bits. A powerful feature of 8051 I/O ports is their capability to access individual bits of the port without altering the rest of the bits in that port.

For example, the following code toggles the bit p1.2 continuously.

BACK: CPL P1.2 ;complement p1.2 only ACALL DELAY SJMP BACK

Notice that P1.2 is the third bit of P1, since the first bit is P1.0, the second bit is P1.1, and so on. Notices in example of those unused portions of port1 are undisturbed. Table bellow shows the bits of 8051 I/O ports. This single bit addressability of I/O ports is one of the features of the 8051 microcontroller.

Page 22: INTRODUCTION TO MICROCONTROLLERS · PDF filemicrocontrollers and applications 3 module 1 muhammed riyas a.m, asst.professor, ece

MICROCONTROLLERS AND APPLICATIONS 22 Module 1

MUHAMMED RIYAS A.M, ASST.PROFESSOR, ECE DEPARTMENT, MCET PATHANAMTHITTA

REVIEW QUESTIONS

1. What is Microcontroller? 2. Differentiate between microprocessor and microcontroller. 3. List the features of 8051 microcontroller. 4. What are the advantages and disadvantages of using Harvard architecture in 8051? 5. Draw and explain the block diagram of 8051. 6. Explain the functional pin diagram of 8051 Microcontroller. 7. Name the five interrupt sources of 8051?. 8. What is the use of flags in 8051 microcontroller? 9. What is meant by interrupt? 10. Write a short note on Oscillator clock in 8051 microcontroller. 11. What are the I/O ports used in 8051 controller? 12. List and explain all SFRs

ESSAY QUESTIONS

1. Describe the architecture of 8051 with neat diagram. 2. Explain the I/O port structure of 8051. 3. Explain the functional pin diagram of 8051 Microcontroller.