Successfully reported this slideshow.
Upcoming SlideShare
×

# L11 assembly-language-programming-of-atmega328 p

680 views

Published on

Explains assembly language programming of Atmega328P

Published in: Engineering
• Full Name
Comment goes here.

Are you sure you want to Yes No
• 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 ﬂash memory is referred to as program memory, we can also store ﬁxed data in it using DB and DW directives as shown above. Since each word address in ﬂash 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 ﬂash 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 Deﬁnitions File This ﬁle contains .EQU directives which deﬁne labels assigned to various ports, port bits, registers and register bits. For Atmega328P device, this ﬁle is named as m328pdef.inc When we select a device for an assembly language project in Atmel Studio, these deﬁnitions are available to the user program through the device deﬁnitions ﬁle. If we are not using Atmel Studio, then, we have to use .INCLUDE directive to include the Device Deﬁnitions File in the .asm ﬁle to use the standard labels. R S Ananda Murthy Assembler Programming of Atmega328P
7. 7. A Glimpse of Device Deﬁnitions File for Atmega328P R S Ananda Murthy Assembler Programming of Atmega328P
8. 8. Labels for Status Flags in m328Pdef.inc File In m328Pdef.inc ﬁle, labels for status ﬂags are deﬁned as shown above. R S Ananda Murthy Assembler Programming of Atmega328P
9. 9. Setting Speciﬁc Flags in SREG using Flag Labels Labels for status ﬂags can be used to set speciﬁc ﬂags 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 ﬁrst 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