Docsity

AVR Assembler Examples, Schemes and Mind Maps of Logic

Partial preview of the text

Download AVR Assembler Examples and more Schemes and Mind Maps Logic in PDF only on Docsity!

AVR Assembler Examples

AVR specific examples Credit to Dr. Robucci for slide information

m169pdef.inc

  • Include file detailing register and bit definitions SBR – Set bits in reg Equivalent to an ORI REQUIRES MASKS, not bit number 1<<PB5 = 8b’00000001 << 5 m169pdef.inc can be found in cmpe website

SRAM and Stack Pointer

  • This sets the stack

pointer to the end of

the SRAM

▫ HIGH(x) - upper 8 bits of a 16-bit word ▫ Low(x) - lower 8 bits of a 16-bit word ▫ SPH is stack pointer high ▫ SPL is stack pointer low

  • Necessary for stack

use

Resources

  • http://www.avr-asm-tutorial.net/avr_en/beginner/index.html
  • http://www.avr-asm-tutorial.net/avr_en/beginner/COMMANDS.html ▫ Commands sorted by function
  • http://www.avr-asm-download.de/beginner_en.pdf ▫ Beginner introduction to Assembly of ATMEL-AVR μProcessors MACROS
  • http://www.avrfreaks.net/forum/tutasmmacros- assembler?name=PNphpBB2&file=viewtopic&t=
  • http://www.avrbeginners.net/assembler/macros.html SUBROUTINES
  • http://www.avr-tutorials.com/assembly/writing-assembly-subroutines-avr- microcontroller ▫ Implementing Subrotines

Macro Directive

  • What is a Macros?
  • In assembly programming a macro is basically a set of instruction represented by a single statement. A macro can be seen as a new instruction created by the programmer for the microcontroller.
  • Note however that this new instruction (the macro) is created by using the existing instructions that are provided with the AVR microcontroller.
  • .MACRO macroname ▫ Tells assembler that this is the start of a macro
  • When name is used later in program, definition is expanded at the place it is used
  • Can take up to 10 parameters ▫ @0-@
  • Terminated by .ENDMACRO

Macro Example

Syntax: .MACRO macroname Example: .MACRO SUBI16 ; Start macro definition subi @1,low(@0) ; Subtract low byte sbci @2,high(@0) ; Subtract high byte .ENDMACRO ; End macro definition .CSEG ; Start code segment SUBI16 0x1234,r16,r17 ; Sub.0x1234 from r17:r

MACRO Example 3

  • Using a MACRO to define UART Transmit .MACRO UART_Transmit USART_Transmit: ; Wait for empty transmit buffer lds TEMP0, UCSR0A sbrs TEMP0,UDRE rjmp USART_Transmit ; send the data ldi TEMP, @ sts UDR0, TEMP .ENDMACRO
  • Calling the MACRO UART_Transmit 0x48 ;0x48 is ASCII equivalent for letter H You can also directly use ‘H’ UART_Transmit ‘H’

#define

  • Create a pre-processor function-style macro
  • Example

▫ #define BITMASK(x) (1<<x)

▫ #define BITMASK3(x1,x2,x3)

(BITMASK(x1)+BITMASK(x2)+BITMASK(x3))

Some built in functions(cont.)

LOG2(expression) returns the integer part of log2(expression) INT() Truncates a floating point expression to integer (i.e. discards fractional part) FRAC() Extracts fractional part of a floating point expression (i.e. discards integer part). Q7() Converts a fractional floating point expression to a form suitable for the FMUL/FMULS/FMULSU instructions. (sign + 7 - bit fraction) Q15() Converts a fractional floating point to the form returned by the FMUL/FMULS/FMULSY instructions (sign + 15-bit fraction). ABS() Returns the absolute value of a constant expression.

Stack and Functions

  • Using functions requires the stack
  • Using the stack requires that it be initialized

▫ See slide 4

  • After which point the following commands may be used

▫ PUSH

▫ POP

▫ RCALL

▫ RET

Setting IO

Implementing Delays

R25 and R

Jumping

  • Conditional Jumps ▫ SBIC – Skip if bit in I/O register cleared ▫ SBIS – Skip if bit in I/O register set ▫ SBRC – Skip if bit in register cleared ▫ SBRS – Skip if bit in register set
  • Unconditional Jumps ▫ RJMP k:  Program execution continues at PC+k+  2 clock cycles, smaller address range ▫ JMP k:  Program execution continues at k  3clock cycles, full address range

Combining jumps