ENGG Microcomputer Interfacing

Document Sample
ENGG Microcomputer Interfacing Powered By Docstoc
Microcomputer Interfacing

         Week #1
Introduction to Interfacing and
    Microcontrollers (M68HC12)
   Huang, Chapter 1 Sections
       1.2 Number System Issue
       1.3 Computer Hardware Organization
       1.4 Memory
       1.6 Program Execution
       1.7 Overview of the HCS12 Microcontroller
       1.8 The HCS12 CPU Registers
       1.9 HCS12 Address Modes
       1.11 A Sample of HCS12 Instructions

                         ENG3640 Fall 2012          2
   Interfacing: Definition
   Microcontrollers
   M68HC12 Architecture

                     ENG3640 Fall 2012   3
Interfaces and Interfacing
Definitions of “interface” from Webster’s Dictionary:
noun: the place at which independent systems meet
        and act or communicate with each other.

             e.g. (1) human - machine interface

                 (2) Digital - analogue interface

                  (3) Digital - digital interface

                               TTL - CMOS interface

                                Parallel - Serial interface

                             ENG3640 Fall 2012                4
System-Level Interfaces

 Human            Human-                           Digital-      Analogue
 Users           Machine                         Analogue       Environment
                 Interface                        Interface
                          Digital-Digital Interface

                         Other Digital Systems

Human-machine interface:
   Input devices: keyboard, mouse, microphone, camera
   Output devices: CRT, printer, light panel, audio amp.
Digital - Analogue Interface:
     Input devices: A/D converters, modems, sensors
     Output devices: D/A converters, modems, transducers,
          actuators, stepper motors
     Control devices: switches, multiplexers, amplifiers,
Digital - Digital Interface:
     Connectors: wires, ribbon cable, coax, twisted pair, PCB
     I/O devices: buffers, level-shifters, synchronizers
                             ENG3640 Fall 2012                           5
Interfaces and Interfacing

                   Informal Definition
The physical, electrical and logical means of exchanging
         Information with a functional module.

  The process of enabling a computer to communicate
  with the external world through Software, Hardware
                     and Protocols.

                      ENG3640 Fall 2012                    6
Why is computer interfacing important ?

 1.       The human-machine interface determines the ultimate
          success or failure of many computer based systems
          (Apple iPhone)
 2.       Digital systems exist within and must successfully
          interact with an analogue natural environment.
          Digital-Analogue interfaces are unavoidable
 3.       Rather than designing digital systems from elementary
          components, computer engineers more typically
          assemble new systems from existing sub-systems.

                                ENG3640 Fall 2012                 7
     Typical Interfacing Activities
1.        Selecting software/hardware subsystems that can (at least potentially)
          interact well with each other.
           Appropriate D/A and A/D converters (speed, accuracy, …)
           Serial vs. parallel communication.
2.        Determining appropriate hardware connections:
           Cabling, connectors, drivers, receivers, correct termination, etc.
3.        Resolving any hardware incompatibilities.
           CMOS with TTL
4.        Configuring hardware interfaces correctly using low-level software
           LCD, Keypads in embedded systems.
5.        Interfacing software components correctly;
           Selecting compatible software versions;
           Calling the correct procedures in the correct sequence with the correct

                                       ENG3640 Fall 2012                              8
           Hardware Interfaces within a
             Personal Computer (PC)
                    (greatly simplified)

                                                            Lab Board

                                                 Port       Keyboard
 CPU           Cache                           Controller

 Main          Memory                                        Mouse
Memory        Controller
 Hard           Disc                             Port        Printer
 Drive        Controller                       Controller

Diskette      Diskette                          Video
 Drive        Controller                       Controller

                                 System Bus
                           ENG3640 Fall 2012                            9
What is a Microcontroller?

   The Central Processing Unit (CPU)?
   The Microprocessor Unit?
   The Microcomputer System?
   The Microcontroller Unit (MCU)?

                    ENG3640 Fall 2012    10
Micro Computer System: 5 Basic Units
   The Arithmetic Logic Unit (ALU)
   Control Unit: directs the operation of all other parts.
   Memory: Store program & data
   Input: Allows data & info to be entered into memory
   Output: Transfers data from memory to outside world

                                    Common Bus (address, data, & control)

             Control Unit


              Logic Unit       Memory
                                                     Output                 Input
                            Program Data
                                                     Units                  Units
              Registers     Storage Storage

                            Figure 1.1 Computer Organization

                                           ENG3640 Fall 2012                        11
The Central Processing Unit (CPU)

   CPU executes program
   Program counter (PC) is
    a special register that
    points to the instructions
   Instruction decoder tells
    the ALU what to do with
    the data
   Control sequencer
    manages the transfer of
    instruction and data
    bytes along the internal
    data bus
                         ENG3640 Fall 2012   12
CPU vs. Microprocessor?

   A Central Processing Unit (CPU) refers to the
    integration of an ALU and Control.
   A Microprocessor refers to the implementation
    of the CPU functions in a single large scale
    integrated circuit (IC).

                     ENG3640 Fall 2012         13
The MicroComputer System

   MicroComputer has 3
    basic parts connected
    by an internal bus
       CPU
       Memory
       Registers
   I/O registers
       data, control, status
   I/O port is a collection
    of I/O pins on the chip
    that represents a unit
    of data                                         Block diagram of a typical
                                                    microcontroller shown in
                                                    single-chip mode
                                ENG3640 Fall 2012                                14
    Micro Computer vs. Micro Controller?

   A Micro Controller is
    a Micro Computer
    with its memory and
    I/O integrated into a
    single chip!

                            ENG3640 Fall 2012   15
The Microcontroller Unit (MCU)
Example of an Input Operation
                           Transfer    CPU

               Port’s                  MEMORY

                   ENG3640 Fall 2012            16
The Microcontroller Unit (MCU)
Example of an Output Operation

              Output Port

 Panel         Port’s

                  ENG3640 Fall 2012         17
The MCU: Applications

         Speed Signal    Set Counter to count
                         on ↑ edges
                             Counter                    reads
Speed                                             CPU   at every
Sensor                                                  time


                             Program timer’s
                             interrupt interval

                        ENG3640 Fall 2012                          18
The Von Neumann Computer
     Principle
       In 1945, the mathematician Von Neumann (VN)
       demonstrated in study of computation that a
       computer could have a simple structure,
       capable of executing any kind of program,
       given a properly programmed control unit,
       without the need of hardware modification

                                   ENIAC - The first electronic
                                   computer (1946)

                        ENG3640 Fall 2012                         19
The Von Neumann Computer
    Structure
        A memory for storing program and data.
             The memory consists of the word with the same length
        A control unit (control path) featuring a program counter for controlling
          program execution
        An arithmetic and logic unit (ALU) also called data path for program

                       Processor or
                   Central processing unit

                         Datapath                         Data
                         Registers                             Data
                     Instruction               Address
                                     PC                      Address

                                     ENG3640 Fall 2012                               20
The Von Neumann Computer

     Coding
       A program is coded as a set of instructions to be
       sequentially executed

     Program execution
         Instruction Fetch (IF): The next instruction to be executed is
          fetched from the memory
         Decode (D): Instruction is decoded (operation?)
         Read operand (R): Operands read from the memory
         Execute (EX): Operation is executed on the ALU
         Write result (W): Results written back to the memory
         Instruction execution in Cycle (IF, D, R, EX, W)

                             ENG3640 Fall 2012                             21
Embedded Systems
   Any device or collection of devices that
    contain one of more dedicated computers,
    microprocessors, or micro-controllers
   Combination of computer hardware and
    software designed to perform a specific
   Broad definition:
     Any computing system that is not a desktop

                    ENG3640 Fall 2012         22
Embedded Systems: Examples

            ENG3640 Fall 2012   23
Typical Microcontroller Embedded Application

   The amount of air and fuel
    and timing of the ignition
    system determine the fuel
    efficiency and amount of
    exhaust emission.
   Input sensors are used as
    feedback to correct any error.
   Microcontroller uses the
    sensor inputs to control the
    ignition system to MAX fuel
    efficiency and MIN exhaust

                                          Microcontroller used to control air/fuel
                                          mixture in an automotive engine
                              ENG3640 Fall 2012                                      24
    History of Micro Controllers
1974 Intel produced first microprocessor 4004

1977 Intel introduced several microcontrollers
8051, 8032

1978 Motorola introduced 6801, RAM , ROM,
EPROM 6805

1985 Motorola introduced the 6811 micro
controller (8-bit CPU, 8K ROM, 256Byte RAM,
512Byte EEPROM)

1997 Motorola introduced the 68HC12 which
is upward compatible with the 6811
                      ENG3640 Fall 2012           25
                 1-KB SRAM
              4-KB EEPROM
68HC812A4        CPU12

            ENG3640 Fall 2012   26
Features of the 68HC12 Microcontroller

 16-bit CPU
 64 kB memory space
 768 bytes to 4 kB of EEPROM
 1 kB to 12 kB of on-chip SRAM
 32 kB to 128 kB flash memory
 Sophisticated timer functions that include:
     input capture, output compare,
     pulse accumulators, real-time interrupt, and COP timer
 Serial communication interfaces: SCI, SPI, CAN, BDLC
 Background debug mode (BDM)
 8-bit or 10-bit A/D converter
 Instructions for supporting fuzzy logic function
                                  ENG3640 Fall 2012            27
On-Chip Memory
   1Kbyte of static RAM
       Re-mappable and removable
   4 Kbytes of EEPROM
       individually byte writable (~10ms)
       bulk erase possible

                          ENG3640 Fall 2012   28
Registers in the
CPU Model          ENG3640 Fall 2012   29
   Accumulators A and B are general-purpose
    8-bit accumulators that contain operands
    and results of arithmetic calculations or data
   Accumulator D is the concatenation of
    accumulators A and B.
       Some instructions treat the combination of these
        two 8-bit accumulators as a 16-bit double

                        ENG3640 Fall 2012              30
Index Registers
   Index registers X and Y are used for indexed
       Indexed addressing adds the value in an index
        register to a constant or to the value in an
        accumulator to form the effective address of the
   Index registers X and Y can also serve as
    temporary data storage locations with some
    combinational capability.

                        ENG3640 Fall 2012              31
Program Counter
   The program counter (PC) contains the
    address of the next instruction to be executed.

   The program counter can also serve as an
    index register in all indexed addressing modes
    except auto increment and auto decrement.

                     ENG3640 Fall 2012           32
Stack Pointer
   The stack pointer (SP) contains the last
    stack address used.
   The CPU12 supports an automatic
    program stack that is used to save system
    context during subroutine calls and
   The stack pointer can also serve as a
    temporary data storage location or as an
    index register for indexed addressing.

                   ENG3640 Fall 2012            33
Condition Code Register
   Z — Zero Flag
       The Z flag is set when the result of an operation is
        all 0s.
   V — Two’s Complement Overflow Flag
       The V flag is set when a two’s complement
        overflow occurs.
   C — Carry/Borrow Flag
       The C flag is set when an addition or subtraction
        operation produces a carry or borrow.

                            ENG3640 Fall 2012                  34
   H — Half-Carry Flag
       The H flag is used only for BCD arithmetic
        operations. It is set when an ABA, ADD, or
        ADC instruction produces a carry from bit 3 of
        accumulator A. The DAA instruction uses the
        H flag and the C flag to adjust the result to
        correct BCD format.
   I — Interrupt Mask Bit
       Setting the I bit disables maskable interrupt
   N — Negative Flag
       The N flag is set when the result of an
        operation is less than 0.
                         ENG3640 Fall 2012               35
   S — Stop Disable Bit
       Setting the S bit disables the STOP instruction.
   X — XIRQ Interrupt Mask Bit
       Setting the X bit masks interrupt requests from
        the XIRQ pin.

                           ENG3640 Fall 2012               36

ENG3640 Fall 2012            37
Register Block

   memory block reserved for internal
       from $00 to $F4 used; $F5 reserved up to $1FF
        (512 memory locations)
       can be moved to any 2K boundary from initial
        location of $0000 using INITRG ($11)
   used for controlling all CPU and
    peripheral functions including status,
    input and output

                        ENG3640 Fall 2012               38
Memory Map on EVB
 Address Range   Description              Location

 $0000 - $01FF   CPU Registers            On-chip (MCU)

 $0800 - $09FF   User Code/Data           1K on-chip RAM
 $1000 - $1FFF   User Code/Data           4K on-chip
                                          EEPROM (MCU)
 $4000 - $7FFF   User Code/Data           16K external RAM

 $8000 - $FFFF   Utilities                32K external

                      ENG3640 Fall 2012                      39
     Steps to Execute a Program

1)   Fetch op-code (instruction)
2)   Decode op-code
3)   Fetch operand address
4)   Execute instruction


 LDDA #$10
 STAA $4000

 LDAB $10
 STAA $4000

                                   ENG3640 Fall 2012   40
    Prefixes for Number Bases
   A number (operand) can be represented in binary,
    octal, decimal, or hexadecimal format.
   An appropriate prefix is added in front of the number
    to indicate its base.

       Base                 Prefix            Example
       binary               %                 %10001010
       octal                @                 @1234567
       decimal                                12345678
       hexadecimal          $                 $5678
                          ENG3640 Fall 2012                 41
           Addressing Modes
   A 68HC12 instruction consists of one or two bytes of
    opcode and zero to five bytes of operand addressing
   Addressing Modes: determines HOW the CPU
    access memory locations to be acted upon.
   Examples:
       Inherent Mode
       Immediate Mode
       Direct Mode
       Extended Mode
       Relative Mode
       Indexed Mode

                         ENG3640 Fall 2012                 42
    Inherent Mode

   Instructions that use this addressing mode either have
    no operands or all operands are in internal CPU
   In either case the CPU does not need to access any
    memory location to complete the instruction.
   Examples:
       NOP  No Operation
       DECA  Decrement Accumulator A ( A = A – 1)
       INX  Increment Index Register ( X = X + 1)

                             ENG3640 Fall 2012           43
Immediate Mode

   Operands for immediate mode instructions are
    included in the instruction stream.
   The CPU does not access memory when this type of
    instruction is executed.
   An immediate value is preceded by a # character in
    the assembly instruction.
   Examples:
       LDAA #$55    ; A  $55
                     ; place the value $55 in ACC A
       LDX #$2000    ; X  $2000
                     ; place the value $2000 in Index Reg X

                        ENG3640 Fall 2012                     44
        Direct Mode

   This addressing mode is used to access operands in
    the address space range of $0000 - $00FF.
   Since these addresses begin with $00, only the eight
    low-order bits of the address need to be included in
    the instruction which saves program space and
    execution time.
   Examples:
       LDAA $55   ; A  m[$55]
                   ; fetches the contents of the memory location
                   ; at $0055 and puts it in ACC A

                         ENG3640 Fall 2012                    45
    Instruction Set

   The instruction set of the 68HC12 contains different
    types of instructions:
       Load & Store Instructions
       Transfer & Exchange Instructions
       Move Instructions
       Add & Subtract Instructions
       Multiply & Divide Instructions

                              ENG3640 Fall 2012            46
Load & Store Instructions
     Table 1.4 Load and store instructions

     Mnemonic                Function                   Operation
     LDAA         Load A                           (M) ÞA
     LDAB         Load B                           (M) Þ B
     LDD          Load D                           (M:M+1) Þ (A:B)
     LDS          Load SP                          (M:M+1) Þ SP
     LDX          Load index register X            (M:M+1) Þ X
     LDY          Load index register Y            (M:M+1) Þ X
     LEAS         Load effective address into SP   Effective address Þ SP
     LEAX         Load effective address into X    Effective address Þ X
     LEAY         Load efective address into Y     Effective address Þ Y

                              Store Instructions
     Mnemonic                Function                   Operation

     STAA                    Store A               (A) Þ M
     STAB                    Store B               (B) Þ M
     STD                     Store D               (A) Þ M, (B) Þ M+1
     STS                     Store SP              (SP) Þ M, M+1
     STX                     Store X               (X) Þ M:M+1
     STY                     Store Y               (Y) Þ M:M+1

                               ENG3640 Fall 2012                            47
Transfer & Exchange Instructions

   Transfer instructions copy the contents of a register
    or accumulator into another register or accumulator.
   Examples:
       TAB       ; Transfer A to B (A)  B
       TBA       ; Transfer B to A (B)  A
       NOTE: Source contents not changed by the operation.
   Exchange instructions exchange the contents of
    pairs of registers or accumulators
   Example:
       EXG A,B     ; Exchanges contents of A and B

                             ENG3640 Fall 2012                48
Add & Subtract Instructions
      Table 1.7 Add and subtract instructions
                                     Add Instructions
      Mnemonic               Function             Operation
         ABA       Add B to A                     (A) + (B) Þ A
         ABX       Add B to X                     (B) + (X) Þ X
         ABY       Add B to Y                     (B) + (Y) Þ Y
        ADCA       Add with carry to A            (A) + (M) + C Þ A
        ADCB       Add with carry to B            (B) + (M) + C Þ B
        ADDA       Add without carry to A         (A) + (M) Þ A
        ADDB       Add without carry to B         (B) + (M) Þ B
        ADDD       Add without carry to D         (A:B) + (M:M+1) Þ A:B
                                  Subtract Instructions
      Mnemonic               Function             Operation

         SBA       Subtract B from A              (A) - (B) Þ A
        SBCA       Subtract with borrow from A    (A) - (M) - C Þ A
        SBCB       Subtract with borrow from B    (B) - (M) - C Þ B
        SUBA       Subtract memory from A         (A) - (M) Þ A
        SUBB       Subtract memory from B         (B) - (M) Þ B
        SUBD       Subtract memory from D         (D) - (M:M+1) Þ D

                                  ENG3640 Fall 2012                       49
                    Move Instructions

   These instructions move data bytes or words from a
    source (M1) to a destination (M2) in memory.
   Example:
       MOVB $1000, $2000 ; copies the contents of the
                                             ; memory loc $1000 to loc $2000

           Table 1.6 Move instructions
                                             Transfer Instructions
           Mnemonic              Function        Operation
            MOVB        Move byte (8-bit)       (M1) Þ M2
            MOVW        Move word (16-bit)      (M:M+11) Þ M:M+12

                                             ENG3640 Fall 2012                 50
Assembly Language Structure

   4 fields
       Label
       Operation
       Operand
       Comment

   start ldaa #$03 ;load a reg

                    ENG3640 Fall 2012   51
Assembly Language Structure

   Label
       Used to mark a certain part of the code
       Useful when doing branches / jumps
       Can use them almost like a GOTO
       You make them up, so can be any word you want
       Labels are optional, use where needed

   start ldaa #$03 ;load A reg

                           ENG3640 Fall 2012            52
Assembly Language Structure

   Operation
       Describes the operation you want to do
       Many operations available because CISC
       Ex. Add, subtract, or, load, store, branch

   start ldaa #$03 ;load A reg

                          ENG3640 Fall 2012          53
Assembly Language Structure

   Operand
       Describes what you want to do the operation to
       Manual will tell you what operands you need with what
       #$03 = immediate, $03 = direct
       $=hex, %=binary, nothing = decimal

   start ldaa #$03 ;load A reg

                             ENG3640 Fall 2012                  54
Assembly Language Structure

   Comment
       Start with a semi-colon for comment to end of line
       Very helpful in assembly code because it can be
        hard to understand

   start ldaa #$03 ;load A reg

                           ENG3640 Fall 2012                 55
Assembly Instructions - load

    Load a register with a value
    Syntax: (label) LDAA value
    LDAA #$30 = load register A with the value $30
    LDAA $30 = load register A with the value held in
     memory location $30

                       ENG3640 Fall 2012                 56
Assembly Instructions - SWI

   SWI stands for software interrupt
   Use it to end your programs and to get back
    out to the D-Bug12 prompt
   No operand

                     ENG3640 Fall 2012            57
Compiler Directives

   Compiler directives give instructions to the
    compiler (NOT INSTRUCTIONS!)
   Reserve space, set memory locations etc.
   Not actually executed like an instruction

                      ENG3640 Fall 2012            58
Compiler Directives - org

   Org is used to tell the compiler where to put the
    program in memory
   Unlike high level languages, where the program gets
    put anywhere it fits, in assembly you have to tell the
    compiler where to start putting the program
   You can have multiple orgs in a program, ex to
    separate space for data and for the program (try not
    to have too many though)

                         ENG3640 Fall 2012               59
Compiler Directives - EQU

   EQU = equate
   Like C #define
   Compiler will go through and substitute before
    the code is compiled
   Requires a label to use
   label EQU $30

                      ENG3640 Fall 2012          60

        org $800
val0    RMB      1
three   EQU      $03

        org       $4000
start   LDAA      #three
        STAA      val0

                ENG3640 Fall 2012   61
   Micro computer interfacing is an important topic since
    it is used extensively in computer architecture and
    embedded systems.
   Micro controllers are used in appliances, telephones,
    vehicles, TVs, e.t.c
   To learn the assembly language programming of
    micro controllers we have to know the internal
    architecture (CPU, registers, memory map, timers, I/O
    ports, ….).
   It is not sufficient to know the instruction set of a micro
    controller but also the addressing modes (i.e., how
    data is accessed)

                          ENG3640 Fall 2012                 62
ENG3640 Fall 2012   63
The Microcontroller Unit (MCU)
   The I/O control and data registers control and
    monitor the microcontroller I/O process
   Microcontrollers have internal timers
   The microcontroller uses other external pins
    for power and control and, if necessary, data
    and address lines
   RESET is a control line that is used to bring
    the microcontroller to an initial state
   CLOCK is also a control line

                     ENG3640 Fall 2012           64
The MCU: Expanded Mode

   Sometimes, a
    microcontroller requires
    more memory or I/O
    ports than are available
    in the chip mode
       need external data and
        address line connections
       some pins can be used
        either as I/O ports or as
        external data and
        address lines
                                                 Block diagram of a typical
       set microcontroller’s                    microcontroller shown in
        mode of operation                        expanded multiplexed mode
                             ENG3640 Fall 2012                                65
    Typical Mechanisms at Interfaces
1) Data buffering and flow control
      -- helps compensate for short-term mismatches in
          data generation and consumption rates
      -- groups data into larger, more efficient chunks
2) Synchronization, handshaking
      -- compensates for asynchronous clocks, clock skew
         affecting synchronous signals, devices with
         different response times
      -- bit stuffing to equalize bit rates (telecom technique)
3) Digital processing
      -- framing, packet assembly and disassembly
      -- data communication protocols
      -- encoding, decoding, code conversion
      -- error detection and error correction
      -- data compression (e.g. using Huffman codes)

4) Analogue signal conversion and conditioning
      -- restore correct voltage and current levels
      -- restore correct signal rise and fall times
      -- pulse-shaping and channel equalization
      -- take precautions to reduce noise and reflections
      -- carrier modulation and demodulation
                               ENG3640 Fall 2012                  66
Addressing Modes

             ENG3640 Fall 2012   67
The Microcontroller System

   Support components
    for the MCU
       Buffers and converters
       Bus – signals
         data: instructions and
         address: location
          where data is stored
         control: coordinate
          operation with
          associated chips
       Clock circuitry
       Power circuit

                              ENG3640 Fall 2012   68

Shared By: