NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Compiling new code

January 03, 2010
by pcman0226
pcman0226's Avatar

I've been playing around writing some code, and I'm not certain about how to compile it. I'm not sure how the "make file" system works. I've done assembly programming for a micro controller, but the compiling and uploading were separate. I wrote the plain text C code, compiled it, then uploaded it with Hyper Terminal. I liked the simplicity of this method. Is there a method to do this or something similar where I can run the compiler to get a binary file then run another program to upload it to the micro controller? Or should I just buckle down and learn how to write make files?

January 03, 2010
by Rick_S
Rick_S's Avatar

The make file does essentially what you described. It uses avrgcc to compile the code into a hex file then uses avrdude to send the compiled code to the micro-controller. You can separate the two functions if you want to do it longhand. The make file just makes life a bit easier -- almost like a batch file in DOS days.

If you look at your make file, you will find a section at the bottom like this:

tempsensor-upload:  tempsensor.hex
    avrdude ${AVRDUDEFLAGS} -U flash:w:tempsensor.hex:a

just comment out the bottom line like this:

tempsensor-upload:  tempsensor.hex
#   avrdude ${AVRDUDEFLAGS} -U flash:w:tempsensor.hex:a

This will prevent the make file from attempting to program the chip and will only "make" the hex and object files.

You can then send the hex file to the chip at any time by running the last line substituting the ${AVRDUDEFLAGS} with the string associated with it at the top of the make file.

Hope that gave some food for thought,

Rick

January 03, 2010
by Rick_S
Rick_S's Avatar

Sorry, to clarify the last statement... You would run that from the command line.

Rick

January 03, 2010
by Farmerjoecoledge
Farmerjoecoledge's Avatar

The compiler, (toolchain) most everybody here uses is the winavr gcc. There's lot's of others but this works fine for most c code.

January 04, 2010
by pcman0226
pcman0226's Avatar

I've been playing around modifying the tempsensor project. I have a new directory with a copy of the makefile and display.c. Here is a screen shot of the problem.

???

Thanks

January 04, 2010
by Rick_S
Rick_S's Avatar

It appears as if make isn't finding display.c. I noticed in your programmers notepad, on the file tabs there is no extension on the file "display". It is saved as "display.c" in your folder correct??

You do also need the libnerdkits folder from the code download one folder above the folder containing your files. This wouldn't produce the error you are getting though if it weren't. You would get a different error :).

You could try to make the file manually from a DOS window. To do this, you take each of the three lines below your display.hex: display.c

line and enter them in one at a time as a command substituting the ${GCCFLAGS}, ${LINKFLAGS}, and ${LINKOBJECTS} with the text defined above.

So, the first line you would type as is: make -C ../libnerdkits The second line requires all the substitutions and is quite long. Make sure you type the case correctly and spacing. If type correctly it will produce display.o in your folder

The third line can be typed as is and will produce the display.hex file.

This file is ready to send to your MCU.

Rick

January 04, 2010
by pcman0226
pcman0226's Avatar

Thanks for the help. I copied the tempsensor.c file, but apparently it didn't carry the extension. Working great now.

January 04, 2010
by Rick_S
Rick_S's Avatar

No problem, glad to help.

January 16, 2010
by pcman0226
pcman0226's Avatar

I have a few more questions about the make file. What does the 'all: display-upload' line do? I noticed that the display-upload section will not run without this line. Why is this? Also, what is the display.ass section doing? I read a little bit about avr_objdump and all I understood was that it had something to do with displaying information about object files. Thanks.

January 16, 2010
by N3Roaster
N3Roaster's Avatar

When make tries to build your project, it starts at the beginning of your makefile with the target all: The text after the : is the dependency of that rule, so it looks at the display-upload: section which has display.hex as a dependency. Before it does anything, it then goes to the display.hex: section which has display.c as a dependency. Since display.c already exists, it performs the commands in the next three lines before going back to the display-upload section where it runs avrdude to send your project to the MCU. Then it goes back to the all: section, but since there are no instructions associated with it, it doesn't have anything to do for that target.

You'll note that there are no sections that have display.ass as a dependency. This means that when you run make as usual, that section does absolutely nothing. It is ignored by make. If, at the command line, you type make display.ass, however, it will run the command in that section. After doing that you'll have a file named display.ass. If you open this, you'll see the assembly language code generated by the compiler with your code as comments so you can see what the MCU is really doing. This can be useful when trying to optimize your program for space or speed. Near the back of the ATmega168 data sheet you'll find a list of the assembly instructions, what they do (briefly), and how many clock cycles it takes the MCU to execute that instruction.

Post a Reply

Please log in to post a reply.

Did you know that NerdKits has a TV commercial, seen on MythBusters and the Science Channel? Learn more...