AVR Logical and Bitwise Instructions - PowerPoint PPT Presentation

1 / 57
About This Presentation
Title:

AVR Logical and Bitwise Instructions

Description:

andi Rd, k ori Rd, k. Rd = Rd AND ... andi R16, $0F ;test low nybble. breq allZero ;branch if low nybble all ... Use ANDI to be sure other bits in the byte are ... – PowerPoint PPT presentation

Number of Views:387
Avg rating:3.0/5.0
Slides: 58
Provided by: timma87
Category:

less

Transcript and Presenter's Notes

Title: AVR Logical and Bitwise Instructions


1
AVR Logical and Bitwise Instructions
  • Assembly Language Programming
  • University of Akron
  • Dr. Tim Margush

2
Boolean Logic
  • An algebraic system for Boolean data
  • Set false, true)
  • Operations and, or , not
  • The operations obey certain axioms such as
    associativity, commutativity, identity
  • Named after George Boole (1816-1864)
  • Implemented via digital logic circuits
    fundamental to the function of a computer

3
0 1
  • Boolean information is generally represented as 0
    and 1
  • Positive logic 0false, 1true
  • Each bit therefore represents a Boolean value

4
Not
  • The Boolean NOT is a unary operator (takes one
    operand)
  • The result is the opposite of the input
  • A Truth Table is a commonway to define
    Booleanoperations

5
AVR's NOT
  • The AVR processor's COM instruction applies the
    Boolean NOT to every bit in a register
  • com Rd
  • Also called the one's complement
  • Affects the status register flags

6
SREG
  • Status Register
  • Bits indicating status of processor
  • Located in I/O space

7
Carry and Zero Flags
  • Carry
  • This generally indicates a carry out of the most
    significant bit during the addition of two bytes
  • Zero
  • This indicates when a result is zero
  • Z0 when result IS NOT zero
  • Z1 when result IS zero

8
Negative and oVerflow Flags
  • Negative
  • This is a copy of bit 7 of the result
  • For signed data, it indicates the result is
    negative
  • Two's complement oVerflow
  • Set (to 1) when an arithmetic operation gives an
    out-of-range result assuming two's complement
    (signed) data

9
Half Carry and Sign Flags
  • Half Carry
  • This usually represents the carry out of the
    first nybble of an addition operation
  • Sign
  • Indicates the expected sign of a two's complement
    arithmetic operation
  • This is always NV (exclusive-or)

10
Global Interrupt and Bit Copy
  • Global Interrupt enable
  • This bit is 1 when interrupts are enabled
  • Interrupt signals are ignored while this bit is 0
  • Except the reset interrupt
  • BiT copy
  • Used as a temporary storage location for a single
    bit when using the AVR bit load and bit store
    instruction

11
And Or
  • The Boolean AND and OR are binary operators (take
    two operands)

12
AVR's And Or
  • The AVR processor's AND and OR instructions apply
    the Boolean operation to each column of a pair of
    registers
  • and Rd, Rr or Rd, Rr
  • These affect the status register flags

13
Exclusive Or
  • The exclusive or operator is commonly defined as
  • (a and (not b)) or ((not a) and b)
  • It is commonly representedusing the
    symbol,the ? symbol, or theabbreviation xor

14
AVR's Exclusive Or
  • The AVR processor's EOR instruction applies the
    Boolean operation to each column of a pair of
    registers
  • eor Rd, Rr
  • This affects the status register flags in the
    same way as the AND and OR instructions

15
Immediate Mode
  • Immediate mode versions of AND and OR are
    available for registers R16 through R31 only
  • andi Rd, k ori Rd, k
  • Rd Rd AND k Rd Rd OR k
  • The second operand is any expression which is
    assumed to be a byte value
  • The byte is coded as part of the instruction

16
Using AND
  • Common application
  • Clear one or more bits in a byte
  • Mask
  • A byte with 0's in positions to be cleared, 1's
    elsewhere
  • Example
  • andi R16, 0F clear high nybble
  • andi R16, ((1ltlt7) (1ltlt5)) clear bits 7 and 5

17
Using AND
  • Common application
  • Test if one or more bits are set in a byte
  • Mask
  • A byte with 0's in positions to be ignored, 1's
    in positions to be tested
  • Example
  • andi R16, 0F test low nybble
  • breq allZero branch if low nybble all zeros

18
Using OR
  • Common application
  • Set one or more bits in a byte
  • Mask
  • A byte with 1's in positions to be set, 0's
    elsewhere
  • Example
  • ori R16, 0b01100000 set bits 6 and 5
  • ori R16, (3ltlt5) same as above

19
Using OR
  • Common application
  • Combining bit fields into one byte
  • Preprocessing
  • Use ANDI to be sure other bits in the byte are
    cleared
  • Example
  • andi R16, 0F isolate low nybble
  • andi R17, F0 isolate high nybble
  • or R16, R17 combine low and high nybbles

20
Using EOR
  • Common application
  • Flip one or more bits in a byte
  • Mask
  • A byte with 1's in positions to be flipped, 0's
    elsewhere
  • Example
  • ldi R16, 1ltlt6 mask to flip bit 6
  • eor R20, R16 eor has no immediate mode

21
Application Letter Case
  • Convert characters to upper case
  • The only difference between ASCII codes for the
    upper and lower case characters is bit 5
  • Uppercase characters bit 5 0
  • Lowercase characters bit 5 1
  • OR can be used to convert to lower case
  • AND can be used to convert to upper case
  • EOR can be used to toggle case

22
Application ASCII - BCD
  • The ASCII codes for the digits have the digits'
    BCD value in the lower nybble.
  • All digits have upper nybble 0b0011
  • Use AND to convert ASCII to BCD
  • Use OR to convert BCD to ASCII

23
Packed BCD
  • If two BCD digits are to be packed into one byte
  • Swap one to the high nybble
  • The AVR instruction has a swap instruction
    exactly for this purpose
  • Use OR to combine them into one byte

24
Other BIT Manipulations
  • Set/Clear bits in register
  • sbr Rd, k cbr Rd, k
  • Only registers 16-31
  • k is a byte with 1's in positions to be set or
    cleared
  • set means make the selected bit 1, clear means
    make it 0
  • You can also do this with ORI or ANDI, in fact,
    these are aliases for
  • ori Rd, k andi Rd, k

25
Other BIT Manipulations
  • Set/Clear bit in I/O register
  • sbi A, b cbi A, b
  • Only I/O registers 0-31 are accessible with this
    instruction
  • The bit number must be 0 through 7
  • Note that these instructions affect only 1 bit in
    the register
  • No flags are affected by these instructions

26
TST
  • The TeST instruction affects status flags, but
    does not change any registers
  • tst Rd
  • This instruction is identical to (an alias for)
    AND Rd, Rd
  • Commonly used to see if a register is zero, or
    contains a negative/non-negative value.

27
CLR SER
  • These instructions clear or set all of the bits
    in a register
  • clr Rd (alias for eor Rd, Rd)
  • This is really the eor instruction which does
    affect status flags
  • ser Rd (alias for ldi Rd, FF)
  • Only available for registers 16-31
  • The load instructions do not affect flags

28
SWAP
  • This swaps the two nybbles in a register
  • swap Rd
  • Does not affect any flags
  • This instruction is commonly used in preparation
    for combining two nybble values into a single byte

29
Shift
  • Shift instructions move bits to adjacent
    positions in a register
  • All bits move at the same time and in the same
    direction, left or right
  • Left shift or right shift
  • What value is used to fill in the vacated spot on
    one end of the register?
  • What happens to the bit that falls out of the
    other end of the register?

30
LSR, LSL
  • These are the logical shifts
  • lsr Rd lsl Rd
  • The bit brought in is 0
  • The bit falling out is copied into the carry flag
  • Flags

31
ASR
  • This is an arithmetic shift
  • asr Rd
  • The sign bit is preserved (R7 Rd7)
  • The lsl instruction is used for an arithmetic
    left shift
  • Flags

32
ROL ROR
  • The rotate instructions use the carry flag to
    determine the bit value in the vacated position
  • rol Rd (R0 C) ror Rd (R7 C)
  • Flags

33
Arithmetic Application
  • lsl Rd calculates Rd 2
  • Overflow may occur
  • Check C for unsigned data
  • Check V for signed data
  • lsr Rd calculates Rd / 2 (unsigned)
  • asr Rd calculates Rd / 2 (signed)
  • Rounding is to the left on the number line
  • -5 shifted right gives -3 (not -2)

34
Application Bit Visiting
  • Examining one bit at a time can be accomplished
    in a loop
  • Each bit falls into the carry flag where it can
    be tested
  • Using rotate, the original data can be preserved
  • .def loopcounter R16
  • .def databyte R17
  • .def bitscounted R18
  • clr bitscounted
  • ldi loopcounter, 8
  • nextbit
  • ror databyte
  • brcc iszero
  • inc bitscounted
  • iszero
  • dec loopcounter
  • brne nextbit
  • ror databyte

Next bit to carry flag
Restore original state
35
Application Bit Reversal
  • Bits from one byte can be rotated into another
    byte an any order
  • In this example, the bits are reversed by
    rotating them into a second byte in the opposite
    order
  • .def loopcounter R16
  • .def databyte R17
  • .def reversedbyte R18
  • ldi loopcounter, 8
  • nextbit
  • lsr databyte
  • rol reversedbyte
  • dec loopcounter
  • brne nextbit
  • databyte now 00
  • reversedbyte has reversed
  • copy of original
  • databyte

rightmost bit to carry flag then to other byte
36
Additional Bit Manipulations
  • Because the AVR processor is designed for
    embedded applications with limited memory,
    bit-based data and operations are quite common
  • As a result, many specialized bit operations are
    present
  • We have already looked at sbr, cbr, sbi, cbi, and
    swap

37
BST BLD
  • The bit store and bit load instructions allow
    transfers between a single bit of any register
    and the T flag in SREG
  • bst Rd, b T Rd(b)
  • Note that Rd is not the destination register, but
    it does appear as the first operand of this
    instruction
  • bld Rd, b Rd(b) T

38
BCLR BSET
  • These instructions clear or set a bit in the
    status register (by its position)
  • bclr s SREG(s) 0
  • bset s SREG(s) 1
  • s is a position 0 .. 7
  • You will never need these instructions - All have
    a special alias so you do not need to lookup the
    bit number of each flag

39
Status Flag Manipulation
  • Individual instructions exist to set or clear
    status flags by name instead of position
  • These are aliases for the corresponding bclr or
    bset instruction

40
Timer/Counters
  • The ATMega16 has three timer/counter devices
    on-chip
  • Each timer/counter has a count register
  • A clock signal can increment or decrement the
    counter
  • Interrupts can be triggered by counter events

41
8-Bit Timer/Counter
External Clock Signal
42
Timer Events
  • Overflow
  • In normal operation, overflow occurs when the
    count value passes FF and becomes 00
  • Compare Match
  • Occurs when the count value equals the contents
    of the output compare register

43
Output Compare Unit
External Output
44
Status via Polling
  • Timer status can be determined through polling
  • Read the Timer Interrupt Flag Register and check
    for set bits
  • The overflow and compare match events set the
    corresponding bits in TIFR
  • TOVn and OCFn (n0, 1, or 2)
  • Timer 1 has two output compare registers 1A and
    1B
  • Clear the bits by writing a 1

45
Automatic Timer Actions
  • The timers (1 and 2 only) can be configured to
    automatically clear, set, or toggle related
    output bits when a compare match occurs
  • This requires no processing time and no interrupt
    handler it is a hardware feature
  • The related OCnx pin must be set as an output
    normal port functionality is suspended for these
    bits
  • OC0 (PB3) OC2 (PD7)
  • OC1A (PD5) OC1B (PD4)

46
Timer Clock Sources
  • The timer/counters can use the system clock, or
    an external clock signal
  • The system clock can be divided (prescaled) to
    signal the timers less frequently
  • Prescaling by 8, 64, 256, 1024 is provided
  • Timer2 has more choices allowing prescaling of an
    external clock signal as well as the internal
    clock

47
ATMega16 Prescaler Unit
External Clock Signals
48
Clock Selection
  • TCCR0 and TCCR1B Timer/Counter Control Register
    (counters 0 and 1)
  • CSn2, CSn1, CSn0 (Bits 20) are the clock select
    bits (n 0 or 1)
  • 000 Clock disabled timer is stopped
  • 001 I/O clock
  • 010 /8 prescale
  • 011 /64 prescale
  • 100 /256 prescale
  • 101 /1024 prescale
  • 110 External clock on pin Tn, falling edge
    trigger
  • 111 External clock on pin Tn, rising edge
    trigger
  • TCCR2 Timer/Counter Control Register (counter
    2)
  • CS22, CS21, CS20 (Bits 20) are the clock select
    bits
  • 000 Clock disabled timer is stopped
  • 001 T2 clock source
  • 010 /8 prescale
  • 011 /32 prescale
  • 100 /64 prescale
  • 101 /128 prescale
  • 110 /256 prescale
  • 111 /1024 prescale
  • ASSR (Asynchronous Status Register), bit AS2 sets
    the clock source to the internal clock (0) or
    external pin TOSC1)

49
Timer Modes
  • Normal Mode
  • Counter counts up,
  • TOV occurs when it reaches 0
  • Clear Timer on Compare Mode (CTC)
  • Counter counts up to match the Output Compare
    Register
  • On the next count, it resets to 0 and the OC Flag
    is set

50
Timer Modes
  • Fast PWM (Pulse Width Modulation) Mode
  • Counter counts from BOTTOM to MAX and resets
  • Inverting mode OC is cleared at BOTTOM and set
    at match
  • Non-Inverting Mode OC is set at BOTTOM and
    cleared at match

51
Timer Modes
  • Phase Correct PWM Mode
  • Counter counts from BOTTOM to MAX , then reverses
  • OC is changed when the count matches the OCR
    value
  • Inverting mode OC is cleared while downcounting,
    set while upcounting
  • Non-Inverting Mode OC is cleared while
    upcounting, set while downcounting

52
Timer Control (Timer0)
  • WGM010 Waveform Generation Mode
  • 00 Normal
  • 01 PWM
  • 10 CTC
  • 11 Fast PWM
  • Clock Select
  • covered previously
  • Compare Match Output Mode
  • 00 Nothing
  • 01 Toggle
  • 10 Clear
  • 11 Set
  • Behavior is slightly different in each WG mode

53
Timer Count Value
  • The timer's current value may be read or written
    at any time
  • in R16, TCNT0 out TCNT0, R17
  • The output compare function is disabled for one
    cycle after a write
  • Modification while the timer is running may also
    cause a missed compare

54
Output Compare Register
  • OCR0 holds a byte that is constantly compared to
    the TCNT0 value
  • Matches can cause several actions
  • Output compare interrupt
  • Or just setting the output compare flag
  • Generate a waveform on OC0
  • Cause a timer reset (TOP)

55
Flags
  • TIFR - Timer/Counter Interrupt Flags
  • Output Compare Flag
  • Timer Overflow Flag
  • Input Capture Flag

Timer 0 flags
56
Timer 0 Switch Debounce
  • Use Timer/Counter 0 to measure a 10 millisecond
    interval each time the switch data changes
  • Debouncing is accomplished by ignoring switch
    data during this interval
  • We must store the last known switch data between
    calls.
  • Function will return switch data once for each
    press/release, at the instant of the release

57
  • byte switchread()
  • static byte state FF
  • byte currentswitches
  • if (timer running) return FF ignore everything
  • currentswitches switches read switch data
  • if (state ! currentswitches) if different from
    previous, start blackout period
  • start timer
  • if (currentswitches!FF) this is not a release
    event, so just store state
  • state currentswitches
  • currentswitchesFF return no switch data
  • else this is a release event
  • currentswitches state return stored state
  • state FF record release
  • return currentswitches
  • else
  • return FF same, return no switch data
Write a Comment
User Comments (0)
About PowerShow.com