L11 assembly-language-programming-of-atmega328 p
-
Upload
rsamurti -
Category
Engineering
-
view
319 -
download
0
Transcript of L11 assembly-language-programming-of-atmega328 p
Assembler Programming of Atmega328P(Lecture-11)
R S Ananda Murthy
Associate Professor and HeadDepartment of Electrical & Electronics Engineering,
Sri Jayachamarajendra College of Engineering,Mysore 570 006
R S Ananda Murthy Assembler Programming of Atmega328P
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 DWdirectives as shown above.Since each word address in flash stores two bytes inlittle-endian order, at any word address, the low-byteaddress is twice the word address and the high-byteaddress is low-byte address+1.
Determine the word and byte addresses in the example givenabove.
R S Ananda Murthy Assembler Programming of Atmega328P
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 topoint at data bytes stored in the flash memory before usingthe 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
Arithmetic and Logical Expressions in ALP
Arithmetic Operators Logical OperatorsSymbol 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 thesymbols given above.
R S Ananda Murthy Assembler Programming of Atmega328P
Examples of Arithmetic and Logical Expressions
R S Ananda Murthy Assembler Programming of Atmega328P
Device Definitions File
This file contains .EQU directives which define labelsassigned to various ports, port bits, registers and registerbits.For Atmega328P device, this file is named asm328pdef.inc
When we select a device for an assembly language projectin Atmel Studio, these definitions are available to the userprogram 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 inthe .asm file to use the standard labels.
R S Ananda Murthy Assembler Programming of Atmega328P
A Glimpse of Device Definitions File for Atmega328P
R S Ananda Murthy Assembler Programming of Atmega328P
Labels for Status Flags in m328Pdef.inc File
In m328Pdef.inc file, labels for status flags are defined asshown above.
R S Ananda Murthy Assembler Programming of Atmega328P
Setting Specific Flags in SREG using Flag Labels
Labels for status flags can be used to set specific flags asshown above.
R S Ananda Murthy Assembler Programming of Atmega328P
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
Stack in Atmega328P
Stack is a portion of R/W memory which is used for storingtemporary data, for storing local variables and for storingreturn addresses before executing subroutines or InterruptService Routines.In AVR MCUs, stack is, implemented as Last-In-First-Out(LIFO), growing from higher memory to lower memorylocations.PUSH Rr command will store the content of Rr in the stackand then decrements the SP by 1.POP Rd command will first increment the SP by 1 andthen, byte from the stack is copied to Rd.
R S Ananda Murthy Assembler Programming of Atmega328P
Stack Pointer in Atmega328P
In Atmega328P SP is a 16-bit register having two 8-bitregisters SPH and SPL in the I/O space of the R/Wmemory in MCU as shown above.In AVR MCUs having very small memory only SPL will bepresent.The Stack Pointer (SP) register always points to the top ofthe stack.
R S Ananda Murthy Assembler Programming of Atmega328P
License
This work is licensed under aCreative Commons Attribution 4.0 International License.
R S Ananda Murthy Assembler Programming of Atmega328P