02-microcontrollers

download 02-microcontrollers

of 37

Transcript of 02-microcontrollers

  • 7/28/2019 02-microcontrollers

    1/37

    CSE 466 -Winter 2008 Microcontrollers 1

    Computational hardware

    Digital logic (CSE370)Gates and flip-flops: glue logic, simple FSMs, registersTwo-level PLDs: FSMs, muxes, decoders

    Programmable logic devices (CSE370, CSE467)Field-programmable gate arrays: FSMs, basic data-pathsMapping algorithms to hardware

    Microprocessors (CSE378)General-purpose computer Instructions can implement complex control structures

    Supports computations/manipulations of data in memory

    CSE 466 -Winter 2008 Microcontrollers 2

    Microprocessors

    Arbitrary computations Arbitrary control structures Arbitrary data structuresSpecify function at high-level and use compilers and debuggers

    Microprocessors can lower hardware costsIf function requires too much logic when implemented with gates/FFs

    Operations are too complex, better broken down as instructionsLots of data manipulation (memory)

    If function does not require higher performance of customized logic

    Ever-increasing performance of processors puts more and more applicationsin this categoryMinimize the amount of external logic

  • 7/28/2019 02-microcontrollers

    2/37

    CSE 466 -Winter 2008 Microcontrollers 3

    Microprocessor basics

    Composed of three partsData-path: data manipulation and storageControl: determines sequence of actions executed in data-path

    and interactions to be had with environmentInterface: signals seen by the environment of the processor

    Instruction execution engine: fetch/execute cycleFlow of control determined by modifications to program counter Instruction classes:

    Data: move, arithmetic and logical operationsControl: branch, loop, subroutine callInterface: load, store from external memory

    CSE 466 -Winter 2008 Microcontrollers 4

    Microprocessor basics (contd)

    Can implement arbitrary state machine with auxiliarydata-path

    Control instructions implement state diagramRegisters and ALUs act as data storage and manipulationInteraction with the environment through memory interfaceHow are individual signal wires sensed and controlled?

  • 7/28/2019 02-microcontrollers

    3/37

    CSE 466 -Winter 2008 Microcontrollers 5

    control

    arithmetic

    logic

    unit

    m

    a

    r

    m

    d

    r

    register

    file

    source bus

    destination bus source bus

    Microprocessor organization

    Controller Inputs: from ALU (conditions), instruction read from memoryOutputs: select inputs for registers, ALU operations, read/write to memory

    Data-pathRegister file to hold data

    Arithmetic logic unit to manipulate dataProgram counter (to implement relative jumps and increments)

    InterfaceData to/from memory (address and data registers in data path)Read/write signals to memory (from control)

    CSE 466 -Winter 2008 Microcontrollers 6

    General-purpose processor

    Programmed by user New applications are developed routinelyGeneral-purpose

    Must handle a wide ranging variety of applications

    Interacts with environment through memory All devices communicate through memory dataDirect-memory access (DMA) operations

    between disk and I/O devicesDual-ported memory (e.g., display screen)Generally, oblivious to passage of time

  • 7/28/2019 02-microcontrollers

    4/37

    CSE 466 -Winter 2008 Microcontrollers 7

    Embedded processor

    Typically programmed onceby manufacturer of system

    Rarely does the user load new softwareExecutes a single program (or a limited suite) with fewparametersTask-specific

    Can be optimized for a specific applicationInteracts with environment in many ways

    Direct sensing and control of signal wiresCommunication protocols to environment and other devicesReal-time interactions and constraintsPower-saving modes of operation to conserve battery power

    CSE 466 -Winter 2008 Microcontrollers 8

    Why embedded processors?

    High overhead in building a general-purpose systemStoring/loading programsOperating system manages running of programs and access to dataShared system resources (e.g., system bus, large memory)Many parts

    Communication through shared memory/busEach I/O device often requires its own separate hardware unit

    Optimization opportunities As much hardware as necessary for application

    Cheaper, portable, lower-power systems As much software as necessary for application

    Doesnt require a complete OS, get a lot done with a smaller processor Can integrate processor, memory, and I/O devices on to a single chip

  • 7/28/2019 02-microcontrollers

    5/37

    CSE 466 -Winter 2008 Microcontrollers 9

    CPU+

    CacheMemory

    Display(with

    dual-portvideo RAM)

    Disk I/O

    (serial line,keyboard,

    mouse)

    Network Interface

    standard interfaces

    system bus

    all the parts around theprocessor are usually required

    Typical general-purpose architecture

    SoundInterface

    Narrow

    performance-orientedview

    Compilers

    Operatingsystems

    CSE 466 -Winter 2008 Microcontrollers 10

    Microcontroller(CPU+mem+)

    ROMSpecial

    I/ODeviceDriver

    RAMCustom

    Logic

    medium-speedinteractions

    high-speedinteractions

    low-speedinteractions

    standard interface

    any of the parts around themicrocontroller are optional

    Typical task-specific architecture

    GeneralPurpose

    I/O A/D-D/A

    Con-version

    Timers

    Feedback/control throughphysical environment

    diagnostictools

  • 7/28/2019 02-microcontrollers

    6/37

    CSE 466 -Winter 2008 Microcontrollers 11

    How does this change things?

    Sense and control of environmentProcessor must be able to readand write individual wiresControls I/O interfaces directly

    Measurement of timeMany applications require precise spacing of events in timeReal-time is not the same thing as fast as possibleReaction times to external stimuli may be constrained

    CommunicationProtocols must be implemented by processor

    Integrate I/O device or emulate in softwareCapability of using external device when necessary

    CSE 466 -Winter 2008 Microcontrollers 12

    Interactions with the environment

    Basic processor only has address and data busses to memoryInputs are read from memoryOutputs are written to memoryThus, for a processor to sense/control signal wires in theenvironment they must be made to appear as memory bits

    How do we make wires look like memory?How long does it take to do these things?

  • 7/28/2019 02-microcontrollers

    7/37

    CSE 466 -Winter 2008 Microcontrollers 13

    RDWR

    WAIT

    ADDR

    DATA

    Micro-processor

    OE

    IN

    OUT

    from environment

    to data bus

    read signaldecoder from address bus

    Sensing external signals

    Map external wire to a bit in the address spaceof the processor External register or latch buffers values comingfrom environment

    Map register into address spaceDecoder selects register for reading

    Output enable (OE) to get value on to data busLets many registers use the same data bus

    CSE 466 -Winter 2008 Microcontrollers 14

    RDWR

    WAIT

    ADDR

    DATA

    Micro-processor

    EN

    IN

    OUT

    from data bus

    write signaldecoderfrom address bus

    to environment

    Controlling external signals

    Map external wire to a bit in the address spaceof the processor Connect output of memory-mapped register to environment

    Map register into address spaceDecoder selects register for writing (holds value indefinitely)

    Input enable (EN) to take value from data busLets many registers use the same data bus

  • 7/28/2019 02-microcontrollers

    8/37

    CSE 466 -Winter 2008 Microcontrollers 15

    Time and instruction execution

    Keep track of detailed timing of each instruction's executionHighly dependent on codeHard to use compilersNot enough control over code generationInteractions with caches/instruction-buffers

    Loops to implement delaysKeep track of time in countersKeeps processor busy counting and not doing other useful things

    Timer Take differences between measurements at different points in code

    Keeps running even if processor is idle to save power An independent co-processor to main processor

    CSE 466 -Winter 2008 Microcontrollers 16

    Time measurement via parallel timers

    Separate and parallel counting unit(s)Co-processor to microprocessor Does not require microprocessor interventionMay be a simple counter or a more featured real-time clock

    Alarms can be set to generate interrupts

    More interesting timer unitsSelf reloading timers for regular interruptsPre-scaling for measuring larger timesStarted by external events

  • 7/28/2019 02-microcontrollers

    9/37

    CSE 466 -Winter 2008 Microcontrollers 17

    Input/output events

    Input captureRecord time when input event occuredCan be used in later handling of event

    Output compareSet output event to happen at a point in the futureReactive outputs

    e.g., set output to happen a pre-defined time after some inputProcessor can go on to do other things in the meantime

    CSE 466 -Winter 2008 Microcontrollers 18

    System bus based communication

    Extend address/data bus outside of chipUse specialized devices to implement communication protocolMap devices and their registers to memory locationsRead/write data to receive/send buffers in shared memory or devicePoll registers for status of communicationWait for interrupt from device on interesting events

    Send completedReceive occurred

  • 7/28/2019 02-microcontrollers

    10/37

    CSE 466 -Winter 2008 Microcontrollers 19

    Support for communication protocols

    Built-in device driversFor common communication protocols

    e.g., RS232, IrDA, USB, Bluetooth, etc.Serial-line protocols most common as they require fewer pins

    Serial-line controller Special registers in memory space for interactionMay use timer unit(s) to generate timing events

    For spacing of bits on signal wireFor sampling rate

    Increase level of integrationNo external devicesMay further eliminate need for shared memory or system bus

    CSE 466 -Winter 2008 Microcontrollers 20

    Microcontrollers

    Embedded processor with much more integrated onsame chip

    Processor core + co-processors + memoryROM for program memory, RAM for data memory, specialregisters to interface to outside worldParallel I/O ports to sense and control wiresTimer units to measure time in various waysCommunication subsystems to permit direct links to other devices

  • 7/28/2019 02-microcontrollers

    11/37

    CSE 466 -Winter 2008 Microcontrollers 21

    Microcontrollers (contd)

    Other features not usually found ingeneral-purpose CPUs

    Expanded interrupt handling capabilitiesMultiple interrupts with priority and selective enable/disable

    Automatic saving of context before handling interruptInterrupt vectoring to quickly jump to handlers

    More instructions for bit manipulationsSupport operations on bits (signal wires) rather than just words

    Integrated memory and support functions for cheaper system cost

    Built-in EEPROM, Flash, and/or RAMDRAM controller to handle refreshPage-mode support for faster block transfers

    CSE 466 -Winter 2008 Microcontrollers 22

    The AVR Microcontroller Family

    MemoryDensity

    Features

    ( 1KB - 2KB )

    ( 1KB- 8KB )

    ( 16KB - 128KB)

    F u l l y

    C o m p

    a t i b l e

  • 7/28/2019 02-microcontrollers

    12/37

    CSE 466 -Winter 2008 Microcontrollers 23

    The AVR Microcontroller Family

    S1200 S2323 S2343 S2313Pins 20 8 8 20Flash 1 KB 2 KB 2 KB 2 KBSRAM - 128 B 128 B 128 BEEPROM 64 B 128 B 128 B 128 BUART - - - 1PWM - - - 1

    t iny11 t iny12 t iny15 t iny28

    Pins 8 8 8 28/32Flash 1 KB 1 KB 1 KB 2 KBEEPROM - 64 B 64 B -PWMs - - 1 1

    ADC - - 4@10-bit -

    S4433 S8515 VC8534 S8535

    Pins 28/32 40/44 48 40/44Flash 4 KB 8 KB 8 KB 8 KBSRAM 128 B 512 B 256 B 512 B

    EEPROM 256 B 512 B 512 B 512 BUART 1 1 - 1PWM 1 2 - 2

    ADC 6@10-bit - 6@10-bit 8@10-bitRTC - - - Yes

    CSE 466 -Winter 2008 Microcontrollers 24

    The AVR Microcontroller Family mega161 mega163 mega32 mega103

    Pins 40/44 40/44 40/44 64Flash 16 KB 16 KB 32 KB 128 KBSRAM 1 KB 1 KB 2 KB 4 KBEEPROM 512 B 512 B 1 KB 2 KBU(S)ART 2 1 1 1TWI 1 1 1 -PWM 4 4 4 4

    ADC - 8@10-bit 8@10-bit 8@10-bitRTC Yes Yes Yes YesJTAG/OCD - - Yes -Self Program Yes Yes Yes -HW MULT Yes Yes Yes -Brown Out Yes Yes Yes -

    mega8 mega16 mega32 mega64 mega128Pins 28/32 40/44 40/44 64 64Flash 8 KB 16 KB 32 KB 64 KB 128 KBSRAM 1 KB 1 KB 2 KB 4 KB 4 KBEEPROM 512 B 512 B 1 KB 2 KB 4 KBU(S)ART 1 1 1 2 2TWI 1 1 1 1 1PWM 3 4 4 8 8

    ADC 8@10-bit 8@10-bit 8@10-bit 8@10-bit 8@10-bitRTC Yes Yes Yes Yes YesJTAG/OCD - Yes Yes Yes YesSelf Program Yes Yes Yes Yes YesHW MULT Yes Yes Yes Yes YesBrown Out Yes Yes Yes Yes Yes

  • 7/28/2019 02-microcontrollers

    13/37

    CSE 466 -Winter 2008 Microcontrollers 25

    Microcontroller we will be using

    Atmel AVR Microcontroller (ATmega16) 16 MIPS at 16 MHz8-bit microcontroller (8-bit data, 16-bit instructions) RISC Architecture

    131 instructions (mostly single-cycle on-chip 2-cycle multiplier)32 general-purpose registersInternal and external interrupts

    Memoryinstruction (16KB Flash memory read-while-write)boot ROM (512 Byte EEPROM)data (1K static RAM)

    Timers/counters2 8-bit and 1 16-bit timer/counters with compare modes and prescalersReal-time clock (32.768 kHz) with separate oscillator Programmable watchdog timer

    Serial communication interfacesJTAG boundary-scan interface for programming/debuggingProgrammable USART (universal synchronous/asynchronous receiver transmitter)Two-wire serial interface (can emulate different communication protocols)SPI serial port (serial peripheral interface)

    Peripheral featuresFour channels with support for pulse-width modulation

    Analog-digital converter (8-channel, 10-bit)Up to 32 general-purpose I/O pins (with interrupt support)

    Six power saving modes

    CSE 466 -Winter 2008 Microcontrollers 26

    Why did we pick the ATmega16

    Modern microcontroller Easy to use C compiler Better performance/power than competitors

    Microchip PICMotorola 68HC11Intel 80C51

    Excellent support for 16-bit arithmetic operations A lot of registers that eliminate moves to and from SRAMSingle cycle execution of most instructionsVery popular family in a variety of applications including manysensor network platforms

  • 7/28/2019 02-microcontrollers

    14/37

    CSE 466 -Winter 2008 Microcontrollers 27

    ATMega16 Overview

    CSE 466 -Winter 2008 Microcontrollers 28

    ATMega16 Pinouts

  • 7/28/2019 02-microcontrollers

    15/37

    CSE 466 -Winter 2008 Microcontrollers 29

    ATmega16 Internals

    CSE 466 -Winter 2008 Microcontrollers 30

    General-Purpose Working Registers

    32 registers6 are special

    Used for addressing modes

    Addressed in regular memory space

    Easier to useinstructions

    R0R1R2R3

    R26R27R28

    R29R30R31

    Register File

    X Pointer

    Y Pointer

    Z Pointer

    XLXHYL

    YHZLZH

  • 7/28/2019 02-microcontrollers

    16/37

    CSE 466 -Winter 2008 Microcontrollers 31

    Operations on Register/Immediate Values

    One cycle register/immediate instructions

    Register File

    ALU

    Register File

    Instruction Word

    ALU

    subi r16,ksub r0,r1

    CSE 466 -Winter 2008 Microcontrollers 32

    Instruction Timing

  • 7/28/2019 02-microcontrollers

    17/37

    CSE 466 -Winter 2008 Microcontrollers 33

    The Five Memory Areas

    General Purpose Register File = 32 BFlash Program Memory = 8 KB ( 8 MB)SRAM Data Memory = 1 KB ( 16 MB)I/O Memory = 64 B ( 64 B)EEPROM Data Memory = 512 B ( 16 MB)

    CSE 466 -Winter 2008 Microcontrollers 34

    Memory Map

  • 7/28/2019 02-microcontrollers

    18/37

    CSE 466 -Winter 2008 Microcontrollers 35

    Data SRAM > Register File (RF)

    LD Rd, Load indirectLD Rd,+ Load indirect with

    post-incrementLD Rd,- Load indirect with

    pre-decrementLDD Rd,+q Load indirect with

    displacement (0-63)*

    * PTR =X, Y or Z

    CSE 466 -Winter 2008 Microcontrollers 36

    Data SRAM RF

    LDI Load a register with animmediate value (1 Cycle) *

    LPM Transfer a byte from programMemory@Z to R0 (3 Cycles)

    LPM Rd,Z Transfer a byte from programMemory@Z to Rd (3 Cycles)

    LPM Rd,Z+ As above but with post-increment of the Z pointer

    * Works on R16 - R31

    CSE 466 -Winter 2008 Microcontrollers 38

    Register File

    > Register File

    OUT Transfer a byte from RF to I/OIN Transfer a byte from I/O to RF

    MOV Copy a register to another register MOVW Copy a register pair to another register pair.

    Aligned.

  • 7/28/2019 02-microcontrollers

    20/37

    CSE 466 -Winter 2008 Microcontrollers 39

    C-like Addressing Modes (1)

    Auto Increment/Decrement:C Source:unsigned char *var1, *var2;*var1++ = *--var2;Generated code:LD R16,-XST Z+,R16

    CSE 466 -Winter 2008 Microcontrollers 40

    C-Like Addressing Modes (2)

    Indirect with displacementEfficient for accessing arrays and structs

    x_minx_maxy_miny_max

    Z+2Z+4Z+6

    SRAM

    Z(my_square)

    Struct square

    {

    int x_min;

    int x_max;

    int y_min;

    int y_max;

    }my_square;

  • 7/28/2019 02-microcontrollers

    21/37

    CSE 466 -Winter 2008 Microcontrollers 41

    The Status Register - SREG

    I

    T

    H

    S

    V

    N

    Z

    C

    Interrupt Enable

    T Flag

    Half Carry

    Signed Flag

    Overflow Flag

    Negative Flag

    Zero Flag

    Carry Flag

    Enables Global Interrupts when Set

    Source and Destination for BL D and BST

    Set if an operation has half carry

    Set if Signed Overflow

    Set if a Result is Negative

    Set if a Result is Zero

    Set if an operation has Carry

    Used for Signed Tests

    7

    0

    TT Flag So urce an d Dest in at io n fo r B LD an d BST

    CSE 466 -Winter 2008 Microcontrollers 42

    Branch on SREG Settings

    I

    T

    H

    S

    V

    N

    Z

    C

    BRID

    BRTC

    BRHC

    BRGE

    BRVC

    BRPL

    BRNE

    BRSH, BRCC

    BRIE

    BRTS

    BRHS

    BRVS

    BRMI

    BREQ

    BRCS, BRLO

    BRLT

    7

    0

    Branches

    if Bi t Set

    Branches

    if B it Clear

  • 7/28/2019 02-microcontrollers

    22/37

    CSE 466 -Winter 2008 Microcontrollers 43

    A Small C Function

    /* Return the maximum value of a table of 16 integers */

    int max(int *array){

    char a;int maximum=SMALLEST_NUMBER; /* this is -32768 */

    for (a=0;amaximum)

    maximum=array[a];return (maximum);

    }

    CSE 466 -Winter 2008 Microcontrollers 44

    AVR Assembly output; 7. f or ( a=0; amaxi mum)

    MOV R30, R22CLR R31LSL R30ROL R31ADD R30, R16ADC R31, R17

    LDD R20, Z+0LDD R21, Z+1CP R18, R20CPC R19, R21BRGE ?0005

    ; 10. maxi mum=ar r ay[ a] ;MOV R18, R20MOV R19, R21

    ?0005:I NC R22RJ MP ?0001

    ?0000:; 11. }; 12. r et urn ( maxi mum) ;

    MOV R16, R18MOV R17, R19; 13. }

    RET

    Code Size: 46 Bytes, Execution time: 335 cycles

    R19/R18 stores maximumR22 stores a (index of loop)R17/R16 stores starting address of array ( parameter)R31/R30 (Z) stores pointer to element of arrayR17/R16 updated with return value

  • 7/28/2019 02-microcontrollers

    23/37

    CSE 466 -Winter 2008 Microcontrollers 45

    I/O Ports General Features

    Push-pull driversHigh current drive (sinks up to 40 mA)Pin-wise controlled pull-up resistorsPin-wise controlled data directionFully synchronized inputsThree control/status bits per bit/pin

    CSE 466 -Winter 2008 Microcontrollers 46

    I/O Ports

  • 7/28/2019 02-microcontrollers

    24/37

    CSE 466 -Winter 2008 Microcontrollers 47

    I/O Port Configurations

    3 Control/Status Bits per PinDDx Data Direction Control BitPORTx Output Data or Pull-Up Control BitPINx Pin Level Bit

    CSE 466 -Winter 2008 Microcontrollers 48

    Port is Input (Default Configuration)

    DDx

    PORTx

    PINx Physical Pin

    Pull-Up

    Direction: INPUTPull-Up: OFF

    ??

    0

    0

  • 7/28/2019 02-microcontrollers

    25/37

    CSE 466 -Winter 2008 Microcontrollers 49

    Port is Open-Collector Input (Switch On Pull-Up)

    DDx

    PORTx

    PINx Physical Pin

    Pull-Up

    Direction: INPUTPull-Up: ON

    ??

    0

    111

    CSE 466 -Winter 2008 Microcontrollers 50

    Port is Output

    DDx

    PORTx

    PINx Physical Pin

    Pull-Up

    Direction: OUTPUTPull-Up: OFF

    11

    1

    11

    11

    1

  • 7/28/2019 02-microcontrollers

    26/37

    CSE 466 -Winter 2008 Microcontrollers 51

    Sample Code from Lab 1

    .include "C:\Program Files\Atmel\AVR Tools\AvrAssembler\Appnotes\m16def.inc"

    .csegldi r16, 0xffout DDRB, r16ldi r16, 0x00out PORTB, r16loop:jmp loop

    CSE 466 -Winter 2008 Microcontrollers 52

    Alternate PortFunctionsGeneralizingI/O ports so thatthey can be usedby other I/O devices

  • 7/28/2019 02-microcontrollers

    27/37

    CSE 466 -Winter 2008 Microcontrollers 53

    I/O Port Registers

    CSE 466 -Winter 2008 Microcontrollers 54

    Full I/O Register Map (pg. 331)

  • 7/28/2019 02-microcontrollers

    28/37

    CSE 466 -Winter 2008 Microcontrollers 55

    Instruction Classes (pg. 333)

    Arithmetic/Logic InstructionsData Transfer InstructionsProgram Control InstructionsBit Set/Test Instructions

    CSE 466 -Winter 2008 Microcontrollers 56

    Arithmetic/Logical Instructions Add Instructions

    ADD Add Two RegistersADC Add Two Registers and CarryINC Increment a Register ADIW Add Immediate to Word *

    Subtract InstructionsSUB Subtract Two RegistersSBC Subtract with Carry Two RegistersSUBI Subtract Immediate from Register*SBCI Subtract with Carry Immediate from Register*DEC Decrement Register

    SBIW Subtract Immediate From Word**Compare Instructions

    CP Compare Two RegistersCPC Compare with Carry Two RegistersCPI Compare Register and Immediate*CPSE Compare Two Registers and Skip Next Instruction if Equal

  • 7/28/2019 02-microcontrollers

    29/37

    CSE 466 -Winter 2008 Microcontrollers 57

    16-bit and 32-bit support

    Carry instructions Addition, subtraction and comparisonRegister with register or immediateZero flag propagation

    SUB R16,R24 SUBI R16,1SBC R17,R25 SBCI R17,0

    All branches can be made based on last result

    Direct 16 bit instructions Addition and subtraction of small immediatesPointer arithmetics

    CSE 466 -Winter 2008 Microcontrollers 58

    Subtracting Two 16-Bit Values

    R1:R0 R3:R2 (e.g., $E104 $E101 )Without zero-flag propagation

    With zero-flag propagation

    R1 R0

    E1sub r0,r2 03

    Z

    0

    sbc r1,r3 0300

    E1 04 X

    1 Wrong!

    R1 R0

    E1sub r0,r2 03

    Z

    0

    sbc r1,r3 0300 0

    E1 04 X

    Correct!

  • 7/28/2019 02-microcontrollers

    30/37

    CSE 466 -Winter 2008 Microcontrollers 59

    Comparing Two 32-Bit Values

    Example: Compare R3:R2:R1:R0 and R7:R6:R5:R4

    cp r0,r4cpc r1,r5cpc r2,r6cpc r3,r7

    After last instruction, status register indicates equal,higher, lower, greater (signed), or less than (signed)

    CSE 466 -Winter 2008 Microcontrollers 60

    Arithmetic/Logical Instructions (contd)Multiply instructions

    MUL 8x8 > 16 (UxU)MULS 8x8 > 16 (SxS)*MULSU 8x8 > 16 (SxU)**

    Logical InstructionsAND Logical AND Two RegistersANDI Logical AND Immediate and Register *OR Logical OR Two RegistersORI Logical OR Immediate and Register *EOR Logical XOR Two Registers

    * Works on Registers R16 - R31** Works on Registers R16-R23

    The result is present in R1:R0. . All multiplication instructions are 2 cycles.

  • 7/28/2019 02-microcontrollers

    31/37

    CSE 466 -Winter 2008 Microcontrollers 61

    Arithmetic/Logical Instructions (contd)

    Shift / Rotate InstructionsLSL Logical Shift LeftLSR Logical Shift RightROL Rotate Left Through CarryROR Rotate Right Through CarryASR Arithmetic Shift Right

    LSRRegister Carry

    0MSB LSB

    ROR

    ASR

    CSE 466 -Winter 2008 Microcontrollers 62

    Data Transfer Instruction Types

    Data SRAM < > Register File (2)Program Memory > Register File (1/3)I/O Memory < > Register File (1)Register File < > Register File (1)

    # of Cycles

  • 7/28/2019 02-microcontrollers

    32/37

    CSE 466 -Winter 2008 Microcontrollers 63

    Data Transfer RF < > SRAM Stack

    PUSH PUSH a register on the stackDecrements stack pointer by 1Decremented by 2 when a return address is pushed on the stack

    POP POP a register from the stackIncrements stack pointer by 1Incremented by 2 when a return address is popped off on return

    Stack grows from higher to lower memory locationsMust start after $0060 (past I/O registers)Size of pointer varies depending on memory available

    CSE 466 -Winter 2008 Microcontrollers 64

    Flow Control

    Unconditional JumpsConditional BranchesSubroutine Call and Returns

  • 7/28/2019 02-microcontrollers

    33/37

    CSE 466 -Winter 2008 Microcontrollers 65

    Unconditional Jump Instructions

    RJMP Relative Jump *JMP Absolute Jump **

    * Reaches 2K instructions from current program location.Reaches all locations for devices up to 8KBytes (wrapping)

    ** 4-Byte Instruction

    CSE 466 -Winter 2008 Microcontrollers 66

    Conditional Branches (Flag Set)

    BREQ Branch if EqualBRSH Branch if Same or Higher BRGE Branch if Greater or Equal (Signed)BRHS Branch if Half Carry SetBRCS Branch if Carry SetBRMI Branch if MinusBRVS Branch if Overflow Flag SetBRTS Branch if T Flag SetBRIE Branch if Interrupt Enabled

  • 7/28/2019 02-microcontrollers

    34/37

    CSE 466 -Winter 2008 Microcontrollers 67

    Subroutine Call and Return

    RCALL Relative Subroutine Call *CALL Absolute Subroutine Call **

    RET Return from SubroutineRETI Return from Interrupt Routine

    * Reaches 2K instructions from current program location.Reaches all locations for devices up to 8KBytes (wrapping)

    ** 4-Byte Instruction

    CSE 466 -Winter 2008 Microcontrollers 68

    Bit Set/Clear and Bit Test Instructions

    SBR Set Bit(s) in Register *SBI Set Bit in I/O Register **SBRS Skip if Bit in Register SetSBIS Skip if Bit in I/O Register Set **CBR Clear Bit(s) in Register *CBI Clear Bit in I/O Register **SBRC Skip if Bit in Register Clear SBIC Skip if Bit in I/O Register Clear **

    * Works on Registers R16 - R31** Works on I/O Addresses $00 - $1F

  • 7/28/2019 02-microcontrollers

    35/37

    CSE 466 -Winter 2008 Microcontrollers 69

    Programming the ATmega16

    Traditional in-system programmingIn-system programmable FLASH, EEPROM, and lock bitsProgrammable at all frequenciesProgrammable at any value of Vcc above 2.7VOnly four pins + ground requiredRequires adapter device to control programming pins

    Self programmingThe AVR reprograms itself without any external componentsRe-programmable through any communication interface

    Does not have to be removed from board

    Uses existing communication portsCritical functions still operatingdevice is running during programming

    CSE 466 -Winter 2008 Microcontrollers 70

    AVR JTAG Interface

    Complies toIEEE std 1149.1 (JTAG)Boundary-scanfor efficient PCB test

    Standard for interconnection test All I/O pins controllable andobservable from tester

    On-chip debuggingin production

    AVR MCU

    Bypass

    Identification

    InstructionTAP

    Controller

    TMSTCK

    TDI TDO

  • 7/28/2019 02-microcontrollers

    36/37

    CSE 466 -Winter 2008 Microcontrollers 71

    JTAG In System Programming

    The JTAG interface can be used to program the Flashand EEPROMSave time and production cost

    No additional programming stageProgramming time independent of system clock

    AVR

    3rd Party

    JTAG equiment

    3rd PartySoftware

    -DOS-DLL

    -UNIX

    CSE 466 -Winter 2008 Microcontrollers 72

    JTAG In-Circuit Emulator

    Controlled by AVR StudioReal-Time emulation in actual silicon

    Debug the real device at the target boardTalks directly to the device throughthe 4-pin JTAG interface

    SupportsProgram and data breakpointsFull execution controlFull I/O-view and watches

  • 7/28/2019 02-microcontrollers

    37/37

    CSE 466 -Winter 2008 Microcontrollers 73

    AVR Studio

    Integrated development environment for AVRFront end for the AVR simulator and emulatorsC and assembly source level debuggingSupports third party compilersMaintains project informationFreely available from www.atmel.comThird-party compilers