In this section we look at some widely used data formats and directives
supported by the AVR assembler.
of each register is also 8 bits. It is the job of the programmer to break down data larger than 8 bits (00 to OxFF, or 0 to 255 in decimal) to be processed by the CPU. The data types used by the AVR can be positive or negative. The bit-addressable data is discussed in later.
1. Put Ox (or OX) in front of the number like this: LDI RI 6, ox99
2. Put S in front of the number, like this: R22, $99.
We use both Of these methods in this book, because many application notes
out there use one of them and we need to get used to them.
Here are a few lines Of code that use the hex format:
LDI R28, $75 ;;R28 0x75
SUBl R28,0X11 ;;R28 0x75 - 0x11=0x64
SUBI 0X20 ;;R28 0X64 - 0x20=0x44
ANDI R28, 0xf ;;R28 ox44-0x0F=Ox35
use it:
LDI R17, 12 ;;00010010 in binary and 0C in hex
SUBI R17, 2 ;;R17= 12-2=10 where 10 is equal to OxOA
AVR data type
The AVR microcontroller has only one data type. It is 8 bits, and the sizeof each register is also 8 bits. It is the job of the programmer to break down data larger than 8 bits (00 to OxFF, or 0 to 255 in decimal) to be processed by the CPU. The data types used by the AVR can be positive or negative. The bit-addressable data is discussed in later.
Data format representation
There are four ways to represent a byte of data in the AVR assembler. The numbers can be in hex, binary', decimal, or ASCII formats. The following are examples of how each works.Hex numbers
There are two ways to show hex numbers:1. Put Ox (or OX) in front of the number like this: LDI RI 6, ox99
2. Put S in front of the number, like this: R22, $99.
We use both Of these methods in this book, because many application notes
out there use one of them and we need to get used to them.
Here are a few lines Of code that use the hex format:
LDI R28, $75 ;;R28 0x75
SUBl R28,0X11 ;;R28 0x75 - 0x11=0x64
SUBI 0X20 ;;R28 0X64 - 0x20=0x44
ANDI R28, 0xf ;;R28 ox44-0x0F=Ox35
Binary numbers
There is only one way to represent binary numbers in an AVR assembler.
It is as follows:
LDI R16,0b10011001;; R16= 10011001 or 99 in hex
The uppercase B will also work. Here are some examples of how to use it:
LDI R23, 0b00100101 ;;;R23=$25
SUBI R25, 0B00010001;;R23=$25-$11=$14
Decimal numbers
To indicate decimal numbers in an AVR assembler we simply use the decimal and nothing before or after it. Here are some examples of how touse it:
LDI R17, 12 ;;00010010 in binary and 0C in hex
SUBI R17, 2 ;;R17= 12-2=10 where 10 is equal to OxOA
ASCII Characters
To represent ASCII data in an AVR assembler we use single quotes as follows:
LDI R23,'2' ;;R23 - 00110010 or 32 in hex.
This is the same as other assemblers such as the 8051 and x86. Here are
some more examples:
LDI R20, '9' ;;R20 0x39, which hex number for ASCII '9'
SUBI R20, '1';;R20 0x39 0X8
;;(31 hex for ASCII '1')
To represent a string. double quotes are used: and for defining ASCII
strings (more than one character), we use the .DB (define byte) directive.
.EOU COUNT = 0x25
.......................
LDI R21, COUNT :R21 = 0x25
When executing the above instruction “LDI R21 , COUNT“. the register R2l will be loaded with the value 25H. What is the advantage of using .EQU. Assume that a constant (a fixed value) is used throughout the program, and the programmer wants to change its value everywhere. By the use of .EQU. the programmer can change it once and the assembler will change all of its occurrences throughout the program. This allows the programmer to avoid searching the entire program trying to find every occurrence.
We mentioned earlier that we can use the names of the I/O registers instead of their addresses (e.g., we can write “OUT PORTA,R20" instead of “OUT 0x1B,R20"). This is done with the help of the .EQU directive. in include files such as M32DEF.INC the I/O register names are associated with their addresses using the .EQU directive. For example in M3ZDEF.INC the following pseudo-instruction exists, which associates 0x1B (the address of PORTB) with the PORTB.
.EQU PORTB 0x1B
;in hexadecimal
.EQU DATA1 0x39 ;;one way to define hex value
.EQU DATA2 = $39 ;;another way to define hex value
;in binary
.EQU DATA3 = 0b00110101 binary (35 in hex)
;in decimal
.EQU DAT4 =39 ;;decimal numbers (27 in hex)
;in ASCII
.EQU DATAS = '2' ;;ASCII characters
We use .DB to allocate code ROM memory locations for fixed data such as ASCII strings.
.EQU COUNTER= 0x00 ;;counter value 00
.EQU PORTB= 0x18 ;;SFR Port B address
LDI R16, COUNTER ;;Rl6= 0x00
OUT PORTB, R16 ;;Port B (loc 0x18) now has 00 too
.EQU SUM= 0x120 ;;assign RAM 120 to SUM
LDI R20, 5 ;;load R20 with 5
LDI R21, 2 ;;load R21 with 2
ADD R20, R21 ;;R20 = R20 + R21
ADD R20, R21 ;;R20= R20 + R21
STS SUM, R20 ;;store the result in location 0x120
This is especially helpful when the address needs to be changed in order to use a different AVR chip for a given project. It is much easier to refer to a name than a number when accessing RAM address locations.
For example, when you want to use ATmega32, you must write the following instruction at the beginning of your program:
. INCLUDE "M32DEF. INC"
Assembler directives
While instructions tell the CPU what to do, directives (also called pseudoinsirucrions) give directions to the assembler. For example, the LDI and ADD instructions are commands to the CPU but .EQU, .DEVlCE and .ORG) are directives to the assembler. The following sections present some more widely used directives of the AVR and how they are used. The directives help us develop our program easier and make our program legible (more readable)..EQU (equate)
This is used to define a constant value or a fixed address. The .EQU directive does not set aside storage for a data item but associates a constant number with a data or an address label so that when the label appears in the program. its constant will be substituted for the label. The following uses .EQU for the counter constant. and then the constant is used to load the R21 register:.EOU COUNT = 0x25
.......................
LDI R21, COUNT :R21 = 0x25
When executing the above instruction “LDI R21 , COUNT“. the register R2l will be loaded with the value 25H. What is the advantage of using .EQU. Assume that a constant (a fixed value) is used throughout the program, and the programmer wants to change its value everywhere. By the use of .EQU. the programmer can change it once and the assembler will change all of its occurrences throughout the program. This allows the programmer to avoid searching the entire program trying to find every occurrence.
We mentioned earlier that we can use the names of the I/O registers instead of their addresses (e.g., we can write “OUT PORTA,R20" instead of “OUT 0x1B,R20"). This is done with the help of the .EQU directive. in include files such as M32DEF.INC the I/O register names are associated with their addresses using the .EQU directive. For example in M3ZDEF.INC the following pseudo-instruction exists, which associates 0x1B (the address of PORTB) with the PORTB.
.EQU PORTB 0x1B
.SET
This directive is used to define a constant value or a fixed address. in this regard. the .SET and .EQU directives are identical. The only difference is that the value assigned by the .SET directive may be reassigned later.Using .EQU for fixed data assignment
To get more practice using .EQU to assign fixed data. examine the following:
;in hexadecimal
.EQU DATA1 0x39 ;;one way to define hex value
.EQU DATA2 = $39 ;;another way to define hex value
;in binary
.EQU DATA3 = 0b00110101 binary (35 in hex)
;in decimal
.EQU DAT4 =39 ;;decimal numbers (27 in hex)
;in ASCII
.EQU DATAS = '2' ;;ASCII characters
We use .DB to allocate code ROM memory locations for fixed data such as ASCII strings.
Using .EQU for SFR address assignment
.EQU is also widely used to assign SFR addresses. Examine the following code:.EQU COUNTER= 0x00 ;;counter value 00
.EQU PORTB= 0x18 ;;SFR Port B address
LDI R16, COUNTER ;;Rl6= 0x00
OUT PORTB, R16 ;;Port B (loc 0x18) now has 00 too
Using .EQU for RAM address assignment
Another common usage of .EQU is for the address assignment of the internal SRAM. Examine the following rewrite of an earlier example using .EQU:.EQU SUM= 0x120 ;;assign RAM 120 to SUM
LDI R20, 5 ;;load R20 with 5
LDI R21, 2 ;;load R21 with 2
ADD R20, R21 ;;R20 = R20 + R21
ADD R20, R21 ;;R20= R20 + R21
STS SUM, R20 ;;store the result in location 0x120
This is especially helpful when the address needs to be changed in order to use a different AVR chip for a given project. It is much easier to refer to a name than a number when accessing RAM address locations.
.ORG (origin)
The .ORG directive is used to indicate the beginning of the address. It can be used for both code and data..INCLUDE directive
The .include directive tells the AVR assembler to add the contents of a file to our program (like the #include directive in C language). In Table shown below, you see the files that you must include whenever you want to use any of the AVRs.For example, when you want to use ATmega32, you must write the following instruction at the beginning of your program:
. INCLUDE "M32DEF. INC"
ATMEGA | ATTINY | Special purpose |
---|---|---|
ATmega8 m8def.inc | ATtiny11 tn11def.inc | AT90CAN32 can32def.inc |
ATmega16 m16def.inc | ATtiny12 tn12def.inc | ATCAN64 can64def.inc |
ATmega32 m32def.inc | ATtiny22 tn22def.inc | AT90PWM2 pwm2def.c |
ATmega64 m64def.inc | ATtiny44 tn44def.inc | AT90PWM3 pwm3def.inc |
ATmega128 m128def.inc | ATtiny85 tn85def.inc | AT90USB646 usb646def.inc |
ATmega256 m256def.inc | ||
ATmega2560 m2560def.inc |
caucavac_nu-Rochester Judy Reuland https://wakelet.com/wake/g2BfX_7HRMMmLJoZgGRic
ReplyDeleteteaupetifens