154x Filetype PDF File size 3.32 MB Source: www.csee.umbc.edu
Beginners Introduction to the Assembly Language of ATMEL-AVR-Microprocessors by Gerhard Schmidt http://www.avr-asm-tutorial.net February 2011 History: Added page on assembler concept in February 2011 Added chapter on code structures in April 2009 Additional corrections and updates as of January 2008 Corrected version as of July 2006 Original version as of December 2003 Avr-Asm-Tutorial 1 http://www.avr-asm-tutorial.net Content 1 Why learning Assembler?......................................................................................................................1 2 The concept behind the language assembler in micro-controllers.........................................................2 2.1 The hardware of micro-controllers.................................................................................................2 2.2 How the CPU works.......................................................................................................................2 2.3 Instructions in assembler................................................................................................................3 2.4 Difference to high-level languages.................................................................................................3 2.5 Assembler is not machine language...............................................................................................3 2.6 Interpreting and assembler..............................................................................................................4 2.7 High level languages and Assembler..............................................................................................4 2.8 What is really easier in assembler?................................................................................................5 3 Hardware for AVR-Assembler-Programming.......................................................................................6 3.1 The ISP Interface of the AVR processor family.............................................................................6 3.2 Programmer for the PC-Parallel-Port.............................................................................................6 3.3 Experimental boards.......................................................................................................................7 3.3.1 Experimental board with an ATtiny13...................................................................................7 3.3.2 Experimental board with an AT90S2313/ATmega2313........................................................8 3.4 Ready-to-use commercial programming boards for the AVR-family............................................9 3.4.1 STK200...................................................................................................................................9 3.4.2 STK500...................................................................................................................................9 3.4.3 AVR Dragon.........................................................................................................................10 4 Tools for AVR assembly programming...............................................................................................11 4.1 The editor.....................................................................................................................................11 4.1.1 A simple typewriter..............................................................................................................11 4.1.2 Structuring assembler code...................................................................................................12 4.2 The assembler...............................................................................................................................15 4.3 Programming the chips.................................................................................................................16 4.4 Simulation in the studio................................................................................................................16 5 What is a register?................................................................................................................................21 5.1 Different registers.........................................................................................................................22 5.2 Pointer-registers............................................................................................................................22 5.2.1 Accessing memory locations with pointers..........................................................................22 5.2.2 Reading program flash memory with the Z pointer..............................................................22 5.2.3 Tables in the program flash memory....................................................................................23 5.2.4 Accessing registers with pointers.........................................................................................23 5.3 Recommendation for the use of registers.....................................................................................24 6 Ports.....................................................................................................................................................25 6.1 What is a Port?.............................................................................................................................25 6.2 Write access to ports.....................................................................................................................25 6.3 Read access to ports......................................................................................................................26 6.4 Read-Modify-Write access to ports..............................................................................................26 6.5 Memory mapped port access........................................................................................................26 6.6 Details of relevant ports in the AVR............................................................................................26 6.7 The status register as the most used port......................................................................................27 6.8 Port details....................................................................................................................................28 7 SRAM..................................................................................................................................................29 7.1 What is SRAM?...........................................................................................................................29 7.2 For what purposes can I use SRAM?...........................................................................................29 7.3 How to use SRAM?......................................................................................................................29 7.3.1 Direct addressing..................................................................................................................29 7.3.2 Pointer addressing.................................................................................................................30 7.3.3 Pointer with offset.................................................................................................................30 7.4 Use of SRAM as stack..................................................................................................................30 7.4.1 Defining SRAM as stack......................................................................................................30 7.4.2 Use of the stack.....................................................................................................................31 7.4.3 Common bugs with the stack operation................................................................................31 8 Jumping and branching........................................................................................................................33 8.1 Controlling sequential execution of the program.........................................................................33 8.2 Linear program execution and branches.......................................................................................34 8.3 Timing during program execution................................................................................................35 8.4 Macros and program execution....................................................................................................35 8.5 Subroutines...................................................................................................................................35 Avr-Asm-Tutorial 2 http://www.avr-asm-tutorial.net 8.6 Interrupts and program execution.................................................................................................37 9 Calculations..........................................................................................................................................39 9.1 Number systems in assembler......................................................................................................39 9.1.1 Positive whole numbers (bytes, words, etc.)........................................................................39 9.1.2 Signed numbers (integers)....................................................................................................39 9.1.3 Binary Coded Digits, BCD...................................................................................................39 9.1.4 Packed BCDs........................................................................................................................40 9.1.5 Numbers in ASCII-format....................................................................................................40 9.2 Bit manipulations.........................................................................................................................40 9.3 Shift and rotate.............................................................................................................................41 9.4 Adding, subtracting and comparing..............................................................................................42 9.4.1 Adding and subtracting 16-bit numbers................................................................................42 9.4.2 Comparing 16-bit numbers...................................................................................................42 9.4.3 Comparing with constants....................................................................................................42 9.4.4 Packed BCD math.................................................................................................................43 9.5 Format conversion for numbers....................................................................................................43 9.5.1 Conversion of packed BCDs to BCDs, ASCII or Binaries...................................................43 9.5.2 Conversion of Binaries to BCD............................................................................................44 9.6 Multiplication...............................................................................................................................44 9.6.1 Decimal multiplication.........................................................................................................44 9.6.2 Binary multiplication............................................................................................................44 9.6.3 AVR assembler program......................................................................................................45 9.6.4 Binary rotation......................................................................................................................46 9.6.5 Multiplication in the studio...................................................................................................46 9.7 Hardware multiplication...............................................................................................................48 9.7.1 Hardware multiplication of 8-by-8-bit binaries....................................................................48 9.7.2 Hardware multiplication of a 16- by an 8-bit-binary............................................................49 9.7.3 Hardware multiplication of a 16- by a 16-bit-binary............................................................50 9.7.4 Hardware multiplication of a 16- by a 24-bit-binary............................................................52 9.8 Division........................................................................................................................................53 9.8.1 Decimal division...................................................................................................................53 9.8.2 Binary division......................................................................................................................54 9.8.3 Program steps during division..............................................................................................54 9.8.4 Division in the simulator......................................................................................................55 9.9 Number conversion......................................................................................................................56 9.10 Decimal Fractions.......................................................................................................................57 9.10.1 Linear conversions..............................................................................................................57 9.10.2 Example 1: 8-bit-AD-converter with fixed decimal output................................................58 9.10.3 Example 2: 10-bit-AD-converter with fixed decimal output..............................................59 10 Project planning.................................................................................................................................60 10.1 How to plan an AVR project in assembler.................................................................................60 10.2 Hardware considerations............................................................................................................60 10.3 Considerations on interrupt operation........................................................................................60 10.3.1 Basic requirements of interrupt-driven operation...............................................................61 10.3.2 Example for an interrupt-driven assembler program..........................................................61 10.4 Considerations on timing............................................................................................................63 11 Annex.................................................................................................................................................64 11.1 Instructions sorted by function...................................................................................................64 11.2 Directives and Instruction lists in alphabetic order....................................................................66 11.2.1 Assembler directives in alphabetic order............................................................................66 11.2.2 Instructions in alphabetic order...........................................................................................67 11.3 Port details..................................................................................................................................69 11.3.1 Status-Register, Accumulator flags....................................................................................69 11.3.2 Stackpointer........................................................................................................................69 11.3.3 SRAM and External Interrupt control................................................................................69 11.3.4 External Interrupt Control...................................................................................................70 11.3.5 Timer Interrupt Control......................................................................................................70 11.3.6 Timer/Counter 0..................................................................................................................71 11.3.7 Timer/Counter 1..................................................................................................................72 11.3.8 Watchdog-Timer.................................................................................................................73 11.3.9 EEPROM............................................................................................................................73 11.3.10 Serial Peripheral Interface SPI..........................................................................................74 11.3.11 UART...............................................................................................................................75 Avr-Asm-Tutorial 3 http://www.avr-asm-tutorial.net 11.3.12 Analog Comparator..........................................................................................................75 11.3.13 I/O Ports............................................................................................................................76 11.4 Ports, alphabetic order................................................................................................................76 11.5 List of abbreviations...................................................................................................................77
no reviews yet
Please Login to review.