AVR Assembly Language - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

AVR Assembly Language

Description:

The STK-500 provides 8 LEDs that can be easily connected to one of the I/O ports ... out PORTB, R16 ;3 LEDs will be on. 9/30/09. Dr. Tim Margush - Assembly ... – PowerPoint PPT presentation

Number of Views:346
Avg rating:3.0/5.0
Slides: 43
Provided by: timma87
Category:
Tags: avr | assembly | language | leds

less

Transcript and Presenter's Notes

Title: AVR Assembly Language


1
AVR Assembly Language
  • Assembly Language Programming
  • University of Akron
  • Dr. Tim Margush

2
Assemblers
  • Assembler's purpose is to output the bytes that
    must be in memory when the program starts
    executing
  • Assembly languages statements describe the bytes
    in various ways

source
E2 4C 3C 21 FF 16 90 FC 66 45 32 13
Assembler
4C 32 FF 00
3
Byte and Word Sequences
  • Define byte, define word
  • .cseg
  • .org 0
  • .db 3C, 87, 0b10110, 'a'
  • .dw 4000, 0, 0x4c3
  • Generates bytes intended to appear in flash as
    follows

4
Little-Endian
  • Word (and other larger) containers are filled in
    little-endian (least significant byte first)
    order
  • .dw 4000 generates 00 followed by 40
  • The listing file shows word data in normal or
    big-endian order

5
Byte Padding
  • Flash storage wants words, not individual bytes,
    so in the code segment
  • .db 3C, 50 specifies two bytes of one word
  • Equivalent to .dw 503C
  • .db 3C, 50, AA specifies a word and a half
  • A null byte (00) is appended to fill out the
    word
  • Equivalent to .db 3C, 50, AA, 00
  • Equivalent to .dw 503C, 00AA
  • Each .db directive is processed independently

6
Directives
  • Directives are instructions to the assembler
  • Some directives generate bytes
  • .db, .dw, .dd, .dq
  • Some directives affect the current segment's
    location counter
  • .byte, .org
  • Some directives define symbols
  • .equ, .set, .def, .undef
  • Some directives change assembler settings
  • .list, .nolist, .cseg, .dseg, .eseg
  • Some directives insert code into the source file
  • .include

7
(Machine) Instructions
  • Mnemonics represent machine instructions that are
    assembled into 16 or 32 bit values and output to
    the code segment
  • Many instructions require operands
  • Operands can be symbols or expressions
  • The use of symbols and expressions instead of
    actual numbers is the main advantage of assembly
    language over machine language
  • The assembler performs many useful and
    time-saving calculations and conversions

8
Expressions
  • Are evaluated by the assembler,
  • not the AVR processor
  • Are used to specify details of an instruction or
    data value,
  • and are not part of the program
  • Are formed from operands and operators,
  • and are evaluated using 64-bit precision

9
Symbols
  • Labels are defined by their appearance at the
    beginning of a line and a colon
  • here
  • this defines a symbol whose value is set to the
    current segment's location counter at the time
    the label is defined
  • Labels represent locations in the program, data,
    or EEPROM
  • Labels in the code segment are word addresses,
    others are byte addresses

10
Literals
  • Literals represent numbers (always)
  • You can write literals in decimal, hexadecimal,
    octal or binary
  • 255, FF, 0xFF, 0377, and 0b11111111 are all the
    same literal value
  • You can use a character notation
  • '\xFF', '\377' are equivalent to the above
  • '\n' is newline (0x0A), 'a' is 0x61, '\'' is the
    single quote ASCII code
  • These are all numbers!

11
String Literals
  • Double-quoted strings of 1 or more characters
    imply an array of bytes and may only be used in
    the .db directive
  • .db "abcde"
  • This is shorthand for a list of byte values
  • .db 'a','b','c','d','e'
  • There is no length information, nor is there a
    terminating byte

12
The PC
  • PC represents the code segment's location counter
  • Only the code segment's location counter is
    accessible in this way
  • You may use it in an expression where an address
    is legal
  • .org 32
  • .dw PC assemble a word with value 0020
  • rjmp PC-2 Jump back 2 instructions to address 31

13
Operators
  • Most of the standard C and Java operators are
    available
  • In addition to more familiar operators, some of
    the operators especially useful in assembly
    language are
  • bitwise complement
  • ltlt gtgt left and right bit shift
  • bitwise and, or, exclusive-or

14
Address Computations
  • Computations involving labels are common
  • .eseg
  • alpha .db "abcdefgh"
  • length .db length-alpha
  • The difference in addresses is the length of the
    string at alpha
  • If the string is changed, the length will still
    be correct when assembled
  • Warning address computations in the code
    segment are based on word addresses

15
Calculations with PC
  • PC is the code segments location counter
  • brne PC2
  • inc R6
  • add R6, R5
  • Branch around the next instruction if the NOT
    EQUAL condition is met
  • Warning some instructions are 32-bits and
    occupy 2 address locations

PC2
16
Functions in Expressions
  • The AVR Assembler provides a limited number of
    functions used in expressions
  • To extract various bytes or words of an
    expression value
  • low(exp) bits 7-0
  • high(exp) or byte2(exp) bits 15-8
  • byte3(exp) bits 23-16
  • byte4(exp) bits 31-24
  • lwrd(exp) bits 15-0
  • hwrd(exp) bits 31-16

byte2
byte4
byte3
low
high
lwrd
hwrd
17
Instruction Formats
  • Machine instructions are sometimes classified
    according to the number of operands
  • AVR instructions are take 0, 1, or 2 operands
  • Operands are either registers or expressions
  • The instruction mnemonic determines the opcode
  • Most instructions are 16 bits some are 32 bits
  • The operands determine the other bits in the
    machine instruction

18
Zero Operands
  • CLC Clear the carry flag
  • This simply clears the carry flag in the status
    register (in the control unit)
  • NOP No operation
  • Does nothing except allow the next instruction to
    be fetched on the next clock cycle
  • RET Return from subroutine
  • Pops the return address off the stack and places
    it in the program counter

19
One Operand
  • BSET s Set bit s of the status register
  • The operand s is any expression that represents a
    bit position 0 through 7
  • BRCC label Branch if carry clear
  • If the carry flag is clear (0), the value of
    label is put into the program counter
  • The destination can be an expression
  • NEG Rd Negate register
  • The number in the register is negated
  • Register operands must be Rsomething or a symbol
    defined with the DEF directive

20
Two Operands
  • SUB Rd, Rr Subtract Rr from Rd
  • The difference is stored into Rd (destination)
  • CPI Rd, K Compare Rd to immediate K
  • Computes Rd-K to determine status register
    settings
  • The result is not stored K is any byte value
  • OUT A, Rr Output to port
  • Stores the byte in Rr into I/O port A (0-63)
  • LDS Rd, addr Load direct from data storage
  • Read byte from address addr of SRAM into Rd
  • The operands for K, A, and addr may be expressions

21
AVR Bidirectional Ports
  • The ATMega16 has 4 bidirectional I/O ports, named
    PORTA through PORTD
  • This means there are 32 pins on the chip that can
    send (assert) or receive a logic signal
  • Each port has 3 I/O registers (d port letter)
  • DDRd Data direction register
  • PORTd Output port
  • PINd Input port

22
Port Setup
  • For output
  • Set corresponding bit in DDRn
  • Set/clear bit in PORTn to output that value
  • For input
  • Clear corresponding bit in DDRn
  • Set/clear corresponding bit in PORTn to
    activate/deactivate internal pullup resistor
  • Read bit in PINn

23
PORT Schematic
DDR
Pull-Up
DDR
PORT
Ext Pin
PORT
PIN
PORT
PIN
24
LED Control
  • The STK-500 provides 8 LEDs that can be easily
    connected to one of the I/O ports
  • Each bit of the port controls one LED
  • If the bit is 0, the LED illuminates
  • If the bit is 1, the LED is off
  • An LED (Light Emitting Diode) emits light when
    current passes through the diode

25
Typical LED Circuit
  • In this circuit, S1 is used to turn the LED on
    and off
  • The microcontroller output pin replaces the
    switch
  • Output a 0 to close the switch
  • 0 electrically means ground
  • Output a 1 to open the switch
  • 1 is Vcc so there is no current flow through the
    LED

26
STK-500 LED
AVR
  • An output pin is connected to the LED via a
    jumper
  • There is an external pull-up
  • A transistor is used to provide constant
    brightness via Vcc regardless of Vtarget level
    (down to 1.8V)

Pin
0 ON 1 OFF
27
Controlling an LED
  • Use PORTB bit 3 as output connected to led3 (does
    not affect other bits)
  • sbi DDRB, 3 direction output
  • sbi PORTB, 3 turn LED off
  • cbi PORTB, 3 turn LED on
  • To control all 8 bits
  • ldi R16, FF use a register to set direction
    settings
  • out DDRB, R16 set all pins of B for output
  • ldi R16, 0b00110111
  • out PORTB, R16 3 LEDs will be on

28
8-Bit LED Counter
  • Program PORTB to control 8 LED's to display a
    counter. The counter begins at 0, counts to 255,
    then overflows. The program runs indefinitely

29
Counter.asm
  • .def count r16 Reg 16 will hold counter value
  • .def temp r17 used as a temporary register
  • .equ PORTB 0x18 Port B's output register
  • .equ DDRB 0x17 Port B's Data Direction
    Register
  • ldi temp,0xFF configure PORTB as output
  • out DDRB,temp
  • ldi count,0x00 Initialize count at 0
  • lp
  • com count flip bits for light signal
  • out PORTB,count Put counter value on PORT B
  • com count flip bits back again
  • inc count increment counter
  • rjmp lp repeat (forever)

30
Test in Simulator
  • On the second iteration R16 is FE (after
    complementing for output) and PORTB matches this
    value
  • Remember, 0's mean LED on

31
Test in STK-500
  • Clock speed must be adjusted
  • The loop has 5 instructions and takes 6 clock
    cycles
  • Jumps typically require an extra cycle
  • At 4MHz, one clock cycle is .25x10-6 sec
  • One iteration is 1.5x10-6 sec
  • Overflow occurs every 384 microseconds

32
1 Count per Second
  • Set clock speed to slowest attainable (14.06 Hz)
    to get about 2 updates per second

Board
Fuses
Set Ext Clock and click Program
Set speed and click Write
33
Before Going On
  • Set the speed back to some faster value, or
    programming will fail
  • Don't forget to click Write or Program to send
    the settings to the STK-500!

34
Pushbutton Input
  • When a pin is configured for input, an external
    device controls the signal
  • The device pulls the signal to ground (0V) to
    indicate a logic 0
  • The device pulls the signal to Vtarget (5V) to
    indicate a logic 1
  • Alternatively, a passive device can disconnect
    and allow the internal pull-up resistor to assert
    a 1 signal

35
STK-500 Pushbutton
AVR
  • Pressing the button pulls the pin to GND
  • The pin reads 0
  • Releasing the button allows the 10K resistor to
    pull the signal to Vtarget
  • The pin reads 1

Pin
0 Pressed 1 Released
36
Reading a Pushbutton
  • Pushbutton connected to bit 3 of PORTD
  • cbi DDRD, 3 make bit 3 input
  • sbic PIND, 3 skip next instr if bit is 0
  • (or use sbis skip if bit in I/O reg is set)
  • Read all 8 buttons
  • clr R16 ddr value for all inputs
  • out DDRD, R16 set all inputs
  • in R16, PIND read all 8 bits

37
Toggler Program
  • Use pushbuttons to toggle the associated LED.
    Each pushbutton press will change the associated
    LED from on to off or off to on. The LEDs are all
    off to begin. SWn is associated with LEDn.

38
Pushbutton Read
  • wp in R17, PIND
  • cpi R17, FF
  • breq wp
  • some button is pressed
  • toggle appropriate LED
  • wr in R17, PIND
  • cpi R17, FF
  • brne wr
  • button released
  • Needs two steps
  • wait for press (some bit is 0)
  • wait for release (pins return to all 1's)
  • Compare immediate compares the real-time switch
    state to FF
  • Branch if equal branches if they match (all
    buttons up)
  • Branch if not equal branches if the do not match
    (some button or buttons are down)
  • After the second loop, we would loop to the top
    and start over

39
Toggle LED
  • Switch data will look like this
  • 0b11111011 (0 is pressed button)
  • LED data is any byte and we want to toggle the
    corresponding bit
  • Idea exclusive-or with 1 to toggle
  • Solution
  • complement switch data
  • eor with LED data and output to PORTB

40
Toggle Code
  • Assuming R17 switches
  • com R17 setup toggle mask
  • in R16, PORTB current LED state
  • eor R16, R17 toggle associated bit
  • out PORTB, R16 set new LED values
  • Add some initialization code and an outer loop

41
Main Loop with Symbols
  • wait for switch to be pressed
  • this will occur when one or more switches are
    depressed
  • waitpress
  • in switches, PIND
  • cpi switches, 0xFF 0xFF means none pressed
  • breq waitpress
  • one or more switches are pressed (0's)
  • com switches flip all bits, now 1's indicate
    pressed
  • in leds, PORTB read current LED state
  • eor leds,switches toggle associated bits in led
    status
  • out PORTB,leds (Re)display LED's
  • wait for all switches to be released
  • waitrelease
  • in switches, PIND
  • cpi switches, 0xFF 0xFF means none pressed
  • brne waitrelease

42
Switch Bounce
  • Physical contacts in switches bounce when opening
    and closing
  • Typical bounce time is measured in milliseconds
  • The AVR can sample the signal thousands of times
    in this short interval
  • Debouncing can be accomplished in software or
    hardware

Bounce interval
1
0
Time
Button pressed
Signal stable
Write a Comment
User Comments (0)
About PowerShow.com