avr main =>
how-to => converting source code
(Diese Seite in Deutsch:
How-to convert an assembler source code file for a different AVR type
This page gives some hints on what to do if you want to assemble
a given source code file for a different AVR type.
Three cases are to be discriminated:
- If you are well familar with assembler, you always
have to outweigh the upcoming work in conversion against the
work load that you would have in re-writing the code on your
own. In cases where you have no or only rudimentary documentation
of the source code: give it up and make your own. In such
cases conversion only eats your time and you will not be very
lucky with the results and the lengthy debug sessions.
- If you are fairly familar with assembler: try it
out. Do'nt be disappointed if the results are not how you
expected. Consider the following hints on potential
- If you are not familar with assembler: learn the
basics of assembler and of the AVR hardware design first.
Any other method will only frustrate you, and you will not
get the project finalized to your contention.
1 Device types with different suffices
If the old and the new type are just differing in the suffix (e.g.
ATtiny13A instead of ATtiny13) you are fine if you
- consult the ATMEL homepage
and search for the term "migration [your old type]"
to find application notes concerning exactly your task,
- change the included "...def.inc" for that type,
in most cases the hex code does not differ and the burner
software will not even realize the difference,
- ensure that the interrupt vector table length and sequence
is identical (see section
2 Device types with different numbers
If the numbers are different, are those numbers for the
same AVR family? Examples for such families are
ATtiny4-5-9-10, ATtiny24-44-84, ATtiny1614-1616-1617,
ATmega48-88-168 or ATmega169-329-649-3290-6490.
In such cases you are fine if you move from a smaller
to a larger device. Nearly nothing has to be changed
In case you move from a larger to a smaller device you
have to ensure that the extra SRAM and EEPROM is not
used. Consult the specific code sections (.dseg and
.eseg) to see what is used to which extent. If the
original software uses more than your target device
offers: forget the conversion task.
A move to a device outside the AVR family scope is a
more tricky task. You will need to go deep into the
hard- and software. The following basic information is
required: collecting basic information
by identifying hardware use
and clock frequency. If you have
absolved this, you can change the
interrupt vector table.
Search for the term "out" and collect those
lines where the term occurs (and preceeding lines that
have an influence on the content written) in an ordered
list. For each identified hardware component identify
the modes to which those components are switched.
Hardware components that require attention are timers
(8 and 16 bit wide, in numerous modes), ADC (in several
different modes, input sources, with/without differential
gain, as temperature sensor), portbits (with all their
alternative uses, e.g. as PWM output).
Check if the target device has all hardware components
and offers the same modes in which those are operated.
If the target device does not cover all those needs,
give up the conversion task and design your own.
Linear programs nearly always use delay loops. In interrupt
driven programs nearly always timers control the program
execution In both cases, the clock frequency is decisive.
The first step is therefore to compare the clock frequency
of the original and the target type. The default values can
be found in the two device databooks (search for
Then check in the source code if you find any hints on fuse
settings that alter the default frequency (search for the
terms "fuse" and "clock"). If the
fuse CLKDIV8 is mentioned, this is significant information.
By searching for "out CLKPR" make sure that the
clock prescaler is not altered.
If the target device can be braught to run with the same
frequency as the original device, you are fine. If not, e.g.
better give up the conversion task and design your own.
- the original uses the clock prescaler CLKPR which the
target does not provide,
- no external clock source can be attached to the target
device that offers the same clock frequency,
If the program is linear (no interrupts used), you can try
to change delay loops to the new frequency. Depending from
the quality of the original documentation that may turn
into a lengthy procedure. And you may fail, if 8- / 16- bit
limitations are exceeded.
If the program involves timers, ensure that you convert any
timer settings to the differing clock frequency. If your
8-bit timer exceeds the 256-stage limit or your 16-bit
timer exceeds the 65536-stage limit: design the whole
One of the basic criteria is: does the source code
use interrupts or not. For checking that search for the reset
and interrupt vectors. If one or more interrupt vectors are
used, the following has to be done.
Copy the interrupt vector table for the target device in the
device databook and paste it to the source code file, e.g.
for an ATtiny48:
1 0x000 RESET External/Power-on/Brown-out/Watchdog Reset
2 0x001 INT0 External Interrupt Request 0
3 0x002 INT1 External Interrupt Request 1
4 0x003 PCINT0 Pin Change Interrupt Request 0
5 0x004 PCINT1 Pin Change Interrupt Request 1
6 0x005 PCINT2 Pin Change Interrupt Request 2
7 0x006 PCINT3 Pin Change Interrupt Request 3
8 0x007 WDT Watchdog Time-out Interrupt
9 0x008 TIMER1_CAPT Timer/Counter1 Capture Event
10 0x009 TIMER1_COMPA Timer/Counter1 Compare Match A
11 0x00A TIMER1_COMPB Timer/Counter1 Compare Match B
12 0x00B TIMER1_OVF Timer/Counter1 Overflow
13 0x00C TIMER0_COMPA Timer/Counter0 Compare Match A
14 0x00D TIMER0_COMPB Timer/Counter0 Compare Match B
15 0x00E TIMER0_OVF Timer/Counter0 Overflow
16 0x00F SPI_STC SPI Serial Transfer Complete
17 0x010 ADC ADC Conversion Complete
18 0x011 EE_RDY EEPROM Ready
19 0x012 ANA_COMP Analog Comparator
20 0x013 TWI 2-wire Serial Interface
Remove the numbers and addresses in those lines and
try to sort the existing interrupt vector jump labels
to their correct position, preceded by an rjmp
instruction. If your target has 16 KB and
more flash memory use rjmp label, followed by
The first vector should rjmp to the init routine, all
other vectors are preceded - and with that decativated
- with reti instructions.
Finally: good luck with the conversion task.
To the top of that page, to the Q+A page
©2014-2017 by http://www.avr-asm-tutorial.net