Skip to main content

AVR data format and directives

In this section we look at some widely used data formats and directives supported by the AVR assembler.

AVR data type

The AVR microcontroller has only one data type. It is 8 bits, and the size
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.

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 to
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

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.

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

Comments

Post a Comment

Popular posts from this blog

8051 Simple Programs

Sum of 8-bit Numbers Stored in Memory Here  we will discuss about 8051 programs but we can't discuss about all of the 8051 instructions. For programming 8051 we should know about assembler directives as well as instruction set. Click  here to download Atmel c51 user guide that will discuss about 8051(c51 family microcontroller) instruction set, assembler directives, c51 cross assembler from Atmel.  Program ORG 00H MOV R0,#50H   ; get memory location in memory pointer R0 MOV R1,#51H   ; get memory location on memory pointer register R1 MOV A,@R0       ; get content of memory location 50H to accumulator ADD A,@R1        ; add content of A with content of memory location 51H and store result in A MOV R0,#52H    ; get 52H to memory pointer R0 MOV@R0,A         ; copy content of A to memory location 52H END Add 16-bit Numbers ...

Introduction to 8051 embedded C

For 8051 we need to include the file reg51.h. This file contains the all the definitions of 8051 registers. With this information C compiler produces hex file that can be downloaded into the ROM of the microcontroller. It is important to note that the size of the hex file produced by the assembly language is much larger than the hex file produced by C compiler. Apart from this fact, there is many reasons for writing programs in C instead of assembly: ●It is much easier and less time consuming to write programs in C assembly. ●C is more flexible; it is easier to modify and update. ●Programming in C allows to use code available in function libraries. ●Program written inC for one microcontroller is portable to other microcontrollers with little or no modifications. Data Types in 8051 Embedded C The table shown below lists the data types that are available in typical C51 compiler. The gives information about the size of the data variable and it's value range. Data type ...

Frequency of Oscillation of RC Phase Shift Oscillator

Derivation of Frequency of Oscillation We have to find out the transfer function of RC feedback network. Feedback Circuit of RC Phase Shift Oscillator Applying KVL to various loops on the figure, we get, $$I_1 \left(R+\frac{1}{j \omega C }\right) -I_2R=V_i \text{ ....(1)}$$ $$-I_1R+I_2\left (2R+\frac {1}{j\omega C}\right)-I_3R=0\text{ ... (2)}$$ $$0-I_2R+I_3\left(2R+ \frac{1}{j\omega C}\right)=0\text{ ...(3)}$$ Replacing \(j\omega\) with \(s\) and writing equations in the matrix form, $$\begin{bmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{sC} & -R \\0 & -R & 2R+\frac{1}{2sC} \end{bmatrix}\begin{bmatrix}I_1\\I_2\\I_3\end{bmatrix}=\begin{bmatrix}V_i\\0\\0\end{bmatrix}$$ Using Cramer's rule to find out \(I_3\), $$\text{Let, }D=\begin{bmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{sC} & -R \\0 & -R & 2R+\frac{1}{2sC} \end{bmatrix}$$ \(|D|=\begin{vmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{...