This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:examples:setup:avrstudio:atmega2560 [2012/05/29 14:52] – raivo.sell | en:examples:setup:avrstudio:atmega2560 [2026/02/19 11:30] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== AVR Studio ATmega2561 ====== | ||
| + | |||
| + | |||
| + | ===== Creating a project ===== | ||
| + | |||
| + | 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, | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | **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 " | ||
| + | |||
| + | 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 // | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | **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 // | ||
| + | |||
| + | < | ||
| + | |||
| + | ===== Testing the configuration ===== | ||
| + | |||
| + | 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. | ||
| + | |||
| + | <code c> | ||
| + | // Simple test program using the HomeLab library. | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | // Set PB7 as output | ||
| + | DDRB = 0x80; | ||
| + | |||
| + | // Infinite loop | ||
| + | while (true) | ||
| + | { | ||
| + | // Toggle PB7 | ||
| + | PORTB ^= 0x80; | ||
| + | hw_delay_ms(500); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code c> | ||
| + | // Simple test program that does not use the HomeLab library. | ||
| + | #include < | ||
| + | |||
| + | 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 (" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | [{{ : | ||
| + | |||
| + | 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 // | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | **5.** In the programmer window, enter the compiled file in the //Input HEX File// field in the //Flash// section. You can use the " | ||
| + | |||
| + | 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! | ||
| + | |||
| + | {{: | ||
| + | |||
| + | ===== Using the debugger ===== | ||
| + | |||
| + | [{{ : | ||
| + | |||
| + | 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, | ||
| + | |||
| + | 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). | ||
| + | |||
| + | ===== Using floating point numbers ===== | ||
| + | |||
| + | Sometimes you need to use floating point numbers in AVR programs. To calculate with them and use // | ||
| + | |||
| + | **1.** Open project settings via //Project -> Configuration Options//. On the // | ||
| + | |||
| + | **2.** Open the //Custom Options// tab and select the //[All files]// section. Add " | ||
| + | |||
| + | **3.** Press OK and close the settings window. | ||