Look for information on C/C++ programming of Atmel AVR series processors and you will keep running into WinAVR. Fifteen years ago this was the package that gave you the avr-gcc.exe compiler toolchain. Utilities like make.exe run automated build scripts and avrdude.exe uploads your compiled hex files to development boards.
WinAVR also gives you the Programmers Notepad[WinAVR] IDE and MFile[WinAVR] editor for makefiles. In this project we will make a simple program to run on the Arduino Uno and upload it to a board.
More than one computer was used to make the screen capture graphics. Win7, Win11 and even Wine Emulator on Linux could run these softwares and generate the compiled binary hex file output to upload to an Arduino board.
Download and Install WinAVR at the Sourceforge webpage
This development suite is quite basic. You are expected to combine the package you have installed with Atmel Studio 4. AS4 furnishes many more advanced features even to the point of simulating a running processor.
Open a command window C: on your computer using the start menu. It will be in your home directory. Type mkdir winAvr to create a new folder named winAVR. Type cd winAvr to enter that directory. Type dir and you will see no files.
Open the Programmer's Notepad application that installed with WinAVR. Close all the windows except for <new>. We will enter our program code into this window and then save it into the winAvr folder with the name main.c. For this project, we have to use only this name.
// PORTB is an 8bit number at address 0x24
#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>
int main(void){ DDRB = 0xFF; // turn PORTB on
while(1){PORTB++; // increment PORTB
_delay_ms(100); // delay
}}
Type in this program. Save it into the winAvr folder with the name main.c. It will generate a compiled hex file that we can upload to an Arduino Uno board and blink the built-in LED about once a second.
One way of building C/C++ programs is to compile from the command line with the avr-gcc compiler program. If you've done this you know how tedious it can be, repetitive and so easy to make mistakes.
Open the MFile[WinAVR] program installed with WinAVR. It gives you a template makefile that requires a few modifications to select MCU and programmer device.
You should look at the options you can select with MFile and see what changes they make in the makefile output. The template expects the main file to be named main.c and has a different MCU and clock setting we have to change. A good makefile is useful and worth keeping.
We will make our changes with Programmer's Notepad so just save the template into our project folder. Be careful to not let your computer change the file name, it must not end up with a .txt or .doc extension.
Extensions such as *.c and *.h are very important to the compiler and must not be changed. Never use a text processor such as MSWord on these files, they will add extra formatting characters. Programmer's Notepad can be used for editing code files or the regular notepad program.
Edit Makefile with Programmer's NotepadThis template has the scripts to build software modules and to program the flash on your development board. Only a few modifications are needed and then we can run the compiler either from this program or from the command line.
Modify both the MCU and F_CPU values. Change the line that says the program is for the atmega128 to say it is for the atmega328p used in the Arduino Uno board. Remove the octothorpe (#) comment from in front of the line for 16MHz clock and put it at the start of the line saying 8MHz. You can even delete the line.
Then modify the programmer settings about half way through the document. We need to change the stk500v2 external programmer for the built in arduino programmer of the Uno.
On my computer Device Manager shows my Arduino is on port com8
Save changes to the makefile and main.c files. The compiler will only work with changes in the saved files. Go to the Tools menu. Make All, Make Clean and Program are the highlighted commands.
Make All should compile the main.c file and generate several output files, including the main.hex that we can upload to our board. We see the same terminal commands we could have typed in, manually. We also see descriptions of the output files.
Make Program should take the file main.hex and upload it to com port 3 (or whatever port you found with Device Manager. On your board the Tx and Rx lamps should blink during the upload process and when finished, the LED on the Uno should blink about once a second.
These outputs tell you precisely what commands have actually been used. You may end up reading them to understand errors. Example: output for make program shows you exactly which port was used for the avrdude command. Look for atmega328p and F_CPU in the outputs.
Make Clean will remove the files generated by the build process.
The WinAVR suite was made for Windows XP and Win7 operating systems. Beginning with Win8 and still with Windows 10 and Win11 you have to manually install a new dll dynamic link library file.
So, if you see this error 0xC0000142 failure message. Here are the steps to fix things.
If you build from the command line you see this error
Atmel Studio 4 will have this error
This has been around for quite a while. Google your error message and you will see many postings. Mad Wizard provides an explanation and you can download the new dll file here. You may have to right-click copy link.
Go to the folder C:\WinAVR-20100110\utils\bin and look for msys-1.0.dll. There are two of them, the second one is called msys-1.0.dll.old. Move the first one msys-1.0.dll, change the name or delete. Copy in the new one that you just downloaded. Notice the different size and date.
Go back to Programmers Notepad and try Make All. It should compile, no error, and generate an output main.hex file that you can upload to an Arduino board.
Comments