MODULE 1 8051 microcontroller

download MODULE 1 8051 microcontroller

of 14

Transcript of MODULE 1 8051 microcontroller

  • 8/14/2019 MODULE 1 8051 microcontroller

    1/14

    MODULE 1

    EMBEDDED SYSTEM

    An embedded systemis acomputer system designed for specific control functions within a largersystem, often withreal-time computing constraints. It is embeddedas part of a complete device often

    including hardware and mechanical parts. By contrast, a general-purpose computer, such as a

    personal computer (PC), is designed to be flexible and to meet a wide range of end-user needs.

    Embedded systems control many devices in common use today.[3]

    Embedded systems contain processing cores that are eithermicrocontrollers ordigital signal

    processors (DSP).

    A processor is an important unit in the embedded system hardware. It is the heart of the embedded

    system.

    The key characteristic, however, is being dedicated to handle a particular task. Since the embedded

    system is dedicated to specific tasks, design engineers can optimize it to reduce the size and cost of

    the product and increase the reliability and performance. Some embedded systems are mass-produced, benefiting fromeconomies of scale.

    Physically, embedded systems range from portable devices such asdigital watches andMP3 players,

    to large stationary installations liketraffic lights,factory controllers.Complexity varies from low,

    with a singlemicrocontroller chip, to very high with multiple units,peripherals and networks

    mounted inside a largechassis or enclosure.

    In general, an Embedded System: It is a combination of hardware and software to performs a specific

    task.

    Is a system built to perform its duty, completely or partially independent of human

    intervention.

    Is specially designed to perform a few tasks in the most efficient way.

    Interacts with physical elements in our environment, viz. controlling and driving a motor,sensing temperature, etc.

    An embedded system can be defined as a control system or computer system designed to perform aspecific task. Common examples of embedded systems include MP3 players, navigation systems on

    aircraft and intruder alarm systems. An embedded system can also be defined as a single purpose

    computer.

    MicrocontrollersAs computer miniaturization progressed through the mid to late 1900s, controllers became smaller

    and smaller. All the parts of a controller, including memory and I/O devices, became integrated with

    the controller as a standard. Finally, when the entire controller apparatus was able to fit on single

    chips, they became known as "microcontrollers." A microcontroller contains everything required to

    control an external system, and nothing else. This limiting of microcontroller functionality to the

    basic requirements of a control unit makes implementing microcontrollers cheap and easy.

    Embedded Processors

    An "embedded" processor is simply a computing device placed inside a system it controls. A

    processor embedded into a system handles all the computation and logical operation of a computer.

    The embedded processor also handles such tasks as storing and retrieving data from memory, and

    processing data from any inputs or outputs. Embedded processors often work as part of a computer

    system, alongside memory and I/O devices.

    http://en.wikipedia.org/wiki/Computer_systemhttp://en.wikipedia.org/wiki/Real-time_computinghttp://en.wikipedia.org/wiki/Personal_computerhttp://en.wikipedia.org/wiki/Embedded_system#cite_note-3http://en.wikipedia.org/wiki/Embedded_system#cite_note-3http://en.wikipedia.org/wiki/Embedded_system#cite_note-3http://en.wikipedia.org/wiki/Microcontrollerhttp://en.wikipedia.org/wiki/Digital_signal_processorhttp://en.wikipedia.org/wiki/Digital_signal_processorhttp://en.wikipedia.org/wiki/Economies_of_scalehttp://en.wikipedia.org/wiki/Digital_watchhttp://en.wikipedia.org/wiki/Digital_audio_playerhttp://en.wikipedia.org/wiki/Traffic_lighthttp://en.wikipedia.org/wiki/Programmable_logic_controllerhttp://en.wikipedia.org/wiki/Microcontrollerhttp://en.wikipedia.org/wiki/Peripheralhttp://en.wikipedia.org/wiki/Chassishttp://en.wikipedia.org/wiki/Chassishttp://en.wikipedia.org/wiki/Peripheralhttp://en.wikipedia.org/wiki/Microcontrollerhttp://en.wikipedia.org/wiki/Programmable_logic_controllerhttp://en.wikipedia.org/wiki/Traffic_lighthttp://en.wikipedia.org/wiki/Digital_audio_playerhttp://en.wikipedia.org/wiki/Digital_watchhttp://en.wikipedia.org/wiki/Economies_of_scalehttp://en.wikipedia.org/wiki/Digital_signal_processorhttp://en.wikipedia.org/wiki/Digital_signal_processorhttp://en.wikipedia.org/wiki/Microcontrollerhttp://en.wikipedia.org/wiki/Embedded_system#cite_note-3http://en.wikipedia.org/wiki/Personal_computerhttp://en.wikipedia.org/wiki/Real-time_computinghttp://en.wikipedia.org/wiki/Computer_system
  • 8/14/2019 MODULE 1 8051 microcontroller

    2/14

    Differences

    The primary difference between microcontrollers and embedded processors is makeup and

    integration. Embedded processors, while in a sense "controlling" the system they are a part of,

    require external resources such as RAM and registers in order to do so. A processor is not a control

    "system." Microcontrollers, on the other hand, contain everything required to control a system in a

    single chip. A microcontroller might contain an embedded processor as part of its makeup, but alsocombines other computer parts, such as memory and signal registers, in a single chip.

    MICROCONTROLLERS AND EMBEDDED PROCESSORS

    Microcontroller versus general-purpose microprocessorBy microprocessor is meant the general-purpose microprocessors such as Intels x86 family (8086,

    80286, 80386, 80486, and the Pentium) or Motorolas 6800 family (68000, 68010, 68020, 68030,

    68040, etc.). These microprocessors contain no RAM, no ROM, and no I/O ports on the chip itself.For this reason, they are commonly referred to asgeneral-purpose microprocessors.

    Figure- Microprocessor System Contrasted With Microcontroller System

    A system designer using a general-purpose microprocessor such as the Pentium or the 68040 must

    add RAM, ROM, I/O ports, and timers externally to make them functional. Although the addition of

    external RAM, ROM, and I/O ports makes these systems bulkier and much more expensive, they

    have the advantage of versatility such that the designer can decide on the amount of RAM, ROM,

    and I/O ports needed to fit the task at hand. This is not the case with microcontrollers. A

    microcontroller has a CPU (a microprocessor) in addition to a fixed amount of RAM, ROM, I/Oports, and a timer all on a single chip. In other words, the processor, RAM, ROM, I/O ports, and

    timer are all embedded together on one chip; therefore, the designer cannot add any external

    memory, I/O, or timer to it. The fixed amount of on-chip ROM, RAM, and number of I/O ports in

    microcontrollers makes them ideal for many applications in which cost and space are critical.

    In many applications, for example a TV remote control, there is no need for the computing power of

    a 486 or even an 8086 microprocessor. In many applications, the space it takes, the power it

    consumes, and the price per unit are much more critical considerations than the computing power.

    These applications most often require some I/O operations to read signals and turn on and off certain

    bits. For this reason some call these processors IBP, itty-bitty processors

    Some microcontroller manufacturers have gone as far as integrating an ADC (analog-to-digi-tal

    converter) and other peripherals into the microcontroller.

  • 8/14/2019 MODULE 1 8051 microcontroller

    3/14

    Microcontrollers for embedded systemsMicroprocessors and microcontrollers are widely used in embedded system products. An embedded

    product uses a microprocessor (or microcontroller) to do one task and one task only. A printer is an

    example of embedded system since the processor inside it performs only one task ; namely, getting

    the data and printing it. Contrast this with a Pentium-based PC (or any x86 IBM-compatible PC). A

    PC can be used for any number of applications such as word processor, print server, bank tellerterminal, video game player, network server, or internet terminal. Software for a variety of

    applications can be loaded and run. Of course the reason a PC can perform myriad tasks is that it has

    RAM memory and an operating system that loads the application software into RAM and lets the

    CPU run it. In an embedded system, there is only one application software that is typically burned

    into ROM. An x86 PC contains or is connected to various embedded products such as the keyboard,

    printer, modem, disk controller, sound card, CD-ROM driver, mouse, and so on. Each one of these

    peripherals has a microcontroller inside it that performs only one task.

    Choosing a microcontrollerThere are four major 8-bit microcontrollers. They are: Freescales 6811, Intels 8051, ZilogsZ8, and

    PIC 16X from Microchip Technology. Each of these microcontrollers has a unique instruction setand register set; therefore, they are not compatible with each other. Programs written for one will not

    run on the others. There are also 16-bit and 32-bit microcontrollers made by various chip makers.

    Three criteria in choosing microcontrollers are as follows: (1) meeting the computing needs of the

    task at hand efficiently and cost effectively, (2) availability of software development tools such as

    compilers, assemblers, and debuggers, and (3) wide availability and reliable sources of the

    microcontroller.

    Criteria for choosing a microcontrollerThe first and foremost criterion in choosing a microcontroller is that it must meet the task at hand

    efficiently and cost effectively. In analyzing the needs of a microcontroller-based project, we must

    first see whether an 8-bit, 16-bit,or 32-bit microcontroller can best handle the computing needs of the

    task most effectively. Among other considerations in this category are:

    Speed. What is the highest speed that the microcontroller supports?

    Packaging. Does it come in a 40-pin DIP (dual inline package) or a QFP(quad flat package),

    or some other packaging format? This is important interms of space, assembling, and

    prototyping the end product.

    Power consumption. This is especially critical for battery-powered products.

    The amount of RAM and ROM on chip.

    The number of I/O pins and the timer on the chip.

    How easy it is to upgrade to higher-performance or lower power-consumption versions.

    Cost per unit. This is important in terms of the final cost of the product inwhich amicrocontroller is used.

    The second criterion in choosing a microcontroller is how easy it is to develop products around it.Key considerations include the availability of an assembler, debugger, a code-efficient C language

    compiler, emulator, technical support, and both in-house and outside expertise.

    The third criterion in choosing a microcontroller is its ready availability in needed quantities both

    now and in the future. For some designers this is even more important than the first two criteria.

    Currently, of the leading 8-bitmicrocontrollers, the 8051 family has the largest number of diversified

    (multiple source) suppliers. By supplier is meant a producer besides the originator of the

    microcontroller. In the case of the 8051, which was originated by Intel, several companies also

  • 8/14/2019 MODULE 1 8051 microcontroller

    4/14

  • 8/14/2019 MODULE 1 8051 microcontroller

    5/14

    von Neumann architecture:computers has a single, common memory space in which both program

    instructions and data are stored. There is a single internal data bus that fetches both instructions and

    data. They cannot be performed at the same time.

    Harvard architecture:computers have separate memory areas for program instructions and data.

    There are two or more internal data buses, which allow simultaneous access to both instructions anddata. The CPU fetches program instructions on the program memory bus.

    The 8051 microcontrollers (MCS-51) have an 8-bit data bus. They can address 64K of external data

    memory and 64K of external program memory. These may be separate blocks of memory, so that up

    to 128K of memory can be attached to the microcontroller. Separate blocks of code and data memory

    are referred to as the Harvard architecture. A single block of memory may be mapped to act as

    both data and program memory. This is referred to as the Von Neumann architecture.

  • 8/14/2019 MODULE 1 8051 microcontroller

    6/14

    Features of the standard 80514K bytes internal ROM (program)

    128 bytes internal RAM (data)

    Four 8-bit I/O ports

    Two 16-bit timers

    Serial interface64K external code memory space

    64K external data memory space

    210 bit-addressable locations

    The Intel 8051 is a very popular general purpose microcontroller widely used for small scale

    embedded systems. 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) Besides internal RAM, the 8051 has various Special Function Registers (SFR) such as theAccumulator, the B register, and many other control registers. 34 8-bit general purpose registers in

    total. The ALU performs one 8-bit operation at a time. 3 internal interrupts (one serial) and 2

    external interrupts. 4 8-bit I/O ports (3 of them are dual purposed). One of them used for serial port,

    Some 8051 chips come with UART for serial communication and ADC for analog to digital

    conversion.

    System Clock and Oscillator The 8051 requires an external oscillator circuit. The oscillator circuit usually runs around

    12MHz. the crystal generates 12M pulses in one second. The pulse is used to synchronize the

    system operation in a controlled pace..

    A machine cycle is minimum amount time a simplest machine instruction must take An 8051 machine cycle consists of 12 crystal pulses (clock cycle).

    instruction with a memory oprand so that it needs multiple memory accesses.

    The first 6 crystal pulses (clock cycle) is used to fetch the opcode and the second 6 pulses areused to perform the operation on the operands in the ALU. This gives an effective machine

    cycle rate at 1MIPS (Million Instructions Per Second).

  • 8/14/2019 MODULE 1 8051 microcontroller

    7/14

    Program counter and Data pointerThe program counter points to the address of the next instruction to be executed

    As the CPU fetches the opcode from the program ROM, the program counter is increasing to point

    to the next instruction

    The program counter is 16 bits wide. This means that it can access program addresses 0000 to

    FFFFH, a total of 64Kbytes of code The PC is automatically incremented after every instruction isfetched and may also be altered by certain instructions. It is the only register that does not have an

    internal address

    The DPTR register is made up of two 8-bit registers, named DPH and DPL that are used to furnish

    memory address for internal and external code access and external data access. The DPTR is under

    the control of program instructions and can be specified by its 16-bit name, DPTR, or by each

    individual byte name, DPH and DPL. DPTR does not have a single internal address; DPH and DPL

    are each assigned an address.

    RegisterS

    Register are used to store information temporarily, while the information could be a byte of data to

    be processed, or an address pointing to the data to be fetched. The vast majority of 8051 register are

    8-bit registers. There is only one data type, 8 bits. The 8 bits of a register are shown from MSB D7 to

    the LSB D0

    D7 D6 D5 D4 D3 D2 D1 D0

    With an 8-bit data type, any data larger than 8 bits must be broken into 8-bit chunks before it is

    processed

    The most widely used registers are A (Accumulator)- For all arithmetic and logic instructions, B,

    R0, R1, R2, R3, R4, R5, R6, R7, DPTR (data pointer), and PC (program counter)

  • 8/14/2019 MODULE 1 8051 microcontroller

    8/14

  • 8/14/2019 MODULE 1 8051 microcontroller

    9/14

    Pinout Description

    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 mostregisters. 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 toover 50 Hz.

    Pin 20:GND Ground.

    Pin 21-28:Port 2 If there is no intention to use external memory then these port pins are configured

    as general inputs/outputs. In case external memory is used, the higher address byte, i.e. addresses

    A8-A15 will appear on this port. Even though memory with capacity of 64Kb is not used, which

    means that not all eight port bits are used for its addressing, the rest of them are not available as

    inputs/outputs.

  • 8/14/2019 MODULE 1 8051 microcontroller

    10/14

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

    Input/Output Ports (I/O Ports)

    All 8051 microcontrollers have 4 I/O ports each comprising 8 bits which can be configured as inputs

    or outputs. Accordingly, in total of 32 input/output pins enabling the microcontroller to be connected

    to peripheral devices are available for use.

  • 8/14/2019 MODULE 1 8051 microcontroller

    11/14

    Pin configuration, i.e. whether it is to be configured as an input (1) or an output (0), depends on its

    logic state. In order to configure a microcontroller pin as an output, it is necessary to apply a logic

    zero (0) to appropriate I/O port bit. In this case, voltage level on appropriate pin will be 0.

    Similarly, in order to configure a microcontroller pin as an input, it is necessary to apply a logic one

    (1) to appropriate port. In this case, voltage level on appropriate pin will be 5V (as is the case with

    any TTL input).

    Port 0 - The P0 port is characterized by two functions. If external memory is used then the lower

    address byte (addresses A0-A7) is applied on it. Otherwise, all bits of this port are configured as

    inputs/outputs. The other function is expressed when it is configured as an output. Unlike other ports

    consisting of pins with built-in pull-up resistor connected by its end to 5 V power supply, pins of this

    port have this resistor left out.

    Port 1 - P1 is a true I/O port, because it doesn't have any alternative functions as is the case with P0,

    but can be configured as general I/O only. It has a pull-up resistor built-in and is completely

    compatible with TTL circuits.

    Port 2 - P2 acts similarly to P0 when external memory is used. Pins of this port occupy addresses

    intended for external memory chip. This time it is about the higher address byte with addresses A8-

    A15. When no memory is added, this port can be used as a general input/output port showing

    features similar to P1.

    Port 3 - All port pins can be used as general I/O, but they also have an alternative function. In order

    to use these alternative functions, a logic one (1) must be applied to appropriate bit of the P3 register.In terms of hardware, this port is similar to P0, with the difference that its pins have a pull-up resistor

    built-in.

    Memory Organization

    The 8051 has two types of memory and these are Program Memory and Data Memory. Program

    Memory (ROM) is used to permanently save the program being executed, while Data Memory

    (RAM) is used for temporarily storing data and intermediate results created and used during the

    operation of the microcontroller. All 8051 microcontrollers have a 16-bit addressing bus and are

    capable of addressing 64 kb memory.

    .

    Program Memory

    The first models of the 8051 microcontroller family did not have internal program memory. It was

    added as an external separate chip. These models are recognizable by their label beginning with 803

    (for example 8031 or 8032). All later models have a few Kbyte ROM embedded. Even though such

    an amount of memory is sufficient for writing most of the programs, there are situations when it is

    necessary to use additional memory as well

  • 8/14/2019 MODULE 1 8051 microcontroller

    12/14

  • 8/14/2019 MODULE 1 8051 microcontroller

    13/14

  • 8/14/2019 MODULE 1 8051 microcontroller

    14/14

    different baud rates. Without it, serial data send and receive would be an enormously complicated

    part of the program in which the pin state is constantly changed and checked at regular intervals.

    When using UART, all the programmer has to do is to simply select serial port mode and baud rate.

    When it's done, serial data transmit is nothing but writing to the SBUF register, while data receive

    represents reading the same register. The microcontroller takes care of not making any error during

    data transmission.

    8051 Microcontroller InterruptsThere are five interrupt sources for the 8051, which means that they can recognize 5 different events

    that can interrupt regular program execution. Each interrupt can be enabled or disabled by setting bits

    of the IE register. Likewise, the whole interrupt system can be disabled by clearing the EA bit of the

    same register..

    Now, it is necessary to explain a few details referring to external interrupts- INT0 and INT1. If the

    IT0 and IT1 bits of the TCON register are set, an interrupt will be generated on high to low

    transition, i.e. on the falling pulse edge (only in that moment). If these bits are cleared, an interrupt

    will be continuously executed as far as the pins are held low.

    Interrupt PrioritiesIt is not possible to forseen when an interrupt request will arrive. If several interrupts are enabled, it

    may happen that while one of them is in progress, another one is requested. In order that the

    microcontroller knows whether to continue operation or meet a new interrupt request, there is a

    priority list instructing it what to do.

    The priority list offers 3 levels of interrupt priority:

    1. Reset! The absolute master. When a reset request arrives, everything is stopped and the

    microcontroller restarts.

    2. Interrupt priority 1 can be disabled by Reset only.

    3.

    Interrupt priority 0 can be disabled by both Reset and interrupt priority 1.The IP Register (Interrupt Priority Register) specifies which one of existing interrupt sources have

    higher and which one has lower priority. Interrupt priority is usually specified at the beginning of the

    program. According to that, there are several possibilities:

    If an interrupt of higher priority arrives while an interrupt is in progress, it will be

    immediately stopped and the higher priority interrupt will be executed first.

    If two interrupt requests, at different priority levels, arrive at the same time then the higher

    priority interrupt is serviced first.

    If the both interrupt requests, at the same priority level, occur one after another, the one

    which came later has to wait until routine being in progress ends.

    If two interrupt requests of equal priority arrive at the same time then the interrupt to be

    serviced is selected according to the following priority list:1. External interrupt INT0

    2 Timer 0 interrupt

    3 External Interrupt INT1

    4 Timer 1 interrupt

    5 Serial Communication Interrupt