Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. AVR Assembly Language An Introduction Assembly Language Programming University of Akron Dr. Tim Margush
  2. 2. Assemblers <ul><li>Translate source code to machine language </li></ul>source Assembler include files object file hex file map file list file
  3. 3. Source Files <ul><li>Usually a text file containing assembly language statements </li></ul><ul><li>Include directives are used to insert text from other files as part of the source </li></ul>
  4. 4. Object File <ul><li>Contains machine program and debugging information used by AVR Studio </li></ul>
  5. 5. Hex File <ul><li>Contains data for AVR Processor flash memory </li></ul><ul><li>This is used by the programmer utility to download to AVR memory </li></ul>
  6. 6. Map File <ul><li>A list of labels and symbols used in the program </li></ul><ul><li>Serves as a reference to locate data in memory </li></ul>
  7. 7. Listing File <ul><li>A complete report of the assembly process </li></ul><ul><ul><li>Includes error messages, address information, bytes generated, and statistics </li></ul></ul>
  8. 8. Syntax <ul><li>There are 3 valid formats for AVR assembly language statements </li></ul><ul><ul><li>[label:] directive operands [;comment] </li></ul></ul><ul><ul><li>[label:] instruction operands [;comment] </li></ul></ul><ul><ul><li>[label:] [;comment] </li></ul></ul><ul><ul><ul><li>[..] indicates optional stuff </li></ul></ul></ul><ul><ul><ul><li>Each line is a directive, an instruction (machine), a label, or is blank </li></ul></ul></ul><ul><ul><ul><li>Any line can have a comment </li></ul></ul></ul>
  9. 9. Comments <ul><li>Single line comments </li></ul><ul><ul><li>Begin with a semi-colon or double slash and extend to the end of the line </li></ul></ul><ul><li>Block comments </li></ul><ul><ul><li>Begin with /*, end with */, and may span several lines </li></ul></ul>
  10. 10. Segment Directives <ul><li>The assembler generates bytes for flash and EEPROM and reserves addresses in SRAM </li></ul><ul><li>Segment directives set the active memory for subsequent commands </li></ul><ul><ul><li>.cseg – code segment (flash) </li></ul></ul><ul><ul><ul><li>This is the default segment if none is specified </li></ul></ul></ul><ul><ul><li>.eseg – EEPROM </li></ul></ul><ul><ul><li>.dseg – data segment (SRAM) </li></ul></ul>
  11. 11. Origin Directive <ul><li>The assembler maintains a location counter for each segment </li></ul><ul><li>.org value </li></ul><ul><ul><li>sets the location counter of the current segment to the specified value </li></ul></ul><ul><ul><ul><li>The default origin for the code segment and EEPROM is $0000 </li></ul></ul></ul><ul><ul><ul><li>The location counter for the data segment is the first address following the I/O registers ($0060) </li></ul></ul></ul>
  12. 12. LED Flasher Program <ul><li>;A simple AVR example to illustrate output to a port. </li></ul><ul><li>;Designed to be executed in a simulator under debug control </li></ul><ul><li>;This program counts from 0 to 255 (and repeats) outputting </li></ul><ul><li>;the current counter value to PORTB. </li></ul><ul><li>.cseg ;select current segment as code </li></ul><ul><li>.org 0 ;begin assembling at address 0 </li></ul><ul><li>.def count = r16 ;Reg 16 will hold counter value </li></ul><ul><li>.def temp = r17 ;used as a temporary register </li></ul><ul><li>.equ PORTB = 0x18 ;Port B's output register </li></ul><ul><li>.equ DDRB = 0x17 ;Port B's Data Direction Register </li></ul><ul><li>ldi temp,0xFF ;configure PORTB as output </li></ul><ul><li>out DDRB,temp </li></ul><ul><li>ldi count,0x00 ;Initialize count at 0 </li></ul><ul><li>lp: </li></ul><ul><li>out PORTB,count ;Put counter value on PORT B </li></ul><ul><li>inc count ;increment counter </li></ul><ul><li>rjmp lp ;repeat (forever) </li></ul>
  13. 13. Assembling the Program <ul><li>After creating a new project and entering the source code, the program is assembled using the Build command. </li></ul><ul><ul><li>The option to create a listing file must be selected under Assembler Options under the Project menu </li></ul></ul><ul><li>If errors occur, correct them and reassemble </li></ul>
  14. 14. Simulating the Program <ul><li>Start debugging (Debug menu) </li></ul><ul><li>Select the I/O View and be sure that Register 16, the Processor, and PORT B are visible </li></ul><ul><li>Single step, observing changes in the I/O View </li></ul><ul><ul><li>Use Auto-step to run the simulator while observing changes at each step </li></ul></ul>
  15. 15. Using the STK-500 <ul><li>Setup the STK-500 as described in the included documentation </li></ul><ul><li>Connect the STK-500 to your computer's serial port and turn on the STK-500 </li></ul><ul><li>Activate the AVR Programmer </li></ul><ul><ul><li>Tools menu: Program AVR: Connect </li></ul></ul><ul><ul><li>Usually the Auto detect finds the board </li></ul></ul>
  16. 16. Program Flash from HEX <ul><li>On the Program Tab, you can accept defaults for everything but the input file under Flash </li></ul><ul><ul><li>Use the browse (…) button to locate the HEX file with the same name as your project </li></ul></ul><ul><ul><li>It will be in the project folder </li></ul></ul><ul><li>Click Program when ready </li></ul>
  17. 17. Internal Clock Speed <ul><li>This program runs too fast under the default processor settings (4MHz internal oscillator) </li></ul><ul><ul><li>The LEDs appear to all stay illuminated </li></ul></ul><ul><li>Under the Fuses tab, select the Ext. Clock </li></ul><ul><ul><li>Click Program to send the options to the processor </li></ul></ul>
  18. 18. External Clock <ul><li>Under the Board tab, set the STK500 Osc to 1024Hz and click Write </li></ul><ul><ul><li>The processor should run slow enough to observe the leading bit cycling once per second </li></ul></ul><ul><ul><li>Note that the dialog indicates the clock will be set to the attainable frequency of 1019 Hz </li></ul></ul><ul><ul><ul><li>Not all frequencies can be produced exactly </li></ul></ul></ul>
  19. 19. Backwards Counter <ul><li>You should notice that the counter appears to run backwards </li></ul><ul><li>The circuit used to control the LEDs causes the LED to emit light when the port output is 0 </li></ul><ul><ul><li>00000000 </li></ul></ul><ul><ul><li>00000001 </li></ul></ul><ul><ul><li>etc… appears to be counting backwards </li></ul></ul>
  20. 20. External Oscillator <ul><li>Be sure to set the STK500 Osc back to a larger value (4 MHz is fine) before downloading other programs </li></ul><ul><ul><li>You must click Write to send the setting to the processor </li></ul></ul><ul><li>If this value is too slow, the programmer cannot download code to flash </li></ul>