AVR - Frequently Asked Questions
Updated: 24/10/2000

 

 

Most common problems 
In System Programming(ISP) of the mega103/603 
AVR emulator can not drive an external load 
A/D converter is reading the value from the last channel 
When I load a file into AVR STUDIO, everything chrashes 
Use of the low byte's of the pointers to access the available memory 
Development tools for megaAVR 
Is there any starter kits available for AVR ? 
Access the AVR internal EEPROM data memory within the IAR C-compiler 
Is the Real Time Clock(RTC) in the ATmega controller Year 2000 compliant 
Minimum Power Down current consumption 
Asynchronously reset of I/O ports 
NC(No connect) pins 
How do I define a string constant in the AVR assembler ? 
My assembler does not produce an Intel Hex file, what is wrong ? 
Assembler editor in file size limitation 
Upgrade of AVR Simulator for Windows 3.11 
PC does not recognize the IAR Dongle 
How can I reach the entire program memory with RJMP and RCALL 
Use of the Timer/Counters used to generate the UART Baud Rate 
Single bit set or clear to the I/O with the SBI and CBI instructions 
Why are there three addresses for each I/O Port ? 
Why does SBI and CBI only operate on I/O registers $00 -$1F ? 
Why do the immediate instructions not work on registers R0-R15 ? 
Why is there no EORI instruction ? 
Use of SBR instruction 
Check and clear flags 
Clearing a register and the carry flag 
How do I implement a constant table in the AT90S1200? 
How to add an immediate constants 
How can Flash and Mask ROM ever cost the same ? 
Is the external clock signal multiplied ? 
How many clock cycles make up one AVR instruction cycle ? 
Why is the AT90S8515 called a 8K MCU ? 
Where can I find datasheets ? 
What added value can the AVR offer over my current MCU ? 
What does "AVR" mean ? 
 

Question: What are the most common problems when starting a new project with the AVR ?

Answer 

The most common mistake is to forget to set up the stack-pointer of the 
devices without hardware stack. On an AT90S8515 this is done like 
this: 
         LDI    R16,low(RAMEND) 
         OUT    spl,temp 
         LDI    R16,high(RAMEND) 
         OUT    sph,temp 

It is also common to forget to set up the ports when using them 
as output. On the AVR, the ports are set as outputs by writing a '1' 
to the Data Direction Register(DDR). For instance to set PORTB to 
output high: 

LDI   R16,0xFF          ;Load FF(hex) into the register file 
OUT   DDRB,R16          ;set PORTB as output 
OUT   PORTB,R16         ;set PORTB high 

When using the AVR assembler a common mistake is to point to the wrong 
location in the program memory while using the Load Program 
Memory(LPM) instruction. The program memory is organized as 16-bit 
words, while the LPM instruction is reading 8-bit bytes. 

The LPM instruction can read either the upper or the lower byte of the 
16-bit word. Because of this, it is necessary to multiply the 16-bit 
 program memory address by two to point to the byte you want to load. 

 This is decribed in app.note 102, "Block Copy routines" 
 

Question: What pins are used for In System Programming(ISP) of the mega103/603 controller ? 

Answer 

The In System Program interface for mega103/603 controller should be 
connected to the following pins on mega103/603: 

SCK     - SCK           (PB1,pin 11) 
MISO    - PDO           (PE1,pin 3) 
MOSI    - PDI           (PE0,pin 2) 
RESET   - RESET         (RESET,pin 20) 

The PEN(Program Enable) pin can be used as an alternative to RESET. 
The two pins have the same functionality, (they activate the internal 
iRESET signal) but PEN is read only during Power On (VCC approx. 1.2 
volts). If PEN is not used, it is recommended to tie this pin to VCC. 
RESET is then used as on all other AVR parts to enable serial 
programming. 
 

Question: I can not get my AVR emulator to drive an external load correctly, when I use a 
voltmeter to measure the pins on the emulator POD it seems like the pin can not drive the load. 
What is wrong ? 

Answer 

This is a problem with version 1.21 of the ICEPRO and version 1.10 
of the megaICE. 

New versions of the emulators are available as software upgrades from 
www.atmel.com->products->AVR 8 bit RISC->software. 

To upgrade the ICEPRO to version 1.22, download the file UPGRD122.ZIP 
To upgrade the megaICE to version 1.11, download the file M111UPGR.ZIP 

These files are compressed files containing a new program file for the 
emulator and a PC program that automatically updates the emulator and 
performs an error check of the code. 
 

Question: I am using an AVR device with A/D converter, the device works excellent, and I 
love the speed of the AVR. But I have one problem: When I change input channel on the A/D 
converter it seems like the A/D converter is reading the value from the last channel. If I do a 
second read I get the right value. Why is this happening ? 

Answer 

Two bits in the A/D Control and Status Register(ADCSR) is used 
to detect the end of an A/D conversion: 

The A/D Start Conversion (ADSC) bit is used to test when a new 
conversion can start, but the result of the previous conversion is not 
yet ready in the A/D data register. When the A/D data register is read 
the result of the last conversion is present. 

To get the result of the latest conversion, test the A/D Interrupt 
Flag (ADIF) or enable global interrupt and execute the reading of the 
A/D converter in the interrupt routine. 

Example code: 

               ldi     R16,1                   ; Select channel 
        out     ADMUX,R16 
        sbi     ADCSR,adif              ; Reset interrupt flag 
        sbi     ADCSR,adsc              ; Start A/D conversion 
wait:   sbis    ADCSR,adif              ; Wait until the ADIF is set 
        rjmp    wait 
 

 

 

 

 

 

Question: When I load a file into AVR STUDIO, everything chrashes. It worked the first time 
I used the file. What is wrong ? 

Answer 

The ".AVD" file contains information about the window settings for the 
current object file. 
Sometimes, the '.AVD' created by AVR STUDIO can get corrupted. 
Depending on which part of the file that is corrupted, different 
symptoms will appear. When you run into 'strange' problems with AVR 
STUDIO, first try to delete the '.avd' file located in the folder 
where your source is placed. 
 

Question: In AVR devices like the AT90S2313, where total Data Memory is less than 256 
locations and there is no access to external memory, can you ignore the upper byte of the X, 
Y, and Z pointers and only use the low byte's of the pointers to access the available memory ? 

Answer 

In devices where total Data Memory (registers, I/O and SRAM) is less 
than 256 bytes, the CPU will ignore the high byte of the pointer when 
accessing data memory using LD/ST instructions. This allows the high 
byte of each pointer (R27, R29 and r31) to be used for general purpose 
storage. The post-increment and pre-decrement will not affect the high 
byte. (like LD -Z, Rd). 

Note that the high byte of the Z pointer is used when accessing flash 
program memory and ADIW/SBIW instructions WILL affect the high byte. 
 

Question: I want to use the megaAVR microcontroller, what development tools are available 
for this part ? 

Answer 

The megaAVR family of microcontrollers have a complete suite of 
development tools: 

Evaluation/Programming board: The ATMEL STK300 Starter Kit, developed 
specifically for the ATMega series. Programming is accomplished via 
the In System Programming(ISP) interface. 

Emulator: The ATMEL MegaICE is available now for real-time emulation. 

C compiler: The third-party tools company IAR Systems is providing a 
C-compiler for our AVR product line, including the ATMega parts. You 
can check their products at http://www.iar.com 

ATMEL also offers most of the front-end software required to work with 
our AVR devices (assembler, simulator) as freely-downloadable software 
on our Web site: http://www.atmel.com. 

All these products are available through an authorized ATMEL 
distributor/sales representative 
 

Question: Is there any starter kits available for AVR ? 

Answer 

The STK200 supports all AVR parts with 8, 20, 28 and 40 pins. 
The STK300 supports all megaAVR parts. 

Both starter kits includes pushbuttons, LED's and RS232 interface. The 
I/O ports are accessible through header connectors on the edge of the 
board. The starter kits includes an In System Programming(ISP) cable 
that can be used to program the AVR controller in the user 
application. 
The STK300 also includes the AVR application builder which generates 
the initialization code for setting up the peripherals. 
 

Question: How can I access the AVR internal EEPROM data memory within the IAR 
C-compiler ? 

Answer 

The internal EEPROM on the AVR is part of the I/O memory of the AVR 
controller. It can not be accessed as normal variables. Special macros 
to read and write the EEPROM is defined in the standard library 
routine "ina90.h" 

The read and write macro has the following prototypes: 
_EEGET(VAR,ADR) /* To read the value of EERPOM address ADR */ 
_EEPUT(ADR,VAL) /* To write VAL to EERPOM address ADR * 

The IAR C-compiler does not include the option to define constants to 
be downloaded in the EEPROM. The easiest way to do this is to use the 
AVR assembler and write code for the EEPROM constants: 

.ESEG 
.org 0x50 
datatable:      .db     $01,$02,$03,$04,$05 
dtableend:      .db     $00 

When this is assembled, it generates a .EEP file. This file can be 
dowloaded into the controller or the emulator. The .org statement 
gives the address of the data. The variables can be accessed with 
EEGET() and EEPUT() in the C-compiler. 
 

Question: Is the Real Time Clock(RTC) in the ATmega controller Year 2000 compliant ? 

Answer 

The real time clock function of the AVR controller consist of an 
asynchronous counter with an oscillator optimized for use with a 32.768kHz 
watch crystal. The timer is set up to generate a periodic interrupt that is 
used to update the clock and calendar variables. The RTC can be made Y2K 
compliant by applying code that takes care of the millennium update. 
 

Question: I am unable to get the Power Down current consumption down to the value in the 
databook. What should I do ? 

Answer 

When an AVR enters Power Down mode, the states of the I/O pins are left 
unchanged. If you have some unconnected pins that are tri-stated (input, no 
pull-up), these pins are floating and will most probably oscillate and cause 
extra current draw. 
To solve this, activate the pull-up resistors on all unconnected inputs. This 
will cause a defined logical level to be applied to these pins. Turning on 
pull-ups on unused I/O pins is a good practice also in active mode, as 
oscillating I/O pins can cause problems in some cases. 
Secondly, you should review what is connected to your output pins. Any load 
on an output pin will draw extra current - also in Power Down mode. 
 

Question: With my current microcontroller I observe activity on the I/O ports during power up. 
This is a problem for my application since the MCU is able to shut off power to the system by 
setting one of the I/O pins high. Even if this pin is pulled low by a resistor, it sometimes goes active 
high during power up. Consequently, when power is turned on, the system shuts down immediately 
and to the user it appears that the system does not work. If I switch to the AVR, can I expect the 
same sort of problem ? 

Answer 

Most microcontrollers have a synchronous reset, i.e. the MCU needs a 
number of valid clocks while RESET is asserted to reset properly. During 
power up, there is a chance that VCC rises to a value close to the operating 
voltage before the crystal oscillator starts. In this period, Power-On reset 
is active, but is not effective and the I/O pin values and directions are 
set to a random state. The I/O ports on the AVR are asynchronously reset and 
are guaranteed to be in tri-state during the entire power-up cycle. 
Switching to an AVR will fix your problem. 
 

 

 

 

 

Question: What shall I do with NC(No connect) pins on the AVR controller ? 

Answer 

Do not make any connections to the NC(No connect) pins. The NC pins are 
reserved for future requirements. 
 

Question: How do I define a string constant in the AVR assembler ? 

Answer 

Strings can be defined both as constants in flash memory or in EEPROM data memory. 
Example: Define a string constant in flash: 

.CSEG 
    fstring: .db "This is a string in flash",0x00 

To define a string constant in EEPROM: 

.ESEG 
    eestring: .db "This is a string in EEPROM",0x00 
 

Question: My assembler does not produce an Intel Hex file, what is wrong ? 

Answer 

By default, AVRASM generate Motorola S-Record file 
format with file extension .ROM. 
To change this to Intel Hex in WAVRASM, do the following: 

  1. Select 'Options' from the menu
  2. At the box "Output file extension", write HEX
  3. At "Output file format" click on "Intel intellec 8/MDS"
The assembler will now generate an Intel HEX file when assembling. 
 

Question: I am working on a quite big assembly program. When I add new lines to the 
program, characters at the end of the file seem to disappear. What do I do to prevent this? 

Answer 
The editor in assembler has a file size limitation of 30K. To overcome this limitation, the file 
must be split into several modules which is joined by using ".include". Alternatively, use an 
external editor and the command-line version of the assembler. 
 

Question: When will the AVR Simulator for Windows 3.11 be upgraded to support new 
devices? 

Answer 
This product is being phased out and is no longer supported by Atmel. For debugging of 
all peripherals and also new parts, use the AVR Studio, which can be downloaded from 
the web. We still keep it on the web for users that do not run Windows95 or NT. 
 

Question: My new Pentium 200 MHz computer does not recognize the IAR Dongle. It 
works fine with my good, old 75. Is this a known problem? 

Answer 
Yes, on Pentium P133 and faster machines, the hardware lock might have synchronization 
problems. Add the following line to your AUTOEXEC.BAT file: 

Set SSI_ACT =10,20,20 

Restart your computer and try again. If the hardware lock is still not working, try 
increasing the numbers gradually in steps of 10 or 20 (the maximum limit is 200). Not that 
too high numbers, e.g. 200,200,200 also may cause communications to fail. 
 

Question: The AT90S8515 has got 8kB of program memory. As the RJMP and RCALL 
commands can take me 2K locations backwards or forwards, how can I reach the entire 
program memory? According to the Instruction Set Summary, this part has no JMP or 
CALL. 

Answer 
The program memory is organized as 4K x 16, so there are only 4k program memory 
locations. In the assembler, select "Options >> Wrap Relative Jumps". This will enable you 
to jump over the boundary of the program memory. As an example, if you do a relative 
jump from $FFE back to $00A, the program counter will be incremented by 12, and wrap 
around the boundary of the program memory. This feature is only applicable to 8K 
devices, as 4K devices do not require wrap-around, and 16K devices need JUMP and 
CALL. 
 

Question: When I use the UART, are any of the Timer/Counters used to generate the 
Baud Rate? 

Answer 
No, the Baud Rate generator has a separate timer that is used by the UART only. All 
Timer/Counters can be used as normal even if the UART is operating. 
 

Question: When I do a single bit set or clear to the I/O with the SBI and CBI instructions, 
will that effect other bits on the same port ? 

Answer 
No, unlike most microcontrollers available, with the AVR you will always be 100% safe 
when operating the I/O with single bit operations. This also applies to operations on the 
entire port. Refer to the question regarding three addresses for each I/O Port. 
 

Question: Why are there three addresses for each I/O Port ? 

Answer 
To allow you to build 100% safe systems, the AVR supports true read-modify-write to the 
I/O ports. If you want to read the physical value on the I/O Pins, read the PIN register. 
When you want to update outputs, read the PORT latch to ensure that you write back the 
correct data to all outputs on the port. This method always gives the desired result 
independent of the physical level of the pins. This feature saves you from having a copy of 
your port in memory (and use many instructions) to build a safe system. When you do a 
SBI or CBI instruction to set/clear single bits in I/O you must always use the PORT 
address. 
 

Question: Why does SBI and CBI only operate on I/O registers $00 -$1F? 
Question: Why do the immediate instructions not work on registers R0-R15? 
Question: Why is there no EORI instruction? 

Answer 
All AVR instructions, with a few exceptions are 2 bytes long. This means that there is only 
65,536 (64K) possible combinations to make an instruction set from. When we specified 
the AVR instruction set, some compromises had to be made to utilize these 64K 
combinations as efficiently as possible. Unlike a CISC controller, where an instruction can 
be one, two, three or even more bytes, we could not implement on the AVR all the 
instructions we would like to have. As an example, an immediate instruction which 
addresses all 32 registers, needs 8 bits for the constant and another 5 bits to address a 
register file. This instruction would occupy 8K of the total instruction space. In other 
words, if we made eight immediate instructions which worked on the entire register file, no 
more instructions could be implemented. Making the instruction word 17 bits long, was 
never been considered as an economical nor convenient solution. 

The process of designing the AVR instruction set included a lot of gives and takes. We 
have consulted C compiler experts, who have given us a lot of advise on how to tune the 
instruction set to yield compact C-code. As an example, the compiler experts advised us 
to sacrifice the ADDI for a SBCI (subtract immediate with carry). 

For those instructions that are missing, convenient work-arounds exist. The code 
efficiency of the AVR should prove that we have found a good compromise between 
which instructions to implement, and which to omit. 
 

 

 

 

 

 

 

 

Question: I am using "sbr r30,3" to set bit 3 in register 30. This does not seem to affect bit 
3, but bits 0 and 1 become set. What am I doing wrong? 

Answer 
"SBR" can be used to set more than one bit in a register (like "CBR" can be used to clear 
more than one bit in a register). The second argument in the instruction does not specify the 
bit number, but the mask to be ORed with the register. In your case, $03 is ORed to R30, 
and consequently the two lowermost bits are set. To set bit 3 only, try one of the following: 

sbr  r30,$08 

sbr  r30,0b00001000 

sbr  r30,(1<<3) 

All these lines will yield the same result. Just pick the one you find most convenient. 
 

Question: In my multi-tasking system, I need to define flags that can be checked and 
cleared without being interrupted. How can I do that? 

Answer 
Use one register per flag. To set a flag, use: 

ldi  flag,01 

Alternatively, if you want to use a low register (r0-r15) for flag, clear it initially after reset 
and use 

inc  flag 

each time you set the flag. 

When the flag is checked and cleared, do this: 

lsr  flag           ;shift flag into carry (flag is cleared) 
brne flag_was_set   ;branch if flag was set 

As long as all your interrupt routines restore the status register before exiting, this method is 
completely safe. 
 

Question: I need to clear a register and also the carry flag. Can this be done with one 
instruction? 

Answer 
Yes, for example to clear r10 and the carry flag, do the following: 

sub  r10,r10 
 

Question: How do I implement a constant table in the AT90S1200? 

Answer 
Since the S1200 does not have the LPM (Load Program Memory) instruction the most 
efficient way is to place small tables in the EEPROM. This will give you a very efficient use 
of the memory and you can access your table directly with the EEPROM address register 
and the EEPROM data register. 

.ESEG          ;define EEPROM segment 
.db my_var00=$45 
.db my_var01=$4c 
.db my_var02=$5f 

.CSEG          ;define general code segment 

If your table does not fit in the EEPROM, you can place the rest of it in Flash like this: 

.def tp   =r16 ;table pointer 
.def output=r17     ;output from table 

table: 

ldi  output,$45 
cpi  tp,01 
breq end 
ldi  output,$4c 
cpi  tp,02 
breq end 
ldi  output,$5f 
cpi  tp,03 
breq end 

end: 
ret 

To access element number 2 in this table, do the following: 

ldi  tp,2 
rcall     table 

Upon return from the table subroutine, "output" will contain "$4c". 
 
 

Question: How do I add an immediate constants to a register when there is no ADDI 
instruction? 

Answer 
You simply subtract with the negative value. The following code example adds 5 to register 
20: 

subi r20,-5 

Note however that with this solution, the carry flag can not be used to detect an overflow. 
If you already have the constant in another register, you can use a general ADD. If the 
constant is 1, you can use INC to increment the register. 

16-bit addition is done the same way. The following code example adds $0b3c to r17:r16: 

subi r16,low(-$0b3c)     ;subtract low byte 
sbci r17,high(-$0b3c)    ;subtract high byte 
 

Question: How can Flash and Mask ROM ever cost the same ? 

Answer 
This applies when the MCU has got on-chip EEPROM. The EEPROM contains a charge 
pump and some sense amplifiers. These resources can be used by the Flash as well. 
Hence, when there is an EEPROM on the chip, Atmel can give you Flash program 
memory without increasing the price. 
 

Question: To achieve single cycle execution, is the external clock signal multiplied ? 

Answer 
No, the clock is used as is. No clock division or multiplication is performed. 
 

Question: How many clock cycles make up one AVR instruction cycle ? 

Answer 
Only one. All it takes to e.g. add two numbers in the register file, is one positive and one 
negative transition on the XTAL1 pin. 
 
 

Question: Why is the AT90S8515 called a 8K MCU ? 

Answer 
Most CISC controllers have instructions that vary in size. Some instructions consist of a 
single byte, whereas others can be up to four bytes long. Most AVR instructions are two 
bytes long (a few are also four bytes), so the CPU does not have to fetch one byte at a 
time and determine when to start execution. In our code size benchmarks, we always 
compare the number of bytes. Hence, a C program that fills 8K in an 80C51, will certainly 
fit into an 8K AVR. 
 

 

 

 

 

 

Question: Where can I find datasheets ? 

Answer 
They can be downloaded from www.atmel.com. Alternatively, get a datatbook from your 
local distributor or Atmel office. You can also send an email to literature@atmel.com. 
 

Question: What added value can the AVR offer over my current MCU? 

Answer 
More compact code, especially for C-programming 
4 - 10 times higher speed 
Program Flash memory, in-system downloadable 
On-chip EEPROM, in-system downloadable 
Lower Power Consumption 
 

Question: What does "AVR" mean? 

Answer 
Nothing, it's just a name.