Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

L11 assembly-language-programming-of-atmega328 p

600 views

Published on

Explains assembly language programming of Atmega328P

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

L11 assembly-language-programming-of-atmega328 p

  1. 1. Assembler Programming of Atmega328P (Lecture-11) R S Ananda Murthy Associate Professor and Head Department of Electrical & Electronics Engineering, Sri Jayachamarajendra College of Engineering, Mysore 570 006 R S Ananda Murthy Assembler Programming of Atmega328P
  2. 2. Storing Data in Flash Memory Though flash memory is referred to as program memory, we can also store fixed data in it using DB and DW directives as shown above. Since each word address in flash stores two bytes in little-endian order, at any word address, the low-byte address is twice the word address and the high-byte address is low-byte address+1. Determine the word and byte addresses in the example given above. R S Ananda Murthy Assembler Programming of Atmega328P
  3. 3. HIGH() and LOW() Functions AVR assembler provides these two functions. HIGH() returns the higher byte of a 16-bit value. LOW() returns the lower byte of a 16-bit value. These functions are typically used to initialize Z register to point at data bytes stored in the flash memory before using the LPM instruction. For example: LDI ZH,HIGH($100<<1);Initialize ZH. LDI ZL,LOW($100<<1) ;Initialize ZL. LPM R S Ananda Murthy Assembler Programming of Atmega328P
  4. 4. Arithmetic and Logical Expressions in ALP Arithmetic Operators Logical Operators Symbol Operation Symbol Operations + Addition & Bitwise AND − Subtraction | Bitwise OR * Multiplication ^ Bitwise XOR / Division ~ Bitwise NOT % Modulo << k Shifts left by k bits >> k Shifts right by k bits We can write arithmetic and logical operations in ALP using the symbols given above. R S Ananda Murthy Assembler Programming of Atmega328P
  5. 5. Examples of Arithmetic and Logical Expressions R S Ananda Murthy Assembler Programming of Atmega328P
  6. 6. Device Definitions File This file contains .EQU directives which define labels assigned to various ports, port bits, registers and register bits. For Atmega328P device, this file is named as m328pdef.inc When we select a device for an assembly language project in Atmel Studio, these definitions are available to the user program through the device definitions file. If we are not using Atmel Studio, then, we have to use .INCLUDE directive to include the Device Definitions File in the .asm file to use the standard labels. R S Ananda Murthy Assembler Programming of Atmega328P
  7. 7. A Glimpse of Device Definitions File for Atmega328P R S Ananda Murthy Assembler Programming of Atmega328P
  8. 8. Labels for Status Flags in m328Pdef.inc File In m328Pdef.inc file, labels for status flags are defined as shown above. R S Ananda Murthy Assembler Programming of Atmega328P
  9. 9. Setting Specific Flags in SREG using Flag Labels Labels for status flags can be used to set specific flags as shown above. R S Ananda Murthy Assembler Programming of Atmega328P
  10. 10. Labels Related to Memories in m328Pdef.inc File We can use RAMEND to initialize Stack Pointer as shown later. R S Ananda Murthy Assembler Programming of Atmega328P
  11. 11. Stack in Atmega328P Stack is a portion of R/W memory which is used for storing temporary data, for storing local variables and for storing return addresses before executing subroutines or Interrupt Service Routines. In AVR MCUs, stack is, implemented as Last-In-First-Out (LIFO), growing from higher memory to lower memory locations. PUSH Rr command will store the content of Rr in the stack and then decrements the SP by 1. POP Rd command will first increment the SP by 1 and then, byte from the stack is copied to Rd. R S Ananda Murthy Assembler Programming of Atmega328P
  12. 12. Stack Pointer in Atmega328P In Atmega328P SP is a 16-bit register having two 8-bit registers SPH and SPL in the I/O space of the R/W memory in MCU as shown above. In AVR MCUs having very small memory only SPL will be present. The Stack Pointer (SP) register always points to the top of the stack. R S Ananda Murthy Assembler Programming of Atmega328P
  13. 13. License This work is licensed under a Creative Commons Attribution 4.0 International License. R S Ananda Murthy Assembler Programming of Atmega328P

×