| AVR Libc Home Page | ![]() |
AVR Libc Development Pages | ||
| Main Page | FAQ | Library Reference | Additional Documentation | Example Projects |
Makefile can be configured.PWM) to ramp an LED on and off every two seconds. An AT90S2313 processor will be used as the controller. The circuit for this demonstration is shown in the schematic diagram. If you have a development kit, you should be able to use it, rather than build the circuit, for this project.
Schematic of circuit for demo project
The source code is given in demo.c. For the sake of this example, create a file called demo.c containing this source code. Some of the more important parts of the code are:
iocompat.h tries to abstract between all this differences using some preprocessor #ifdef statements, so the actual program itself can operate on a common set of symbolic names.PWM is being used in 10-bit mode, so we need a 16-bit variable to remember the current value.PWM.PWM register. Since we are in an interrupt routine, it is safe to use a 16-bit assignment to the register. Outside of an interrupt, the assignment should only be performed with interrupts disabled if there's a chance that an interrupt routine could also access this register (or another register that uses TEMP), see the appropriate FAQ entry.PWM and enables interrupts.sleep_mode() puts the processor on sleep until the next interrupt, to conserve power. Of course, that probably won't be noticable as we are still driving a LED, it is merely mentioned here to demonstrate the basic principle./* * ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42): * <joerg@FreeBSD.ORG> wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you think * this stuff is worth it, you can buy me a beer in return. Joerg Wunsch * ---------------------------------------------------------------------------- * * Simple AVR demonstration. Controls a LED that can be directly * connected from OC1/OC1A to GND. The brightness of the LED is * controlled with the PWM. After each period of the PWM, the PWM * value is either incremented or decremented, that's all. * * $Id: demo.c,v 1.6.2.1 2005/12/28 22:35:08 joerg_wunsch Exp $ */ #include <inttypes.h> #include <avr/io.h> #include <avr/interrupt.h> #include <avr/sleep.h> #include "iocompat.h" /* Note [1] */ enum { UP, DOWN }; volatile uint16_t pwm; /* Note [2] */ volatile uint8_t direction; ISR (TIMER1_OVF_vect) /* Note [3] */ { switch (direction) /* Note [4] */ { case UP: if (++pwm == 1023) direction = DOWN; break; case DOWN: if (--pwm == 0) direction = UP; break; } OCR = pwm; /* Note [5] */ } void ioinit (void) /* Note [6] */ { /* tmr1 is 10-bit PWM */ TCCR1A = _BV (WGM10) | _BV (WGM11) | _BV (COM1A1); /* tmr1 running on full MCU clock */ TCCR1B = _BV (CS10); /* set PWM value to 0 */ OCR = 0; /* enable OC1 as output */ DDROC = _BV (OC1); /* enable timer 1 overflow interrupt */ TIMSK = _BV (TOIE1); sei (); } int main (void) { ioinit (); /* loop forever, the interrupts are doing the rest */ for (;;) /* Note [7] */ sleep_mode(); return (0); }
-mmcu option is specified. The -Os option will tell the compiler to optimize the code for efficient space usage (at the possible expense of code execution speed). The -g is used to embed debug info. The debug info is useful for disassemblies and doesn't end up in the .hex files, so I usually specify it. Finally, the -c tells the compiler to compile and stop -- don't link. This demo is small enough that we could compile and link in one step. However, real-world projects will have several modules and will typically need to break up the building of the project into several compiles and one link.
$ avr-gcc -g -Os -mmcu=atmega8 -c demo.c
The compilation will create a demo.o file. Next we link it into a binary called demo.elf.
$ avr-gcc -g -mmcu=atmega8 -o demo.elf demo.o
It is important to specify the MCU type when linking. The compiler uses the -mmcu option to choose start-up files and run-time libraries that get linked together. If this option isn't specified, the compiler defaults to the 8515 processor environment, which is most certainly what you didn't want.
Now we have a binary file. Can we do anything useful with it (besides put it into the processor?) The GNU Binutils suite is made up of many useful tools for manipulating object files that get generated. One tool is avr-objdump, which takes information from the object file and displays it in many useful ways. Typing the command by itself will cause it to list out its options.
For instance, to get a feel of the application's size, the -h option can be used. The output of this option shows how much space is used in each of the sections (the .stab and .stabstr sections hold the debugging information and won't make it into the ROM file).
An even more useful option is -S. This option disassembles the binary file and intersperses the source code in the output! This method is much better, in my opinion, than using the -S with the compiler because this listing includes routines from the libraries and the vector table contents. Also, all the "fix-ups" have been satisfied. In other words, the listing generated by this option reflects the actual code that the processor will run.
$ avr-objdump -h -S demo.elf > demo.lst
Here's the output as saved in the demo.lst file:
demo.elf: file format elf32-avr
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 000000ee 00000000 00000000 00000094 2**0
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 .data 00000000 00800060 000000ee 00000182 2**0
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000003 00800060 00800060 00000182 2**0
ALLOC
3 .noinit 00000000 00800063 00800063 00000182 2**0
CONTENTS
4 .eeprom 00000000 00810000 00810000 00000182 2**0
CONTENTS
5 .stab 00000774 00000000 00000000 00000184 2**2
CONTENTS, READONLY, DEBUGGING
6 .stabstr 000007da 00000000 00000000 000008f8 2**0
CONTENTS, READONLY, DEBUGGING
Disassembly of section .text:
00000000 <__vectors>:
0: 12 c0 rjmp .+36 ; 0x26 <__ctors_end>
2: 74 c0 rjmp .+232 ; 0xec <__bad_interrupt>
4: 73 c0 rjmp .+230 ; 0xec <__bad_interrupt>
6: 72 c0 rjmp .+228 ; 0xec <__bad_interrupt>
8: 71 c0 rjmp .+226 ; 0xec <__bad_interrupt>
a: 70 c0 rjmp .+224 ; 0xec <__bad_interrupt>
c: 6f c0 rjmp .+222 ; 0xec <__bad_interrupt>
e: 6e c0 rjmp .+220 ; 0xec <__bad_interrupt>
10: 11 c0 rjmp .+34 ; 0x34 <__vector_8>
12: 6c c0 rjmp .+216 ; 0xec <__bad_interrupt>
14: 6b c0 rjmp .+214 ; 0xec <__bad_interrupt>
16: 6a c0 rjmp .+212 ; 0xec <__bad_interrupt>
18: 69 c0 rjmp .+210 ; 0xec <__bad_interrupt>
1a: 68 c0 rjmp .+208 ; 0xec <__bad_interrupt>
1c: 67 c0 rjmp .+206 ; 0xec <__bad_interrupt>
1e: 66 c0 rjmp .+204 ; 0xec <__bad_interrupt>
20: 65 c0 rjmp .+202 ; 0xec <__bad_interrupt>
22: 64 c0 rjmp .+200 ; 0xec <__bad_interrupt>
24: 63 c0 rjmp .+198 ; 0xec <__bad_interrupt>
00000026 <__ctors_end>:
26: 11 24 eor r1, r1
28: 1f be out 0x3f, r1 ; 63
2a: cf e5 ldi r28, 0x5F ; 95
2c: d4 e0 ldi r29, 0x04 ; 4
2e: de bf out 0x3e, r29 ; 62
30: cd bf out 0x3d, r28 ; 61
32: 4f c0 rjmp .+158 ; 0xd2 <main>
00000034 <__vector_8>:
volatile uint16_t pwm; /* Note [2] */
volatile uint8_t direction;
ISR (TIMER1_OVF_vect) /* Note [3] */
{
34: 1f 92 push r1
36: 0f 92 push r0
38: 0f b6 in r0, 0x3f ; 63
3a: 0f 92 push r0
3c: 11 24 eor r1, r1
3e: 2f 93 push r18
40: 8f 93 push r24
42: 9f 93 push r25
switch (direction) /* Note [4] */
44: 80 91 60 00 lds r24, 0x0060
48: 99 27 eor r25, r25
4a: 00 97 sbiw r24, 0x00 ; 0
4c: 19 f0 breq .+6 ; 0x54 <__SREG__+0x15>
4e: 01 97 sbiw r24, 0x01 ; 1
50: 31 f5 brne .+76 ; 0x9e <__SREG__+0x5f>
52: 14 c0 rjmp .+40 ; 0x7c <__SREG__+0x3d>
{
case UP:
if (++pwm == 1023)
54: 80 91 61 00 lds r24, 0x0061
58: 90 91 62 00 lds r25, 0x0062
5c: 01 96 adiw r24, 0x01 ; 1
5e: 90 93 62 00 sts 0x0062, r25
62: 80 93 61 00 sts 0x0061, r24
66: 80 91 61 00 lds r24, 0x0061
6a: 90 91 62 00 lds r25, 0x0062
6e: 8f 5f subi r24, 0xFF ; 255
70: 93 40 sbci r25, 0x03 ; 3
72: a9 f4 brne .+42 ; 0x9e <__SREG__+0x5f>
direction = DOWN;
74: 81 e0 ldi r24, 0x01 ; 1
76: 80 93 60 00 sts 0x0060, r24
7a: 11 c0 rjmp .+34 ; 0x9e <__SREG__+0x5f>
break;
case DOWN:
if (--pwm == 0)
7c: 80 91 61 00 lds r24, 0x0061
80: 90 91 62 00 lds r25, 0x0062
84: 01 97 sbiw r24, 0x01 ; 1
86: 90 93 62 00 sts 0x0062, r25
8a: 80 93 61 00 sts 0x0061, r24
8e: 80 91 61 00 lds r24, 0x0061
92: 90 91 62 00 lds r25, 0x0062
96: 89 2b or r24, r25
98: 11 f4 brne .+4 ; 0x9e <__SREG__+0x5f>
direction = UP;
9a: 10 92 60 00 sts 0x0060, r1
break;
}
OCR = pwm; /* Note [5] */
9e: 80 91 61 00 lds r24, 0x0061
a2: 90 91 62 00 lds r25, 0x0062
a6: 9b bd out 0x2b, r25 ; 43
a8: 8a bd out 0x2a, r24 ; 42
aa: 9f 91 pop r25
ac: 8f 91 pop r24
ae: 2f 91 pop r18
b0: 0f 90 pop r0
b2: 0f be out 0x3f, r0 ; 63
b4: 0f 90 pop r0
b6: 1f 90 pop r1
b8: 18 95 reti
000000ba <ioinit>:
}
void
ioinit (void) /* Note [6] */
{
/* tmr1 is 10-bit PWM */
TCCR1A = _BV (WGM10) | _BV (WGM11) | _BV (COM1A1);
ba: 83 e8 ldi r24, 0x83 ; 131
bc: 8f bd out 0x2f, r24 ; 47
/* tmr1 running on full MCU clock */
TCCR1B = _BV (CS10);
be: 81 e0 ldi r24, 0x01 ; 1
c0: 8e bd out 0x2e, r24 ; 46
/* set PWM value to 0 */
OCR = 0;
c2: 1b bc out 0x2b, r1 ; 43
c4: 1a bc out 0x2a, r1 ; 42
/* enable OC1 as output */
DDROC = _BV (OC1);
c6: 82 e0 ldi r24, 0x02 ; 2
c8: 87 bb out 0x17, r24 ; 23
/* enable timer 1 overflow interrupt */
TIMSK = _BV (TOIE1);
ca: 84 e0 ldi r24, 0x04 ; 4
cc: 89 bf out 0x39, r24 ; 57
sei ();
ce: 78 94 sei
d0: 08 95 ret
000000d2 <main>:
}
int
main (void)
{
d2: cf e5 ldi r28, 0x5F ; 95
d4: d4 e0 ldi r29, 0x04 ; 4
d6: de bf out 0x3e, r29 ; 62
d8: cd bf out 0x3d, r28 ; 61
ioinit ();
da: ef df rcall .-34 ; 0xba <ioinit>
/* loop forever, the interrupts are doing the rest */
for (;;) /* Note [7] */
sleep_mode();
dc: 85 b7 in r24, 0x35 ; 53
de: 80 68 ori r24, 0x80 ; 128
e0: 85 bf out 0x35, r24 ; 53
e2: 88 95 sleep
e4: 85 b7 in r24, 0x35 ; 53
e6: 8f 77 andi r24, 0x7F ; 127
e8: 85 bf out 0x35, r24 ; 53
ea: f8 cf rjmp .-16 ; 0xdc <main+0xa>
000000ec <__bad_interrupt>:
ec: 89 cf rjmp .-238 ; 0x0 <__heap_end>
avr-objdump is very useful, but sometimes it's necessary to see information about the link that can only be generated by the linker. A map file contains this information. A map file is useful for monitoring the sizes of your code and data. It also shows where modules are loaded and which modules were loaded from libraries. It is yet another view of your application. To get a map file, I usually add -Wl,-Map,demo.map to my link command. Relink the application using the following command to generate demo.map (a portion of which is shown below).
$ avr-gcc -g -mmcu=atmega8 -Wl,-Map,demo.map -o demo.elf demo.o
Some points of interest in the demo.map file are:
.rela.plt
*(.rela.plt)
.text 0x00000000 0xee
*(.vectors)
.vectors 0x00000000 0x26 /junk/AVR/avr-libc-1.4/avr/lib/avr4/atmega8/crtm8.o
0x00000000 __vectors
0x00000000 __vector_default
0x00000026 __ctors_start = .
The .text segment (where program instructions are stored) starts at location 0x0.
*(.fini2)
*(.fini1)
*(.fini0)
0x000000ee _etext = .
.data 0x00800060 0x0 load address 0x000000ee
0x00800060 PROVIDE (__data_start, .)
*(.data)
*(.gnu.linkonce.d*)
0x00800060 . = ALIGN (0x2)
0x00800060 _edata = .
0x00800060 PROVIDE (__data_end, .)
.bss 0x00800060 0x3
0x00800060 PROVIDE (__bss_start, .)
*(.bss)
*(COMMON)
COMMON 0x00800060 0x3 demo.o
0x00800060 direction
0x00800061 pwm
0x00800063 PROVIDE (__bss_end, .)
0x000000ee __data_load_start = LOADADDR (.data)
0x000000ee __data_load_end = (__data_load_start + SIZEOF (.data))
.noinit 0x00800063 0x0
0x00800063 PROVIDE (__noinit_start, .)
*(.noinit*)
0x00800063 PROVIDE (__noinit_end, .)
0x00800063 _end = .
0x00800063 PROVIDE (__heap_start, .)
.eeprom 0x00810000 0x0
*(.eeprom*)
0x00810000 __eeprom_end = .
The last address in the .text segment is location 0x114 ( denoted by _etext ), so the instructions use up 276 bytes of FLASH.
The .data segment (where initialized static variables are stored) starts at location 0x60, which is the first address after the register bank on an ATmega8 processor.
The next available address in the .data segment is also location 0x60, so the application has no initialized data.
The .bss segment (where uninitialized data is stored) starts at location 0x60.
The next available address in the .bss segment is location 0x63, so the application uses 3 bytes of uninitialized data.
The .eeprom segment (where EEPROM variables are stored) starts at location 0x0.
The next available address in the .eeprom segment is also location 0x0, so there aren't any EEPROM variables.
.hex files. The GNU utility that does this is called avr-objcopy.The ROM contents can be pulled from our project's binary and put into the file demo.hex using the following command:
$ avr-objcopy -j .text -j .data -O ihex demo.elf demo.hex
The resulting demo.hex file contains:
:1000000012C074C073C072C071C070C06FC06EC0C7 :1000100011C06CC06BC06AC069C068C067C066C0F0 :1000200065C064C063C011241FBECFE5D4E0DEBF4D :10003000CDBF4FC01F920F920FB60F9211242F9376 :100040008F939F93809160009927009719F00197F3 :1000500031F514C0809161009091620001969093F7 :1000600062008093610080916100909162008F5FD7 :100070009340A9F481E08093600011C080916100F9 :10008000909162000197909362008093610080914B :10009000610090916200892B11F4109260008091B0 :1000A0006100909162009BBD8ABD9F918F912F91BD :1000B0000F900FBE0F901F90189583E88FBD81E0C1 :1000C0008EBD1BBC1ABC82E087BB84E089BF7894DC :1000D0000895CFE5D4E0DEBFCDBFEFDF85B7806800 :0E00E00085BF889585B78F7785BFF8CF89CF0C :00000001FF
The -j option indicates that we want the information from the .text and .data segment extracted. If we specify the EEPROM segment, we can generate a .hex file that can be used to program the EEPROM:
$ avr-objcopy -j .eeprom --change-section-lma .eeprom=0 -O ihex demo.elf demo_eeprom.hex
The resulting demo_eeprom.hex file contains:
:00000001FF
which is an empty .hex file (which is expected, since we didn't define any EEPROM variables).
make, save the following in a file called Makefile.
Makefile can only be used as input for the GNU version of make.PRG = demo OBJ = demo.o MCU_TARGET = atmega8 OPTIMIZE = -O2 DEFS = LIBS = # You should not have to change anything below here. CC = avr-gcc # Override is only needed by avr-lib build system. override CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) override LDFLAGS = -Wl,-Map,$(PRG).map OBJCOPY = avr-objcopy OBJDUMP = avr-objdump all: $(PRG).elf lst text eeprom $(PRG).elf: $(OBJ) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) clean: rm -rf *.o $(PRG).elf *.eps *.png *.pdf *.bak rm -rf *.lst *.map $(EXTRA_CLEAN_FILES) lst: $(PRG).lst %.lst: %.elf $(OBJDUMP) -h -S $< > $@ # Rules for building the .text rom images text: hex bin srec hex: $(PRG).hex bin: $(PRG).bin srec: $(PRG).srec %.hex: %.elf $(OBJCOPY) -j .text -j .data -O ihex $< $@ %.srec: %.elf $(OBJCOPY) -j .text -j .data -O srec $< $@ %.bin: %.elf $(OBJCOPY) -j .text -j .data -O binary $< $@ # Rules for building the .eeprom rom images eeprom: ehex ebin esrec ehex: $(PRG)_eeprom.hex ebin: $(PRG)_eeprom.bin esrec: $(PRG)_eeprom.srec %_eeprom.hex: %.elf $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ %_eeprom.srec: %.elf $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@ %_eeprom.bin: %.elf $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@ # Every thing below here is used by avr-libc's build system and can be ignored # by the casual user. FIG2DEV = fig2dev EXTRA_CLEAN_FILES = *.hex *.bin *.srec dox: eps png pdf eps: $(PRG).eps png: $(PRG).png pdf: $(PRG).pdf %.eps: %.fig $(FIG2DEV) -L eps $< $@ %.pdf: %.fig $(FIG2DEV) -L pdf $< $@ %.png: %.fig $(FIG2DEV) -L png $< $@