After the development software is installed you can start writing code. To write programs for the AVR controller you must create a new project, which typically contains many different files: source code, header files, compiled program, etc. To keep projects separate, it is best to create a new folder for each project (the new project wizard offers this option).
To create a new project, follow these steps:
1. Open AVR Studio and press the New Project button. If the window does not open automatically, choose Project - New project from the menu. After entering the required information press Next.
2. A window opens where you set compiler and file defaults. Choose AVR GCC as the compiler and enter the project name and the main source file name on the right side. The source file name should end with the “.c” extension. You can also select options to automatically create a folder with the project name and the main source file. Both options are recommended. Also specify the directory where the project folder should be created. After selecting options, press Next.
NB! If AVR GCC is missing from the compiler list, WinAVR is not installed correctly and must be installed before writing C programs.
3. Next, a window opens where you select the debug platform and microcontroller type. In HomeLab (v5) the debug platform is JTAGICE mkII and the microcontroller is ATmega2561. To finish creating the project, press Finish.
4. Now the programming interface opens and you can start writing the program source code.
5. Before compiling the code, set the project options. The most important settings are the controller clock frequency and the compiler optimization method. The HomeLab controller clock frequency is 14.7456 MHz, i.e., 14745600 Hz. Enter this frequency in hertz in Project → Configuration Options → General. Use -Os for optimization unless you have a specific reason to use another method.
6. Using the HomeLab library with AVR Studio requires that it is installed on the system according to the software installation guide. For each project, add the library to the list of objects to link in Project → Configuration Options → Libraries.
If the object libhomelab2561.a is missing from the list, then the HomeLab library has not been installed correctly.
If the development environment is installed and configured for the first time, it is good to test that everything was done correctly. The easiest way is to write a small program, compile it, and upload it to the controller.
1. Connect the controller board to the computer with a USB cable. After connecting the board, a small green power LED (PWR) should light up.
2. Start the HappyJTAG2 interface program and then AVR Studio. The order is important. First connect the controller to the computer and verify it powers on. Then start HappyJTAG2 and AVR Studio.
3. Create a project in AVR Studio if needed and enter simple C code:
You can test two different codes. The first uses the HomeLab library, the second does the same without the library.
// Simple test program using the HomeLab library. #include <avr/io.h> #include <homelab/delay.h> int main(void) { // Set PB7 as output DDRB = 0x80; // Infinite loop while (true) { // Toggle PB7 PORTB ^= 0x80; hw_delay_ms(500); } }
// Simple test program that does not use the HomeLab library. #include <avr/io.h> int main(void) { unsigned int x,y; // Set PB7 as output DDRB = 0x80; // Infinite loop while (1) { // Toggle PB7 PORTB ^= 0x80; y=3200; while(y--){ x=260; while(x--){ asm volatile ("nop"); } } } }
Compile the program using Build (shortcut F7) and verify the compile succeeded. The end of the compiler output in the message window should be:
Build succeeded with 0 Warnings...
4. Open the programming window using Tools → Program AVR → Auto Connect. This opens the window for uploading the compiled file to the microcontroller. Make sure the Program tab is open.
If the above window does not open and you see Connection Failed, then there is no connection to the programmer. First check that the microcontroller has power. If it does, check the HappyJTAG2 status indicators. FT2232 and AVRStudio should be green.
5. In the programmer window, enter the compiled file in the Input HEX File field in the Flash section. You can use the “…” button. The compiled file is usually in the project subfolder default and has the same name as the project, but with the “.hex” extension, for example “labor1.hex”. Then press Program to upload the selected program to the controller. If everything succeeds, a message appears in the lower part of the programming interface:
OK Reading FLASH input file.. OK Setting device parameters for jtag programming ..OK Entering programming mode.. OK Erasing device.. OK Programming FLASH .. OK Reading FLASH .. OK FLASH contents is equal to file.. OK Leaving programming mode.. OK
The program should cause the PB7 status LED on the controller board to blink periodically. If it works, the software installation is successful and your first project is done. Congratulations!
Debugging is the process of finding errors in a program. For this purpose debuggers are used, which allow the program to run step-by-step and stop at desired points. This makes it possible to inspect variable values, register contents, and the execution order at each stage. Debugging is especially important for complex programs where the cause of errors can be difficult to find. In microcontrollers, step-by-step execution runs in the controller itself and also allows you to see real output changes, in addition to register values. To use debugging, two conditions must be met: the microcontroller must support debugging, and you must have debugging hardware, i.e., a JTAG programmer. Cheaper programmers that use ISP can upload the compiled program but cannot debug.
To run a program in debug mode with AVR Studio, first compile with Build (F7) and then start the compiled program with Run (F5). You can set break points in the source code before this (F9). When program execution reaches a break point, execution stops and you can inspect the microcontroller state at that point. You can continue with Run or step through using Step Into (F11).
Sometimes you need to use floating point numbers in AVR programs. To calculate with them and use printf-style functions, make the following changes in project settings:
1. Open project settings via Project → Configuration Options. On the Libraries tab, after libhomelab.a add libprintf_flt.a and libm.a.
2. Open the Custom Options tab and select the [All files] section. Add “-lprintf_flt” and “-lm” to the right-hand box. In the [Linker Options] section add “-uvfprintf”.
3. Press OK and close the settings window.